instrument_dump = kite.instruments("NSE") instrument_df = pd.DataFrame(instrument_dump) instrument_df.to_csv("NSE_Instruments.csv",index=False) scrip = {1270529:{'name':'ICICIBANK'},738561:{'name':'RELIANCE'},779521:{'name':'SBIN'},55483143:{'name':'SILVERM20AUGFUT'}} quantity=1 order_id_market=0 order_id_slm=0 order_id_limit=0 order_id_market_add1=0 order_id_slm_add1=0 order_id_limit_add1=0 TOKENS = [55483143]#SILVERM DATABASE = {token:{'o':[],'h':[],'l':[],'c':[],'dateTime':[],'ltp':[],'ph':[],'pl':[]} for token in TOKENS} CANDLE = 60 # in seconds starttime = 0 pivot_high = 100000 # At 9:15am provide the value as 100000. In the middle of the day provide recent PH value pivot_low = 20 # At 915:am provide the value as 20. In the middle of the day provide recent PL value prev_ph2=200000 # should be greater than pivot_high prev_pl2=10 # should be leass than pivot_low import datetime kws = KiteTicker(api_key, data["access_token"]) def on_ticks(ws, ticks): print("inside main on_ticks") def on_connect(ws, response): global starttime starttime = time.time() # Callback on successful connect. # Subscribe to a list of instrument_tokens (RELIANCE and ACC here). ws.subscribe(TOKENS) # ws.set_mode(ws.MODE_FULL, [738561]) # def on_close(ws, code, reason): # ws.stop() # Assign the callbacks. kws.on_ticks = on_ticks kws.on_connect = on_connect # kws.on_close = on_close # Infinite loop on the main thread. Nothing after this will run. # You have to use the pre-defined callbacks to manage subscriptions. kws.connect(threaded=True) while True: def on_ticks(ws, ticks): strategy_logic(ticks) def strategy_logic(ticks): global starttime global DATABSE global order_id_market global order_id_slm global order_id_limit global data global pivot_high global pivot_high_prev global pivot_low global pivot_low_prev global prev_ph2 global prev_pl2 for tick in ticks: print("inside for loop") DATABASE[tick['instrument_token']]['ltp'].append(tick['last_price']) # print(DATABASE) print("time before CANDLE if cond", time.time()) if(time.time()-starttime>CANDLE): print("inside if - CANDLE condition") print(time.time()) for tick in ticks: print("inside for before passing ohlc") DATABASE[tick['instrument_token']]['dateTime'].append(datetime.datetime.now().time()) DATABASE[tick['instrument_token']]['o'].append(DATABASE[tick['instrument_token']]['ltp'][0]) DATABASE[tick['instrument_token']]['h'].append(max(DATABASE[tick['instrument_token']]['ltp'])) DATABASE[tick['instrument_token']]['l'].append(min(DATABASE[tick['instrument_token']]['ltp'])) DATABASE[tick['instrument_token']]['c'].append(DATABASE[tick['instrument_token']]['ltp'][-1]) DATABASE[tick['instrument_token']]['ltp'] = [] starttime = time.time() if len(DATABASE[tick['instrument_token']]['h'])<=3: print("inside if <=3") DATABASE[tick['instrument_token']]['ph'].append(pivot_high) elif len(DATABASE[tick['instrument_token']]['h'])>3: print("inside ELif >3") if (((DATABASE[tick['instrument_token']]['h'][-2]>DATABASE[tick['instrument_token']]['h'][-3]) and (DATABASE[tick['instrument_token']]['h'][-2]>DATABASE[tick['instrument_token']]['h'][-1]))or ((DATABASE[tick['instrument_token']]['h'][-3]>DATABASE[tick['instrument_token']]['h'][-4]) and (DATABASE[tick['instrument_token']]['h'][-2]==DATABASE[tick['instrument_token']]['h'][-3]) and (DATABASE[tick['instrument_token']]['h'][-2]>DATABASE[tick['instrument_token']]['h'][-1]))): DATABASE[tick['instrument_token']]['ph'].append(DATABASE[tick['instrument_token']]['h'][-2]) else: DATABASE[tick['instrument_token']]['ph'].append(DATABASE[tick['instrument_token']]['ph'][-1]) if len(DATABASE[tick['instrument_token']]['l'])<=3: print("inside if for PL <=3") DATABASE[tick['instrument_token']]['pl'].append(pivot_low) elif len(DATABASE[tick['instrument_token']]['l'])>3: print("inside if for PL >3") if (((DATABASE[tick['instrument_token']]['l'][-2]pivot_high) and ("bought" not in scrip[instrument].values())and ("sl2hit" not in scrip[instrument].values()): scrip[instrument]['buystatus'] = "bought" print("5:ltp>pivot_high") if (pivot_high==prev_ph2) and ("sl1hit" not in scrip[instrument].values()): scrip[instrument]['sl1hitstatus'] = "sl1hit" print("6:inside innser if") elif (pivot_high==prev_ph2) and ("sl1hit" in scrip[instrument].values()) and ("sl2hit" not in scrip[instrument].values()): scrip[instrument]['sl2hitstatus'] = "sl2hit" print("7:inside inner elif") order_id = kite.place_order(tradingsymbol=symbol,exchange=kite.EXCHANGE_MCX,transaction_type=kite.TRANSACTION_TYPE_BUY, quantity=quantity,order_type=kite.ORDER_TYPE_MARKET,product=kite.PRODUCT_MIS,variety=kite.VARIETY_REGULAR) order_id_market=order_id order_id = kite.place_order(tradingsymbol=symbol,exchange=kite.EXCHANGE_MCX, transaction_type=kite.TRANSACTION_TYPE_SELL, quantity=quantity,order_type=kite.ORDER_TYPE_SLM, trigger_price=pivot_high-0.5, product=kite.PRODUCT_MIS,variety=kite.VARIETY_REGULAR) order_id_slm=order_id order_id = kite.place_order(tradingsymbol=symbol,exchange=kite.EXCHANGE_MCX, transaction_type=kite.TRANSACTION_TYPE_SELL, quantity=quantity,order_type=kite.ORDER_TYPE_LIMIT, price=pivot_high+0.5, product=kite.PRODUCT_MIS,variety=kite.VARIETY_REGULAR) order_id_limit=order_id prev_ph2=pivot_high if (ltp