Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

backtest error when opening and closing position on the same candle

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

    backtest error when opening and closing position on the same candle


    Good afternoon and thanks in advance.

    My problem is the following. When performing a backtest of a strategy, when the entry and exit of the position occurs in different candles, everything works well. But when the entry and exit of the position occurs in the same candle, it executes them over and over, increasing my profit or loss.

    This in turn prevents obtaining correct information when optimizing.

    I have only one input activated per signal.

    Regards.

    #2
    Hello mcjosejavier, thanks for your question.

    It looks like the entry condition is becoming true multiple times within a bar. Its a problem with the strategy logic. We have this method that you can use to prevent this from happening:


    If that is 0, then don't enter the trade again.

    You could also check the CurrentBar value to control this.

    You can prove the strategy is entering multiple times using a Print statement above your entry order. e.g.

    if(EntryCondition)
    {
    Print("Entering Position");
    EnterLong();
    }

    Kind regards.

    Comment


      #3
      Thank you very much. Finally I have made the result of the backtest equal to the result in playback.

      protected override void OnBarUpdate ()
      {
      if (BarsSinceEntryExecution () == 0)
      return;

      Regards.

      Comment

      Latest Posts

      Collapse

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