Not getting 'last_price' through websocket

nicetochatting
,
  • nicetochatting
    nicetochatting edited March 13

    in this code,
    i) if enter_into_trade is executed: when i try to fetch last_price using websockets, it shows keyerror:Last_price.
    ii) if enter_into_trade is not executed (maybe because it is not the right time now): when i try to fetch last price, it gets executed.

    What i have to do here is that i have to stop the code manually after enter_into_trade() is executed and re-run it.
    help please
  • nicetochatting
    nicetochatting edited March 13
    .
  • nicetochatting


    def on_order_update(ws, data):
    global websocket
    # print('order update as per websocket')
    websocket = 1

    def on_ticks(ws, ticks):
    for tick in ticks:
    inst_token = tick['instrument_token']
    last_price = tick['last_price']
    abc[inst_token]['last_price'] = last_price

    def on_connect(ws, response):
    ws.subscribe(list(stock.keys()))
    ws.set_mode(ws.MODE_LTP, list(stock.keys()))

    def fetch_positions():
    global abc
    global stock
    data = kite.positions()
    stock = {}
    for entry in data['net']:
    instrument_token = entry['instrument_token']
    trading_symbol = entry['tradingsymbol']
    abc[instrument_token] = {} # Initialize the nested dictionary if not present
    qty = entry['quantity']
    sell_value = entry['sell_value']
    buy_value = entry['buy_value']
    abc[instrument_token]['trading_symbol'] = trading_symbol
    abc[instrument_token]['qty'] = qty
    abc[instrument_token]['sell_value'] = sell_value
    abc[instrument_token]['buy_value'] = buy_value
    stock[instrument_token] = trading_symbol

    fetch_positions()


    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.connect(threaded=True)

    def continuous_runner(self):
    global websocket
    global abc
    while True:
    try:
    if time_condition:
    enter_into_trade()

    websocket = 0
    time.sleep(2)
    fetch_positions()
    for x in abc.values():
    if x['trading_symbol'] == 'NIFTY':
    last_price_1 = str(x['last_price'])

  • nicetochatting
    nicetochatting edited March 13
    .
Sign In or Register to comment.