kiteticker-async 0.1.1

Async version of the ticker module of the kiteconnect-rs crate.
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
use serde::{Deserialize, Serialize};
use std::{ops::Div, time::Duration};

fn value(input: &[u8]) -> Option<u32> {
  let value = i32::from_be_bytes(input[0..=3].try_into().unwrap());
  value.try_into().ok()
}

fn value_short(input: &[u8]) -> Option<u16> {
  let value = i16::from_be_bytes(input[0..=1].try_into().unwrap());
  value.try_into().ok()
}

fn price(input: &[u8], exchange: &Exchange) -> Option<f64> {
  let value = i32::from_be_bytes(input[0..4].try_into().unwrap()) as f64;
  if exchange.divisor() > 0_f64 {
    Some(value.div(exchange.divisor()))
  } else {
    None
  }
}

pub(crate) fn packet_length(bs: &[u8]) -> usize {
  i16::from_be_bytes(bs[0..=1].try_into().unwrap()) as usize
}

#[derive(Debug, Clone, Default)]
///
/// Quote packet structure
///
pub struct Tick {
  pub mode: Mode,
  pub instrument_token: u32,
  pub exchange: Exchange,
  pub is_tradable: bool,
  pub is_index: bool,

  pub last_traded_qty: Option<u32>,
  pub avg_traded_price: Option<f64>,
  pub last_price: Option<f64>,
  pub volume_traded: Option<u32>,
  pub total_buy_qty: Option<u32>,
  pub total_sell_qty: Option<u32>,
  pub ohlc: Option<OHLC>,

  pub last_traded_timestamp: Option<Duration>,
  pub oi: Option<u32>,
  pub oi_day_high: Option<u32>,
  pub oi_day_low: Option<u32>,
  pub exchange_timestamp: Option<Duration>,

  pub net_change: Option<f64>,
  pub depth: Option<Depth>,
}

impl Tick {
  fn set_instrument_token(&mut self, input: &[u8]) -> &mut Self {
    self.instrument_token = value(&input[0..=3]).unwrap();
    self.exchange = ((self.instrument_token & 0xFF) as usize).into();
    self
  }

  fn set_change(&mut self) -> &mut Self {
    self.net_change = self
      .ohlc
      .as_ref()
      .map(|o| o.close)
      .map(|close_price| {
        if let Some(last_price) = self.last_price {
          if close_price == 0_f64 {
            return None;
          } else {
            Some(((last_price - close_price) * 100.0).div(close_price))
          }
        } else {
          None
        }
      })
      .unwrap_or_default();
    self
  }
}

impl From<&[u8]> for Tick {
  fn from(input: &[u8]) -> Self {
    let mut tick = Tick::default();

    let parse_ltp = |t: &mut Tick, i: &[u8]| {
      // 0 - 4 bytes : instrument token
      t.set_instrument_token(i);
      // 4 - 8 bytes : ltp
      if let Some(bs) = i.get(4..8) {
        t.mode = Mode::LTP;
        t.last_price = price(bs, &t.exchange);
      }
    };

    let parse_quote = |t: &mut Tick, i: &[u8], is_index: bool| {
      if is_index {
        if let Some(bs) = i.get(8..28) {
          t.mode = Mode::Quote;
          // 8 - 24 bytes : ohlc
          t.ohlc = OHLC::from(&bs[0..16], &t.exchange);
          // 24 - 28 bytes : Price change
          // t.net_change = price(&bs[16..=19], &t.exchange);
          t.set_change();
        }
      } else {
        if let Some(bs) = i.get(8..44) {
          t.mode = Mode::Quote;
          // 8 - 12 bytes : last traded quantity
          t.last_traded_qty = value(&bs[0..4]);
          // 12 - 16 bytes : avg traded price
          t.avg_traded_price = price(&bs[4..8], &t.exchange);
          // 16 - 20 bytes : volume traded today
          t.volume_traded = value(&bs[8..12]);
          // 20 - 24 bytes : total buy quantity
          t.total_buy_qty = value(&bs[12..16]);
          // 24 - 28 bytes : total sell quantity
          t.total_sell_qty = value(&bs[16..20]);
          // 28 - 44 bytes : ohlc
          t.ohlc = OHLC::from(&bs[20..36], &t.exchange);

          t.set_change();
        }
      }
    };

    let parse_full = |t: &mut Tick, i: &[u8], is_index: bool| {
      if is_index {
        if let Some(bs) = i.get(28..32) {
          t.mode = Mode::Full;
          // 28 - 32 bytes : exchange time
          t.exchange_timestamp =
            value(bs).map(|x| Duration::from_secs(x.into()));
        }
      } else {
        if let Some(bs) = i.get(44..184) {
          t.mode = Mode::Full;
          // 44 - 48 bytes : last traded timestamp
          t.last_traded_timestamp =
            value(&bs[0..4]).map(|x| Duration::from_secs(x.into()));

          // 48 - 52 bytes : oi
          t.oi = value(&bs[4..8]);
          // 52 - 56 bytes : oi day high
          t.oi_day_high = value(&bs[8..12]);
          // 56 - 60 bytes : oi day low
          t.oi_day_low = value(&bs[12..16]);
          // 60 - 64 bytes : exchange time
          t.exchange_timestamp =
            value(&bs[16..20]).map(|x| Duration::from_secs(x.into()));
          // 64 - 184 bytes : market depth
          t.depth = Depth::from(&bs[20..140], &t.exchange);
        }
      }
    };

    parse_ltp(&mut tick, input);
    if !tick.exchange.is_tradable() {
      tick.is_index = true;
      tick.is_tradable = false;

      parse_quote(&mut tick, input, true);
      parse_full(&mut tick, input, true);
    } else {
      tick.is_index = false;
      tick.is_tradable = true;

      parse_quote(&mut tick, input, false);
      parse_full(&mut tick, input, false);
    }

    tick
  }
}

