Straddlecode
Straddlecode
while True:
import time
import datetime
from datetime import datetime
def get_current_time():
current_time = datetime.now()
time_str = current_time.strftime('%H%M%S')
return int(time_str)
current_time = get_current_time()
if 90000 <= current_time <= 150100:
import re
sd = api.searchscrip('NFO', 'NIFTY')
sd = (sd['values'])
for Symbol in sd:
(Symbol['tsym'])
for i in ob.itertuples():
if i.status == 'TRIGGER_PENDING':
ret = api.cancel_order(i.norenordno) # cancel all
if i.status == 'OPEN':
ret = api.cancel_order(i.norenordno)
for i in k.itertuples():
if int(i.netqty) < 0:
api.place_order(buy_or_sell='B', product_type=i.prd,
exchange=i.exch, tradingsymbol=i.tsym, quantity=abs(int(i.netqty)), discloseqty=0,
price_type='MKT', price=0, trigger_price=None,
retention='DAY', remarks='Buy_All')
if int(i.netqty) > 0:
api.place_order(buy_or_sell='S', product_type=i.prd,
exchange=i.exch, tradingsymbol=i.tsym, quantity=int(i.netqty), discloseqty=0,
price_type='MKT', price=0, trigger_price=None,
retention='DAY', remarks='Sell_All')
return True
except Exception:
return False
exit_flag = False
while True:
current_time = get_current_time()
if current_time > 150000:
print("Time is greater than 150000")
break
elif current_time > 92000:
ret = api.get_quotes(exchange='NSE', token='26000')
time.sleep(3)
ltp = ret.get("lp")
ltp = float(ltp)
ltp_str = str(ltp)
sym = ret.get("symname")
ExpDate = Expiry_date
TYPE = "P"
Strike = int(round(ltp/50,0)*50)
For_token = sym+ExpDate+TYPE+str(Strike)
CE_SYMBOL = sym+ExpDate+'C'+str(Strike)
PE_SYMBOL = sym+ExpDate+'P'+str(Strike)
print(CE_SYMBOL,PE_SYMBOL)
#print(Strike)
CE_order = api.place_order(buy_or_sell='S',
product_type='I',
exchange='NFO', tradingsymbol=CE_SYMBOL,
quantity=50,
discloseqty=0,price_type='MKT', price=0, trigger_price=0,
retention='DAY', remarks='CEleg')
time.sleep(2)
CE_orderno = CE_order['norenordno']
PE_order = api.place_order(buy_or_sell='S',
product_type='I',
exchange='NFO',
tradingsymbol=PE_SYMBOL,
quantity=50,
discloseqty=0,price_type='MKT', price=0, trigger_price=0,
retention='DAY', remarks='PEleg')
time.sleep(2)
PE_orderno = PE_order['norenordno']
if PE_order['stat'] == 'Ok' and CE_order['stat'] == 'Ok':
print(f"Order {CE_orderno} & {PE_orderno} Status is ok
")
else:
print(CE_order['emsg'],PE_order['emsg'])
time.sleep(3)
print(get_order_status(CE_orderno),"FOR CE LEG")
print(get_order_status(PE_orderno),"FOR PE LEG")
CE_fillprice = (get_fillprice(CE_orderno)) # should print
'86406'
PE_fillprice = (get_fillprice(PE_orderno)) # should print
'86407'
print(CE_fillprice,PE_fillprice)
CE_fillprice = float(CE_fillprice)
PE_fillprice = float(PE_fillprice)
SL_CE = float(format(round(CE_fillprice * 0.9 / 0.05) *
0.05,'.2f'))
SL_PE = float(format(round(PE_fillprice * 0.9 / 0.05) *
0.05,'.2f'))
SLT_CE = format(round(SL_CE * .97 / 0.05) * 0.05,'.2f')
SLT_PE = format(round(SL_PE * .97 / 0.05) * 0.05,'.2f')
print(SL_PE,SLT_PE)
print(SL_CE,SLT_CE )
PE_orderSL = api.place_order(buy_or_sell='B',
product_type='I',
exchange='NFO', tradingsymbol=PE_SYMBOL,
quantity=50, discloseqty=0,price_type='SL-
LMT', price=SL_PE, trigger_price=SLT_PE,
retention='DAY', remarks='my_PE-SL')
time.sleep(3)
CE_orderSL = api.place_order(buy_or_sell='B',
product_type='I',
exchange='NFO',
tradingsymbol=CE_SYMBOL,
quantity=50,
discloseqty=0,price_type='SL-LMT', price=SL_CE, trigger_price=SLT_CE,
retention='DAY', remarks='my_CE-
SL')
time.sleep(3)
SL_CE_Number = CE_orderSL['norenordno']
SL_PE_Number = PE_orderSL['norenordno']
print(SL_CE_Number,SL_PE_Number)
time.sleep(3)
print(get_order_status(SL_CE_Number),"FOR SL CE LEG")
print(get_order_status(SL_PE_Number),"FOR SL PE LEG")
time.sleep(5)
while True:
mtm = get_daily_mtm()
try:
Sl_mtm = mtm <= (-Mtm_SL)
Target_mtm = mtm >= Mtm_target
except TypeError:
pass
try:
if Sl_mtm or Target_mtm:
print('MTM SL OR TARGET Exiting positions and
cancelling all standing orders ')
universal_exit()
exit_flag = True # set flag to exit outer loop
break # exit inner loop
except NameError:
pass
time.sleep(1)