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, 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.
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']
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)
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
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.
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)
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}]