architect-api 11.6.3

Architect.xyz Trading Platform API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! A product is a thing you can have a position in.

use super::*;
use anyhow::{anyhow, bail, Result};
use chrono::{DateTime, Datelike, NaiveDate, Utc};
use derive_more::{AsRef, Deref, Display, From};
use rust_decimal::Decimal;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_with::{DeserializeFromStr, SerializeDisplay};
use std::str::FromStr;
use strum_macros::{EnumString, IntoStaticStr};

#[derive(
    Debug,
    Display,
    From,
    AsRef,
    Deref,
    Clone,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    Deserialize,
    Serialize,
    JsonSchema,
)]
#[as_ref(forward)]
#[serde(transparent)]
#[cfg_attr(feature = "graphql", derive(juniper::GraphQLScalar))]
#[cfg_attr(feature = "graphql", graphql(transparent))]
#[cfg_attr(feature = "postgres", derive(postgres_types::ToSql))]
#[cfg_attr(feature = "postgres", postgres(transparent))]
pub struct Product(pub(crate) String);

impl Product {
    fn new(
        symbol: &str,
        venue_discriminant: Option<&str>,
        product_kind: &str,
    ) -> Result<Self> {
        if symbol.contains('/')
            || venue_discriminant.is_some_and(|v| v.contains('/'))
            || product_kind.contains('/')
        {
            bail!("product symbol cannot contain the forward slash character '/'");
        }
        let inner = match venue_discriminant {
            Some(venue_discriminant) => {
                if venue_discriminant.is_empty() {
                    bail!("venue discriminant cannot be empty if provided");
                }
                format!(
                    "{} {} {}",
                    symbol,
                    venue_discriminant.to_uppercase(),
                    product_kind
                )
            }
            None => format!("{} {}", symbol, product_kind),
        };
        Ok(Self(inner))
    }

    pub fn fiat(symbol: &str) -> Result<Self> {
        if symbol.contains(char::is_whitespace) {
            bail!("fiat product symbol cannot contain whitespace");
        }
        if symbol.contains('/') {
            bail!("fiat product symbol cannot contain the forward slash character '/'");
        }
        Ok(Self(symbol.to_string()))
    }

    pub fn commodity(symbol: &str) -> Result<Self> {
        Self::new(symbol, None, "Commodity")
    }

    pub fn crypto(symbol: &str) -> Result<Self> {
        Self::new(symbol, None, "Crypto")
    }

    pub fn index(symbol: &str, venue_discriminant: Option<&str>) -> Result<Self> {
        Self::new(symbol, venue_discriminant, "Index")
    }

    pub fn equity(symbol: &str, country: &str) -> Result<Self> {
        let symbol = format!("{symbol} {country}");
        Self::new(&symbol, None, "Equity")
    }

    pub fn future(
        symbol: &str,
        expiration: NaiveDate,
        venue_discriminant: Option<&str>,
    ) -> Result<Self> {
        let symbol = format!("{symbol} {}", expiration.format("%Y%m%d"));
        Self::new(&symbol, venue_discriminant, "Future")
    }

    pub fn futures_spread<'a>(
        legs: impl IntoIterator<Item = &'a str>,
        ratios: Option<impl IntoIterator<Item = Decimal>>,
        expiration: NaiveDate,
        venue_discriminant: Option<&str>,
    ) -> Result<Self> {
        let legs_str = legs.into_iter().collect::<Vec<_>>().join("-");
        let expiration_str = expiration.format("%Y%m%d");
        let symbol = if let Some(ratios) = ratios {
            format!(
                "{legs_str} {} {}",
                ratios.into_iter().map(|k| k.to_string()).collect::<Vec<_>>().join(":"),
                expiration_str
            )
        } else {
            format!("{legs_str} {expiration_str}")
        };
        Self::new(&symbol, venue_discriminant, "Futures Spread")
    }

    pub fn perpetual(symbol: &str, venue_discriminant: Option<&str>) -> Result<Self> {
        Self::new(symbol, venue_discriminant, "Perpetual")
    }

    /// E.g. "AAPL  241227C00300000 Option" (OSI format)
    pub fn option(
        stem: &str,
        expiration: NaiveDate,
        strike: Decimal,
        put_or_call: PutOrCall,
        venue_discriminant: Option<&str>,
    ) -> Result<Self> {
        let strike_str = strike.to_string();
        let (dollar_part, decimal_part) =
            strike_str.split_once('.').unwrap_or((&strike_str, "000"));
        let put_or_call_char = match put_or_call {
            PutOrCall::Put => "P",
            PutOrCall::Call => "C",
        };
        let osi_symbol = format!(
            "{:<6}{:02}{:02}{:02}{}{:0>5}{:0<3}",
            stem,
            expiration.year() % 100,
            expiration.month(),
            expiration.day(),
            put_or_call_char,
            dollar_part,
            &decimal_part[..decimal_part.len().min(3)]
        );
        Self::new(&osi_symbol, venue_discriminant, "Option")
    }

    // pub fn is_series(&self) -> bool {
    //     self.0.ends_with("Option") || self.0.ends_with("Event Contract")
    // }

    /// For futures products, return the expiration date indicated by the
    /// symbol itself, e.g. by parsing rather than looking up the product
    /// information from a catalog.
    pub fn nominative_expiration(&self) -> Option<NaiveDate> {
        if self.ends_with(" Future") {
            let date_part = self.split(' ').nth(1)?;
            NaiveDate::parse_from_str(date_part, "%Y%m%d").ok()
        } else {
            None
        }
    }
}

