-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconnection.py
306 lines (235 loc) · 9.25 KB
/
connection.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
from smartapi import SmartWebSocket as WebSocket
from smartapi import SmartConnect
from config import authInfo
from stockTokens import stockTokens
import dataFilter as df
import toCsv
from strategy import strategy
from indicators import preparePastData
import json
from copy import deepcopy
import sys
import datetime as dt
import numpy as np
import pandas as pd
import os
def apiLogin():
smartApi= SmartConnect(api_key= authInfo['apiKey'])
login= smartApi.generateSession(authInfo['clientCode'], authInfo['password'])
refreshToken= login['data']['refreshToken']
feedToken= smartApi.getfeedToken()
#profile= smartApi.getProfile(refreshToken)
# print('login jwt token: ', login['data']['jwtToken'])
# print('auth may be: ', smartApi.Authorization)
smartApi.generateToken(refreshToken)
print("successfully logged in")
print('ft', feedToken)
return {
'login': login,
'feedToken': feedToken,
'apiObj': smartApi
}
def convertTimeStamp(timestampString):
l= timestampString.split('T')
date, time= l[0], l[1]
timestampString= date + ' ' + time.split('+')[0]
return dt.datetime.strptime(timestampString, '%Y-%m-%d %H:%M:%S')
def getHistoricData(symbol, interval, fromDate, toDate): # (fromDate is exclusive, toDate is inclusive)
# agar boht sara data aane wala hai toh usse chunks mai call krna, ie. period ko parts mai break krna aur parallely pichhle wale ko operate krna
apiLoginObj= apiLogin()
apiObj= apiLoginObj['apiObj']
try:
tkn= stockTokens[symbol]
except:
print('wrong stock symbol maybe, check stocksTokens.py for reference')
sys.exit()
reqObj= {
'exchange': 'NSE',
'symboltoken': tkn,
'interval': interval,
'fromdate': fromDate,
'todate': toDate
}
res= apiObj.getCandleData(reqObj)
# data= json.loads(r.text)
if res['message']!= 'SUCCESS':
print('connection error occurred, try again 2-3 times')
sys.exit()
data= res['data']
print('successfully connected to historical api')
# data= df.historicDataFilter(data)
# print comment krna hai baad mai
for i in range(len(data)):
data[i][0]= convertTimeStamp(data[i][0])
return data
# # abb isse send kar skte hain
def getDateRange():
# Format: '2021-05-20 16:00'
# past 15 days is enough
a= dt.timedelta(days= 15)
x= dt.datetime.now()
y= x - a
return str(y.date()) + ' 09:00', str(x.date()) + ' 09:00'
def prepareStockData(symbol):
date1, date2= getDateRange()
try:
tkn= stockTokens[symbol]
except:
print('wrong stock symbol maybe, check stocksTokens.py for reference')
sys.exit()
print(date1, date2)
# '2021-06-24 09:15', '2021-07-09 09:15'
data= getHistoricData(symbol, 'FIVE_MINUTE', date1, date2)
data= np.array(data)
tempDic= {}
columns= ['intervalStartTime', 'intervalOpen', 'intervalHigh', 'intervalLow', 'currentPrice', 'intervalVolume']
for i, key in enumerate(columns):
temp= []
for j in data:
temp.append(j[i])
tempDic[key]= temp
df= pd.DataFrame(tempDic)
df= preparePastData(df, tkn, symbol)
df.to_csv(os.path.join(os.getcwd(), 'daySummary', 'tatamotor.csv'), index= False)
#*********************************
feedString= ''
# interval sumary for 1 stock, there shall be a dictionary of many such dictionaries
intervalSummary= {
'intervalStartTime': 'NA',
'lastStrategyTime': 'NA',
'lastStrategyPrice': 'NA',
'symbol': 'NA',
'token': 'NA',
'currentPrice':'NA',
'intervalOpen': 'NA',
'intervalHigh': float('-inf'),
'intervalLow': float('inf'),
'dayHigh': float('-inf'),
'dayLow': float('inf'),
'buyOffers': 'NA',
'sellOffers': 'NA',
'bestBuyPrice': 'NA',
'bestSellPrice': 'NA',
'bestBuyVol': 'NA',
'bestSellVol': 'NA',
'vwap': 'NA',
'intervalVolume': 0,
'commulativeVolume': 0,
'intervalOpenVolume': 0,
'commulativeTotal': 0,
'ema50':'NA',
'ema9':'NA',
'ema13':'NA',
'ema26':'NA',
'currentResistance':'NA',
'currentSupport':'NA',
'pivotpoint': 'NA',
'r1': 'NA',
'r2': 'NA',
'r3': 'NA',
's1': 'NA',
's2': 'NA',
's3': 'NA'
}
tokenSymbolMap= {} # reverse of stocktokens dictionary
daySummary= {} # dictionary of interval summaries of all stocks in consideration
def roundOffTime(x, gap):
y= ''
if gap=='minute':
y= dt.datetime(x.year, x.month, x.day, x.hour, 5*(x.minute//5) , 0, 0)
elif gap=='second':
y= dt.datetime(x.year, x.month, x.day, x.hour, x.minute, 10* (x.second//10), 0)
else:
print('unidentified gap value for roundoffTime function')
sys.exit()
return y
def temp(ws, message):
print(message)
def onTick(ws, tick):
global intervalSummary, daySummary, tokenSymbolMap
# comment krni hai ye line
print('Tick: ', tick)
for i in tick:
for key in i:
try:
if key!= 'tk':
i[key]= float(i[key])
except:
continue
##############################
print('name: ', i.get('name', 'undefined'), end= ' -> ')
# print('tick: ', i)
try:
if i.get('name', 'undefined')== 'sf':
if not i['tk'] in daySummary:
temp= deepcopy(intervalSummary)
temp['symbol']= tokenSymbolMap[i['tk']] # example 'TATACHEM'
temp['token']= i['tk'] # example '3561'
daySummary[i['tk']]= temp
daySummary[i['tk']]['intervalStartTime']= roundOffTime(dt.datetime.now(), 'minute')
daySummary[i['tk']]['intervalOpen']= i['ltp']
daySummary[i['tk']]['intervalStartTime']= daySummary[i['tk']]['intervalStartTime']
daySummary[i['tk']]['lastStrategyTime']= roundOffTime(dt.datetime.now(), 'minute')
x= df.socket(i, daySummary[i['tk']])
if x!=0:
daySummary[i['tk']]= x
# ------------- save to CSV after 5 minutes------------------
diff= dt.datetime.now() - daySummary[i['tk']]['intervalStartTime']
if diff.total_seconds() >= 5*60: # 5 mins
toCsv.newEntry(daySummary[i['tk']], i['tk'], daySummary[i['tk']]['symbol'])
# -----------------------------------------------------------------------------
daySummary[i['tk']]['intervalStartTime']= roundOffTime(dt.datetime.now(), 'minute') # roundoff to latest 5 min
# -----------------------------------------------------------------------------
daySummary[i['tk']]['intervalOpen']= daySummary[i['tk']]['currentPrice']
daySummary[i['tk']]['intervalHigh']= daySummary[i['tk']]['currentPrice']
daySummary[i['tk']]['intervalLow']= daySummary[i['tk']]['currentPrice']
daySummary[i['tk']]['intervalVolume']= 0
daySummary[i['tk']]['intervalOpenVolume']= daySummary[i['tk']]['commulativeVolume']
# ------------- apply strategy function after 10 sec ------------------
diff= dt.datetime.now() - daySummary[i['tk']]['lastStrategyTime']
if diff.total_seconds() >= 10:
strategy(daySummary[i['tk']])
daySummary[i['tk']]['lastStrategyTime']= roundOffTime(dt.datetime.now(), 'second') # roundoff to latest 10 sec
except:
print('some error might have occured. But nothing to worry')
def onConnect(ws):
global feedString
print('connecting')
print(feedString)
# ws.websocket_connection()
ws.subscribe('mw', "nse_cm|2885&nse_cm|1594&nse_cm|11536")
def onClose(ws):
print("connection closed!!")
def onError(ws, error):
print('ERROR OCCURRED\n--------------')
print(error, '\n------------')
def createSocketConnection(symbols):
feedToken= apiLogin()['feedToken']
print('ft2', feedToken)
tokenList= []
global tokenSymbolMap
for i in symbols:
if i in stockTokens:
tokenSymbolMap[stockTokens[i]]= i # eg. '3461': 'TATACHEM'
tokenList.append('nse_cm|'+stockTokens[i])
else:
print('{} is not a recognized symbol, check stockTokens.py for reference\ntry again'.format(i))
sys.exit()
global feedString
feedString= '&'.join(tokenList)
print(feedString)
ss= WebSocket(feedToken, authInfo['clientCode'])
ss._on_open= onConnect
# ss._on_message= onTick
ss._on_message= temp
ss._on_error= onError
ss._on_close= onClose
ss.connect()
print(feedToken)
#*****************************
# space for debugging
if __name__=='__main__':
#apiLogin()
prepareStockData('TATAMOTORS')
# stockList= ['TATAMOTORS', 'RELIANCE']
# createSocketConnection(stockList)