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
use crate::{Call, KiteConnect, Request};
use chrono::{NaiveDate, NaiveDateTime};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
/// OHLC prices returned by the Kite quote endpoints.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Ohlc {
/// Opening price.
pub open: f64,
/// Highest price.
pub high: f64,
/// Lowest price.
pub low: f64,
/// Closing price (previous session close in quotes).
pub close: f64,
}
/// A depth level returned by GET /quote.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct DepthItem {
/// Price of the depth level.
pub price: f64,
/// Total quantity at this price.
pub quantity: i64,
/// Number of orders at this price.
pub orders: u32,
}
/// Market depth returned by GET /quote.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Depth {
/// Buy (bid) depth levels.
#[serde(default)]
pub buy: Vec<DepthItem>,
/// Sell (offer) depth levels.
#[serde(default)]
pub sell: Vec<DepthItem>,
}
/// An instrument quote returned by GET /quote.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Quote {
/// Numeric instrument identifier.
pub instrument_token: u32,
/// Time the quote was generated.
#[serde(with = "crate::time::datetime")]
pub timestamp: NaiveDateTime,
/// Last traded price.
pub last_price: f64,
/// Quantity of the last trade.
pub last_quantity: Option<i64>,
/// Time of the last trade.
#[serde(default, with = "crate::time::optional_datetime")]
pub last_trade_time: Option<NaiveDateTime>,
/// Volume weighted average traded price.
pub average_price: Option<f64>,
/// Total volume traded for the day.
pub volume: Option<i64>,
/// Total quantity pending on the buy side.
pub buy_quantity: Option<i64>,
/// Total quantity pending on the sell side.
pub sell_quantity: Option<i64>,
/// Day open, high, low and close prices.
pub ohlc: Ohlc,
/// Absolute change from the previous close.
pub net_change: f64,
/// Open interest.
pub oi: Option<f64>,
/// Day high of open interest.
pub oi_day_high: Option<f64>,
/// Day low of open interest.
pub oi_day_low: Option<f64>,
/// Lower circuit price limit.
pub lower_circuit_limit: Option<f64>,
/// Upper circuit price limit.
pub upper_circuit_limit: Option<f64>,
/// Market depth.
pub depth: Option<Depth>,
}
/// An instrument last price returned by GET /quote/ltp.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct QuoteLtp {
/// Numeric instrument identifier.
pub instrument_token: u32,
/// Last traded price.
pub last_price: f64,
}
/// An instrument OHLC quote returned by GET /quote/ohlc.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct QuoteOhlc {
/// Numeric instrument identifier.
pub instrument_token: u32,
/// Last traded price.
pub last_price: f64,
/// Day open, high, low and close prices.
pub ohlc: Ohlc,
}
/// An instrument returned by GET /instruments or GET /instruments/{exchange}.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Instrument {
/// Numeric identifier used to subscribe to market data.
pub instrument_token: u32,
/// Exchange's numeric identifier for the instrument.
pub exchange_token: u32,
/// Trading symbol of the instrument.
pub tradingsymbol: String,
/// Name of the company or instrument.
pub name: String,
/// Last traded price at the time of the dump.
pub last_price: f64,
/// Expiry date, for derivatives.
#[serde(default, with = "crate::time::optional_date")]
pub expiry: Option<NaiveDate>,
/// Strike price, for options.
pub strike: f64,
/// Minimum price movement.
pub tick_size: f64,
/// Number of units in one lot.
pub lot_size: f64,
/// Instrument type (for example `EQ`, `FUT`, `CE`, `PE`).
pub instrument_type: String,
/// Exchange segment of the instrument.
pub segment: String,
/// Exchange the instrument trades on.
pub exchange: String,
}
impl KiteConnect {
/// `GET /quote`.
/// Keyed by `EXCHANGE:SYMBOL`.
pub fn quote(&self, instruments: &[&str]) -> Call<HashMap<String, Quote>> {
Call::new(
self.clone(),
Request::get("/quote").query(instruments.iter().map(|instrument| ("i", *instrument))),
)
}
/// `GET /quote/ltp`.
/// Keyed by `EXCHANGE:SYMBOL`.
pub fn ltp(&self, instruments: &[&str]) -> Call<HashMap<String, QuoteLtp>> {
Call::new(
self.clone(),
Request::get("/quote/ltp")
.query(instruments.iter().map(|instrument| ("i", *instrument))),
)
}
/// `GET /quote/ohlc`.
/// Keyed by `EXCHANGE:SYMBOL`.
pub fn ohlc(&self, instruments: &[&str]) -> Call<HashMap<String, QuoteOhlc>> {
Call::new(
self.clone(),
Request::get("/quote/ohlc")
.query(instruments.iter().map(|instrument| ("i", *instrument))),
)
}
/// `GET /instruments`.
/// Decoded from CSV.
pub fn instruments(&self) -> Call<Vec<Instrument>> {
Call::with_decoder(
self.clone(),
Request::get("/instruments"),
crate::call::decode_csv,
)
}
/// `GET /instruments/{exchange}`.
/// Decoded from CSV.
pub fn instruments_by_exchange(&self, exchange: &str) -> Call<Vec<Instrument>> {
Call::with_decoder(
self.clone(),
Request::get(format!(
"/instruments/{}",
crate::client::path_segment(exchange)
)),
crate::call::decode_csv,
)
}
}