Extracting positions under on_tick()

RP3436
When I use :
"
kite=KiteConnect(api_key,api_secret)
kite.set_access_token('vy5jnl7piqyvkprjh0tolur3yndnyl4g')
positions = kite.positions()
positions
"
I get a dictionary as expected. However , I want to know how to call kite.positions() in on_tick method . I tried it and got the following message:
2018-03-13 11:47:04,243 - root - INFO - Connecting to WebSocket...
connection established at 2018-03-13 11:47:06.056505
2018-03-13 11:47:06,072 - root - INFO - Successfully connected to WebSocket
2018-03-13 11:47:09,275 - root - INFO - Subscribing to 12152578
2018-03-13 11:47:09,494 - urllib3.connectionpool - DEBUG - Starting new HTTPS connection (1): api.kite.trade
2018-03-13 11:47:09,791 - urllib3.connectionpool - DEBUG - https://api.kite.trade:443 "GET /portfolio/positions?access_token=vy5jn**************g&api_key=h4*********8kxlz8y HTTP/1.1" 200 None

Any hint for using kite.positions() in on_tick as the strategies are executed under on_tick and positions are also required to be known only there?
Regards
  • sujith
    sujith edited March 2018
    I wouldn't make API call for each tick. You should fetch and store it in a variable and use it inside on_tick.
  • RP3436
    @sujith Thanks. But the positions would keep changing. This may help for the first "BUY or "SELL" trigger , but what about the subsequent triggers.. So while I understand that API call should not be made to fetch positions on every tick but even when it is to be need based say when the second signal is generated by the strategy (first already executed using the positions fetched outside on_tick method) , and I need to evaluate the signal based on existing positions, how can I get the same fetched outside on_tick without breaking the for loop? My requirement is to get the positions at the resampling frequency.
    If moving between the on_tick and the function for fetching the positions is the normal solution then I am limited with the knowledge , else I stand helped with a clarification.
  • sujith
    You should only fetch positions initially and store it. A position can only change when you place an order or order is filled. You can fetch positions when you get order update via Kite Ticker(websockets) or postbacks.
  • RP3436
    @sujith thanks. I could solve it. And that too calling kite.positions() inside on_tick. I call it at the completion of a candle which happens to be 5 minutes in my case.
    You may kindly close the thread.
This discussion has been closed.