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'}} # scrip = {738561:{'name':'RELIANCE'},779521:{'name':'SBIN'}} quantity=1 # entryvalue = 2000 # stoploss=1790 # squareoff=2010 # entryvalue_sell = 2000 # stoploss_sell=2010 # squareoff_sell=1800 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 = [738561, 5633] # TOKENS = [738561, 5633] # TOKENS = [56409351,55483143] # TOKENS = [738561, 779521] TOKENS = [55483143]#ICICIBANK DATABASE = {token:{'o':[],'h':[],'l':[],'c':[],'dateTime':[],'ltp':[],'ph':[],'pl':[]} for token in TOKENS} # print(scrip[738561['name') CANDLE = 60 # in seconds starttime = 0 pivot_high = 100000 pivot_low = 20 prev_ph2=200000 prev_pl2=10 import datetime kws = KiteTicker(api_key, data["access_token"]) def on_ticks(ws, 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("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(99999) 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(00000) 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