use super::symbol::{ContractSymbol, Right, SymbolKind};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ContractKind {
IndexFuture,
VixFuture,
PolicyRateFuture,
TermRateFuture,
OilFuture,
IndexOption,
NameOption,
}
impl ContractKind {
pub fn root(self) -> &'static str {
match self {
ContractKind::IndexFuture => "IDX",
ContractKind::VixFuture => "VIX",
ContractKind::PolicyRateFuture => "FF",
ContractKind::TermRateFuture => "TR3",
ContractKind::OilFuture => "OIL",
ContractKind::IndexOption => "IDX",
ContractKind::NameOption => "",
}
}
pub fn is_future(self) -> bool {
match self {
ContractKind::IndexFuture
| ContractKind::VixFuture
| ContractKind::PolicyRateFuture
| ContractKind::TermRateFuture
| ContractKind::OilFuture => true,
ContractKind::IndexOption | ContractKind::NameOption => false,
}
}
pub fn family(self) -> &'static str {
if self.is_future() {
"future"
} else {
"option"
}
}
pub fn settlement(self) -> SettlementRule {
match self {
ContractKind::IndexFuture => SettlementRule::OpeningPrintIndex,
ContractKind::VixFuture => SettlementRule::PublishedVixAtOpen,
ContractKind::PolicyRateFuture => SettlementRule::AveragePolicyRateAtClose,
ContractKind::TermRateFuture => SettlementRule::CompoundedPolicyRateAtClose,
ContractKind::OilFuture => SettlementRule::OilPriceAtOpen,
ContractKind::IndexOption => SettlementRule::OpeningPrintIndex,
ContractKind::NameOption => SettlementRule::PhysicalAtClose,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SettlementRule {
OpeningPrintIndex,
PublishedVixAtOpen,
AveragePolicyRateAtClose,
CompoundedPolicyRateAtClose,
OilPriceAtOpen,
PhysicalAtClose,
}
impl SettlementRule {
pub fn as_str(self) -> &'static str {
match self {
SettlementRule::OpeningPrintIndex => "opening_print_index",
SettlementRule::PublishedVixAtOpen => "published_vix_at_open",
SettlementRule::AveragePolicyRateAtClose => "average_policy_rate_at_close",
SettlementRule::CompoundedPolicyRateAtClose => "compounded_policy_rate_at_close",
SettlementRule::OilPriceAtOpen => "oil_price_at_open",
SettlementRule::PhysicalAtClose => "physical_at_close",
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ContractSpec {
pub symbol: String,
pub kind: ContractKind,
pub root: String,
pub expiry: i64,
pub roll: i64,
pub multiplier: f64,
pub tick: f64,
pub settlement: SettlementRule,
pub front: bool,
pub right: Option<Right>,
pub strike: Option<f64>,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Quote {
pub symbol: String,
pub kind: ContractKind,
pub expiry: i64,
pub bid: Option<f64>,
pub ask: Option<f64>,
pub mid: Option<f64>,
pub price: f64,
pub fair: f64,
pub mark: Option<f64>,
pub basis_bp: f64,
pub index: f64,
pub rate: f64,
pub dividends: f64,
pub sessions_to_expiry: f64,
pub multiplier: f64,
pub tick: f64,
pub daily_volume: f64,
pub initial_margin: Option<f64>,
pub maintenance_margin: Option<f64>,
pub expected: Option<f64>,
pub premium: Option<f64>,
pub loading: Option<f64>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SettlementEvent {
Settlement,
Exercise,
Assignment,
}
impl SettlementEvent {
pub fn as_str(self) -> &'static str {
match self {
SettlementEvent::Settlement => "settlement",
SettlementEvent::Exercise => "exercise",
SettlementEvent::Assignment => "assignment",
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Settlement {
pub symbol: String,
pub kind: ContractKind,
pub root: String,
pub session: i64,
pub value: f64,
pub reference: f64,
pub event: SettlementEvent,
pub agent: Option<String>,
pub quantity: f64,
pub shares: f64,
pub cash: f64,
}
impl Settlement {
pub fn new(symbol: String, kind: ContractKind, root: String, session: i64, value: f64, reference: f64) -> Self {
Settlement {
symbol,
kind,
root,
session,
value,
reference,
event: SettlementEvent::Settlement,
agent: None,
quantity: 0.0,
shares: 0.0,
cash: 0.0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct IndexFutureSpec {
pub multiplier: f64,
pub tick: f64,
pub volume_share: f64,
pub maker_levels: usize,
pub maker_level_share: f64,
pub depth_coefficient: f64,
pub depth_exponent: f64,
pub depth_reach: f64,
pub refill_half_life: f64,
pub impact_coefficient: f64,
pub impact_half_life: f64,
}
pub const INDEX_FUTURE: IndexFutureSpec = IndexFutureSpec {
multiplier: 250.0,
tick: 0.05,
volume_share: 1.0,
maker_levels: 10,
maker_level_share: 0.003,
depth_coefficient: 0.75,
depth_exponent: 0.5,
depth_reach: 1.0,
refill_half_life: 27.0,
impact_coefficient: 0.15,
impact_half_life: 30.0,
};
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct VixPremium {
pub centre: f64,
pub k: f64,
pub p: f64,
pub beta: f64,
pub tau: f64,
pub response_gamma: f64,
pub response_tau: f64,
}
impl VixPremium {
pub fn level(&self, n: f64) -> f64 {
if n <= 1.0 {
0.0
} else {
self.k * crate::mathx::pow(n - 1.0, self.p)
}
}
pub fn slope(&self, n: f64) -> f64 {
if n <= 1.0 {
0.0
} else {
self.beta * (1.0 - crate::mathx::exp(-(n - 1.0) / self.tau))
}
}
pub fn at(&self, n: f64, vix: f64) -> f64 {
self.level(n) + self.slope(n) * (vix - self.centre)
}
pub fn response(&self, n: f64) -> f64 {
if n <= 1.0 {
0.0
} else {
self.response_gamma * (1.0 - crate::mathx::exp(-(n - 1.0) / self.response_tau))
}
}
pub fn at_response(&self, n: f64, vix: f64, w: f64) -> f64 {
if w == 0.0 {
return self.at(n, vix);
}
let slope = (1.0 - w) * self.slope(n) + w * self.response(n);
self.level(n) + slope * (vix - self.centre)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct VixFutureSpec {
pub multiplier: f64,
pub tick: f64,
pub front_daily_volume: [f64; 3],
pub deferred_daily_volume: f64,
pub maker_levels: usize,
pub maker_level_share: f64,
pub depth_coefficient: f64,
pub depth_exponent: f64,
pub depth_reach: f64,
pub refill_half_life: f64,
pub daily_sigma: f64,
pub impact_coefficient: f64,
pub impact_half_life: f64,
pub premium: VixPremium,
}
pub const VIX_FUTURE: VixFutureSpec = VixFutureSpec {
multiplier: 1000.0,
tick: 0.05,
front_daily_volume: [110_000.0, 55_000.0, 25_000.0],
deferred_daily_volume: 8_000.0,
maker_levels: 10,
maker_level_share: 0.003,
depth_coefficient: 0.75,
depth_exponent: 0.5,
depth_reach: 1.0,
refill_half_life: 27.0,
daily_sigma: 0.05,
impact_coefficient: 0.15,
impact_half_life: 30.0,
premium: VixPremium { centre: 19.3197, k: 0.26, p: 0.4925, beta: 0.36, tau: 148.0, response_gamma: -0.0716,
response_tau: 0.5 },
};
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct RatePremium {
pub k: f64,
pub p: f64,
pub cap: f64,
}
impl RatePremium {
pub fn at(&self, h: f64) -> f64 {
if h <= 0.0 {
return 0.0;
}
let h = crate::mathx::min(h, self.cap);
self.k * crate::mathx::pow(h / 21.0, self.p) / 100.0
}
}
pub const RATE_PREMIUM: RatePremium = RatePremium { k: 1.5956, p: 1.683, cap: 126.0 };
pub const RATE_PREMIUM_PER_FIXING: RatePremium = RatePremium { k: 3.1679, p: 1.3235, cap: 126.0 };
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct RateFutureSpec {
pub multiplier: f64,
pub tick: f64,
pub daily_volume: &'static [f64],
pub maker_levels: usize,
pub maker_level_share: f64,
pub daily_sigma: f64,
pub impact_coefficient: f64,
pub impact_half_life: f64,
}
pub const POLICY_RATE_FUTURE: RateFutureSpec = RateFutureSpec {
multiplier: 4167.0,
tick: 0.0025,
daily_volume: &[100_000.0, 80_000.0, 60_000.0, 40_000.0, 25_000.0, 15_000.0, 10_000.0, 6_000.0,
4_000.0, 3_000.0, 2_000.0, 1_500.0, 1_000.0],
maker_levels: 10,
maker_level_share: 0.003,
daily_sigma: 0.015,
impact_coefficient: 0.15,
impact_half_life: 30.0,
};
pub const TERM_RATE_FUTURE: RateFutureSpec = RateFutureSpec {
multiplier: 2500.0,
tick: 0.0025,
daily_volume: &[300_000.0, 300_000.0, 250_000.0, 200_000.0, 150_000.0, 100_000.0, 80_000.0, 60_000.0],
maker_levels: 10,
maker_level_share: 0.003,
daily_sigma: 0.05,
impact_coefficient: 0.15,
impact_half_life: 30.0,
};
pub fn rate_future_symbol(kind: ContractKind, expiry: i64) -> String {
ContractSymbol { root: kind.root().to_string(), expiry, kind: SymbolKind::Future }.to_string()
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct OilFutureSpec {
pub multiplier: f64,
pub tick: f64,
pub daily_volume: &'static [f64],
pub maker_levels: usize,
pub maker_level_share: f64,
pub daily_sigma: f64,
pub impact_coefficient: f64,
pub impact_half_life: f64,
}
pub const OIL_FUTURE: OilFutureSpec = OilFutureSpec {
multiplier: 1000.0,
tick: 0.01,
daily_volume: &[300_000.0, 150_000.0, 80_000.0, 50_000.0, 35_000.0, 25_000.0, 20_000.0, 15_000.0,
12_000.0, 10_000.0, 8_000.0, 6_000.0],
maker_levels: 10,
maker_level_share: 0.003,
daily_sigma: 0.02,
impact_coefficient: 0.15,
impact_half_life: 30.0,
};
pub fn oil_future_symbol(expiry: i64) -> String {
ContractSymbol { root: ContractKind::OilFuture.root().to_string(), expiry, kind: SymbolKind::Future }
.to_string()
}
pub fn vix_future_symbol(expiry: i64) -> String {
ContractSymbol { root: ContractKind::VixFuture.root().to_string(), expiry, kind: SymbolKind::Future }
.to_string()
}
pub fn index_future_symbol(expiry: i64) -> String {
ContractSymbol { root: ContractKind::IndexFuture.root().to_string(), expiry, kind: SymbolKind::Future }
.to_string()
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct IndexOptionSpec {
pub multiplier: f64,
pub strikes_each_side: i64,
pub strike_step_sd: f64,
}
pub const INDEX_OPTION: IndexOptionSpec = IndexOptionSpec { multiplier: 100.0, strikes_each_side: 20, strike_step_sd: 0.2 };
impl IndexOptionSpec {
pub fn strike_step(&self, index: f64, vol: f64, sessions: f64) -> f64 {
let raw = self.strike_step_sd * index * vol * crate::mathx::sqrt(crate::mathx::max(sessions, 1.0) / 252.0);
if !(raw > 0.05 && raw.is_finite()) {
return 0.05;
}
let decade = (crate::mathx::log(raw) / std::f64::consts::LN_10).floor();
let power = crate::mathx::pow(10.0, decade);
let mut best = power;
let mut gap = f64::INFINITY;
for m in [1.0, 2.0, 2.5, 5.0, 10.0] {
let step = m * power;
let d = (crate::mathx::log(step) - crate::mathx::log(raw)).abs();
if d < gap {
gap = d;
best = step;
}
}
crate::mathx::max(0.05, best)
}
}
pub fn index_option_symbol(expiry: i64, right: Right, strike: f64) -> String {
let cents = (strike * 100.0).round() as u64;
ContractSymbol {
root: ContractKind::IndexOption.root().to_string(),
expiry,
kind: SymbolKind::Option { right, strike_cents: cents },
}
.to_string()
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct NameOptionSpec {
pub multiplier: f64,
pub strikes_each_side: i64,
pub tiers: [(f64, f64); 4],
pub top_step: f64,
}
pub const NAME_OPTION: NameOptionSpec = NameOptionSpec {
multiplier: 100.0,
strikes_each_side: 10,
tiers: [(5.0, 0.5), (50.0, 1.0), (100.0, 2.5), (200.0, 5.0)],
top_step: 10.0,
};
impl NameOptionSpec {
pub fn strike_step(&self, price: f64) -> f64 {
for (below, step) in self.tiers {
if price < below {
return step;
}
}
self.top_step
}
}
pub fn name_option_symbol(ticker: &str, expiry: i64, right: Right, strike: f64) -> String {
let cents = (strike * 100.0).round() as u64;
ContractSymbol { root: ticker.to_string(), expiry, kind: SymbolKind::Option { right, strike_cents: cents } }
.to_string()
}