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
/* Copyright 2023 Architect Financial Technologies LLC. This is free
* software released under the GNU Affero Public License version 3. */
//! The protocol for talking to the core about limits
use crate::{
packed_value, pool,
protocol::{Account, Desk, DirPair, Trader},
symbology::{Product, Route, Symbolic, TradableProduct, Venue},
};
use chrono::Duration;
use netidx::pool::Pooled;
use netidx_derive::Pack;
use rust_decimal::Decimal;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
pool!(pub, pool_limit, Vec<Limit>, 10, 10_000);
pool!(pub, pool_limit_query, Vec<(LimitSet, LimitScope)>, 10, 10_000);
pool!(pub, pool_global_limit_query, Vec<GlobalLimitName>, 10, 10);
pool!(pub, pool_pos, Vec<Position>, 1000, 1000);
pool!(pub, pool_pos_pos, Vec<Pooled<Vec<Position>>>, 10, 10_000);
pool!(pub, pool_dec_opt, Vec<Option<Decimal>>, 10, 10_000);
/// Each LimitSet is an entire set of limits that apply to the
/// specified entities
///
/// in addition to limits in the set above. e.g. an individual trader
/// is subject to, Firm limits, Desk limits and Trader
/// limits. LimitSet is also the unit of aggregation for positions and
/// open interest.
#[derive(
Debug, Clone, Copy, Hash, PartialEq, Eq, Pack, Serialize, Deserialize, JsonSchema,
)]
#[serde(tag = "type", content = "value")]
pub enum LimitSet {
Firm,
Desk(Desk),
DeskDefault,
Trader(Trader),
TraderDefault,
Account(Account),
AccountDefault,
}
packed_value!(LimitSet);
/// Each LimitSet contains a full set of LimitScopes
///
/// which are different ways of aggregating limits within that
/// set. For example, Global applies to the entire set. Venue applies
/// to trading in a specific venue within the set.
///
/// ProductDefault and TradableProductDefault sets the default limit
/// for a product within the set. If a specific limit is set for a
/// product then it overrides the default limit, but if no limit is
/// set for a given product then it will use the default.
///
/// Positions and open interest also aggregate by scopes.
#[derive(
Debug, Clone, Copy, Hash, PartialEq, Eq, Pack, Serialize, Deserialize, JsonSchema,
)]
#[serde(tag = "type", content = "value")]
pub enum LimitScope {
/// All the trading in this aggregate
Global,
/// All the trading on the specified venue
Venue(Venue),
/// All the trading to any venue via the specified route
Route(Route),
/// All the trading in the specified product
Product(Product),
/// The default product limit if none is specified
ProductDefault,
/// All the trading in the specified tradable product
TradableProduct(TradableProduct),
/// The default tradable product limit if none is specified
TradableProductDefault,
}
packed_value!(LimitScope);
impl LimitScope {
/// true if there are no unknown/invalid symbols
pub fn is_valid(&self) -> bool {
match self {
Self::Global | Self::ProductDefault | Self::TradableProductDefault => true,
Self::Venue(v) => v.is_valid(),
Self::Route(v) => v.is_valid(),
Self::Product(v) => v.is_valid(),
Self::TradableProduct(v) => v.is_valid(),
}
}
}
/// Enforce trading limits
#[derive(
Debug, Clone, Copy, Hash, PartialEq, Eq, Pack, Serialize, Deserialize, JsonSchema,
)]
#[serde(tag = "type", content = "value")]
pub enum Limit {
/// How long to consider fills recent for the purposes of
/// computing MaxOpenandrecentlyfilled. Default 5 minutes.
#[serde(
serialize_with = "crate::serialize_duration",
deserialize_with = "crate::deserialize_duration"
)]
#[schemars(with = "String")]
FillRecencyThreshold(Duration),
/// How long to keep fills in the orderdb. When a fill is retired
/// it no longer counts towards either MaxFilled or
/// MaxOpenandrecentlyfilled. Default 1 day.
#[serde(
serialize_with = "crate::serialize_duration",
deserialize_with = "crate::deserialize_duration"
)]
#[schemars(with = "String")]
FillRetireThreshold(Duration),
/// How long to keep around outed orders. This is strictly a
/// setting to deal with broken counterparties that might send
/// fills for orders that were previously outed. Default 1 minute.
#[serde(
serialize_with = "crate::serialize_duration",
deserialize_with = "crate::deserialize_duration"
)]
#[schemars(with = "String")]
OutRetireThreshold(Duration),
/// How long after an order is canceled but not outed should it be
/// considered not out. Default 30 seconds.
#[serde(
serialize_with = "crate::serialize_duration",
deserialize_with = "crate::deserialize_duration"
)]
#[schemars(with = "String")]
NotOutThreshold(Duration),
/// The maximum amount that an order or a fill price can deviate
/// from the limit dollar price. The absolute value of the
/// difference in percentage terms. Default 0.5.
MaxPriceDeviation(Decimal),
/// Set whether a given LimitSet is enforcing or not. Non
/// enforcing limit sets just build aggregates, they don't check
/// limits.
SetEnforcing(LimitSet, bool),
/// Maximum open limit dollars in the specified set and scope
MaxOpen(LimitSet, LimitScope, Decimal),
/// Maximum open and recently filled limit dollars in the specified set and scope
MaxOpenAndRecentlyFilled(LimitSet, LimitScope, Decimal),
/// Maximum filled limit dollars in the spefified set and scope
MaxFilled(LimitSet, LimitScope, Decimal),
}
packed_value!(Limit);
impl Limit {
/// true if there are no invalid/unknown symbols in the limit
pub fn is_valid(&self) -> bool {
match self {
Self::MaxFilled(_, s, _)
| Self::MaxOpen(_, s, _)
| Self::MaxOpenAndRecentlyFilled(_, s, _) => s.is_valid(),
Self::FillRecencyThreshold(_)
| Self::FillRetireThreshold(_)
| Self::MaxPriceDeviation(_)
| Self::NotOutThreshold(_)
| Self::OutRetireThreshold(_)
| Self::SetEnforcing(_, _) => true,
}
}
}
/// The name of the OMS global limits
#[derive(
Debug, Clone, Copy, Hash, PartialEq, Eq, Pack, Serialize, Deserialize, JsonSchema,
)]
pub enum GlobalLimitName {
FillRecencyThreshold,
FillRetireThreshold,
OutRetireThreshold,
NotOutThreshold,
MaxPriceDeviation,
}
/// The name of the aggregate limits
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Pack, Serialize, Deserialize)]
pub enum AggregateLimitName {
MaxOpen,
MaxOpenAndRecentlyFilled,
MaxFilled,
}
/// The name of any limit
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Pack, Serialize, Deserialize)]
pub enum LimitName {
Global(GlobalLimitName),
Aggregate(AggregateLimitName),
}
packed_value!(GlobalLimitName);
/// Position in a particular product
#[derive(Debug, Clone, Copy, PartialEq, Eq, Pack)]
pub struct Position {
pub quote: Product,
pub position: DirPair<Decimal>,
}
impl Position {
pub fn new(quote: Product, position: DirPair<Decimal>) -> Self {
Self { quote, position }
}
}