#[derive(Debug, Clone, Default)]
///
/// OHLC packet structure
///
pub struct OHLC {
  pub open: f64,
  pub high: f64,
  pub low: f64,
  pub close: f64,
}

impl OHLC {
  fn from(value: &[u8], exchange: &Exchange) -> Option<Self> {
    if let Some(bs) = value.get(0..16) {
      Some(OHLC {
        open: price(&bs[0..=3], exchange).unwrap(),
        high: price(&bs[4..=7], exchange).unwrap(),
        low: price(&bs[8..=11], exchange).unwrap(),
        close: price(&bs[12..=15], exchange).unwrap(),
      })
    } else {
      None
    }
  }
}

#[derive(Debug, Clone, Default)]
///
/// Market depth packet structure
///
pub struct Depth {
  pub buy: [DepthItem; 5],
  pub sell: [DepthItem; 5],
}

impl Depth {
  fn from(input: &[u8], exchange: &Exchange) -> Option<Self> {
    if let Some(bs) = input.get(0..120) {
      let parse_depth_item = |v: &[u8], start: usize| {
        v.get(start..start + 10)
          .and_then(|xs| DepthItem::from(xs, exchange))
          .unwrap_or_default()
      };
      let mut depth = Depth::default();
      for i in 0..5 {
        let start = i * 12;
        depth.buy[i] = parse_depth_item(bs, start)
      }
      for i in 0..5 {
        let start = 60 + i * 12;
        depth.sell[i] = parse_depth_item(bs, start);
      }

      Some(depth)
    } else {
      None
    }
  }
}

#[derive(Debug, Clone, Default)]
///
/// Structure for each market depth entry
///
pub struct DepthItem {
  pub qty: u32,
  pub price: f64,
  pub orders: u16,
}

impl DepthItem {
  pub fn from(input: &[u8], exchange: &Exchange) -> Option<Self> {
    if let Some(bs) = input.get(0..10) {
      Some(DepthItem {
        qty: value(&bs[0..=3]).unwrap(),
        price: price(&bs[4..=7], exchange).unwrap(),
        orders: value_short(&bs[8..=9]).unwrap(),
      })
    } else {
      None
    }
  }
}

#[derive(Debug, Clone)]
///
/// Parsed message from websocket
///
pub enum TickerMessage {
  /// Quote packets for subscribed tokens
  Ticks(Vec<TickMessage>),
  /// Error response
  Error(String),
  /// Order postback
  Order(serde_json::Value),
  /// Messages and alerts from broker
  Message(serde_json::Value),
  /// Websocket closing frame
  ClosingMessage(serde_json::Value),
}

impl From<TextMessage> for TickerMessage {
  fn from(value: TextMessage) -> Self {
    let message_type: TextMessageType = value.message_type.into();
    match message_type {
      TextMessageType::Order => Self::Order(value.data),
      TextMessageType::Error => Self::Error(value.data.to_string()),
      TextMessageType::Message => Self::Message(value.data),
    }
  }
}

