-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkiteImp.go
130 lines (110 loc) · 3.33 KB
/
kiteImp.go
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
package main
import (
"fmt"
"strconv"
"time"
kiteconnect "github.com/zerodhatech/gokiteconnect"
"github.com/zerodhatech/gokiteconnect/ticker"
)
var (
ticker *kiteticker.Ticker
ticker2 *kiteticker.Ticker
ticker3 *kiteticker.Ticker
)
var tokens = []uint32{}
var tokens2 = []uint32{}
var tokens3 = []uint32{}
// Triggered when any error is raised
func onError(err error) {
fmt.Println("Error: ", err)
}
// Triggered when websocket connection is closed
func onClose(code int, reason string) {
fmt.Println("Close: ", code, reason)
}
// Triggered when connection is established and ready to send and accept data
func onConnect() {
fmt.Println("Connected")
err := ticker.Subscribe(tokens)
if err != nil {
fmt.Println("onConnect", err)
}
}
func onConnect2() {
fmt.Println("Connected")
err := ticker2.Subscribe(tokens2)
if err != nil {
fmt.Println("onConnect", err)
}
}
func onConnect3() {
fmt.Println("Connected")
err := ticker3.Subscribe(tokens3)
if err != nil {
fmt.Println("onConnect", err)
}
}
// Triggered when tick is recevived
func onTick(tick kiteticker.Tick) {
go setValueRedis(strconv.Itoa(int(tick.InstrumentToken)), strconv.Itoa(int(tick.InstrumentToken))+":"+strconv.FormatFloat(tick.LastPrice, 'f', -1, 64)+":"+strconv.FormatFloat(tick.OHLC.Close, 'f', -1, 64))
go alerting(strconv.Itoa(int(tick.InstrumentToken)), strconv.FormatFloat(tick.LastPrice, 'f', -1, 64))
}
// Triggered when reconnection is attempted which is enabled by default
func onReconnect(attempt int, delay time.Duration) {
fmt.Println("Reconnect attempt ", attempt, " in ", delay.Seconds())
}
// Triggered when maximum number of reconnect attempt is made and the program is terminated
func onNoReconnect(attempt int) {
fmt.Println("Maximum no of reconnect attempt reached: ", attempt)
}
func connectToKite() {
ticker = kiteticker.New(apiKey, accessToken)
ticker2 = kiteticker.New(apiKey, accessToken)
ticker3 = kiteticker.New(apiKey, accessToken)
loadTickersToSubscribe()
// Assign callbacks
ticker.OnError(onError)
ticker.OnClose(onClose)
ticker.OnConnect(onConnect)
ticker.OnReconnect(onReconnect)
ticker.OnNoReconnect(onNoReconnect)
ticker.OnTick(onTick)
// Assign callbacks
ticker2.OnError(onError)
ticker2.OnClose(onClose)
ticker2.OnConnect(onConnect2)
ticker2.OnReconnect(onReconnect)
ticker2.OnNoReconnect(onNoReconnect)
ticker2.OnTick(onTick)
// Assign callbacks
ticker3.OnError(onError)
ticker3.OnClose(onClose)
ticker3.OnConnect(onConnect3)
ticker3.OnReconnect(onReconnect)
ticker3.OnNoReconnect(onNoReconnect)
ticker3.OnTick(onTick)
// Start the connection
go ticker.Serve()
go ticker2.Serve()
go ticker3.Serve()
}
func loadTickersToSubscribe() {
// pass tickers
kiteclient := kiteconnect.New(apiKey)
instruments, err := kiteclient.GetInstrumentsByExchange("NSE")
if err != nil {
fmt.Println("parseTokens", err)
return
}
for _, instrument := range instruments {
fmt.Println(instrument.InstrumentToken, instrument.Tradingsymbol)
if len(tokens) < 3000 {
tokens = append(tokens, uint32(instrument.InstrumentToken))
} else if len(tokens) < 6000 {
tokens2 = append(tokens2, uint32(instrument.InstrumentToken))
} else if len(tokens) < 9000 {
tokens3 = append(tokens3, uint32(instrument.InstrumentToken))
}
}
}