Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop Loss Backtest tick

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

    Stop Loss Backtest tick

    Hi,

    i want to backtest strategy with two different timeframe:
    - 5 m : where the indicator is calcultate
    - 1 tick: where the entry is done

    so, the entry is not a problem becuse the strategy enter wehn the condicion is true but the problem is stop loss: becuase it is not calculate by tick.
    For exemple: if price open at 10 and wehen price go to 6 strategy open a position with SL and TP = 3 pt. in these case (for exemple the price start at 10, go to 6, go to 2 (stop loss hit) and return on 9) the strategy inicate take profit and not a stop loss, when the stop loss was hitten befaore take profit​.

    Code:
    else if (State == State.Configure)
                {
                    AddDataSeries(BarsPeriodType.Tick, 1);
                    Calculate = Calculate.OnEachTick;
                }
    if (BarsInProgress == 0){
                    emaValue = EMA(Closes[0], Period)[0];
                    emaSeries[0] = emaValue;
                    smaUP = emaValue * (1+scost);
                    emaIncreasedSeries[0] = smaUP;
                    smaDW = emaValue * (1-scost);
                    emaDecreasedSeries[0] = smaDW;
                    Values[0][0] = emaSeries[0];
                    Values[1][0] = emaIncreasedSeries[0];
                    Values[2][0] = emaDecreasedSeries[0];
                }
                if (BarsInProgress == 1){    
                    if (Closes[1][0] < emaValue) {
                        CondicionSell2 = 1;
                    }
    
                    if (Closes[1][0] > emaValue) {
                        CondicionBuy2 = 1;
                    }
    
                    if (Closes[1][0] >= smaUP && CondicionSell2==1){
                    EnterShort(lots, "sell");
                    SetStopLoss(CalculationMode.Pips, pipstoploss); // Stop loss di 10 pips
                    SetProfitTarget("sell", CalculationMode.Pips, piptakeprofit); // Take profit di 12 pips
                    CondicionSell2 = 0;
                    }
                    if (Closes[1][0] <= smaDW && CondicionBuy2==1){
                    // Calcola la dimensione della posizione, ad esempio con una fissa percentuale dell'equità
                    // Apri una posizione di vendita e imposta stop loss e take profit
                    EnterLong(lots, "buy");
                    SetStopLoss(CalculationMode.Pips, pipstoploss); // Stop loss di 10 pips
                    SetProfitTarget("buy", CalculationMode.Pips, piptakeprofit); // Take profit di 12 pips
                    CondicionBuy2 = 0;
                    }​
                    ​
    Click image for larger version

Name:	image.png
Views:	126
Size:	7.9 KB
ID:	1277845Click image for larger version

Name:	image.png
Views:	101
Size:	5.0 KB
ID:	1277846​​
    Last edited by itsmattiia; 11-14-2023, 03:32 AM.

    #2
    Hello itsmattiia,

    Thank you for your post.

    Set methods like SetStopLoss and SetProfitTarget cannot be submitted to an added series.

    You can use exit order methods instead. Please see the sample script ProfitChaseStopTrailExitOrdersExample linked below, which demonstrates using exit methods to submit protective orders on an added data series.

    https://forum.ninjatrader.com/forum/...269#post802269

    If you have any questions, please let me know.

    Comment


      #3
      thank u, but this's very tricky...

      if i invert data series? i load backtest on 1 tick and in the code add data series 5 min?

      Comment


        #4
        Hello,

        If you will be submitting the orders to the 1 tick series instead of the added 5 minute series, then you can do that instead.

        From the Help Guide:

        "Should you have multiple Bars objects of the same instrument while using SetStopLoss() in your strategy, you should only submit orders for this instrument to the first Bars context of that instrument."

        https://ninjatrader.com/support/help...etstoploss.htm

        If the 1-tick series is your primary series that you want to submit orders to, and you are adding a 5-minute series, you'll need to re-do the logic in your script to submit orders to BarsInProgress 0.

        Please let me know if you need further assistance.

        Comment


          #5
          hi, i have modified my script

          Code:
          if (CurrentBars[0] < 0)
              return;
          
          if (BarsInProgress == 1)
          {
              emaValue = EMA(Closes[1], 10)[0];  
              smaUP = emaValue * (1 + scost);
              smaDW = emaValue * (1 - scost);
              Values[0][0] = emaValue;
              Values[1][0] = smaUP;
              Values[2][0] = smaDW;
          }
          
          if (BarsInProgress == 0)
          {
              if (Closes[0][0] < emaValue)
              {
                  CondicionSell2 = 1;
              }
          
              if (Closes[0][0] > emaValue)
              {
                  CondicionBuy2 = 1;
              }
          
              if (Closes[0][0] >= smaUP && CondicionSell2 == 1)
              {
                  EnterShort(lots, "FirsSell");
                  SetStopLoss(CalculationMode.Pips, pipstoploss);
                  SetProfitTarget("FirsSell", CalculationMode.Pips, piptakeprofit);
                  CondicionSell2 = 0;
              }
          
              if (Closes[0][0] <= smaDW && CondicionBuy2 == 1)
              {
                  EnterLong(lots, "BuyPosition");
                  SetStopLoss(CalculationMode.Pips, pipstoploss);
                  SetProfitTarget("BuyPosition", CalculationMode.Pips, piptakeprofit);
                  CondicionBuy2 = 0;
              }
          }
          ​
          but i think that the ema is not calculated on 5 minute timeframe.

          Click image for larger version

Name:	image.png
Views:	106
Size:	26.6 KB
ID:	1277920
          for example, the range of ema taht start from 00:10 and end in 00:15 shoud be flat, isn't?​ because it range is a 1 bar in m5 TF

          Comment


            #6
            Hello,

            If your added data series is a 5 minute series, Closes[1] is the 5 minute series being supplied to the EMA for calculation.

            The reason you are seeing it displayed this way is because it is updating every tick, not every 5 minutes since it is evaluating the logic in BarsInProgress 0 (the primary 1 tick series), not BarsInProgress 1 (the added 5 minute series).

            We would still recommend instead of "inverting" the data series, you use your original 5 minute primary data series and added 1 tick added series for the exit orders barsInProgressIndex parameter.

            Please let me know if you have any other questions.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            52 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            130 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            70 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            44 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            49 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X