|
1 |
| -# The Kite Connect API Go client |
| 1 | +# kiteapi |
2 | 2 |
|
3 |
| -The official Go client for communicating with the Kite Connect API. |
| 3 | +The source is taken from project OTAMIA which is private. |
4 | 4 |
|
5 |
| -Kite Connect is a set of REST-like APIs that expose many capabilities required to build a complete investment and trading platform. Execute orders in real time, manage user portfolio, stream live market data (WebSockets), and more, with the simple HTTP API collection. |
| 5 | +# UNOFFICIAL |
6 | 6 |
|
7 |
| -Zerodha Technology (c) 2018. Licensed under the MIT License. |
| 7 | +This api is unofficial although, it source codes are borrowed from official API. This is **incompatible** with official api of Zerodha. |
8 | 8 |
|
9 |
| -## Documentation |
| 9 | +# Major Differences |
10 | 10 |
|
11 |
| -- [Client API documentation - GoDoc](https://godoc.org/github.com/zerodhatech/gokiteconnect) |
12 |
| -- [Kite Connect HTTP API documentation](https://kite.trade/docs/connect/v3) |
| 11 | +1. Supports OI flag in historical data. |
| 12 | +2. The error is displayed in debug mode and you will not receive error = nil when there is one. - __FIX__ |
| 13 | +3. NewConnect and NewTicker are the functions instead of two packages with New. - __Improvement__ |
| 14 | +4. The structure used for Ticker data and Connect data is now common. - __Improvement__ (Major) |
| 15 | +5. int64 is used everywhere while offical discrepancy is: uint32 in ticker VS and int64 in connect that in official API. __Improvement__ which avoids various conversions. |
| 16 | +6. Fixed the minute historical data date is not parsed in known utility when doing unmarshal - Converting the JSON to Struct was making dates as 0000-00-00 __FIX__ (Major) |
13 | 17 |
|
14 |
| -## Installation |
| 18 | +# Status |
15 | 19 |
|
16 |
| -``` |
17 |
| -go get github.com/zerodhatech/gokiteconnect |
18 |
| -``` |
| 20 | +This is used in the production environment and I will improve it wherever needed! |
19 | 21 |
|
20 |
| -## API usage |
21 | 22 |
|
22 |
| -```go |
23 |
| -package main |
24 |
| - |
25 |
| -import ( |
26 |
| - "fmt" |
27 |
| - |
28 |
| - "github.com/zerodhatech/gokiteconnect" |
29 |
| -) |
30 |
| - |
31 |
| -const ( |
32 |
| - apiKey string = "my_api_key" |
33 |
| - apiSecret string = "my_api_secret" |
34 |
| -) |
35 |
| - |
36 |
| -func main() { |
37 |
| - // Create a new Kite connect instance |
38 |
| - kc := kiteconnect.New(apiKey) |
39 |
| - |
40 |
| - // Login URL from which request token can be obtained |
41 |
| - fmt.Println(kc.GetLoginURL()) |
42 |
| - |
43 |
| - // Obtained request token after Kite Connect login flow |
44 |
| - requestToken := "request_token_obtained" |
45 |
| - |
46 |
| - // Get user details and access token |
47 |
| - data, err := kc.GenerateSession(requestToken, apiSecret) |
48 |
| - if err != nil { |
49 |
| - fmt.Printf("Error: %v", err) |
50 |
| - return |
51 |
| - } |
52 |
| - |
53 |
| - // Set access token |
54 |
| - kc.SetAccessToken(data.AccessToken) |
55 |
| - |
56 |
| - // Get margins |
57 |
| - margins, err := kc.GetUserMargins() |
58 |
| - if err != nil { |
59 |
| - fmt.Printf("Error getting margins: %v", err) |
60 |
| - } |
61 |
| - fmt.Println("margins: ", margins) |
62 |
| -} |
63 |
| -``` |
64 |
| - |
65 |
| -## Kite ticker usage |
66 |
| - |
67 |
| -```go |
68 |
| -package main |
69 |
| - |
70 |
| -import ( |
71 |
| - "fmt" |
72 |
| - "time" |
73 |
| - |
74 |
| - kiteconnect "github.com/zerodhatech/gokiteconnect" |
75 |
| - "github.com/zerodhatech/gokiteconnect/ticker" |
76 |
| -) |
77 |
| - |
78 |
| -var ( |
79 |
| - ticker *kiteticker.Ticker |
80 |
| -) |
81 |
| - |
82 |
| -// Triggered when any error is raised |
83 |
| -func onError(err error) { |
84 |
| - fmt.Println("Error: ", err) |
85 |
| -} |
86 |
| - |
87 |
| -// Triggered when websocket connection is closed |
88 |
| -func onClose(code int, reason string) { |
89 |
| - fmt.Println("Close: ", code, reason) |
90 |
| -} |
91 |
| - |
92 |
| -// Triggered when connection is established and ready to send and accept data |
93 |
| -func onConnect() { |
94 |
| - fmt.Println("Connected") |
95 |
| - err := ticker.Subscribe([]uint32{53718535}) |
96 |
| - if err != nil { |
97 |
| - fmt.Println("err: ", err) |
98 |
| - } |
99 |
| -} |
100 |
| - |
101 |
| -// Triggered when tick is recevived |
102 |
| -func onTick(tick kiteticker.Tick) { |
103 |
| - fmt.Println("Tick: ", tick) |
104 |
| -} |
105 |
| - |
106 |
| -// Triggered when reconnection is attempted which is enabled by default |
107 |
| -func onReconnect(attempt int, delay time.Duration) { |
108 |
| - fmt.Printf("Reconnect attempt %d in %fs\n", attempt, delay.Seconds()) |
109 |
| -} |
110 |
| - |
111 |
| -// Triggered when maximum number of reconnect attempt is made and the program is terminated |
112 |
| -func onNoReconnect(attempt int) { |
113 |
| - fmt.Printf("Maximum no of reconnect attempt reached: %d", attempt) |
114 |
| -} |
115 |
| - |
116 |
| -// Triggered when order update is received |
117 |
| -func onOrderUpdate(order kiteconnect.Order) { |
118 |
| - fmt.Printf("Order: ", order.OrderID) |
119 |
| -} |
120 |
| - |
121 |
| -func main() { |
122 |
| - apiKey := "my_api_key" |
123 |
| - accessToken := "my_access_token" |
124 |
| - |
125 |
| - // Create new Kite ticker instance |
126 |
| - ticker = kiteticker.New(apiKey, accessToken) |
127 |
| - |
128 |
| - // Assign callbacks |
129 |
| - ticker.OnError(onError) |
130 |
| - ticker.OnClose(onClose) |
131 |
| - ticker.OnConnect(onConnect) |
132 |
| - ticker.OnReconnect(onReconnect) |
133 |
| - ticker.OnNoReconnect(onNoReconnect) |
134 |
| - ticker.OnTick(onTick) |
135 |
| - ticker.OnOrderUpdate(onOrderUpdate) |
136 |
| - |
137 |
| - // Start the connection |
138 |
| - ticker.Serve() |
139 |
| -} |
140 |
| -``` |
141 |
| - |
142 |
| -# Examples |
143 |
| - |
144 |
| -Check [examples folder](https://github.com/zerodhatech/gokiteconnect/tree/master/examples) for more examples. |
145 |
| - |
146 |
| -You can run the following after updating the API Keys in the examples: |
147 |
| - |
148 |
| -```bash |
149 |
| -go run examples/connect/basic/connect.go |
150 |
| -``` |
151 |
| - |
152 |
| -## Run unit tests |
153 |
| - |
154 |
| -``` |
155 |
| -go test -v |
156 |
| -``` |
157 |
| - |
158 |
| -## Changelog |
159 |
| - |
160 |
| -[Check CHANGELOG.md](CHANGELOG.md) |
161 | 23 |
|
0 commit comments