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
use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Currency {
    RUB,
    USD,
    EUR,
    GBP,
    HKD,
    CHF,
    JPY,
    CNY,
    TRY,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum InstrumentType {
    Stock,
    Currency,
    Bond,
    Etf,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum OperationType {
    Buy,
    Sell,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum OrderStatus {
    New,
    PartiallyFill,
    Fill,
    Cancelled,
    Replaced,
    PendingCancel,
    Rejected,
    PendingReplace,
    PendingNew,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum OrderType {
    Limit,
    Market,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum BrokerAccountType {
    Tinkoff,
    TinkoffIis,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MoneyAmount {
    pub currency: Currency,
    pub value: f64,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserAccount {
    #[serde(rename(serialize = "brokerAccountId", deserialize = "brokerAccountId"))]
    pub broker_account_id: String,
    #[serde(rename(serialize = "brokerAccountType", deserialize = "brokerAccountType"))]
    pub broker_account_type: BrokerAccountType,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserAccountsPayload {
    pub accounts: Vec<UserAccount>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MarketInstrument {
    pub figi: String,
    pub ticker: String,
    pub name: String,
    pub isin: Option<String>,
    #[serde(rename(deserialize = "minPriceIncrement"))]
    pub min_price_increment: Option<f64>,
    pub lot: u64,
    pub currency: Option<Currency>,
    #[serde(rename(deserialize = "type"))]
    pub type_: InstrumentType,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MarketInstrumentsPayload {
    pub instruments: Vec<MarketInstrument>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Order {
    #[serde(rename(serialize = "orderId", deserialize = "orderId"))]
    pub order_id: String,
    pub figi: String,
    pub operation: OperationType,
    pub status: OrderStatus,
    #[serde(rename(serialize = "requestedLots", deserialize = "requestedLots"))]
    pub requested_lots: u64,
    #[serde(rename(serialize = "executedLots", deserialize = "executedLots"))]
    pub executed_lots: u64,
    #[serde(rename(serialize = "type", deserialize = "type"))]
    pub type_: OrderType,
    pub price: f64,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PlacedOrder {
    #[serde(rename(serialize = "orderId", deserialize = "orderId"))]
    pub order_id: String,
    pub operation: OperationType,
    pub status: OrderStatus,
    #[serde(rename(serialize = "rejectReason", deserialize = "rejectReason"))]
    pub reject_reason: Option<String>,
    pub message: Option<String>,
    #[serde(rename(serialize = "requestedLots", deserialize = "requestedLots"))]
    pub requested_lots: u64,
    #[serde(rename(serialize = "executedLots", deserialize = "executedLots"))]
    pub executed_lots: u64,
    pub commission: Option<MoneyAmount>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ErrorPayload {
    pub code: String,
    pub message: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ResponseData<P> {
    #[serde(rename(serialize = "trackingId", deserialize = "trackingId"))]
    pub tracking_id: String,
    pub payload: P,
    pub status: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PortfolioPosition {
    pub figi: String,
    pub ticker: Option<String>,
    pub isin: Option<String>,
    #[serde(rename(serialize = "instrumentType", deserialize = "instrumentType"))]
    pub instrument_type: InstrumentType,
    pub balance: f64,
    pub blocked: Option<f64>,
    #[serde(rename(serialize = "expectedYield", deserialize = "expectedYield"))]
    pub expected_yield: Option<MoneyAmount>,
    pub lots: u64,
    #[serde(rename(
        serialize = "averagePositionPrice",
        deserialize = "averagePositionPrice"
    ))]
    pub average_position_price: Option<MoneyAmount>,
    #[serde(rename(
        serialize = "averagePositionPriceNoNkd",
        deserialize = "averagePositionPriceNoNkd"
    ))]
    pub average_position_price_no_nkd: Option<MoneyAmount>,
    pub name: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PortfolioPayload {
    pub positions: Vec<PortfolioPosition>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CurrencyPortfolioPosition {
    pub currency: Currency,
    pub balance: f64,
    pub blocked: Option<f64>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CurrencyPortfolioPayload {
    pub currencies: Vec<CurrencyPortfolioPosition>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum OperationTypeWithCommission {
    Buy,
    BuyCard,
    Sell,
    BrokerCommission,
    ExchangeCommission,
    ServiceCommission,
    MarginCommission,
    OtherCommission,
    PayIn,
    PayOut,
    Tax,
    TaxLucre,
    TaxDividend,
    TaxCoupon,
    TaxBack,
    Repayment,
    PartRepayment,
    Coupon,
    Dividend,
    SecurityIn,
    SecurityOut,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum OperationStatus {
    Done,
    Decline,
    Progress,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OperationTrade {
    #[serde(rename(serialize = "tradeId", deserialize = "tradeId"))]
    pub trade_id: String,
    pub date: String,
    pub price: f64,
    pub quantity: u64,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Operation {
    pub id: String,
    pub status: OperationStatus,
    pub trades: Option<Vec<OperationTrade>>,
    pub commission: Option<MoneyAmount>,
    pub currency: Currency,
    pub payment: f64,
    pub price: Option<f64>,
    pub quantity: Option<u64>,
    pub figi: Option<String>,
    #[serde(rename(serialize = "instrumentType", deserialize = "instrumentType"))]
    pub instrument_type: Option<InstrumentType>,
    #[serde(rename(serialize = "isMarginCall", deserialize = "isMarginCall"))]
    pub is_margin_call: bool,
    pub date: String,
    #[serde(rename(serialize = "operationType", deserialize = "operationType"))]
    pub operation_type: Option<OperationTypeWithCommission>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OperationsPayload {
    pub operations: Vec<Operation>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum CandlestickResolution {
    #[serde(rename(serialize = "1min", deserialize = "1min"))]
    MIN1,
    #[serde(rename(serialize = "2min", deserialize = "2min"))]
    MIN2,
    #[serde(rename(serialize = "3min", deserialize = "3min"))]
    MIN3,
    #[serde(rename(serialize = "5min", deserialize = "5min"))]
    MIN5,
    #[serde(rename(serialize = "10min", deserialize = "10min"))]
    MIN10,
    #[serde(rename(serialize = "15min", deserialize = "15min"))]
    MIN15,
    #[serde(rename(serialize = "30min", deserialize = "30min"))]
    MIN30,
    #[serde(rename(serialize = "hour", deserialize = "hour"))]
    HOUR,
    #[serde(rename(serialize = "day", deserialize = "day"))]
    DAY,
    #[serde(rename(serialize = "week", deserialize = "week"))]
    WEEK,
    #[serde(rename(serialize = "month", deserialize = "month"))]
    MONTH,
}

impl fmt::Display for CandlestickResolution {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            CandlestickResolution::MIN1 => write!(f, "1min"),
            CandlestickResolution::MIN2 => write!(f, "2min"),
            CandlestickResolution::MIN3 => write!(f, "3min"),
            CandlestickResolution::MIN5 => write!(f, "5min"),
            CandlestickResolution::MIN10 => write!(f, "10min"),
            CandlestickResolution::MIN15 => write!(f, "15min"),
            CandlestickResolution::MIN30 => write!(f, "30min"),
            CandlestickResolution::HOUR => write!(f, "hour"),
            CandlestickResolution::DAY => write!(f, "day"),
            CandlestickResolution::WEEK => write!(f, "week"),
            CandlestickResolution::MONTH => write!(f, "month"),
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Candlestick {
    pub figi: String,
    pub interval: CandlestickResolution,
    #[serde(rename(serialize = "time", deserialize = "time"))]
    pub datetime: String,
    #[serde(rename(serialize = "o", deserialize = "o"))]
    pub open: f64,
    #[serde(rename(serialize = "c", deserialize = "c"))]
    pub close: f64,
    #[serde(rename(serialize = "h", deserialize = "h"))]
    pub high: f64,
    #[serde(rename(serialize = "l", deserialize = "l"))]
    pub low: f64,
    #[serde(rename(serialize = "v", deserialize = "v"))]
    pub volume: u64,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CandlesticksPayload {
    pub figi: String,
    pub interval: CandlestickResolution,
    pub candles: Vec<Candlestick>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OrderResponse {
    pub price: f64,
    pub quantity: u64,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum TradeStatus {
    NormalTrading,
    NotAvailableForTrading,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Orderbook {
    pub figi: String,
    pub depth: u8,
    pub bids: Vec<OrderResponse>,
    pub asks: Vec<OrderResponse>,
    #[serde(rename(serialize = "tradeStatus", deserialize = "tradeStatus"))]
    pub trade_status: TradeStatus,
    #[serde(rename(serialize = "minPriceIncrement", deserialize = "minPriceIncrement"))]
    pub min_price_increment: f64,
    #[serde(rename(serialize = "faceValue", deserialize = "faceValue"))]
    pub face_value: Option<f64>,
    #[serde(rename(serialize = "lastPrice", deserialize = "lastPrice"))]
    pub last_price: Option<f64>,
    #[serde(rename(serialize = "closePrice", deserialize = "closePrice"))]
    pub close_price: Option<f64>,
    #[serde(rename(serialize = "limitUp", deserialize = "limitUp"))]
    pub limit_up: Option<f64>,
    #[serde(rename(serialize = "limitDown", deserialize = "limitDown"))]
    pub limit_down: Option<f64>,
}