Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Limit the number of bars to apply indicator

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

    Limit the number of bars to apply indicator

    I have recognized the even I set the indicator setting panel (default without any addtional setting coded in indicator) to 256,
    the indicator will apply the calculation and drawing to all (infinate) bars

    1) Is there simple way to apply indicator to calculate according to options selected in indicator panel?

    2) As a turnaround (not preferred)I coded below to limit the numbers to bars for calculation, and marker drawing. However the calucation and marker is drawn from the oldest to BarsToLookBack number of bars. How can I correct to draw and calculate for the bars from the latest?


    l
    Code:
    atestprotected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 1)
    
    // Debugging statements to check values
    Print("BarsToLookBack: " + BarsToLookBack);
    Print("CurrentBar: " + CurrentBar);
    
    // Ensure the drawing logic respects the custom BarsToLookBack setting
    if (BarsToLookBack == -1 || CurrentBar < BarsToLookBack) // Use the custom property
    {
    Print("Entering drawing logic for CurrentBar: " + CurrentBar);
    
    // Ensure there are enough bars for the calculations
    if (BarsToLookBack != -1 && CurrentBar < 25) // Adjust this value based on the maximum BarsAgo you use
    return;
    
    Print("Entering drawing logic for CurrentBar: " + CurrentBar);
    
    
    // Iterate from the latest bar to the oldest bar within the lookback range
    for (int i = CurrentBar; i >= Math.Max(0, CurrentBar - BarsToLookBack + 1); i--)
    {
    
    // Cache repeated calculations
    bool deltaTickIncrease = DeltaTickLine1.BaseValue[1] > DeltaTickLine1.BaseValue[0];
    bool deltaTickDecrease = DeltaTickLine1.BaseValue[1] < DeltaTickLine1.BaseValue[0];
    
    
    if (Absoprtion){
    if (deltaTickDecrease && deltaVolumeDecrease && deltaVolumeTickDiffIncrease && closeEqualOrDecrease){
    DrawPlusDiamondp(this, "VnTnD_Diamond_1" + CurrentBar, false, 0, High[0] + (3 * TickSize), Brushes.Yellow, (float)MarkerSize, Brushes.Transparent);
    }
    else if (deltaTickIncrease && deltaVolumeIncrease && deltaVolumeTickDiffDecrease && closeIncrease){
    DrawPlus.Diamondp(this, "VnTnD_Diamond_1" + CurrentBar, false, 0, Low[0] + (-3 * TickSize), Brushes.Yellow, (float)MarkerSize, Brushes.Transparent);
    }
    }
    
    ​

    #2
    Hello Marble,

    Thank you for your post.

    MaximumBarsLookBack only determines memory performance of custom Series<T> objects. It does not determine how many bars the indicator will calculate over, only how much of that memory is stored. When using MaximumBarsLookBack.TwoHundredFiftySix, only the last 256 values of the series object will be stored in memory and be accessible for reference.

    If you only wanted to keep drawings x number of bars back, you could use CurrentBar as well, as a forum user has pointed out on this post:

    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Hello
      I have tried below folloing the link but markers following the coded logic is drawd for the number BarsToLookBack from the oldest not the latest.

      approach 1

      Code:
      if (CurrentBar > BarsToLookBack)
      {// logic to draw marker
      }
      Approach 2
      Code:
      {
      if (CurrentBar > BarsToLookBack)
      
      {
      for (int index = 0; index < BarsToLookBack; index++)​
      {// logic to draw marker
      }

      Comment


        #4
        Hello,

        You need to call RemoveDrawObject() on the tag name of the object + (CurrentBar - 50) if you use the first approach, not include the logic to draw.
        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          The below logic worked but I want to know whether below is reducing system resouce. The intention is to reduce the loading time.
          Is this only calculating required Number ro bars or simplyg removing markers?
          Code:
          if (CurrentBars[0]>NumberOfBars)
          
          RemoveDrawObject("BullishMarker" + (CurrentBar-NumberOfBars));
          RemoveDrawObject("BearishMarker" + (CurrentBar-NumberOfBars));
          
          /////
          if (High[0] > highestHigh && BaseDeltaClose[0] <= highestDeltaClose && Open[0]<Close[0])
          {
          isBearishDivergence = true;
          //double adjustedHigh = High[0] + TickSize * 4; // Adjust Y position upwards for visibility
          Draw.TriangleDown(this, "BearishMarker" + CurrentBar, false, 0, adjustedHigh, Brushes.Transparent,7f,Brushes.Aqua);
          }
          ​
          isBullishDivergence = true;
          //double adjustedHigh = High[0] + TickSize * 4; // Adjust Y position upwards for visibility
          Draw.TriangleDown(this, "BullishMarker" + CurrentBar, false, 0, adjustedHigh, Brushes.Transparent,7f,Brushes.Aqua);
          }
          ​
          if not redujing system resource, appreciate if you could advise me how to reduce - not approach 1.​

          Comment


            #6
            This would simply be removing the markers. This approach would be required if you wanted to continue to continually draw the markers and then keep removing them after a certain number of bars have elapsed (in essence always only keeping the last 50 objects on the chart).
            Gaby V.NinjaTrader Customer Service

            Comment


              #7
              Then approach 1 is not fulfilling any of my intention.

              what i need to indicator calculate only from the lasst BarsToLookback to handle resource required logic.
              I have tried belowwhat is wrong. I have indicator to start calculation from the last BarsToLookBack bars but the markers can remain along with bars progressioin.
              Again intention is to reduce system resouces.
              Code:
              {
              if (CurrentBar > BarsToLookBack)
              
              {
              for (int index = 0; index < BarsToLookBack; index++)​
              {// logic to draw marker
              }
              ​

              Comment


                #8
                I have tried too loop logic for the latest 200 bars but still caculates from 200th candle from the oldest. what is wrong with this code?

                Code:
                protected override void OnBarUpdate()
                {
                
                if (CurrentBars[0]>200)
                
                for (int i = 1; i <Count-200; i++)
                {
                double sma0 = sma[0];
                double stdDev0 = stdDev[0];
                
                Upper[0] = sma0 + NumStdDev * stdDev0;
                Middle[0] = sma0;
                Lower[0] = sma0 - NumStdDev * stdDev0;
                
                // Set 1
                if (Close[0] >= Upper[0])
                {
                BackBrushAll = ColorHigh;
                }
                
                // Set 2
                if (Close[0] <= Lower[0])
                {
                BackBrushAll = ColorLow;
                }
                }
                }

                Comment


                  #9
                  Hello Marble,

                  what i need to indicator calculate only from the lasst BarsToLookback to handle resource required logic.
                  If you don't want to indicator to start calculating any further logic until CurrentBar is greater than BarsToLookBack then you would have to use a CurrentBar check at the top of OnBarUpdate.

                  Code:
                  if (CurrentBar < BarsToLookBack)
                  return;
                  This will prevent the indicator from ​starting any other logic (such as drawing) until the script has processed the required number of bars.


                  Keep in mind that this will not only keep the last 200 drawings (or whatever BarsToLookBack is) on the chart. For that, you would need additional logic that removes the drawings after they have already been drawn, as we have discussed. You cannot draw on bars without processing the bars first. There is no way around that.
                  Gaby V.NinjaTrader Customer Service

                  Comment


                    #10
                    No this is not what I am asking for.
                    If I apply below the indicator will calculate after processing BarsToLoookback bars.and draw objects.
                    But what I need to only to calculate last BarsToLoookback bars to reduce system resources and time to load the indicator

                    What i need it BarsToLookBack (eg 200) from the newest not from the oldest.


                    Code:
                    protected override void OnBarUpdate()
                    {
                    
                    if (CurrentBar < 200)
                    return;
                    
                    
                    double sma0 = sma[0];
                    double stdDev0 = stdDev[0];
                    
                    Upper[0] = sma0 + NumStdDev * stdDev0;
                    Middle[0] = sma0;
                    Lower[0] = sma0 - NumStdDev * stdDev0;
                    
                    // Set 1
                    if (Close[0] >= Upper[0])
                    {
                    BackBrushAll = ColorHigh;
                    }
                    
                    }
                    }
                    Last edited by Marble; 03-25-2025, 09:38 AM.

                    Comment


                      #11
                      Hello Marble,

                      What i need it BarsToLookBack (eg 200) from the newest not from the oldest.
                      If you only want to draw on the most recent (newest) 200 bars on the chart, there is no way to "reduce resources" as you're looking to. In order for the script to know what the most recent or newest 200 bars are on the chart, it first needs to process all previous bars. As new bars are processed, "the newest 200 bars" change.

                      As an example imagine a script that runs on 5-minute bars, and it draws on each bar. And we only want the script to be drawing on the last (most recent) 5 bars. So the script draws on the 1:00PM, 1:05PM, 1:10PM, 1:15PM, and 1:20PM bars.

                      Then the next bar update comes, which is 1:25PM. Now the last 5 bars are 1:05PM, 1:10PM, 1:15PM, 1:20PM, 1:25PM.

                      It needs to have logic to go back and delete the draw object on the 1:00PM bar since it is now 6 bars back and not 5 bars ago. The script needed to process the 1:00PM bar in order to draw on it, and it needs logic to go back and remove the drawing on that bar. There is no way to have the script both not process that bar and also draw on that bar at the time it was considered to be within the last 5 bars. There is no way to 'reduce resources' and also achieve this logic.
                      Gaby V.NinjaTrader Customer Service

                      Comment


                        #12
                        I dont think intention is understood.
                        Currently, the indicator is taking too much time when applied. The main interntion is reduce the time when applied.
                        Once applied. wheter drawing object remains more than bars to lookback is not at all a problem.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by SiebertCowen, Yesterday, 11:58 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post SiebertCowen  
                        Started by hunghnguyen2016, Yesterday, 08:00 PM
                        0 responses
                        8 views
                        0 likes
                        Last Post hunghnguyen2016  
                        Started by cbentrikin, Yesterday, 03:49 PM
                        0 responses
                        11 views
                        0 likes
                        Last Post cbentrikin  
                        Started by MiCe1999, 04-14-2025, 06:54 PM
                        7 responses
                        77 views
                        0 likes
                        Last Post b.j.d
                        by b.j.d
                         
                        Started by NISNOS69, Yesterday, 02:20 PM
                        0 responses
                        18 views
                        0 likes
                        Last Post NISNOS69  
                        Working...
                        X