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
mod plans; use std::collections::BTreeMap; use matches::matches; use serde::Deserialize; use serde::de::{Deserializer, Error as _}; use crate::broker_statement::StatementsMergingStrategy; use crate::commissions::CommissionSpec; use crate::config::{Config, BrokersConfig, BrokerConfig}; use crate::core::GenericResult; use crate::currency::CashAssets; use crate::types::Decimal; #[derive(Debug, Clone, Copy)] pub enum Broker { Bcs, Firstrade, InteractiveBrokers, Open, Tinkoff, } impl Broker { pub fn get_info(self, config: &Config, plan: Option<&String>) -> GenericResult<BrokerInfo> { let config = config.brokers.as_ref() .and_then(|brokers| self.get_config(brokers)) .ok_or_else(|| format!( "{} configuration is not set in the configuration file", self.get_name()))? .clone(); let statements_merging_strategy = match self { Broker::Bcs => StatementsMergingStrategy::Sparse, Broker::InteractiveBrokers => StatementsMergingStrategy::SparseOnHolidays(1), _ => StatementsMergingStrategy::ContinuousOnly, }; Ok(BrokerInfo { type_: self, name: self.get_name(), config: config, commission_spec: self.get_commission_spec(plan)?, allow_future_fees: matches!(self, Broker::Tinkoff), fractional_shares_trading: matches!(self, Broker::InteractiveBrokers), statements_merging_strategy: statements_merging_strategy, }) } fn get_name(self) -> &'static str { match self { Broker::Bcs => "ООО «Компания БКС»", Broker::Firstrade => "Firstrade Securities Inc.", Broker::InteractiveBrokers => "Interactive Brokers LLC", Broker::Open => "АО «Открытие Брокер»", Broker::Tinkoff => "АО «Тинькофф Банк»", } } fn get_config(self, config: &BrokersConfig) -> Option<&BrokerConfig> { match self { Broker::Bcs => &config.bcs, Broker::Firstrade => &config.firstrade, Broker::InteractiveBrokers => &config.interactive_brokers, Broker::Open => &config.open_broker, Broker::Tinkoff => &config.tinkoff, }.as_ref() } fn get_commission_spec(self, plan: Option<&String>) -> GenericResult<CommissionSpec> { type PlanFn = fn() -> CommissionSpec; let (default, plans): (PlanFn, BTreeMap<&str, PlanFn>) = match self { Broker::Bcs => (plans::bcs::investor, btreemap!{ "Инвестор" => plans::bcs::investor as PlanFn, "Инвестор Про" => plans::bcs::investor_pro as PlanFn, "Профессиональный" => plans::bcs::professional as PlanFn, }), Broker::Firstrade => (plans::firstrade::free, btreemap!{}), Broker::InteractiveBrokers => (plans::ib::fixed, btreemap!{ "Fixed" => plans::ib::fixed as PlanFn, }), Broker::Open => (plans::open::iia, btreemap!{ "Самостоятельное управление (ИИС)" => plans::open::iia as PlanFn, }), Broker::Tinkoff => (plans::tinkoff::trader, btreemap!{ "Трейдер" => plans::tinkoff::trader as PlanFn, }), }; let plan = match plan { Some(plan) => { *plans.get(plan.as_str()).ok_or_else(|| format!( "Invalid plan for {}: {}. Available plans: {}", self.get_name(), plan, plans.keys().copied().collect::<Vec<_>>().join(", "), ))? }, None => default, }; Ok(plan()) } } impl<'de> Deserialize<'de> for Broker { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> { let value = String::deserialize(deserializer)?; Ok(match value.as_str() { "bcs" => Broker::Bcs, "firstrade" => Broker::Firstrade, "interactive-brokers" => Broker::InteractiveBrokers, "open-broker" => Broker::Open, "tinkoff" => Broker::Tinkoff, _ => return Err(D::Error::unknown_variant(&value, &[ "bcs", "firstrade", "interactive-brokers", "open-broker", "tinkoff", ])), }) } } #[derive(Debug)] pub struct BrokerInfo { pub type_: Broker, pub name: &'static str, config: BrokerConfig, pub commission_spec: CommissionSpec, pub allow_future_fees: bool, pub fractional_shares_trading: bool, pub statements_merging_strategy: StatementsMergingStrategy, } impl BrokerInfo { pub fn get_deposit_commission(&self, assets: CashAssets) -> GenericResult<Decimal> { let currency = assets.cash.currency; let commission_spec = match self.config.deposit_commissions.get(currency) { Some(commission_spec) => commission_spec, None => return Err!(concat!( "Unable to calculate commission for {} deposit to {}: there is no commission ", "specification in the configuration file"), currency, self.name), }; Ok(commission_spec.fixed_amount) } }