Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

The Bollinger band plot issue

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

    The Bollinger band plot issue

    I'm trying to fetch the data for Bollinger band of previous bar, it seems like something is wrong that prevents plotting. Recent value works fine though!
    This is my code:


    ---------------------------
    public class _my : Indicator
    {
    private Bollinger bollinger;
    private double upperValue;
    private double lowerValue;


    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "_my";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    //NumStdDev = 1.75;
    //Period = 2;
    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;
    //Period = 2;

    AddPlot(Brushes.Blue, "Upper BB");//2
    AddPlot(Brushes.Blue, "Lower BB");//3

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

    bollinger = Bollinger(1.5, 2);


    }
    }

    protected override void OnBarUpdate()
    {


    upperValue = bollinger.Upper[1];
    lowerValue = bollinger.Lower[1];


    Values[0][0] = upperValue;
    Values[1][0] = lowerValue;



    }
    }

    ----------------------------------
    note that if I change the Bollinger band value calls to current bar (as shown bellow), it works fine and shows the indicator without issue
    upperValue = bollinger.Upper[0];
    lowerValue = bollinger.Lower[0];

    Does anyone have an idea what I'm doing wrong?​​
    Last edited by Ray11; 09-13-2022, 05:37 PM.

    #2
    Hello Ray11,

    If the indicator is not showing up its likely that you are having an error, make sure to check the NinjaScript output window or control center log tab.

    The given code won't work because you are using 1 BarsAgo but not checking if at least 1 bar of data exists before doing that.

    Code:
    protected override void OnBarUpdate()
    {
    if(CurrentBar < 1) return; 
    
    upperValue = bollinger.Upper[1];
    lowerValue = bollinger.Lower[1];
    
    
    Values[0][0] = upperValue;
    Values[1][0] = lowerValue;
    
    
    
    }

    Comment


      #3
      Thanks that worked

      Comment

      Latest Posts

      Collapse

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