forked from himanjim/TradingScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.py
393 lines (293 loc) · 14.5 KB
/
Utils.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import asyncio
import csv
import datetime
import math
import pickle
import time
import traceback
from datetime import timedelta
from kiteconnect import KiteTicker
import TradingScripts.DerivativeUtils as d_util
# import Indicators as ind
import TradingScripts.ScrapUtils as sutils
import pandas as pd
from TradingScripts.PatternRecognition import *
from dateutil import parser
from kiteconnect import KiteConnect
# from upstox_api.api import *
import tempfile
NSE = 'NSE:'
NFO = 'NFO:'
UPSTOX_API_KEY = '5LfPWD6ZJh8MqTeHvikvU6USVnK6w1uk7imllV2z'
KITE_API_KEY = '453dipfh64qcl484'
KITE_API_SECRET = 'c15l0pcg2gvuw70e4i45pdmur9y1sztc'
STOCK_DATA_OBJ_FILE = 'F:/Trading_Responses/StockDataObj'
INSTRUMENT_LATEST_DATA_FILE = 'F:/IntrumentLatestDataFileName.txt'
UPSTOX_LATEST_ACCESS_CODE_FILE = 'UpstoxLatestAccessCode.txt'
KITE_LATEST_ACCESS_CODE_FILE = tempfile.gettempdir() + '/KiteLatestAccessCode.txt'
min_tick_size = .05
TRADING_HOLIDAYS = [datetime.datetime (2019, 3, 4).date (), datetime.datetime (2019, 3, 21).date (),
datetime.datetime (2019, 4, 17).date (), datetime.datetime (2019, 4, 19).date (),
datetime.datetime (2019, 5, 1).date (), datetime.datetime (2019, 6, 5).date (),
datetime.datetime (2019, 8, 12).date (), datetime.datetime (2019, 8, 15).date (),
datetime.datetime (2019, 9, 2).date (), datetime.datetime (2019, 9, 10).date (),
datetime.datetime (2019, 10, 2).date (), datetime.datetime (2019, 10, 8).date (),
datetime.datetime (2019, 10, 28).date (), datetime.datetime (2019, 11, 12).date (),
datetime.datetime (2019, 12, 25).date ()]
MARKET_START_TIME = datetime.time (9, 14, 59, 0)
MARKET_END_TIME = datetime.time (15, 30, 0)
TRADE_START_TIME = datetime.time (9, 15, 7)
oco_future_expenses = 500
def round_to_tick(x):
base = min_tick_size * 100
return round(int(base * math.ceil(float(x * 100) / base)) / 100, 2)
def get_instrument_latest_data_file_name():
with open(INSTRUMENT_LATEST_DATA_FILE, 'r') as the_file:
return the_file.readline()
return None
def get_break_even_points(lot):
return oco_future_expenses / lot
def get_no_of_subscribed_symbols(upstox_api):
earlier_subs = upstox_api.get_subscriptions ()
if earlier_subs is not None and len (earlier_subs) > 0:
if 'FULL' in earlier_subs:
return len (earlier_subs['FULL'])
elif 'LTP' in earlier_subs:
return len (earlier_subs['LTP'])
return 0
def unsubscribe_symbols(upstox_api):
earlier_subs = upstox_api.get_subscriptions()
if earlier_subs is not None and len(earlier_subs) > 0:
if 'FULL' in earlier_subs:
prev_subs = earlier_subs['FULL']
for prev_sub in prev_subs:
upstox_api.unsubscribe(upstox_api.get_instrument_by_symbol(prev_sub['exchange'], prev_sub['symbol']),
LiveFeedType.Full)
time.sleep(.5)
elif 'LTP' in earlier_subs:
prev_subs = earlier_subs['LTP']
for prev_sub in prev_subs:
upstox_api.unsubscribe(upstox_api.get_instrument_by_symbol(prev_sub['exchange'], prev_sub['symbol']),
LiveFeedType.LTP)
time.sleep(.5)
def is_trade_time():
if datetime.datetime.now ().time () > TRADE_START_TIME:
return True
return False
def is_market_open():
now = datetime.datetime.now()
if now.date() in TRADING_HOLIDAYS or now.date().weekday() > 4:
return False
if now.time() < MARKET_START_TIME or now.time() > MARKET_END_TIME:
return False
return True
async def fetch_stock_data(stock_id, fetched_stocks_data, upstox_api, start_date, end_date, exchange, stocks_data_obj,
fetch_livefeed):
time.sleep (.5)
try:
fetched_stocks_data[stock_id] = get_stock_latest_data(stock_id, upstox_api, start_date, end_date, exchange,
stocks_data_obj, fetch_livefeed)
except Exception as e:
print(traceback.format_exc())
async def fetch_stocks_data(stocks, fetched_stocks_data, upstox_api, start_date, end_date, stocks_data_obj,
fetch_livefeed):
tasks = []
for stock in stocks:
tasks.append(asyncio.ensure_future(
fetch_stock_data(stock[sutils.STOCK_ID], fetched_stocks_data, upstox_api, start_date, end_date,
stock[sutils.EXCHANGE], stocks_data_obj, fetch_livefeed)))
await asyncio.gather(*tasks)
def run_fetch_stocks_data(stocks_latest_info, fetched_stocks_data, upstox_api, start_date, end_date, stocks_data_obj,
fetch_livefeed):
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(
fetch_stocks_data(stocks_latest_info, fetched_stocks_data, upstox_api, start_date, end_date,
stocks_data_obj, fetch_livefeed))
finally:
loop.close()
def get_stock_date_str_for_pickle(stock_id, end_date):
return stock_id + '#' + end_date.strftime("%Y_%m_%d")
def cancel_order(upstox_api, symbol):
orders = upstox_api.get_order_history()
for order in orders:
if order['symbol'] == symbol:
upstox_api.cancel_order(order['order_id'])
def check_if_instrument_in_positions(upstox_api, symbol, time_out):
start_time = time.time ()
while True:
for position in upstox_api.get_positions():
if position['symbol'] == symbol:
return True
if (time.time() - start_time) > time_out:
return False
time.sleep(2)
return False
def not_too_long_or_short_candle(stock_data, last_10_day_stock_data):
# open = stock_data['open']
# close = [stock_data['close'], stock_data['cp']][stock_data['cp'] > stock_data['close']]
# range = (abs (close - open) / ((open + close) / 2)) * 100
# return 1 < range < 20
total_candle_length = 0
for stock in last_10_day_stock_data:
total_candle_length += abs (stock['close'] - stock['open'])
return abs (stock_data['close'] - stock_data['open']) >= total_candle_length / len (last_10_day_stock_data)
def get_panda_series_of_stock_closing_prices(stock_data,type_of_price='close'):
stock_data_closing_prices = []
for stock_session_data in stock_data:
stock_data_closing_prices.append(stock_session_data[type_of_price])
return pd.Series(stock_data_closing_prices)
def nearly_equal(var1, var2, variation_percent):
return math.isclose (var1, var2, rel_tol=variation_percent / 100)
def get_current_day_current_price(stock_data):
stock_data_date = datetime.datetime.fromtimestamp(stock_data['timestamp'] / 1000)
if (stock_data_date.date () == datetime.datetime.today ().date ()) and is_market_open ():
return stock_data['cp']
else:
return stock_data['close']
def calculate_last_10_days_average_volume(last_10_day_stock_data):
return sum (stock['volume'] for stock in last_10_day_stock_data) / len (last_10_day_stock_data)
# def intialize_upstox_api(contracts):
# with open (UPSTOX_LATEST_ACCESS_CODE_FILE, 'r') as the_file:
# upstox_latest_access_code = the_file.readline ()
#
# upstox_api = Upstox (UPSTOX_API_KEY, upstox_latest_access_code)
#
# for contract in contracts:
# upstox_api.get_master_contract (contract)
#
# return upstox_api
def intialize_kite_api():
with open (KITE_LATEST_ACCESS_CODE_FILE, 'r') as the_file:
kite_latest_access_code = the_file.readline ()
kite = KiteConnect (api_key=KITE_API_KEY)
try:
kite.set_access_token(kite_latest_access_code)
except Exception as e:
print("Authentication failed", str(e))
raise
return kite
def intialize_kite_ticker():
with open (KITE_LATEST_ACCESS_CODE_FILE, 'r') as the_file:
kite_latest_access_code = the_file.readline ()
kite = KiteConnect (api_key=KITE_API_KEY)
return KiteTicker(KITE_API_KEY, kite_latest_access_code)
def trim_date(end_date):
new_date = end_date
while new_date in TRADING_HOLIDAYS:
new_date = new_date - timedelta (days=1)
while new_date.weekday () > 4:
new_date = new_date - timedelta (days=1)
return new_date
# def get_stock_latest_data(stock_id, upstox_api, start_date, end_date, exchange, stocks_data_obj=None,
# fetch_livefeed=True, interval=OHLCInterval.Day_1):
# stock_data = None
#
# end_date = trim_date (end_date)
# if stocks_data_obj is not None:
# stocks_data_obj_key = get_stock_date_str_for_pickle(stock_id, end_date)
# if stocks_data_obj_key in stocks_data_obj:
# stock_data = stocks_data_obj[stocks_data_obj_key]
#
# if stock_data is None:
# print('Empty cache for stock', stock_id)
# stock_data = upstox_api.get_ohlc (upstox_api.get_instrument_by_symbol (exchange, stock_id), interval,
# start_date, end_date)
#
# if stocks_data_obj is not None:
# stocks_data_obj_key = get_stock_date_str_for_pickle (stock_id,
# get_date_from_timestamp(
# int(stock_data[-1]['timestamp'])))
# stocks_data_obj[stocks_data_obj_key] = stock_data
# pickle.dump (stocks_data_obj, open (STOCK_DATA_OBJ_FILE, 'wb'))
#
# if fetch_livefeed:
# stock_live_feed_data = upstox_api.get_live_feed (upstox_api.get_instrument_by_symbol (exchange, stock_id),
# LiveFeedType.Full)
#
# live_feed_date = datetime.datetime.fromtimestamp(stock_live_feed_data['timestamp'] / 1000)
# if live_feed_date.date () == datetime.datetime.today ().date ():
# volume = 0
# if 'vtt' in stock_live_feed_data:
# volume = stock_live_feed_data['vtt']
#
# stock_data.append({'timestamp': stock_live_feed_data['timestamp'], 'open': stock_live_feed_data['open'],
# 'high': stock_live_feed_data['high'], 'low': stock_live_feed_data['low'],
# 'close': stock_live_feed_data['ltp'], 'volume': volume,
# 'cp': stock_live_feed_data['ltp'], 'yearly_low': stock_live_feed_data['yearly_low'],
# 'yearly_high': stock_live_feed_data['yearly_high']})
#
# for stock in stock_data:
# stock.update(
# {'yearly_low': stock_live_feed_data['yearly_low'], 'yearly_high': stock_live_feed_data['yearly_high']})
#
# for stock in stock_data:
# stock['close'] = float(stock['close'])
# stock['open'] = float(stock['open'])
# stock['high'] = float(stock['high'])
# stock['low'] = float(stock['low'])
# stock['timestamp'] = int(stock['timestamp'])
# if 'volume' in stock:
# stock['volume'] = int(stock['volume'])
#
# return stock_data
def get_future_historical_data(stock_id, future_current_month_historical_data, future_near_month_historical_data,
future_far_month_historical_data):
with open(d_util.FUTURE_FILE_LOCATION + stock_id + d_util.FUTURE_FILE_SUFFIX) as csvfile:
futures_data = csv.reader(csvfile, delimiter=',', quotechar='"')
row_count = 0
for future_data in futures_data:
if row_count > 0:
try:
data_set = {'close': float (future_data[6].strip ()), 'high': float (future_data[4].strip ()),
'low': float (future_data[5].strip ()), 'ltp': float (future_data[7].strip ()),
'date': parser.parse (future_data[1].strip ()),
'expiry': parser.parse (future_data[2].strip ())}
if row_count % 3 == 1:
future_current_month_historical_data.append (data_set)
if row_count % 3 == 2:
future_near_month_historical_data.append (data_set)
if row_count % 3 == 0:
future_far_month_historical_data.append (data_set)
except Exception as e:
print (traceback.format_exc (), ' in data:' + str (future_data))
row_count += 1
def get_equity_historical_data(stock_id):
data = []
with open(d_util.EQ_FILE_LOCATION + stock_id + d_util.EQ_FILE_SUFFIX) as csvfile:
equity_datas = csv.reader (csvfile, delimiter=',', quotechar='"')
row_count = 0
for equity_data in equity_datas:
if row_count > 0:
try:
data_set = {'open': float (equity_data[4].strip ()), 'close': float (equity_data[8].strip ()),'high': float (equity_data[5].strip ()), 'low': float (equity_data[6].strip ()), 'ltp': float (equity_data[7].strip ()), 'date': parser.parse (equity_data[2].strip ()), 'symbol': equity_data[0].strip (), 'prev_close': float (equity_data[3].strip ()), 'volume': float (equity_data[10].strip ())}
data.append (data_set)
except Exception as e:
print (traceback.format_exc (), ' in data:' + str (equity_data))
row_count += 1
return data
# def get_rsi_14_9_period_SMA(stock_data_closing_prices_series):
# rsi_series = ind.rsi (stock_data_closing_prices_series, 14)
# sma = ind.sma (rsi_series, 9)
# return sma
def get_date_from_timestamp(timestamp):
return datetime.datetime.fromtimestamp(timestamp / 1000)
def is_number(s):
try:
float (s)
return True
except ValueError:
return False
def remove_non_no_chars(s):
return re.sub('[^0-9\\.]', '', s)
def remove_non_alphanum_chars(s):
return re.sub('[^0-9a-zA-Z]', '', s)
def convert_nav_str_to_str(s):
if s is None:
return ''
return s.string
def get_target(buy_price, stoploss, rrr, action):
if action.value == Action.LONG.value:
return (rrr * (buy_price - stoploss)) + buy_price
else:
return buy_price - (rrr * (stoploss - buy_price))