The attached back-test includes an implementation of the risk management model using ATR values to create a trailing stop loss. I THINK it works, but have some questions:
- When backtesting, what should we looking to compare the stop value to? In my code below, I compared it to the high or low of the bar depending on direction of trade.. Any suggestions for a more realistic method?
protected virtual bool StopLossTriggered (Security security, HoldingsState state )
{
return state.Position == PositionSide.Long
? state.StopValue >= security.Low
: state.StopValue <= security.High;
}
2. Inside the ManageRisk function, I am checking if stop loss is triggered before calculating a new stop loss value. Is this the order you would do it in? Different suggestion?
...
// liquidate if stop loss triggered
if (StopLossTriggered(security, state))
{
_holdingsState.Remove(symbol);
// liquidate
yield return new PortfolioTarget(security.Symbol, 0);
}
//Calculate new stop value
state.StopValue = position == PositionSide.Long
? Math.Max(state.StopValue, security.Close - (_multiplier * data.ATR))
: Math.Min(state.StopValue, security.Close + (_multiplier * data.ATR));
...
Louis Szeto
Hi Dennis
In general, I believe your implementation is correct.
IMHO, I believe you're doing it correctly since we should liquidate once we know the stop price was hit. That was also the fastest information that we can know if the stop price was hit.
Absolutely correct! Otherwise, the stop price will never be touched!
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.
Dennis Maude
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!