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
//! # Zrsclient
//!
//! <div style="display:inline-block"><pre class="warning" style="white-space:normal;font:inherit;">
//!
//! **Warning**: This SDK is in active development. Please do not use for live trading
//! </pre></div>
//!
//! zrsclient is an effort to create a Rust based SDK over the API provided by the indian stock
//! broker Zerodha. We strive to actively maintain this SDK to follow changes that zerodha makes to
//! their APIs.
//!
//! We have also included retry mechanisms to handle throttling requests on the REST API, to ease the
//! algorithm writer's pain of handling unnecessary panics from recoverable errors.
//!
//!
//!
//! # Example
//!
//! ```
//! use zrsclient::connect::KiteConnect;
//! use serde_json::{Value as JsonValue};
//! use zrsclient::config::ZrsClientConfig;
//! fn main(){
//! // Create a KiteConnect client. Place your credentials in ~/.zrsclient folder
//! let config = ZrsClientConfig{
//! api_key:Some("<API_KEY>"),
//! access_token:Some("<ACCESS_TOKEN>"),
//! base_url: Some("<KITE_API_URL>".to_string())
//! };
//! let connect = KiteConnect::new(Some(config));
//!
//! // Set a count on how many times you want zrsclient to retry before panicking.
//! // If you set the retry count to 0 the client will not perform any retries on recoverable failures.
//! let retry_count_from_recoverable_errors = 10;
//! let data: JsonValue = connect.holdings(retry_count_from_recoverable_errors);
//! }
//!
//! ```
//!
//!
//! # Credentials file
//!
//! For the SDK to detect credentials. Place your credentials in the path ~/.zrsclient/credentials
//!
//!
//! # Example for Websocket
//!
//! ```
//! use zrsclient::ticker::{KiteTicker, KiteTickerHandler, WebSocketHandler};
//!
//! fn main() {
//! #[derive(Debug)]
//! pub struct CustomHandler {
//! tokens: Vec<u32>,
//! mode: String,
//! _count: u32,
//! }
//!
//! impl KiteTickerHandler for CustomHandler {
//! fn on_open<T>(&mut self, _ws: &mut WebSocketHandler<T>)
//! where T: KiteTickerHandler {
//! // Subscribe to a list of tokens on opening the websocket connection
//! _ws.subscribe(self.tokens.clone()).unwrap();
//! _ws.set_mode(&self.mode, self.tokens.clone()).unwrap();
//! // println!("Fellow on_open callback");
//! }
//! fn on_ticks<T>(&mut self, _ws: &mut WebSocketHandler<T>, tick: Vec<serde_json::Value>)
//! where T: KiteTickerHandler {
//! println!("{:?}", tick);
//! // println!("Fellow on_ticks callback");
//! }
//!
//! fn on_close<T>(&mut self, _ws: &mut WebSocketHandler<T>)
//! where T: KiteTickerHandler {
//! println!("Fellow on_close callback");
//! }
//!
//! fn on_error<T>(&mut self, _ws: &mut WebSocketHandler<T>)
//! where T: KiteTickerHandler {
//! println!("Fellow on_error callback");
//! }
//! }
//!
//! let tokens: Vec<u32> = vec![256265, 260105];
//! let mut ticker = KiteTicker::new("<API_KEY>", "<ACCESS_TOKEN>");
//!
//! let handler = CustomHandler{tokens: tokens, mode: "ltp".to_string(), _count: 10};
//!
//! ticker.connect(handler, None).unwrap();
//!
//! loop {}
//! }
//! ```