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 NullPointStrategies, 03-13-2026, 05:17 AM
      0 responses
      96 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      154 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      82 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      54 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      72 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X