impl std::borrow::Borrow<str> for Product {
    fn borrow(&self) -> &str {
        &self.0
    }
}

impl FromStr for Product {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        if s.contains('/') {
            bail!("product symbol cannot contain the forward slash character '/'");
        }
        Ok(Self(s.to_string()))
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct ProductInfo {
    pub product_type: ProductType,
    pub primary_venue: Option<String>,
    pub price_display_format: Option<PriceDisplayFormat>,
}

impl ProductInfo {
    pub fn series(&self) -> Option<&str> {
        match &self.product_type {
            ProductType::Future { series, .. } => series.as_deref(),
            _ => None,
        }
    }

    pub fn multiplier(&self) -> Option<Decimal> {
        match &self.product_type {
            ProductType::Crypto
            | ProductType::Fiat
            | ProductType::Equity { .. }
            | ProductType::Index
            | ProductType::Commodity
            | ProductType::Unknown
            | ProductType::Option { .. }
            | ProductType::EventContract { .. }
            | ProductType::FutureSpread { .. } => None,
            ProductType::Perpetual { multiplier, .. }
            | ProductType::Future { multiplier, .. } => Some(*multiplier),
        }
    }

    pub fn underlying(&self) -> Option<&Product> {
        match &self.product_type {
            ProductType::Crypto
            | ProductType::Fiat
            | ProductType::Equity { .. }
            | ProductType::Index
            | ProductType::Commodity
            | ProductType::Unknown
            | ProductType::Option { .. }
            | ProductType::EventContract { .. }
            | ProductType::FutureSpread { .. } => None,
            ProductType::Perpetual { underlying, .. }
            | ProductType::Future { underlying, .. } => underlying.as_ref(),
        }
    }

    pub fn expiration(&self) -> Option<DateTime<Utc>> {
        match &self.product_type {
            ProductType::Crypto
            | ProductType::Fiat
            | ProductType::Equity { .. }
            | ProductType::Index
            | ProductType::Commodity
            | ProductType::Unknown
            | ProductType::Perpetual { .. }
            | ProductType::FutureSpread { .. } => None,
            ProductType::Option {
                instance: OptionsSeriesInstance { expiration, .. },
                ..
            } => Some(*expiration),
            ProductType::EventContract { instance, .. } => instance.expiration(),
            ProductType::Future { expiration, .. } => Some(*expiration),
        }
    }

    pub fn is_expired(&self, cutoff: DateTime<Utc>) -> bool {
        if let Some(expiration) = self.expiration() {
            expiration <= cutoff
        } else {
            false
        }
    }

    pub fn derivative_kind(&self) -> Option<DerivativeKind> {
        match &self.product_type {
            ProductType::Future { derivative_kind, .. }
            | ProductType::Perpetual { derivative_kind, .. } => Some(*derivative_kind),
            _ => None,
        }
    }

    pub fn first_notice_date(&self) -> Option<NaiveDate> {
        match &self.product_type {
            ProductType::Future { first_notice_date, .. } => *first_notice_date,
            _ => None,
        }
    }

