Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multi timeframe strategy

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

    Multi timeframe strategy

    Hello, I've been testing the multi timeframe scenario and I've found something I don't understand.
    I would appreciate an explanation or how to solve it.
    The strategy has 4 timeframes 60 minutes, 30 minutes, 15 minutes and 1 minute
    3 EMAs on each timeframe and send a long order when Close[0] is higher on all timeframes.
    The strategy works well.
    Now what I don't understand:
    If I put the strategy on a 1-minute chart and look at the history it has a result, if I change the chart timeframe to 5 minutes I have another different result in the history and so on with any timeframe I put on the chart and this happens the same regardless of whether I have set the calculation mode to "On Bar Close", "On price Change" or "On each Tick"
    I think that the ideal is that the results are always the same regardless of the time frame of the chart, so:
    1st Why does it happen?
    2nd how can I solve it?

    Thank you so much!​

    #2
    Hello franki,

    Thank you for your post.

    This depends on how your strategy is using the additional data series to do the calculations. I would expect that if you are simply using Close[0] for a calculation, changing the primary data series would cause a difference in how the strategy is calculating.

    If you apply it to a 1 minute chart, when OnBarUpdate() is updating on the primary series Close[0] will be the closing value of the 1 minute bar. If you apply it to a 5 minute chart, then Close[0] will be the closing value of the 5 minute bar when OBU is updating on the primary series. These are two different values.

    If you want your strategy to only access the closing value for a certain series, you could use the Closes collection.



    Closes[0][0] would be the the primary bar's closing price, Closes[1][0] is the secondary bars closing price, and so on.

    I recommend you review the help guide page on Multi Time Frame and Instruments.



    Please let us know if you have any further questions.

    Comment


      #3
      Originally posted by NinjaTrader_Gaby View Post

      If you want your strategy to only access the closing value for a certain series, you could use the Closes collection.



      Closes[0][0] would be the the primary bar's closing price, Closes[1][0] is the secondary bars closing price, and so on.

      I recommend you review the help guide page on Multi Time Frame and Instruments.



      Please let us know if you have any further questions.

      Hello again ^^
      In my free time these days I have tried different configurations following the articles in your post
      And I have not been able to prevent the strategy from being executed within the time frame of the chart.
      It's the only thing I'm trying to do, but all the settings give me different results when I change the time period of the chart.
      I think it has to be easy, but I can't figure out how to do it.


      I show you the code that I have modified several times following the part that "Closes[0][0]" advised me and other variants​



      Code:
      namespace NinjaTrader.NinjaScript.Strategies
      {
          public class MTFStrategyX1 : Strategy
          {
              private ZLEMA ZLEMA1;
              private ZLEMA ZLEMA2;
              private ZLEMA ZLEMA3;
              private ZLEMA ZLEMA4;
              private ZLEMA ZLEMA5;
              private ZLEMA ZLEMA6;
              private ZLEMA ZLEMA7;
              private ZLEMA ZLEMA8;
              private ZLEMA ZLEMA9;
              private ZLEMA ZLEMA10;
              private ZLEMA ZLEMA11;
              private ZLEMA ZLEMA12;
      //        private Series<double> ZLEMA1;
      //        private Series<double> ZLEMA2;
      //        private Series<double> ZLEMA3;
      //        private Series<double> ZLEMA4;
      //        private Series<double> ZLEMA5;
      //        private Series<double> ZLEMA6;
      //        private Series<double> ZLEMA7;
      //        private Series<double> ZLEMA8;
      //        private Series<double> ZLEMA9;
      //        private Series<double> ZLEMA10;
      //        private Series<double> ZLEMA11;
      //        private Series<double> ZLEMA12;
              
              
              
      
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"Enter the description for your new custom Strategy here.";
                      Name                                        = "MTFStrategyX1";
                      Calculate                                    = Calculate.OnEachTick;
                      EntriesPerDirection                            = 1;
                      EntryHandling                                = EntryHandling.AllEntries;
                      IsExitOnSessionCloseStrategy                = true;
                      ExitOnSessionCloseSeconds                    = 30;
                      IsFillLimitOnTouch                            = false;
                      MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                      OrderFillResolution                            = OrderFillResolution.Standard;
                      Slippage                                    = 3;
                      StartBehavior                                = StartBehavior.WaitUntilFlat;
                      TimeInForce                                    = TimeInForce.Day;
                      TraceOrders                                    = true;
                      RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                      StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                      BarsRequiredToTrade                            = 20;
                      // Disable this property for performance gains in Strategy Analyzer optimizations
                      // See the Help Guide for additional information
                      IsInstantiatedOnEachOptimizationIteration    = true;
                      SuperZLEMA1                    = 10;
                      SuperZLEMA2                    = 20;
                      SuperZLEMA3                    = 40;
                      Target                    = 50;
                      Stop                    = 25;
                  }
                  else if (State == State.Configure)
                  {
                      AddDataSeries(Data.BarsPeriodType.Minute, 1);
                      AddDataSeries(Data.BarsPeriodType.Minute, 15);
                      AddDataSeries(Data.BarsPeriodType.Minute, 30);
                      AddDataSeries(Data.BarsPeriodType.Minute, 60);
                  }
                  else if (State == State.DataLoaded)
                  {                
                      ZLEMA1    = ZLEMA(Closes[1], Convert.ToInt32(SuperZLEMA1));
                      ZLEMA2    = ZLEMA(Closes[1], Convert.ToInt32(SuperZLEMA2));
                      ZLEMA3    = ZLEMA(Closes[1], Convert.ToInt32(SuperZLEMA3));
                      ZLEMA4    = ZLEMA(Closes[2], Convert.ToInt32(SuperZLEMA1));
                      ZLEMA5    = ZLEMA(Closes[2], Convert.ToInt32(SuperZLEMA2));
                      ZLEMA6    = ZLEMA(Closes[2], Convert.ToInt32(SuperZLEMA3));
                      ZLEMA7    = ZLEMA(Closes[3], Convert.ToInt32(SuperZLEMA1));
                      ZLEMA8    = ZLEMA(Closes[3], Convert.ToInt32(SuperZLEMA2));
                      ZLEMA9    = ZLEMA(Closes[3], Convert.ToInt32(SuperZLEMA3));
                      ZLEMA10    = ZLEMA(Closes[4], Convert.ToInt32(SuperZLEMA1));
                      ZLEMA11    = ZLEMA(Closes[4], Convert.ToInt32(SuperZLEMA2));
                      ZLEMA12    = ZLEMA(Closes[4], Convert.ToInt32(SuperZLEMA3));
      
                      
      //                ZLEMA1    = new Series<double>(ZLEMA(BarsArray[1], SuperZLEMA1));
      //                ZLEMA2    = new Series<double>(ZLEMA(BarsArray[1], SuperZLEMA2));
      //                ZLEMA3    = new Series<double>(ZLEMA(BarsArray[1], SuperZLEMA3));
      //                ZLEMA4    = new Series<double>(ZLEMA(BarsArray[2], SuperZLEMA1));
      //                ZLEMA5    = new Series<double>(ZLEMA(BarsArray[2], SuperZLEMA2));
      //                ZLEMA6    = new Series<double>(ZLEMA(BarsArray[2], SuperZLEMA3));
      //                ZLEMA7    = new Series<double>(ZLEMA(BarsArray[3], SuperZLEMA1));
      //                ZLEMA8    = new Series<double>(ZLEMA(BarsArray[3], SuperZLEMA2));
      //                ZLEMA9    = new Series<double>(ZLEMA(BarsArray[3], SuperZLEMA3));
      //                ZLEMA10    = new Series<double>(ZLEMA(BarsArray[4], SuperZLEMA1));
      //                ZLEMA11    = new Series<double>(ZLEMA(BarsArray[4], SuperZLEMA2));
      //                ZLEMA12    = new Series<double>(ZLEMA(BarsArray[4], SuperZLEMA3));
                      
                      SetProfitTarget("", CalculationMode.Ticks, Target);
                      SetStopLoss("", CalculationMode.Ticks, Stop, false);
                  }
              }
      
              protected override void OnBarUpdate()
              {
                      if (BarsInProgress != 0)
                      return;
      
                  if (
      //                CurrentBars[0] < 1
      //            ||  CurrentBars[1] < 0
      //            || CurrentBars[2] < 0
      //            || CurrentBars[3] < 0
      //            || CurrentBars[4] < 0
                      
                      
                      
      //          CurrentBars[1] < 0
      //            || CurrentBars[2] < 0
      //            || CurrentBars[3] < 0
      //            || CurrentBars[4] < 0
                      
                      
      //              CurrentBars[1] < 0
      //            || CurrentBars[2] < 0
      //            || CurrentBars[3] < 0
      //            || CurrentBars[4] < 0
                      
                      
                  CurrentBars[1] > ZLEMA1[0]
                  && CurrentBars[2] > ZLEMA1[0]
                  && CurrentBars[3] > ZLEMA1[0]
                  && CurrentBars[4] > ZLEMA1[0]
                  && CurrentBars[1] > ZLEMA2[0]
                  && CurrentBars[2] > ZLEMA2[0]
                  && CurrentBars[3] > ZLEMA2[0]
                  && CurrentBars[4] > ZLEMA2[0]    
                  && CurrentBars[1] > ZLEMA3[0]
                  && CurrentBars[2] > ZLEMA3[0]
                  && CurrentBars[3] > ZLEMA3[0]
                  && CurrentBars[4] > ZLEMA3[0]
                  && CurrentBars[1] > ZLEMA4[0]
                  && CurrentBars[2] > ZLEMA4[0]
                  && CurrentBars[3] > ZLEMA4[0]
                  && CurrentBars[4] > ZLEMA4[0]
                      )
                      return;
      
                   // Set 1
                  
      //                if ((Close[0] > ZLEMA1[0])
      //                 && (Close[0] > ZLEMA2[0])
      //                 && (Close[0] > ZLEMA3[0])
      //                 && (Close[0] > ZLEMA4[0])
      //                 && (Close[0] > ZLEMA5[0])
      //                 && (Close[0] > ZLEMA6[0])
      //                 && (Close[0] > ZLEMA7[0])
      //                 && (Close[0] > ZLEMA8[0])
      //                 && (Close[0] > ZLEMA9[0])
      //                 && (Close[0] > ZLEMA10[0])
      //                 && (Close[0] > ZLEMA11[0])
      //                 && (Close[0] > ZLEMA12[0])
      //                 && (Position.MarketPosition == MarketPosition.Flat))
      //            {
      //                EnterLong(Convert.ToInt32(DefaultQuantity), "");
      //            }
                  
      //            if ((Closes[1][0] > ZLEMA1[0])
      //                 && (Closes[1][0] > ZLEMA2[0])
      //                 && (Closes[1][0] > ZLEMA3[0])
      //                 && (Closes[2][0] > ZLEMA4[0])
      //                 && (Closes[2][0] > ZLEMA5[0])
      //                 && (Closes[2][0] > ZLEMA6[0])
      //                 && (Closes[3][0] > ZLEMA7[0])
      //                 && (Closes[3][0] > ZLEMA8[0])
      //                 && (Closes[3][0] > ZLEMA9[0])
      //                 && (Closes[4][0] > ZLEMA10[0])
      //                 && (Closes[4][0] > ZLEMA11[0])
      //                 && (Closes[4][0] > ZLEMA12[0])
      //                 && (Position.MarketPosition == MarketPosition.Flat))
      //            {
      //                EnterLong(Convert.ToInt32(DefaultQuantity), "");
      //            }
                  
                  
                      if ((Closes[1][0] > ZLEMA1[0])
                       && (Closes[1][0] > ZLEMA2[0])
                       && (Closes[1][0] > ZLEMA3[0])
                       && (Closes[2][0] > ZLEMA4[0])
                       && (Closes[2][0] > ZLEMA5[0])
                       && (Closes[2][0] > ZLEMA6[0])
                       && (Closes[3][0] > ZLEMA7[0])
                       && (Closes[3][0] > ZLEMA8[0])
                       && (Closes[3][0] > ZLEMA9[0])
                       && (Closes[4][0] > ZLEMA10[0])
                       && (Closes[4][0] > ZLEMA11[0])
                       && (Closes[4][0] > ZLEMA12[0])
                       && (Position.MarketPosition == MarketPosition.Flat))
                  {
                      EnterLong(Convert.ToInt32(DefaultQuantity), "");
                  }
                  
              }
      
              #region Properties
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="SuperZLEMA1", Order=1, GroupName="Parameters")]
              public int SuperZLEMA1
              { get; set; }
      
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="SuperZLEMA2", Order=2, GroupName="Parameters")]
              public int SuperZLEMA2
              { get; set; }
      
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="SuperZLEMA3", Order=3, GroupName="Parameters")]
              public int SuperZLEMA3
              { get; set; }
      
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="Target", Order=4, GroupName="Parameters")]
              public int Target
              { get; set; }
      
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="Stop", Order=5, GroupName="Parameters")]
              public int Stop
              { get; set; }
              #endregion
      
          }
      }​

      Comment


        #4
        Hello franki,

        "And I have not been able to prevent the strategy from being executed within the time frame of the chart.​"

        Could you explain what you mean by this?

        "It's the only thing I'm trying to do, but all the settings give me different results when I change the time period of the chart."

        This would be expected. If you change the time period of the chart, the results will almost certainly change. For example, a strategy running on a 1 minute chart would not have the same results as a strategy running on a 5 minute chart because the data being supplied to the strategy is different.

        Comment


          #5
          Originally posted by NinjaTrader_Gaby View Post
          Hello franki,

          "And I have not been able to prevent the strategy from being executed within the time frame of the chart.​"

          Could you explain what you mean by this?
          I mean that I have not been able to exclude the timeframe where the chart bars are from the secondary timeframes


          Originally posted by NinjaTrader_Gaby View Post
          "It's the only thing I'm trying to do, but all the settings give me different results when I change the time period of the chart."

          This would be expected. If you change the time period of the chart, the results will almost certainly change. For example, a strategy running on a 1 minute chart would not have the same results as a strategy running on a 5 minute chart because the data being supplied to the strategy is different.
          I understand that:
          The main timeframe is the timeframe of the chart itself, example:
          I have a 1 minute bar chart and in the strategy it would be Close[0] or Closes[0][0] in multiple timeframes
          So if in State.Configure I add a new timeframe it will be Closes[1][0] and if I add one more time frame it will be Closes[2][0]
          If I make a strategy with nothing more than Closes[1][0] and Closes[2][0]
          Why when I change the chart bars from 1 minute to 2 minutes do the results change?

          Exactly what I want is that, that under any period of time on the chart there are always the same results.

          Is that possible?​

          Comment


            #6
            Hello franki,

            The Close[1][0], Close[2][0], etc values will not change if the primary series is changed. Please see the attached example script which demonstrates. It prints out the time stamp for the added series as well as the Close[1][0] and Close[2][0]. You will note that if you change the primary series, these values do not change.

            That said, if you do not want your strategy to process data from the primary data series at all, you could add the following to OnBarUpdate():

            if (BarsInProgress == 0)
            return;


            Attached Files

            Comment


              #7
              Hallo, thanks for sharing this sample script. I want use this to access the past and current highs and lows on daily, hourly, 5 min and 1 min chart. If i run the strategy on the 1 min chart, and request the Highs[1][0] and Lows[1][0] for each interval, on the next 1 min candle it prints the highs and lows of the last closed candle. For the 1 min interval, this is still acceptable, but i also want the highs and lows of the higher time frame candles at that instance, even if they have not closed yet. So you can i get the highs and Lows on the current bars forming?

              Comment


                #8
                Hello konganda,

                Thank you for your post.

                To get the values for currently forming bars, you would need to use Calculate.OnEachTick or Calculate.OnPriceChange for your strategy:



                Please let us know if you have any further questions.

                Comment


                  #9
                  ok thanks. yes i do. I found this free indicator on the internet, that provides the VWAP bands on a chart. How can i call this Indicator to give me the current vwap value, so that i can use it in my strategy? https://www.trading-revealed.com/ind...jatrader-free/

                  Comment


                    #10
                    Hello konganda,

                    You would need to reach out to the developers of that indicator for more information on how to use it within the strategy and the specific syntax that should be used. Unfortunately we do not have information to offer on how third-party indicators work.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by NullPointStrategies, Today, 05:17 AM
                    0 responses
                    53 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