Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Working with multi timeframes.

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

    Working with multi timeframes.

    I am trying to write an indicator that calculates bar strength based on Lower time frames. For example, a 5min bar requires 10x30 seconds bars. I am trying to get the variable value from the lower timeframe after the last 30s bar is finished. However, it appears no matter what I try, the script is only able to effectively get LTF[1], it doesn't wait for the last lower time frame bar to finish. How can I solve this?

    Code:
    protected override void OnBarUpdate()
    {
    if (CurrentBars[0] < 2 || CurrentBars[1] < 10)
    return;
    
    if (BarsInProgress == 1) {
    // some calculation of lower time frame;
    var LTF[0];
    
    }
    if (BarsInProgress == 0) {
    // some calculation of current time frame;
    var CTF[0] = LTF[0];
    }
    }​

    #2
    Hello dwrety,

    Below is a link to an example that uses values from a different series interval you may find helpful.


    When adding multiple series with AddDataSeries() each series will update OnBarUpdate(). The series updating OnBarUpdate() will be indicated by the BarsInProgress index.

    You can access other series by using the BarsInProgress index.

    For example to get the close price of an added series when the primary series is updating:

    if (CurrentBars[0] < 1 || CurrentBars[1] < 1)
    return;

    if (BarsInProgress == 0)
    {
    Print(Times[1][0].ToString() + " | Closes[1][0]: " + Closes[1][0].ToString());
    }​
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello dwrety,

      Below is a link to an example that uses values from a different series interval you may find helpful.


      When adding multiple series with AddDataSeries() each series will update OnBarUpdate(). The series updating OnBarUpdate() will be indicated by the BarsInProgress index.

      You can access other series by using the BarsInProgress index.

      For example to get the close price of an added series when the primary series is updating:

      if (CurrentBars[0] < 1 || CurrentBars[1] < 1)
      return;

      if (BarsInProgress == 0)
      {
      Print(Times[1][0].ToString() + " | Closes[1][0]: " + Closes[1][0].ToString());
      }​

      Thank you Chelsea. The issues I had was when I was trying to access values of the LTF of a custom data series by the time of the HTF bar's close, it's always the 2nd last LTF bar's value gets referenced. For instance, 5min bar as HTF and 1min as LTF. By the close of the 5min bar, I can only get the 4th 1min bar's value (custom data series, not the bar's OHLCV). But I will take a look at the code you referred.

      Comment


        #4
        I will post all of my indicator code here. Basically, when calculating the IER, I need to reference a LTF custom data series.
        Code:
        namespace NinjaTrader.NinjaScript.Indicators.Dwrety
        {
            public class Dwrety_IntrabarEfficiencyRatio : Indicator
            {
        
                private int tffactor;
                private Series<double> travel;
                private Series<double> diffSeries;
                private double sum;
                
                
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
                        Description                                    = @"Intrabar Efficiency Ratio";
                        Name                                        = "Dwrety_IntrabarEfficiencyRatio";
                        Calculate                                    = Calculate.OnEachTick;
                        IsOverlay                                    = false;
                        DisplayInDataBox                            = true;
                        DrawOnPricePanel                            = false;
                        DrawHorizontalGridLines                        = false;
                        DrawVerticalGridLines                        = false;
                        PaintPriceMarkers                            = false;
                        ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                        //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                        //See Help Guide for additional information.
                        IsSuspendedWhileInactive                    = true;
                        CTF                                            = 5;
                        LTF                                            = 30;
                        AddPlot(new Stroke(Brushes.DeepSkyBlue, 2), PlotStyle.Bar, "IER");
                    }
                    else if (State == State.Configure)
                    {
                        tffactor = CTF * 60 / LTF;
                        AddDataSeries(Data.BarsPeriodType.Second, LTF);
                    }
                    else if (State == State.DataLoaded)
                    {    
                        diffSeries = new Series<double>(BarsArray[1]);
                        travel = new Series<double>(BarsArray[1]);
                    }
                }
        
                protected override void OnBarUpdate()
                {
                    if (CurrentBars[0] < 2 || CurrentBars[1] < tffactor)
                        return;
                    
                    if (BarsInProgress == 1) {
                        diffSeries[0] = CurrentBars[1] > 0 ? Math.Abs(Inputs[1][0] - Inputs[1][1]): Inputs[1][0];
                        travel[0] = SUM(diffSeries, tffactor)[0];
                        sum = travel[0];
                    }
                    if (BarsInProgress == 0) {
                        // Print(string.Format("body range: {0}, travel: {1}", Math.Abs(Closes[0][0] - Opens[0][0]), sum));
                        IER[0] = Math.Abs(Closes[0][0] - Opens[0][0]) / sum * 100;
                        if (IER[0] >= 65) {
                            PlotBrushes[0][0] = Brushes.Magenta;
                        }
                        else if (IER[0] >= 40) {
                            PlotBrushes[0][0] = Brushes.Green;
                        }
                        if (IER[0] < 20) {
                            PlotBrushes[0][0] = Brushes.Red;
                        }
                    }
                }
        
                #region Properties
                [NinjaScriptProperty]
                [Range(1, int.MaxValue)]
                [Display(Name="CTF", Description="Current Time Frame (Minutes)", Order=1, GroupName="Parameters")]
                public int CTF
                { get; set; }
                
                [NinjaScriptProperty]
                [Range(1, 300)]
                [Display(Name="LTF", Description="Lower Time Frame (seconds)", Order=1, GroupName="Parameters")]
                public int LTF
                { get; set; }
        
        
                [Browsable(false)]
                [XmlIgnore]
                public Series<double> IER
                {
                    get { return Values[0]; }
                }
                #endregion
        
            }
        }​

        Comment


          #5
          Hello dwrety,

          The series value from barsAgo index [0] will be for the most recent bar update of that series.

          If this used in historical, TickReplay would need to be enabled to allow Calculate.OnEachTick to update for each tick in historical.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Hi Chelsea

            I think you might have misunderstood. I have a 5min frame as primary data source. And I have a lower time frame 30s.
            Every 10 bars of 30s is a 5min bar. And when the 10th 30s bar closes, the 5min bar also closes. This shouldn't require any TickReplay to work and should really matter if it's OnEachTick or OnBarClose. I need to calculate a custom series on the lower timeframe at the close of 10th 30s-bar and pass that data to the 5min frame, but the indicator is always passing the value at 9th bar. That's my issue. I don't think I did it correctly.

            Thank you.

            Comment


              #7
              Hello dwrety,

              I'm not able to reproduce.

              Attached is a test script and output.
              MTFTest_NT8.zip
              NinjaScript Output 8_13_2024 6_32 AM.txt

              In the output you can see when BIP 0 is updating, the 5 minute bar (Times[0][0]) and 30 second bar (Times[1][0]) are showing the same bar time.

              BIP: 1, Times[0][0]: 8/13/2024 6:05:00 AM, Times[1][0]: 8/13/2024 6:08:30 AM
              BIP: 1, Times[0][0]: 8/13/2024 6:05:00 AM, Times[1][0]: 8/13/2024 6:09:00 AM
              BIP: 1, Times[0][0]: 8/13/2024 6:05:00 AM, Times[1][0]: 8/13/2024 6:09:30 AM
              BIP: 0, Times[0][0]: 8/13/2024 6:10:00 AM, Times[1][0]: 8/13/2024 6:10:00 AM
              BIP: 1, Times[0][0]: 8/13/2024 6:10:00 AM, Times[1][0]: 8/13/2024 6:10:00 AM

              BIP: 1, Times[0][0]: 8/13/2024 6:10:00 AM, Times[1][0]: 8/13/2024 6:10:30 AM​

              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              667 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              377 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              110 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              575 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              580 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X