    pub fn easy_to_borrow(&self) -> Option<bool> {
        match &self.product_type {
            ProductType::Equity { easy_to_borrow, .. } => *easy_to_borrow,
            _ => None,
        }
    }
}

#[derive(Debug, Clone, IntoStaticStr, Deserialize, Serialize, JsonSchema)]
#[serde(tag = "product_type")]
pub enum ProductType {
    #[schemars(title = "Fiat")]
    Fiat,
    #[schemars(title = "Commodity")]
    Commodity,
    #[schemars(title = "Crypto")]
    Crypto,
    #[schemars(title = "Equity")]
    Equity { easy_to_borrow: Option<bool> },
    #[schemars(title = "Index")]
    Index,
    #[schemars(title = "Future")]
    Future {
        series: Option<String>,
        underlying: Option<Product>,
        multiplier: Decimal,
        expiration: DateTime<Utc>,
        derivative_kind: DerivativeKind,
        #[serde(default)]
        first_notice_date: Option<NaiveDate>,
    },
    #[schemars(title = "FutureSpread")]
    FutureSpread { legs: Vec<SpreadLeg> },
    #[schemars(title = "Perpetual")]
    Perpetual {
        underlying: Option<Product>,
        multiplier: Decimal,
        derivative_kind: DerivativeKind,
    },
    #[schemars(title = "Option")]
    Option { series: OptionsSeries, instance: OptionsSeriesInstance },
    #[schemars(title = "EventContract")]
    EventContract { series: EventContractSeries, instance: EventContractSeriesInstance },
    #[serde(other)]
    #[schemars(title = "Unknown")]
    Unknown,
}

#[derive(
    Debug, Copy, Clone, EnumString, IntoStaticStr, Serialize, Deserialize, JsonSchema,
)]
#[strum(ascii_case_insensitive)]
pub enum DerivativeKind {
    /// Normal futures
    Linear,
    /// Futures settled in the base currency
    Inverse,
    /// Quote currency different from settle currency
    Quanto,
}

#[cfg(feature = "postgres")]
crate::to_sql_str!(DerivativeKind);

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[cfg_attr(feature = "graphql", derive(juniper::GraphQLObject))]
pub struct SpreadLeg {
    pub product: Product,
    /// Some spreads have different ratios for their legs, like buy 1 A, sell 2 B, buy 1 C;
    /// We would represent that with quantities in the legs: 1, -2, 1
    pub quantity: Decimal,
}

impl std::fmt::Display for SpreadLeg {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.quantity > Decimal::ZERO {
            write!(f, "+{} {}", self.quantity, self.product)
        } else {
            write!(f, "{} {}", self.quantity, self.product)
        }
    }
}

impl std::str::FromStr for SpreadLeg {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        let (quantity, product) =
            s.split_once(' ').ok_or_else(|| anyhow!("invalid leg format"))?;
        Ok(Self { product: product.parse()?, quantity: quantity.parse()? })
    }
}

#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    EnumString,
    IntoStaticStr,
    Deserialize,
    Serialize,
    JsonSchema,
)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
pub enum AliasKind {
    CmeGlobex,
    Cfe,
}

#[cfg(feature = "postgres")]
crate::to_sql_str!(AliasKind);

#[derive(Debug, Clone, Copy, PartialEq, Eq, DeserializeFromStr, SerializeDisplay)]
pub enum PriceDisplayFormat {
    CmeFractional {
        main_fraction: i32,
        sub_fraction: i32,  // 0 if no subfraction
        tick_decimals: i32, // number of decimals to the right of the tick mark
    },
}

impl PriceDisplayFormat {
    pub fn main_fraction(&self) -> Option<i32> {
        match self {
            Self::CmeFractional { main_fraction, .. } => Some(*main_fraction),
        }
    }

    pub fn tick_decimals(&self) -> Option<i32> {
        match self {
            Self::CmeFractional { tick_decimals, .. } => Some(*tick_decimals),
        }
    }
}

crate::json_schema_is_string!(PriceDisplayFormat);

impl std::fmt::Display for PriceDisplayFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::CmeFractional { main_fraction, sub_fraction, tick_decimals } => {
                write!(
                    f,
                    "CME_FRACTIONAL({main_fraction},{sub_fraction},{tick_decimals})"
                )
            }
        }
    }
}

impl std::str::FromStr for PriceDisplayFormat {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        if s.starts_with("CME_FRACTIONAL(") {
            let s = s.trim_end_matches(')');
            let parts: Vec<&str> = s["CME_FRACTIONAL(".len()..].split(',').collect();
            if parts.len() != 3 {
                return Err(anyhow!("invalid CME_FRACTIONAL format"));
            }
            let main_fraction = parts[0].parse()?;
            let sub_fraction = parts[1].parse()?;
            let tick_decimals = parts[2].parse()?;
            Ok(Self::CmeFractional { main_fraction, sub_fraction, tick_decimals })
        } else {
            Err(anyhow!("invalid price display format"))
        }
    }
}

#[cfg(feature = "postgres")]
crate::to_sql_display!(PriceDisplayFormat);