What would be the best approach to continuously update a limit order?
Let's say I have an equity, MSFT, in my on data method, I obtain the current bid/ask spread, then create a buy limit order at the current bid price. The order is sent to the broker.
I understand that this will generate an OrderEvent that will be consumed by the OnOrderEvent method and within this method I can check the status.
My question is how can I continuous update the limit order so that I can obtain the best fill price. For example, let's say MSFT is trading at 215,1.0 and current bid is 215.05 and current ask is 215.20. I'd like to place initial bid price at 215.05, then wait 3 seconds, if it doesn't fill, then update the limit order to 215.06, and the process continues until filled.
Where in the algorithm would be the best place to code this? In the OnOrderEvent? Or should it somehow be done in the OnData method? Or in another method? Also, how can I handle this asynchronously so that it doesn't block my code.
Any help would be greatly appreciated.
Python is preferred.
Ryan Riordon
Hello Nkabram,
OnData runs every minute if you have minutely resolution. I would probably in "Initialize" make a timer to keep track eg. "self.timer = 0". Since we know that the OnData cycles on your resolution if set to minute then if we put "self.timer +=1" it will add +1 each time it cycles. Then you could make some logic like "if self.timer >= 3: put code to reset limit order here, then one line below self.timer = 0" Your timer is reset, so is your limit order and now the countdown can begin again.
Nick Abram
Hi Ryan,
Thank you for your reply and ideas. This approach sounds feasible, unfortunately, it wouldn't be robust enough to cover my intended functionality. Any ideas on how to implement in another way that doesn't depend on the resolution of the data? If I had minute resolution data but wanted to adjust the limit every few seconds, then I wouldn't have a function that runs this frequently.
Ryan Riordon
Hi Nkabram,
You can use something like "datetime.now() + timedelta(minutes = 10)" or
end_time = datetime.now()
repeat_5mins = end_time + timedelta(minutes=5) # convert current time to timestamp
if datetime.now() >= repeat_5mins:
Here are some examples I searched for in the community forums with keywords "time" "datetime" and such.
https://www.quantconnect.com/forum/discussion/4154/wait-x-minutes-to-execute-codehttps://www.quantconnect.com/forum/discussion/7122/selling-stocks-after-a-certain-time/p1https://www.quantconnect.com/forum/discussion/5983/feed-live-data-from-bitfinex-to-a-live-strategyhttps://www.quantconnect.com/forum/discussion/7903/how-do-we-exit-positions-after-a-specific-holding-time/p1Let me know it this helps.
Jared Broad
Hi Nkabram, in backtesting the progression of time is based on the data. You would need to use second resolution data in this case and then scheduled events or some OnData check to update your limit order.
Using datetime.now() would not be recommended as it'll disconnect backtesting from live trading code. datetime.now() will be wall clock time, not the backtest time.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Nick Abram
Okay, thanks for the reply. Is there a way to change the resolution based on a schedule? I would only need the second resolution during the time that the algorithm rebalances. If I had it on the second resolution continuously that would take up a lot of unnecessary computation time.
Shile Wen
Hi Nick,
In order to change the resolution, we would need to remove the security and re-add it with a different resolution. Furthermore, I suggest comparing minute against second resolution to test the sensibility in the final state of development.
Best,
Shile Wen
Nick Abram
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!