-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkite_ticker_producer.py
163 lines (94 loc) · 4.26 KB
/
kite_ticker_producer.py
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
import redis
from kiteconnect import KiteTicker
import _thread, sys, json, os
from datetime import datetime, date, time, timedelta
import calendar
from time import sleep
from queue import Queue
import pandas as pd
df = pd.read_csv("~/kite_candles/instruments.csv", low_memory = False)
userdata = pd.read_json("~/kite_candles/userdata")
redis_client = redis.Redis('localhost')
messageQueue = Queue()
dataQueue = Queue()
write_queue = Queue()
instrumentMap = {}
allinstrumentMap = {}
user_id = userdata['YOURNAME'].user
api_key = userdata['YOURNAME'].apikey
access_token = redis_client.hget("token.{}".format(user_id), "access_token").decode("utf-8")
def read_queue():
global allinstrumentMap
while True:
bulk_ticks = messageQueue.get()
redis_client.publish('ticks', f'{bulk_ticks}')
def data_queue():
while True:
dataToWrite = dataQueue.get()
timestamp = dataToWrite.get('timestamp')
stockname = allinstrumentMap.get(dataToWrite.get('instrument_token'))
open_price = dataToWrite['ohlc'].get('open',0)
high_price = dataToWrite['ohlc'].get('high',0)
low_price = dataToWrite['ohlc'].get('low',0)
close_price = dataToWrite['ohlc'].get('close',0)
buy_quantity = dataToWrite.get('buy_quantity',0)
last_price = dataToWrite.get('last_price',0)
sell_quantity = dataToWrite.get('sell_quantity',0)
volume = dataToWrite.get('volume',0)
redis_client.hset("tick_last", stockname, last_price)
redis_client.hset("tick_open", stockname, open_price)
redis_client.hset("tick_high", stockname, high_price)
redis_client.hset("tick_low", stockname, low_price)
redis_client.hset("tick_close", stockname, close_price)
redis_client.hset("tick_buyer", stockname, buy_quantity)
redis_client.hset("tick_seller", stockname, sell_quantity)
def on_order_update(ws, data):
global user_id
try:
filehandle = open("{}-order.log".format(user_id), "a")
filehandle.write("{}\n".format(json.dumps(data)))
filehandle.close()
except:
pass
def on_ticks(ws , ticks):
dt = datetime.now()
if dt.time() >= time(9,0) and dt.time() < time(15,31):
messageQueue.put(ticks)
else:
print("exiting")
kws.close()
os._exit(0)
def on_connect(ws, response):
global df, instrumentMap, allinstrumentMap
weekday = datetime.now().weekday()
expiry = ''
if weekday <= 3:
expiry = (datetime.now().today() + timedelta(days=3 - weekday)).date().isoformat()
if weekday >= 4:
expiry = (datetime.now().today() + timedelta(days=6)).date().isoformat()
fno_nearest_expiry = sorted(df[(df.segment == 'NFO-FUT')].expiry)[0]
options = df[(df.expiry <= expiry) & (df.segment == 'NFO-OPT') & df.instrument_type.isin(['CE','PE']) & (df.name.isin(['BANKNIFTY','NIFTY']))]
front_month = date(datetime.now().date().year, datetime.now().date().month,1) + timedelta(days=70)
front_month = date(front_month.year, front_month.month, calendar.monthrange(front_month.year, front_month.month)[1])
front_month = front_month.isoformat()
indices = df[(df.segment == 'INDICES') & (df.name.isin(['NIFTY 50','NIFTY BANK']))]
vix = df[(df.segment == 'INDICES') & (df.name == 'INDIA VIX')]
fno = df[(df.exchange == 'NFO') & (df.instrument_type == 'FUT') & (df.expiry <= front_month)]
stocks = df[(df.segment== 'NSE') & (df.exchange == 'NSE') & ~(df.name.isna()) & (df.tradingsymbol.isin(fno.name.to_list()))]
final = pd.concat([fno , stocks, indices, vix])
instrument_tokens = final.instrument_token.to_list()
instrumentMap = dict(zip(final.instrument_token, final.tradingsymbol))
allinstrumentMap = dict(zip(df.instrument_token, df.tradingsymbol))
ws.subscribe(instrument_tokens)
ws.set_mode(ws.MODE_FULL,instrument_tokens)
print(final.shape[0])
stocks = indices = fno = final = None
_thread.start_new_thread(read_queue, ())
_thread.start_new_thread(read_queue, ())
_thread.start_new_thread(read_queue, ())
_thread.start_new_thread(read_queue, ())
kws = KiteTicker(api_key, access_token)
kws.on_ticks = on_ticks
kws.on_order_update = on_order_update
kws.on_connect = on_connect
kws.connect()