Hello all,
I'm using the Algorithm Framework for an alpha and I'm trying to modify the SpreadExecutionModel to use limit orders instead of market orders.
My intention with limit orders is not to get a good fill but rather to guarantee I'm not getting a terrible fill if the security I'm trading happens to be illiquid at that moment in time.
For example, if the closing price for a security is P, limit orders might use 0.995 x P to save a couple cents per share on a large order. What I'm trying to do is actually set the limit to 1.10 x P to ensure that the purchase price of the share is never more than 10% of the current price. This is to handle rare scenarios when a market order can execute from an insane Ask price for illiquid stock (e.g. Ask = 5 x Bid).
I've been using Market Orders up to this time. My understanding is that if I set the limit price significantly higher than the current price, it should act just like a Market Order since the buy condition is immediately met (except in those rare situations). This is why in the SpreadExecutionModel, I updated
if self.SpreadIsFavorable(security):
algorithm.MarketOrder(symbol, unorderedQuantity)
to
if self.SpreadIsFavorable(security):
if unorderedQuantity > 0: # different limits for short and long orders
algorithm.LimitOrder(symbol, unorderedQuantity, security.Close * 1.1)
elif unorderedQuantity < 0:
algorithm.LimitOrder(symbol, unorderedQuantity, security.Close * 0.9)
However, I'm finding that these Limit Orders with a limit significantly higher than the market close lead to fills different than market orders and the results are frequently worse than just sticking with Market Orders. I find it hard to believe that every order is this “rare” situation.
I've attached a backtest below using limit orders. If you comment out the limit order logic and replace it with the original market order logic, you can see the difference/issues firsthand (6% returns vs 22% returns).
I'm guessing my understanding of limit orders in LEAN might be incorrect, but any explanation how would be helpful.
Thanks.
Fred Painchaud
Hi AK,
The effect that you are describing of moving from market orders to limit orders seems to make sense to me.
With market orders, as you know, buying and selling is done at market price, which in backtests, will be the open of the next bar.
With limit orders, with the code you use, you set to buy at +10% of current close and sell at -10% of current close. Of course, those limit orders only trigger when and if the price reaches those limits. So 1) if the price does not reach the limit, the order is never executed and 2) if the price does reach the limit, it can take a while and then, of course, you just lost 10% of your profit already when buying long and another 10% when exiting long… The scenario is similar but reciprocal when entering and exiting short, if you go short, but it does not matter if you don't.
So overall, with limit orders in that particular strategy, wrt market orders, you are missing trades which could be profitable and you are entering late in other trades.
Fred
P.S. I am not mentioning the fact that you also should manage your limit orders or you could get surprises, when both an old limit order and a very recent one triggers at the same time and messes with the quantity of some asset you have in your portfolio. Say you hold 80, and have a limit order for -80. But you have a lingering limit order that never triggered so far for -100. Both triggers because you finally reach the level which they are both (they are both at a similar price level but say the price drops below both). From 80, you're down to -100. You're shorting when you just wanted to exit.
AK M
Thanks for getting back to me Fred.
What you are saying makes sense. I think my understanding of limit orders is incorrect and maybe another order type will be useful for me.
Also good note, this is not something I considered when moving towards limit orders.
AK M
I looked into this more. Great note on the limit order management edge case but the behavior for this algorithm is still different than expected.
Limit orders are supposed to be able to buy or sell at a specified price or a better one. Investopedia even notes the example I'm running into: “For buy limit orders, the order will be executed only at the limit price or a lower one”
Interactive brokers (I'm trading with IB) has a similar definition: “an order to buy or sell at a specified price or better”
So in my code, if I'm setting the limit price to 10% above the close price, it should be executed immediately like a market order since the condition is met right away. I don't believe it should be waiting to hit that price before executing. In my backtest I expect to see the same results if I replace my market orders with limit orders with limits 10% above close for buying and 10% below close for selling.
Reopening because this still seems like an issue to me. Please correct me if I'm wrong (maybe I'm not seeing the reasoning) but I think the original post still stands.
Louis Szeto
Hi AK M
We have an open GitHub issue to review the default fill model:
https://github.com/QuantConnect/Lean/issues/4567
That said, we model limit and stop market order using the worst-case scenario approach, which means that a buy limit order is filled at the minimum between the latest High and the limit price, and the sell limit order is filled at the maximum between the latest Low and the limit price. Why? Because this allows far out of the money limits to be executed properly.
We have to keep in mind that we are modeling a fill without all the information: we only have the OHLC with minute resolution (what happens between the open and close? Can we assume continuous price? Or should we account for price gaps?). If we use tick resolution (instantaneous prices like in live mode), we would get "an order to buy or sell at a specified price or better" as defined by IB or any other source.
Best
Louis
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.
AK M
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!