Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

MTF indicator does produce correct values for second data series

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

    MTF indicator does produce correct values for second data series

    I'm running my indicator on a 16 range chart. Within my indicator I have the following code:

    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Range, 32);
    Davg2 = BOP(BarsArray[1],14); // I want to have the BOP indicator calculating the values using the larger time frame of 32 range.​
    }

    protected override void OnBarUpdate()
    {

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

    Print ("16 tick close "+ (Closes[0][1]));// prints the correct value for the primary data series
    Print ("32 tick close"+ Closes[1][1]);// prints the correct value for the secondary data series
    Print ("32 tick "+ (Davg2[1])); //prints the incorrect expected value of BOP for the secondary data series. Instead I see the calculated //value using the primary data series of 16 range.
    }

    Not sure how to make sure I get expected valued of the indicator calculated for the secondary data series.

    #2
    Hello Oscarblopenza,

    Thank you for your post.

    I see the following line of code is included in State.Configure:
    Code:
    Davg2 = BOP(BarsArray[1],14); // I want to have the BOP indicator calculating the values using the larger time frame of 32 range.​
    I suggest moving it to State.DataLoaded, as that is used for logic that needs to access data related objects and will be called after all data series have been loaded. What are the results if you try the following instead:

    Code:
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Range, 32);
    }
    
    else if (State == State.DataLoaded)
    {
    Davg2 = BOP(BarsArray[1],14); // I want to have the BOP indicator calculating the values using the larger time frame of 32 range.​
    }
    
    protected override void OnBarUpdate()
    {
    
    if (CurrentBar < BarsRequiredToTrade || CurrentBars[0] < 0 || CurrentBars[1] < 0)
    return;
    ​
    Print ("16 tick close "+ (Closes[0][1]));// prints the correct value for the primary data series
    Print ("32 tick close"+ Closes[1][1]);// prints the correct value for the secondary data series
    Print ("32 tick "+ (Davg2[1])); //prints the incorrect expected value of BOP for the secondary data series. Instead I see the calculated //value using the primary data series of 16 range.
    }​
    For more details regarding OnStateChange, including when each state is called and where you should add certain types of logic, please see the following page:


    Please let me know if I may be of further assistance.

    Comment


      #3
      Thanks NinjaTrader_Emily, this seems to be working on both playback and live data.

      Comment


        #4
        NinjaTrader_Emily, I spoke too soon. I tried implemening the BarsInProgress ==1 and things broke. I've stripped a lot of the code and what I have below is not working due to error " Indicator 'A1PolaritySingleModeMTF2': Error on calling 'Update' method on bar 228: Object reference not set to an instance of an object."

        It never makes it past line Print ("made it to line 56"). I'm loading the indicator on a tick enabled chart with a Range 16 for NQ.

        namespace NinjaTrader.NinjaScript.Indicators
        {
        public class A1PolaritySingleModeMTF2 : Indicator
        {
        private AADeltaAvg3W2 Davg = null;
        private BOP Davg2 = null;
        private int BarsRequiredToTrade = 50;

        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        Description = @"Demonstrates adding buttons to Chart Trader";
        Name = "A1PolaritySingleModeMTF2";
        Calculate = Calculate.OnEachTick;
        IsOverlay = true;
        DisplayInDataBox = false;
        PaintPriceMarkers = false;

        }
        else if (State == State.Configure)
        {

        Davg = AADeltaAvg3W2(3,5);
        // Add NQ 32 range
        AddDataSeries(Data.BarsPeriodType.Range, 32);

        }
        else if (State == State.DataLoaded)
        {
        Davg2 = BOP(BarsArray[1],14);
        }

        }



        protected override void OnBarUpdate()
        {

        Print ("made it to line 56");

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

        Print ("made it to line 68");

        if (((Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0))
        && (Times[0][0].TimeOfDay <= new TimeSpan(17, 30, 0))))​

        {

        }
        }

        Comment


          #5
          Hello Oscarblopenza,

          Thank you for your reply.

          The error you are receiving is related to a null reference. For more details, please see the following page:


          Since you have added prints and you are seeing the following print:

          Print ("made it to line 56");

          The issue most likely has to do with the next line of code:

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

          I suggest adding print statements to print the values of each object used in this condition; this can help to understand what values are being used for CurrentBar, BarsRequiredToTrade, CurrentBars[0], and CurrentBars[1]. If any of the prints result in an error, you can identify which object might have a null reference. For more details on using prints to debug your scripts:


          Another item to consider: is there a reason you are using BarsRequiredToTrade in an indicator script? This typically applies to strategies, where indicators use BarsRequiredToPlot instead.

          Please let us know if we may be of further assistance.

          Comment


            #6
            The issue gets "fixed" when I setup the chart to load 700 bars, where I initially wanted it to load around 200. Is there a bit of code that I can introduce that tells me the exact number of bars I need to load so that it works? Also, I was using BarsRequiredToTrade because I picked it up from another script that had been published, but I didn't think it mattes because I'm seting up that number manually, not sure what the best approach is to set it programattically.

            Comment


              #7
              Hello Oscarblopenza,

              Thank you for your reply.

              Typically, the number of bars required has to do with the barsAgo you are using in your logic or if you are using an indicator, the period. You would need at least the number of bars for the period times the number of barsAgo to be able to access the data. Just based on the information you have shared so far, it is important to analyze why your script would behave differently between 700 bars to load and 200 bars to load. What are you using as your primary data series? Is it still a 16 range chart? Then you would have to consider that you are adding a 32 range series, along with an indicator with a period of 14 based on that added series. You would need enough data loaded on the primary series so that the 32 range series could look back at least 14 bars to generate data for the indicator. This may mean adding several 16 range bars until you are loading data far enough back to satisfy the criteria of 32 range with a period of 14.

              Please feel free to reach out with any additional questions or concerns.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              566 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              330 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              101 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              547 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              548 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X