Navigation

    SmartAPI Forum
    • Register
    • Login
    • Search
    • Categories
    • Popular
    • Groups
    • FAQs
    • API Docs

    Websocket Connection automatically closes after placing a order

    Python SDK
    0
    3
    25
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      rohit4417k last edited by

      Hi @Moderator_1 , @Moderator_2 , @Moderator_3

      I am using Websocket to get the live feed. Now, upon reaching a specific LTP, I am placing a order. But after it, the websocket is getting automatically closed.
      Here is a part of my logic inside on_data() method:

      symbol1 = "BANKNIFTY26JUN24"+str(strikePrice)+"CE"
      symbol2 = "BANKNIFTY26JUN24"+str(strikePrice)+"PE"
      orderParam1 = {
      "variety": "NORMAL",
      "tradingsymbol": symbol1,
      "symboltoken": str(name_to_token[symbol1]),
      "transactiontype": "SELL",
      "exchange": "NFO",
      "ordertype": "MARKET",
      "producttype": "CARRYFORWARD",
      "duration": "DAY",
      "price": "0",
      "squareoff": "0",
      "stoploss": "0",
      "quantity": "15"
      }
      orderParam2 = {
      "variety": "NORMAL",
      "tradingsymbol": symbol2,
      "symboltoken": str(name_to_token[symbol2]),
      "transactiontype": "SELL",
      "exchange": "NFO",
      "ordertype": "MARKET",
      "producttype": "CARRYFORWARD",
      "duration": "DAY",
      "price": "0",
      "squareoff": "0",
      "stoploss": "0",
      "quantity": "15"
      }
      orderCE = smartApi.placeOrder(orderParam1)
      orderPE = smartApi.placeOrder(orderParam2)
      orderPriceCE = currCE
      orderPricePE = currPE
      orderSymbolCE = symbol1
      orderSymbolPE = symbol2
      logger.info(orderCE)
      logger.info(orderPE)

      Here is part of log file: -

      I 240626 13:58:30 index:247] Ticks: {'subscription_mode': 1, 'exchange_type': 2, 'token': '59624', 'sequence_number': 43031516, 'exchange_timestamp': 1719390510000, 'last_traded_price': 14445, 'subscription_mode_val': 'LTP'}
      [I 240626 13:58:30 index:247] Ticks: {'subscription_mode': 1, 'exchange_type': 2, 'token': '59623', 'sequence_number': 43031943, 'exchange_timestamp': 1719390510000, 'last_traded_price': 2870, 'subscription_mode_val': 'LTP'}
      [I 240626 13:58:31 index:247] Ticks: {'subscription_mode': 1, 'exchange_type': 2, 'token': '63893', 'sequence_number': 43032659, 'exchange_timestamp': 1719390511000, 'last_traded_price': 7950, 'subscription_mode_val': 'LTP'}
      [I 240626 13:58:31 index:247] Ticks: {'subscription_mode': 1, 'exchange_type': 2, 'token': '63893', 'sequence_number': 43032992, 'exchange_timestamp': 1719390511000, 'last_traded_price': 8055, 'subscription_mode_val': 'LTP'}
      [I 240626 13:58:31 index:252] Prices Crossed! 52900:7975,8055
      [I 240626 13:58:32 index:291] 240626001102493
      [I 240626 13:58:32 index:292] 240626001102559
      [W 240626 13:58:32 smartWebSocketV2:319] Attempting to resubscribe/reconnect (Attempt 1)..

      After logging the orderID, the websocket immediately gets closed. I have tried multiple times, it happens only after the last statement of on_data() has been executed. Its like it closes automatically as if It has nothing do more.
      Kindly help me, I don't want it to get disconnected.

      P 1 Reply Last reply Reply Quote 0
      • P
        projectSB @rohit4417k last edited by

        @rohit4417k Are you placing orders from the same thread as Websocket?
        If yes, then it will disconnect as order placement makes the Websocket out of sync. Always, use a separate thread for monitoring data and placing orders etc. Your Websocket thread should ONLY fetch the data and store in some data structure.

        I had faced this issue, and this was my way of resolving it. Others might have different opinion as well.

        R 1 Reply Last reply Reply Quote 0
        • R
          rohit4417k @projectSB last edited by

          @projectSB Hey, Thanks mate. I suspected the same issue. Thanks for the reply.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post