-
Notifications
You must be signed in to change notification settings - Fork 1
/
overnight_open_positions.py
611 lines (527 loc) · 23 KB
/
overnight_open_positions.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# customized short strangle - overnight
# Take trades - Open positions Automation using Fyers API & Samco API
# NIFTY BANK OTM Weekly options - Overnight positions
# Samco API is for live market data
# Fyers API is for live order placement
# Friday - buy options
# Tuesday, Wednesday, Thursday - sell options
import sys
import json
import requests
import os
import os.path
import overnight_props as props
from os import path
from time import sleep
from datetime import datetime, timedelta
from fyers_api import fyersModel
from truedata_ws.websocket.TD import TD
from snapi_py_client.snapi_bridge import StocknoteAPIPythonBridge
class OpenPositions:
# check if given date is a trading holiday
@staticmethod
def check_is_trading_holiday(date):
global holiday_msg
holiday_flag = "False"
today = date.strftime("%d-%b-%Y")
weekday = date.strftime("%A")
if weekday == "Saturday" or weekday == "Sunday":
holiday_flag = "True"
holiday_msg = weekday
elif today in props.holidays2020:
holiday_flag = "True"
holiday_msg = props.holidays2020[today]
return holiday_flag
# find next trading day
@staticmethod
def next_trading_day(date):
i = 1
while True:
fut = date + timedelta(i)
if OpenPositions.check_is_trading_holiday(fut) == "False":
break
i += 1
return fut
# perform task
@staticmethod
def main_task():
global access_token, data_token
global holiday_msg
error_flag = "False"
# start time
now = datetime.now()
timestamp = now.strftime("%a %d %b %Y %H:%M:%S.%f")[:-3]
prefix = now.strftime("%b%Y")
# log file
f = "C:/MyApps/automate/overnight/" + prefix + "_BNFOTMHedging.txt"
file = open(f, "a")
try:
# check if today is a trading holiday
holiday_flg = OpenPositions.check_is_trading_holiday(now)
if holiday_flg == "True":
file.write("Open positions - Trading Holiday : " + holiday_msg + " - " + timestamp + "\n")
file.close()
# sys.exit()
return
# take positions on selective days
weekday = now.strftime("%A")
ntd = (OpenPositions.next_trading_day(now)).strftime("%A")
if ntd == 'Monday': # ntd == 'Monday' or ntd == 'Tuesday' or ntd == 'Friday':
file.write(
"Open positions - Next trading day is " + ntd + " : not taking positions" + " - " + timestamp + "\n")
file.close()
# sys.exit()
return
file.write("Open positions - start time: " + timestamp + "\n")
# main task - start
is_async = False
fyers = fyersModel.FyersModel(is_async)
# read access token
access_token = ''
tf = "C:/MyApps/automate/overnight/token.txt"
if path.exists(tf) and os.path.getsize(tf) > 0:
with open(tf, 'r') as fp:
access_token = fp.readline()
if access_token == '':
file.write("Open positions - Error: Unable to read the access token from token.txt " + "\n")
error_flag = "True"
else:
file.write("Open positions - Info: fetched access token from token.txt " + "\n")
# flg = generate_data_token(fyers=fyers, file=file) # old - market data from truedata
# if flg == "True":
# market data - fetch BNF Index spot price from samco
ltp = OpenPositions.fetch_bnf_spot_price(file=file)
if ltp != 0:
# find ce, pe strike price symbols
cesymbol, pesymbol = OpenPositions.find_otm_hedge_symbols(ltp=ltp)
file.write(
"Open positions - Info: ltp " + str(
ltp) + ", symbols " + str(cesymbol) + ", " + str(pesymbol) + "\n")
if ntd == 'Friday':
# place ATM hedge position buy orders
OpenPositions.place_buy_orders(fyers=fyers, token=access_token, ceatmsymbol=cesymbol,
peatmsymbol=pesymbol,
file=file)
else:
# place OTM hedge position sell orders
OpenPositions.place_sell_orders(fyers=fyers, token=access_token, ceotmsymbol=cesymbol,
peotmsymbol=pesymbol,
file=file)
# main task - end
# end time
now = datetime.now()
timestamp = now.strftime("%a %d %b %Y %H:%M:%S.%f")[:-3]
file.write("Open positions - end time: " + timestamp + "\n")
except Exception as e:
if file is not None:
file.write("Open positions - Exception: " + str(e) + "\n")
finally:
if file is not None:
file.close()
# if error_flag == "True":
# sys.exit(1)
# fetch bnf index spot price using samco api
@staticmethod
def fetch_bnf_spot_price(file):
error_flag = "False"
ltp = 0
samco = StocknoteAPIPythonBridge()
st = ''
# do login & get token
res = samco.login(
body={'userId': props.SAMCO_ID, 'password': props.SAMCO_PWD, 'yob': props.SAMCO_YOB})
resd = json.loads(res)
now = datetime.now()
timestamp = now.strftime("%a %d %b %Y %H:%M:%S.%f")[:-3]
if resd['status'] == 'Success':
st = resd['sessionToken']
file.write("Open positions - Info: samco login successful - " + timestamp + "\n")
else:
error_flag = "True"
file.write("Open positions - Error: samco login failed - " + timestamp + " - " + str(res) + "\n")
if error_flag == "False":
# set session token
samco.set_session_token(sessionToken=st)
# res = samco.get_quote(symbol_name=props.SAMCO_BNFFUT_TRADINGSYMBOL, exchange=samco.EXCHANGE_NFO)
# resd = json.loads(res)
# if 'status' in resd and resd['status'] == 'Success' and 'statusMessage' in resd and resd[
# 'statusMessage'] == 'Quote details retrieved successfully':
# ltp = resd['lastTradedPrice'] # ltt = resd['lastTradedTime']
# get bnf index quote
headers = {
'Accept': 'application/json',
'x-session-token': st
}
res = requests.get('https://api.stocknote.com/quote/indexQuote', params={
'indexName': props.SAMCO_BNF_INDEXNAME
}, headers=headers)
resd = res.json()
if 'status' in resd and resd['status'] == 'Success' and 'statusMessage' in resd and resd[
'statusMessage'] == 'Index Quote details retrieved successfully':
ltp = resd['spotPrice']
now = datetime.now()
timestamp = now.strftime("%a %d %b %Y %H:%M:%S.%f")[:-3]
if ltp == 0:
file.write(
"Open positions - Error: fetching BNF Index spot price from samco is failed - " + timestamp + " - " + str(
res) + "\n")
else:
ltp = int(ltp.split(".")[0])
file.write(
"Open positions - Info: fetching BNF Index spot price from samco is success - " + timestamp + "\n")
# logout
res = samco.logout()
resd = json.loads(res)
now = datetime.now()
timestamp = now.strftime("%a %d %b %Y %H:%M:%S.%f")[:-3]
if resd['status'] == 'Success':
file.write("Open positions - Info: samco logout successful - " + timestamp + "\n")
else:
error_flag = "True"
file.write("Open positions - Error: samco logout failed - " + timestamp + " - " + str(res) + "\n")
return ltp
# fetch NIFTY BANK Index LTP from TrueData
@staticmethod
def fetch_bnf_ltp(file):
global data_token
ltp = 0
error_flag = "True"
td_obj = ''
resd = ''
# symbols = ['NIFTY 50', 'NIFTY BANK', 'BANKNIFTY-I', 'BANKNIFTY-II']
symbols = [props.TD_BNF_SYMBOL]
try:
td_obj = TD(props.TD_APP_USER, props.TD_APP_PWD, broker_token=data_token)
req_ids = td_obj.start_live_data(symbols)
sleep(1) # let the data populate
for req_id in req_ids:
res = td_obj.live_data[req_id]
resd = res.__dict__
if 'symbol' in resd and 'ltp' in resd and resd['symbol'] == 'NIFTY BANK':
ltp = resd['ltp']
error_flag = "False"
now = datetime.now()
timestamp = now.strftime("%a %d %b %Y %H:%M:%S.%f")[:-3]
file.write(
"Open positions - Info: fetch NIFTY BANK Index LTP response - " + timestamp + " - " + str(
resd) + "\n")
# sleep(1)
except Exception as e:
file.write("Open positions - Exception: " + str(e) + "\n")
finally:
if td_obj is not None:
td_obj.stop_live_data(symbols)
td_obj.disconnect()
if error_flag == "True":
file.write("Open positions - Info: " + str(resd) + "\n")
file.write("Open positions - Error: Unable to fetch NIFTY BANK Index LTP, please check immediately" + "\n")
return ltp
# find OTM hedge strike price symbols
@staticmethod
def find_otm_hedge_symbols(ltp):
x = ltp % 100
if x > 75:
ltp += 100
y = ltp // 100
atmr = y * 100
atmf = (ltp // 100) * 100
now = datetime.now()
ntd = (OpenPositions.next_trading_day(now)).strftime("%A")
symbol_format = OpenPositions.find_symbol_format()
if ntd == 'Friday':
# find weekly expiry atm option symbols
cesymbol = props.FYERS_BNF_SYMBOL_PREFIX + symbol_format + str(int(atmr)) + 'CE'
pesymbol = props.FYERS_BNF_SYMBOL_PREFIX + symbol_format + str(int(atmr)) + 'PE'
else:
if ntd == 'Wednesday' or ntd == 'Thursday':
delta = props.BNFOTM_DELTA_POINTS
else:
delta = props.BNFOTM_DELTA_DEFAULT_POINTS
if (ntd == 'Wednesday' or ntd == 'Thursday') and atmr % 500 == 0: # ntd == 'Tuesday' or
ceotm = atmr
peotm = atmr
elif (ntd == 'Wednesday' or ntd == 'Thursday') and atmf % 500 == 0:
ceotm = atmf
peotm = atmf
else:
ceotm = atmr + delta
peotm = atmr - delta
# find otm prices divisible by 500
while ceotm % 500 != 0:
ceotm += 100
while peotm % 500 != 0:
peotm -= 100
# find weekly expiry otm option symbols
# ex: NSE:BANKNIFTY20O1524000CE, NSE:BANKNIFTY20OCT24000CE
cesymbol = props.FYERS_BNF_SYMBOL_PREFIX + symbol_format + str(int(ceotm)) + 'CE'
pesymbol = props.FYERS_BNF_SYMBOL_PREFIX + symbol_format + str(int(peotm)) + 'PE'
return cesymbol, pesymbol
# find expiry symbol format
@staticmethod
def find_symbol_format():
now = datetime.now()
curexp = OpenPositions.get_expiry_date(now)
nextexp = OpenPositions.get_expiry_date(curexp)
curexpmon = (curexp.strftime("%b")).upper()
nextexpmon = (nextexp.strftime("%b")).upper()
part1 = curexp.strftime("%y") # yy
# weekly option strikes - yyMdd
if curexpmon == nextexpmon:
M = curexpmon[0]
dd = curexp.strftime("%d")
part2 = M + dd
# monthly option strikes - yyMON
else:
part2 = curexpmon
symbol_format = part1 + part2
return symbol_format
# find expiry date for given date
@staticmethod
def get_expiry_date(date):
exp = ''
weekday = date.strftime("%A")
# get coming thursday
if weekday == 'Thursday':
exp = date + timedelta(7)
elif weekday == 'Friday':
exp = date + timedelta(6)
elif weekday == 'Saturday':
exp = date + timedelta(5)
elif weekday == 'Sunday':
exp = date + timedelta(4)
elif weekday == 'Monday':
exp = date + timedelta(3)
elif weekday == 'Tuesday':
exp = date + timedelta(2)
elif weekday == 'Wednesday':
exp = date + timedelta(1)
if OpenPositions.check_is_trading_holiday(exp) == "True":
exp = exp - timedelta(1)
return exp
# place bnf atm hedging position buy orders
@staticmethod
def place_buy_orders(fyers, token, ceatmsymbol, peatmsymbol, file):
# ce atm buy order
res = fyers.place_orders(
token=token,
data={
"symbol": ceatmsymbol,
"qty": props.ORDER_BNF_OTM_QTY,
"type": props.ORDER_TYPE_MARKET_ORDER,
"side": props.ORDER_SIDE_BUY,
"productType": props.ORDER_PRODUCTTYPE_MARGIN,
"limitPrice": 0,
"stopPrice": 0,
"disclosedQty": 0,
"validity": props.ORDER_VALIDITY_DAY,
"offlineOrder": props.ORDER_OFFLINEORDER, # True for AMO orders
"stopLoss": 0,
"takeProfit": 0
}
)
now = datetime.now()
timestamp = now.strftime("%a %d %b %Y %H:%M:%S.%f")[:-3]
if 'code' in res and res['code'] == 200:
file.write(
"Open positions - Info: Placing CE Long Order successful - " + timestamp + " - " + str(res) + "\n")
else:
file.write("Open positions - Error: Placing CE Long Order failed - " + timestamp + " - " + str(res) + "\n")
return
# pe atm buy order
res = fyers.place_orders(
token=token,
data={
"symbol": peatmsymbol,
"qty": props.ORDER_BNF_OTM_QTY,
"type": props.ORDER_TYPE_MARKET_ORDER,
"side": props.ORDER_SIDE_BUY,
"productType": props.ORDER_PRODUCTTYPE_MARGIN,
"limitPrice": 0,
"stopPrice": 0,
"disclosedQty": 0,
"validity": props.ORDER_VALIDITY_DAY,
"offlineOrder": props.ORDER_OFFLINEORDER, # True for AMO orders
"stopLoss": 0,
"takeProfit": 0
}
)
now = datetime.now()
timestamp = now.strftime("%a %d %b %Y %H:%M:%S.%f")[:-3]
if 'code' in res and res['code'] == 200:
file.write(
"Open positions - Info: Placing PE Long Order successful - " + timestamp + " - " + str(res) + "\n")
else:
file.write("Open positions - Error: Placing PE Long Order failed - " + timestamp + " - " + str(res) + "\n")
# exit open positions
OpenPositions.exit_positions(fyers=fyers, token=token, file=file)
return
# place bnf otm hedging position sell orders
@staticmethod
def place_sell_orders(fyers, token, ceotmsymbol, peotmsymbol, file):
# ce otm sell order
res = fyers.place_orders(
token=token,
data={
"symbol": ceotmsymbol,
"qty": props.ORDER_BNF_OTM_QTY,
"type": props.ORDER_TYPE_MARKET_ORDER,
"side": props.ORDER_SIDE_SELL,
"productType": props.ORDER_PRODUCTTYPE_MARGIN,
"limitPrice": 0,
"stopPrice": 0,
"disclosedQty": 0,
"validity": props.ORDER_VALIDITY_DAY,
"offlineOrder": props.ORDER_OFFLINEORDER, # True for AMO orders
"stopLoss": 0,
"takeProfit": 0
}
)
now = datetime.now()
timestamp = now.strftime("%a %d %b %Y %H:%M:%S.%f")[:-3]
if 'code' in res and res['code'] == 200:
file.write(
"Open positions - Info: Placing CE Short Order successful - " + timestamp + " - " + str(res) + "\n")
else:
file.write(
"Open positions - Error: Placing CE Short Order failed - " + timestamp + " - " + str(res) + "\n")
return
# pe otm sell order
res = fyers.place_orders(
token=token,
data={
"symbol": peotmsymbol,
"qty": props.ORDER_BNF_OTM_QTY,
"type": props.ORDER_TYPE_MARKET_ORDER,
"side": props.ORDER_SIDE_SELL,
"productType": props.ORDER_PRODUCTTYPE_MARGIN,
"limitPrice": 0,
"stopPrice": 0,
"disclosedQty": 0,
"validity": props.ORDER_VALIDITY_DAY,
"offlineOrder": props.ORDER_OFFLINEORDER, # True for AMO orders
"stopLoss": 0,
"takeProfit": 0
}
)
now = datetime.now()
timestamp = now.strftime("%a %d %b %Y %H:%M:%S.%f")[:-3]
if 'code' in res and res['code'] == 200:
file.write(
"Open positions - Info: Placing PE Short Order successful - " + timestamp + " - " + str(res) + "\n")
else:
file.write(
"Open positions - Error: Placing PE Short Order failed - " + timestamp + " - " + str(res) + "\n")
# exit open positions
OpenPositions.exit_positions(fyers=fyers, token=token, file=file)
return
# generate data token
@staticmethod
def generate_data_token(fyers, file):
global access_token, data_token
token_flag = "True"
try:
# generate data token aka broker token
res = fyers.generate_data_token(
token=access_token,
data={
"vendorApp": props.FYERS_VENDORAPP
}
)
if 'code' in res and res['code'] == 200:
data_token = res['data']['token_id']
# log token time
now = datetime.now()
timestamp = now.strftime("%a %d %b %Y %H:%M:%S.%f")[:-3]
file.write("Open positions - Info: data token " + str(data_token) + " - " + timestamp + "\n")
else:
file.write("Open positions - Error: Generating data token failed - " + str(res) + "\n")
token_flag = "False"
except Exception as e:
file.write("Open positions - Exception: " + str(e) + "\n")
finally:
pass
return token_flag
# validate & generate tokens
@staticmethod
def validate_and_generate_tokens(fyers, file):
global access_token, data_token
token_valid_flag = "False"
token_failed_flag = "False"
# 1st line - access token; 2nd line - data token
tf = 'C:/MyApps/automate/overnight/token.txt'
# test access token is valid
if path.exists(tf) and os.path.getsize(tf) > 0:
with open(tf, 'r') as fp:
access_token = fp.readline()
res = fyers.get_profile(token=access_token)
if 'code' in res or res['code'] == 200:
token_valid_flag = "True"
data_token = fp.readline()
# generate new tokens
if token_valid_flag == "False":
# generate access token
url = props.FYERS_TOKEN_URL
request_params = {
"fyers_id": props.FYERS_ID,
"password": props.FYERS_PWD,
"pan_dob": props.FYERS_PAN_DOB,
"appId": props.FYERS_APPID,
"create_cookie": False
}
res = requests.post(url, json=request_params)
resd = json.loads(res.text)
if 'Url' not in resd:
file.write("Open positions - Info: " + str(resd) + "\n")
file.write("Open positions - Error: Authorization failed, please check immediately" + "\n")
token_failed_flag = "True"
else:
url_str = resd['Url']
temp = (url_str.split('?')[1]).split('&')[0]
access_token = temp[13:]
# print("access_token: " + access_token)
# generate data token aka broker token
res = fyers.generate_data_token(
token=access_token,
data={
"vendorApp": props.FYERS_VENDORAPP
}
)
if 'code' not in res or res['code'] != 200:
file.write("Open positions - Info: " + str(res) + "\n")
file.write("Open positions - Error: Generating data token failed, please check immediately" + "\n")
token_failed_flag = "True"
else:
data_token = res['data']['token_id']
# print("data_token: " + data_token)
if token_failed_flag == "False":
tfile = open(tf, 'w')
tfile.write(access_token + "\n")
tfile.write(data_token + "\n")
tfile.close()
else:
tfile = open(tf, 'w') # create emtpy file
tfile.write('')
tfile.close()
file.close()
# sys.exit(1)
# exit all positions
@staticmethod
def exit_positions(fyers, token, file):
try:
res = fyers.exit_positions(
token=token
)
if ('code' in res and res['code'] == 200) or (
'code' in res and res['code'] == 400 and res[
'message'] == 'Looks like you have no open positions.'):
file.write("Open positions - Info: Exit all positions successful - " + str(res) + "\n")
else:
file.write("Open positions - Error: Exit all positions failed - " + str(res) + "\n")
except Exception as e:
file.write("Open positions - Exception: " + str(e) + "\n")
finally:
pass
# execution starts from here
if __name__ == '__main__':
OpenPositions.main_task()
# the end