Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

several barsinprogress logic

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

    several barsinprogress logic

    HI i have a question about several adddattaseries.
    Since we have to hardcode dataseries. Can we use bools and then based on bools we can select highertime frame? something like below?

    else if (State == State.Configure)
    {
    // Adds a secondary bar object to the indicator
    AddDataSeries(BarsPeriodType.Minute, 5);
    AddDataSeries(BarsPeriodType.Minute, 60);
    AddDataSeries(BarsPeriodType.Minute, 240);
    }​

    protected override void OnBarUpdate()
    {


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

    if(5minbool)
    {
    if (BarsInProgress == 1)
    {logic here
    }
    }

    if(60minbool)
    {
    if (BarsInProgress == 3)
    {

    }
    }

    if(240minbool)
    {
    if (BarsInProgress == 3)
    {

    }
    }​



    }​

    #2
    Hello tkaboris,

    You can absolutely have code to run logic during a specific bars in progress using bools in this way.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi i am getting common error but i looked at the code and made sure i have enough verification coverage. What am i missing?
      Error on calling 'OnBarUpdate' method on bar 0:

      BarsRequiredToTrade = 10;
      else if (State == State.Configure)
      {
      AddDataSeries(Data.BarsPeriodType.Tick, 1);
      AddDataSeries(Data.BarsPeriodType.Tick, 6000);
      AddDataSeries(Data.BarsPeriodType.Tick, 2000);
      AddDataSeries(Data.BarsPeriodType.Tick, 3000);
      AddDataSeries(Data.BarsPeriodType.Tick, 4000);
      AddDataSeries(Data.BarsPeriodType.Minute, 15);
      AddDataSeries(Data.BarsPeriodType.Minute, 30);
      AddDataSeries(Data.BarsPeriodType.Minute, 60);
      AddDataSeries(Data.BarsPeriodType.Minute, 240);

      fastEMASF = EMA(Close, fastEMAPeriodSF);
      slowEMASF = EMA(Close, slowEMAPeriodSF);
      TrendMagicModified1 = TrendMagicModified(Close, 20, 14, 1, false);


      }


      else if (State == State.DataLoaded)
      {
      ClearOutputWindow(); //Clears Output window every time strategy is enabled
      fastEMASF = EMA(fastEMAPeriodSF);
      slowEMASF = EMA(slowEMAPeriodSF);
      fastEMASF.Plots[0].Brush = Brushes.Blue;
      slowEMASF.Plots[0].Brush = Brushes.DarkOrange;
      if(ShowEMAs)
      {
      AddChartIndicator(fastEMASF);
      AddChartIndicator(slowEMASF);
      }
      atrIndicator = ATR(ATRPeriod);


      ParabolicSAR1 = ParabolicSAR(Acc, AccStep, AccMax);
      ParabolicSAR1.Plots[0].Brush = Brushes.Goldenrod;
      AddChartIndicator(ParabolicSAR1);
      TrendMagicModified1 = TrendMagicModified(Close, 20, 14, 1, false);
      // TrendMagicModified2 = TrendMagicModified(Closes[1], 20, 14, 1, false);
      AddChartIndicator(TrendMagicModified1);

      }​

      if (BarsInProgress == 0)
      {
      if (CurrentBars[0] < 15 || CurrentBars[1] < 15 || CurrentBars[2] < 15 || CurrentBars[3] < 15
      || CurrentBars[4] < 15 || CurrentBars[5] < 15 || CurrentBars[6] < 15
      || CurrentBars[7] < 15 || CurrentBars[8] < 15 || CurrentBars[9] < 15
      )
      return;​

      Comment


        #4
        Also when i am in barsinprogress2, do i specify
        CurrentBars[0] < 2 or CurrentBars[2] < 2 ?

        region area barsinprogress = 2
        else if(BarsInProgress == 2)
        {
        if (CurrentBars[0] < 2 )
        return;​
        }

        Comment


          #5
          Hello tkaboris,

          The CurrentBars[barsInProgress index] should be for the BarsInProgress processing.

          if (BarsInProgress == 2 && CurrentBars[2] < 2)
          return;

          What is the full error message you are getting?

          What specific line of code is causing the error?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            HI it seems to be working when i changed barsinprogress from 0 to 2 as you said.

            Also are we allowed to do this in stateconfigure or we need to perform selection logic on barsinprogress level?

            else if (State == State.Configure)
            {
            if(tick){
            AddDataSeries(Data.BarsPeriodType.Tick, 1);
            }
            if(tick6000){
            AddDataSeries(Data.BarsPeriodType.Tick, 6000);​
            }

            Comment


              #7
              Hello tkaboris,

              Using a condition to call AddDataSeries() would be loading series dynamically, which is not supported.
              (Specifically this will break optimization)
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                HI actually when i run strategy in market replay i get this error

                Indicator 'ParabolicSAR': Error on calling 'OnBarUpdate' method on bar 1478: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.


                Strategy 'TrendMagicAlgoAlgo': Error on calling 'EventHandlerBarsUpdate' method: Object reference not set to an instance of an object.

                Code:
                else if (State == State.Configure)
                            {
                                AddDataSeries(Data.BarsPeriodType.Tick, 1);
                                AddDataSeries(Data.BarsPeriodType.Tick, 6000);
                                AddDataSeries(Data.BarsPeriodType.Tick, 2000);
                //                AddDataSeries(Data.BarsPeriodType.Tick, 3000);
                                AddDataSeries(Data.BarsPeriodType.Tick, 4000);
                                AddDataSeries(Data.BarsPeriodType.Minute, 15);
                                AddDataSeries(Data.BarsPeriodType.Minute, 30);
                                AddDataSeries(Data.BarsPeriodType.Minute, 60);
                //                AddDataSeries(Data.BarsPeriodType.Minute, 240);
                
                //                AddDataSeries("MNQ 09-23", Data.BarsPeriodType.Tick, 1600, Data.MarketDataType.Last);
                                fastEMASF = EMA(Close, fastEMAPeriodSF);
                                slowEMASF = EMA(Close, slowEMAPeriodSF);
                                TrendMagicModified1    = TrendMagicModified(Close, 20, 14, 1, false);
                
                
                            }
                
                
                            else if (State == State.DataLoaded)
                            {
                                ClearOutputWindow(); //Clears Output window every time strategy is enabled                    
                                fastEMASF = EMA(fastEMAPeriodSF);
                                slowEMASF = EMA(slowEMAPeriodSF);            
                                fastEMASF.Plots[0].Brush = Brushes.Blue;
                                slowEMASF.Plots[0].Brush = Brushes.DarkOrange;                
                                if(ShowEMAs)
                                    {
                                    AddChartIndicator(fastEMASF);
                                    AddChartIndicator(slowEMASF);    
                                    }
                                atrIndicator  = ATR(ATRPeriod);        
                
                
                                ParabolicSAR1                = ParabolicSAR(Acc, AccStep, AccMax);
                                ParabolicSAR1.Plots[0].Brush = Brushes.Goldenrod;
                                AddChartIndicator(ParabolicSAR1);
                                TrendMagicModified1                = TrendMagicModified(Close, 20, 14, 1, false);
                //                TrendMagicModified2                = TrendMagicModified(Closes[1], 20, 14, 1, false);
                                AddChartIndicator(TrendMagicModified1);                
                
                            }​

                Comment


                  #9
                  Hello tkaboris,

                  The indicator is instantiated with BarsInProgress 0

                  Is the indicator only called in BarsInProgress 0 in OnBarUpdate()?
                  If not, before calling the indicator, may I confirm CurrentBars[0] is greater than 0?

                  I gave this a test and wasn't able to reproduce the error. Below is a link to a video of the test
                  https://drive.google.com/file/d/1FoB...w?usp=drivesdk

                  Are you able to reproduce using the attached test script?
                  PSARTest_NT8.zip
                  Attached Files
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    HI i tested your version and i cant reproduce the error either.

                    in my script i call indicator in inbarsinprogress2 and others as well

                    How can i confirm this?
                    If not, before calling the indicator, may I confirm CurrentBars[0] is greater than 0?

                    region area barsinprogress = 2
                    else if(BarsInProgress == 2)
                    {
                    if (CurrentBars[2] < 2 )
                    return;
                    // if (CurrentBars[0] <= BarsRequiredToTrade )
                    // return;
                    // double indicatorValue = TrendMagicModified(Closes[1], 20, 14, 1, false);
                    TrendMagicModified2 = TrendMagicModified(20, 14, 1, false);
                    ParabolicSAR2 = ParabolicSAR(0.02, 0.2, 0.02);
                    // Print("TrendMagicModified1.BullOrBear[0]"+TrendMagicModified1.BullOrBear[0]);
                    // Print("TrendMagicModified2.BullOrBear[0]"+TrendMagicModified2.BullOrBear[0]);
                    if ((TrendMagicModified2.BullOrBear[1] == 0)
                    && (ParabolicSAR2[0] <= Low[0])
                    && (Close[1] > Open[1] && Close[1] > Open[2]))

                    {
                    HTFBull = true;
                    }
                    else
                    {
                    HTFBull = false;
                    }
                    if ((TrendMagicModified2.BullOrBear[1] == 1)
                    && (ParabolicSAR2[0] >= High[0])
                    && (Close[1] < Open[1] && Close[1] < Open[2]))
                    {
                    HTFBear = true;
                    }
                    else
                    {
                    HTFBear = false;
                    }


                    }
                    #endregion​

                    Comment


                      #11
                      its also called in state configure and data loaded. I provided snippets above

                      Comment


                        #12
                        Hello tkaboris,

                        Code:
                        if (BarsInProgress == 2)
                        {
                            if (CurrentBars[0] < 1 || CurrentBars[2] < 1)
                                return;
                        
                           if (ParabolicSAR2[0] >= High[0])
                           {
                           }
                        }
                        OR
                        Code:
                        if (CurrentBars[0] < 1 || CurrentBars[2] < 2)
                        return;
                        
                        if (BarsInProgress == 2)
                        {
                            if (ParabolicSAR2[0] >= High[0])
                            {
                            }
                        ​}
                        Chelsea B.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by NullPointStrategies, Yesterday, 05:17 AM
                        0 responses
                        63 views
                        0 likes
                        Last Post NullPointStrategies  
                        Started by argusthome, 03-08-2026, 10:06 AM
                        0 responses
                        139 views
                        0 likes
                        Last Post argusthome  
                        Started by NabilKhattabi, 03-06-2026, 11:18 AM
                        0 responses
                        75 views
                        0 likes
                        Last Post NabilKhattabi  
                        Started by Deep42, 03-06-2026, 12:28 AM
                        0 responses
                        45 views
                        0 likes
                        Last Post Deep42
                        by Deep42
                         
                        Started by TheRealMorford, 03-05-2026, 06:15 PM
                        0 responses
                        50 views
                        0 likes
                        Last Post TheRealMorford  
                        Working...
                        X