-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkc_stream_sqlite3.py
100 lines (80 loc) · 3.09 KB
/
kc_stream_sqlite3.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
from kiteconnect import KiteTicker, KiteConnect
import datetime
import sys
import pandas as pd
import os
import sqlite3
#generate trading session
access_token = open("access_token.txt",'r').read()
key_secret = open("api_key.txt",'r').read().split()
kite = KiteConnect(api_key=key_secret[0])
kite.set_access_token(access_token)
# db = sqlite3.connect('D:/Udemy/Zerodha KiteConnect API/7_streaming_data/ticks.db')
def create_tables(tokens):
c=db.cursor()
for i in tokens:
c.execute("CREATE TABLE IF NOT EXISTS TOKEN{} (ts datetime primary key,price real(15,5), volume integer)".format(i))
try:
db.commit()
except:
db.rollback()
def insert_ticks(ticks):
c=db.cursor()
for tick in ticks:
try:
tok = "TOKEN"+str(tick['instrument_token'])
vals = [tick['timestamp'],tick['last_price'], tick['volume']]
query = "INSERT INTO {}(ts,price,volume) VALUES (?,?,?)".format(tok)
c.execute(query,vals)
except:
pass
try:
db.commit()
except:
db.rollback()
#get dump of all NSE instruments
instrument_dump = kite.instruments("NSE")
instrument_df = pd.DataFrame(instrument_dump)
def tokenLookup(instrument_df,symbol_list):
"""Looks up instrument token for a given script from instrument dump"""
token_list = []
for symbol in symbol_list:
token_list.append(int(instrument_df[instrument_df.tradingsymbol==symbol].instrument_token.values[0]))
return token_list
#####################update ticker list######################################
tickers = ["ZEEL","WIPRO","VEDL","ULTRACEMCO","UPL","TITAN","TECHM","TATASTEEL",
"TATAMOTORS","TCS","SUNPHARMA","SBIN","SHREECEM","RELIANCE","POWERGRID",
"ONGC","NESTLEIND","NTPC","MARUTI","M&M","LT","KOTAKBANK","JSWSTEEL","INFY",
"INDUSINDBK","IOC","ITC","ICICIBANK","HDFC","HINDUNILVR","HINDALCO",
"HEROMOTOCO","HDFCBANK","HCLTECH","GRASIM","GAIL","EICHERMOT","DRREDDY",
"COALINDIA","CIPLA","BRITANNIA","INFRATEL","BHARTIARTL","BPCL","BAJAJFINSV",
"BAJFINANCE","BAJAJ-AUTO","AXISBANK","ASIANPAINT","ADANIPORTS"]
#############################################################################
#create KiteTicker object
kws = KiteTicker(key_secret[0],kite.access_token)
tokens = tokenLookup(instrument_df,tickers)
#create table
create_tables(tokens)
def on_ticks(ws,ticks):
insert_tick=insert_ticks(ticks)
print(ticks)
def on_connect(ws,response):
ws.subscribe(tokens)
ws.set_mode(ws.MODE_FULL,tokens)
while True:
now = datetime.datetime.now()
if (now.hour >= 9 and now.minute >= 15 ):
kws.on_ticks=on_ticks
kws.on_connect=on_connect
kws.connect()
if (now.hour >= 15 and now.minute >= 30):
sys.exit()
db.close()
"""
c.execute('SELECT name from sqlite_master where type= "table"')
c.fetchall()
c.execute('''PRAGMA table_info(TOKEN975873)''')
c.fetchall()
for m in c.execute('''SELECT * FROM TOKEN975873'''):
print(m)
"""