I am seeing what appears to be unusual behavior of StopMarketOrder in combination with MarketOrder during backtesting. Can you verify that my understanding of the use of MarketOrder and StopMarketOrder is correct?
Suppose I issue a MarketOrder for some number N shares of an equity E at some price P.
If N is positive, I am going long (buying the equity), which means I am betting that the equity price will increase. To protect myself from a price decrease, I can also issue a StopMarketOrder to sell the equity if the price decreases to some price lower by a delta D. That would be a StopMarketOrder at -N shares at price P - D.
If the share price of E goes up, I can Liquidate E, which will sell the N shares at a profit, and cancel the StopMarketOrder at the same time. If the share price for E goes down by more than or equal to D, then the StopMarketOrder is triggered, and the shares are sold at market value, limiting the loss. A later Liquidation of E will act as a no-op since the shares have already been liquidated.
On the other hand, suppose N is negative. That is, I issue a MarketOrder for some number -N shares of equity E at some price P. I am going short (selling the equity), which means I am betting that the equity price will decrease. To protect myself from a price increase, I can also issue a StopMarketOrder to buy the equity if the price increases to some price higher by a delta D. That would be a StopMarketOrder at positive N shares at price P + D.
If the share price of E goes down, I can Liquidate E, which will buy the N shares at a profit, and cancel the StopMarketOrder at the same time. If the share price for E goes up by more than or equal to D, then the StopMarketOrder is triggered, and the shares are bought at market value, limiting the loss. A later liquidation of E will act as a no-op since the shares have already been liquidated.
Please let me know if this is a correct description of how MarketOrder and StopMarketOrder are supposed to work.
Thomas Beauchamp-Arnold
The reason I make this request is that I have good empirical evidence that MarketOrder and StopMarketOrder do not work this way, at least during backtesting.
I am trying to get my algorithm ready for live deployment, but there is no way I will go live, risking tens of thousands of dollars of my real money, with serious discrepancies in these backtest results.
My algorithm is a version of Early Range Breakout, inspired by the ERB algorithm presented during Bootcamp. It downloads a basket of equities from a file every end-of-week, and trades those equities daily, looking for breakout high or breakout low from a bar captured at market open. If a trade is made, it is closed by market exit time, which is currently 2 pm local trading time for the equity in question.
Here is the backtest result trading TSLA from 2021-2-25 – 2021-7-24, starting with $35k. In this case, the StopMarketOrder is commented out, so no StopMarketOrder is issued.
This is the Execute method in class OrderExecution. As you see, algorithm.StopMarketOrder is commented out, and will not execute.
This backtest resulted in a Return of 14.19% and a Net Profit of $5,835.24
Thomas Beauchamp-Arnold
In this next backtest, StopMarketOrder is implemented as per my understanding in my original post. Here is the Execute method.
Note in particular that the algorithm.StopMarketOrder call uses the negative of the order quantity, as my original post specifies.
The stopPrices[target.Symbol] quantity is calculated in alpha.py using this code, which determines the midpoint between the openingBar.High and openingBar.Low.
This should result in a stop price which is lower than the breakout high, and higher than the breakout low, which is correct in my understanding.
This backtest used the same parameters as before, and resulted in a return of 2.90% and a profit of $1,1171.98. For a protective maneuver, it seriously hurt the profits.
Something is wrong.
Thomas Beauchamp-Arnold
Conversely, if I change the Execute method to issue a StopMarketOrder with a quantity with the same sign as the MarketOrder, the profits soar!
Why does doing it wrong work so well?
Here is the Execute method.
As you see, the algorithm.StopMarketOrder uses a quantity with the same sign as algorithm.MarketOrder.
This backtest results in a return of 35.99%, with net profit of $12,829.57.
Clearly, I should use this code, right? It makes much more money.
But I don't trust a result that directly contradicts my understanding of how these calls are supposed to work.
I could see that perhaps Dancing Green Kitten represents a case of buying on margin, but that doesn't explain why buying on margin when the market moves against me makes me more money.
I hope folks can understand why I feel confused.
Thomas Beauchamp-Arnold
I've done these tests now dozens of times. Earlier in the runs, I had determined that the signs for the quantity in MarketOrder and StopMarketOrder must be the same, because that always gave me much better profits.
But reviewing the documentation again sure seemed to imply that StopMarketOrder wants to sell when liquidating a long buy, and wants to buy when liquidating a short sell.
Unless StopMarketOrder inverts the quantity deliberately, I can't explain the results I consistently get, and the documentation does not say it does that.
Thomas Beauchamp-Arnold
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!