Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

TrailStop making strategy crash on backtesting

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    TrailStop making strategy crash on backtesting

    Hi, I'm working in a TrailStop for my strategy but backtest is becoming incredible difficult due to some random errors with the StopLoss change, sometimes I get an error saying the Order was rejected to StopLoss set over the market price but sometimes I don't get any errors on the same entry on the same day, sometimes I get this error backtesting several months but sometimes I don't get any, sometimes the Replay Speed trigger the error and sometimes not, it seems to me that the speed of the order resolution is different to the market replay speed causing issues.

    I attached the piece of code that I use for my TrailStop, any help would be greatly appreciated:
    Code:
    // UPDATE TargetStopPrice
    if (Position.MarketPosition == MarketPosition.Long
    && UnRealizedPnl > _targetPnl)
    {
    _targetPnl += _stepPnl;
    var target = Open[0] > _lastClose && GetCurrentAsk() > Open[0]
    ? Open[0]
    : _lastClose;
    
    _currentStopPrice = (target + (_trailStopDistance * TickSize));
    
    }
    
    if (Position.MarketPosition == MarketPosition.Short
    && UnRealizedPnl > _targetPnl)
    {
    _targetPnl += _stepPnl;
    var target = Open[0] < _lastClose && GetCurrentAsk() < Open[0]
    ? Open[0]
    : _lastClose;
    _currentStopPrice = (target - (_trailStopDistance * TickSize));
    }
    
    // SET TargetStop
    if (_currentStopPrice != 0)
    {
    if (Position.MarketPosition == MarketPosition.Long
    && _currentOrder.OrderState != OrderState.Rejected)
    {
    VPrint(Time[0].ToString() + " StopLoss:" + _currentStopPrice.ToString("N6") + " Entry: " + _lastClose.ToString("N6"));
    ExitLongStopMarket(DefaultQuantity, _currentStopPrice);
    }
    if (Position.MarketPosition == MarketPosition.Short
    && _currentOrder.OrderState != OrderState.Rejected)
    {
    VPrint(Time[0].ToString() + " StopLoss:" + _currentStopPrice.ToString("N6") + " Entry: " + _lastClose.ToString("N6"));
    ExitShortStopMarket(DefaultQuantity, _currentStopPrice);
    }
    }

    #2
    Hi eramosr21, thanks for writing in.

    The Print method will be the best tool for troubleshooting this kind of issue with a custom strategy. Print the price of GetCurrentAsk/Bid and compare that price to the price being used in the Exit orders. Double checking the price of the stop order right before the stop order is submitted is a good way to avoid the rejected order.
    Making a copy of the strategy by right clicking>Save As can also be a good tool for troubleshooting so you can reduce the strategy down to make it easier to isolate issues, like stripping down the strategy to do long orders only, and once the long-only strategy is working perfectly, the short side can be added.

    Kind regards,
    -ChrisL

    Comment


      #3
      Thanks for the note Chris, I was able to fix some of the issues I was having, I'm not having the error so often as before, nothing that a good strategy restart wouldn't solve.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      62 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      34 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      53 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      59 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      48 views
      0 likes
      Last Post CarlTrading  
      Working...
      X