Incorrect P&L in positions call

Sidney
Sidney edited April 2022 in Python client
Hi there

I am seen a outdated pnl in the position call made via python to the kite connect module.

Date returned from positions = pd.DataFrame(kite.positions()['net'])
tradingsymbol sell_value pnl buy_quantity sell_quantity
0 GODREJIND 4960 16 0 10

Actual Pnl on zerodha site is 12.50 at the same time
MIS GODREJIND NSE -10 496.00 494.75 +12.50 -0.25%
Total +12.50
  • Sidney
    Hi Srijan

    I have seen that thread. Even the manual pnl calculation below is not giving me the correct data
    my code is structured like this for a sell trade that has been placed:
    sellValue = pos['sell_value']
    netQuantity = pos['quantity']
    lastPrice = pos['last_price']
    multiplier = pos['multiplier']
    buyValue = lastPrice * abs(netQuantity)
    profit = (sellValue - buyValue)

    Value returned through API:
    tradingsymbol sell_value pnl buy_quantity sell_quantity last_price
    0 GODREJIND 4960 22.5 0 10 493.75

    position in Kite terminal:
    Product Instrument Qty. Avg. LTP P&L Chg.
    MIS GODREJIND NSE -10 496.00 493.85 +21.50 -0.43%
    Total +21.50
  • Sidney
    Shouldn't the kite call return the accurate PNL so a decision can be made on when to place a corresponding buy for a short trade?
  • SRIJAN
    SRIJAN edited April 2022
    You have to use last price from websocket/quote for the calculation.
  • sujith
    @Sidney,
    We don't want users to keep polling positions API just to get P&L value. You can fetch positions when an order is placed or trade happens.
    Calculate the realtime P&L using the Websocket API data and make your decision.
  • Sidney
    thanks Srijan. I tried using the websocket for the realtime ltp but it keeps failing. That is why I resorted to using the positions 'last_price' value

    My code as follows:
    def on_ticks(ws, ticks):
    logging.debug("Ticks: {}".format(ticks))


    def on_connect(ws, response):
    # Callback on successful connect.
    # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
    ws.subscribe([instrument])
    ws.set_mode(ws.MODE_LTP, [instrument])

    Code in main area:(kiteconnect code and setting access
    kws = KiteTicker(api_key, access_token)
    pos = pd.DataFrame(kite.positions()['net'])
    instrument = pos['instrument_token']

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

    Keep getting this error even though I am not performing any computation:
    ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: close reason without close code)
    ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: close reason without close code)
    ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: close reason without close code)
    ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: close reason without close code)
    ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: close reason without close code)
    ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: close reason without close code)
    ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: close reason without close code)
  • Sidney
    Thanks for your Amazing support. Once I added int() to the variable it worked. doh!

    instrument = [int(pos['instrument_token'].values)]

    2796801
    Ticks: [{'tradable': True, 'mode': 'ltp', 'instrument_token': 2796801, 'last_price': 493.75}]
    Ticks: [{'tradable': True, 'mode': 'ltp', 'instrument_token': 2796801, 'last_price': 493.75}]
    Ticks: [{'tradable': True, 'mode': 'ltp', 'instrument_token': 2796801, 'last_price': 493.95}]
Sign In or Register to comment.