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:

  1. 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));
...