My websocket writes all captured tick to mongodb and an infinite loop runs on mongodb to check whether target price is reached to place an order. Instead of using a infinite while loop on mongodb data, is there any other efficient way?
I am a novice in coding, according to my understanding on_message handler is active only when WS gets disconnected. I use on_message handler to handle WS error. So how to check price in that, can you please help me with a sample code? Think of my need as SLM or SL-Limit order, only that the price check is done in my end and limit or market order is placed
@Guhan, All the tick data are received in the binary format whereas order updates are received as a text message. Hence on_ticks callback is used to read ticks and on_order_update is for listening to order updates and on_message is for listening to promo stuff which I don't think you need (this is used to show promo stuff on Kite).
@sujith, so using on_message for checking and placing is not a good idea? any ideas how to do it? Because for the past two years I am using infinite loop to constantly fetch price from mongodb and check the price. I am looking for a better way to do it.
Use python's Queue. Use global one access from anywhere in program
Firstly, in on_ticks limit yourself to receiving ticks. Python has internal Queue object, use it to pass ticks to it here. Don't overload on_ticks with any other thing. It it not meant to be flooded with logical or computational code. That would be a bad coding style
Now in your own custom function, which has to monitor and place order, you would keep reading the messages from this Queue. When the instrument price is reached, you can call your function to execute trade
def on_ticks # Here just pass ticks into python built in Queue
def watch_my_price # here use while loop to read from Queue
Queue has to be a global one so that it can be used in both on_ticks and any of your other function in program to read from Queue
I will write an example in Node.js
All the tick data are received in the binary format whereas order updates are received as a text message. Hence on_ticks callback is used to read ticks and on_order_update is for listening to order updates and on_message is for listening to promo stuff which I don't think you need (this is used to show promo stuff on Kite).
Use python's Queue. Use global one access from anywhere in program
Firstly, in on_ticks limit yourself to receiving ticks. Python has internal Queue object, use it to pass ticks to it here. Don't overload on_ticks with any other thing. It it not meant to be flooded with logical or computational code. That would be a bad coding style
Now in your own custom function, which has to monitor and place order, you would keep reading the messages from this Queue. When the instrument price is reached, you can call your function to execute trade
def on_ticks
# Here just pass ticks into python built in Queue
def watch_my_price
# here use while loop to read from Queue
Queue has to be a global one so that it can be used in both on_ticks and any of your other function in program to read from Queue