#[derive(Debug, Clone, Default)]
///
/// Parsed quote packet
///
pub struct TickMessage {
  pub instrument_token: u32,
  pub content: Tick,
}

impl TickMessage {
  pub(crate) fn new(instrument_token: u32, content: Tick) -> Self {
    Self {
      instrument_token,
      content,
    }
  }
}

#[derive(Debug, Clone, Default)]
///
/// Exchange options
///
pub enum Exchange {
  #[default]
  NSE,
  NFO,
  CDS,
  BSE,
  BFO,
  BCD,
  MCX,
  MCXSX,
  INDICES,
}

impl Exchange {
  fn divisor(&self) -> f64 {
    match self {
      Self::CDS => 100_000_0.0,
      Self::BCD => 100_0.0,
      _ => 100.0,
    }
  }

  fn is_tradable(&self) -> bool {
    match self {
      Self::INDICES => false,
      _ => true,
    }
  }
}

impl From<usize> for Exchange {
  fn from(value: usize) -> Self {
    match value {
      9 => Self::INDICES,
      8 => Self::MCXSX,
      7 => Self::MCX,
      6 => Self::BCD,
      5 => Self::BFO,
      4 => Self::BSE,
      3 => Self::CDS,
      2 => Self::NFO,
      1 => Self::NSE,
      _ => Self::NSE,
    }
  }
}

#[derive(
  Debug, Clone, Deserialize, Serialize, Default, PartialEq, PartialOrd,
)]
#[serde(rename_all = "lowercase")]
///
/// Modes in which packets are streamed
///
pub enum Mode {
  Full,
  #[default]
  Quote,
  LTP,
}

impl TryFrom<usize> for Mode {
  type Error = String;
  fn try_from(value: usize) -> Result<Self, Self::Error> {
    match value {
      8 => Ok(Self::LTP),
      44 => Ok(Self::Quote),
      184 => Ok(Self::Full),
      _ => Err(format!("Invalid packet size: {}", value)),
    }
  }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
///
/// Websocket request actions
///
enum RequestActions {
  Subscribe,
  Unsubscribe,
  Mode,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
///
/// Websocket request data
///
enum RequestData {
  InstrumentTokens(Vec<u32>),
  InstrumentTokensWithMode(Mode, Vec<u32>),
}

#[derive(Debug, Clone, Deserialize, Serialize)]
///
/// Websocket request structure
///
pub struct Request {
  a: RequestActions,
  v: RequestData,
}

impl Request {
  fn new(action: RequestActions, value: RequestData) -> Request {
    Request {
      a: action,
      v: value,
    }
  }

  ///
  /// Subscribe to a list of instrument tokens
  ///
  pub fn subscribe(instrument_tokens: Vec<u32>) -> Request {
    Request::new(
      RequestActions::Subscribe,
      RequestData::InstrumentTokens(instrument_tokens),
    )
  }

  ///
  /// Subscribe to a list of instrument tokens with mode
  ///
  pub fn mode(mode: Mode, instrument_tokens: Vec<u32>) -> Request {
    Request::new(
      RequestActions::Mode,
      RequestData::InstrumentTokensWithMode(mode, instrument_tokens),
    )
  }

  ///
  /// Unsubscribe from a list of instrument tokens
  ///
  pub fn unsubscribe(instrument_tokens: Vec<u32>) -> Request {
    Request::new(
      RequestActions::Unsubscribe,
      RequestData::InstrumentTokens(instrument_tokens),
    )
  }
}

impl ToString for Request {
  fn to_string(&self) -> String {
    serde_json::to_string(self)
      .expect("failed to serialize TickerInput to JSON")
  }
}

#[derive(Debug, Clone)]
///
/// Postbacks and non-binary message types
///
enum TextMessageType {
  /// Order postback
  Order,
  /// Error response
  Error,
  /// Messages and alerts from the broker
  Message,
}

impl From<String> for TextMessageType {
  fn from(value: String) -> Self {
    match value.as_str() {
      "order" => Self::Order,
      "error" => Self::Error,
      _ => Self::Message,
    }
  }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
///
/// Postback and non-binary message structure
///
pub struct TextMessage {
  #[serde(rename = "type")]
  message_type: String,
  data: serde_json::Value,
}