efficient way to continuously check for a price

Guhan
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?
  • amkdev
    can you just check it when you're receiving the tick, on your on message handler?
  • Guhan
    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
  • amkdev
    amkdev edited December 2019
    No. On Message Handler Is Called When Ever There Is A Message.
    I will write an example in Node.js

    testSocket.onmessage = function(e) {
    // Message Is Coming
    if(e.data instanceof ArrayBuffer) {
    if(e.data.byteLength > 2) {
    var d = parseBinary(e.data);
    d.forEach( entry => {
    if( entry.instrument_token == yourInstrumentToken ) {
    if( entry.last_price == yourTriggerPrice )
    // Place Order
    }
    } )
    }
    }
    }
  • Guhan
    Guhan edited December 2019
    please correct me if I am wrong, on_message will be called whenever a new tick or message is received. Also how to unpack the binary using python?
  • sujith
    @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).
  • Guhan
    @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.
  • Guhan
    @amkdev @sujith, I have one more doubt, if I place orders from on_message will it effect receiving ticks? I am so confused right now.
  • tahseen
    @Guhan

    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
  • Guhan
    @tahseen thanks for the suggestion, I will look into it
Sign In or Register to comment.