Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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;
    
    
    
    }
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks that worked

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Mongo, Today, 11:05 AM
      0 responses
      1 view
      0 likes
      Last Post Mongo
      by Mongo
       
      Started by Tim-c, Today, 10:58 AM
      0 responses
      1 view
      0 likes
      Last Post Tim-c
      by Tim-c
       
      Started by traderqz, Yesterday, 09:06 AM
      3 responses
      22 views
      0 likes
      Last Post NinjaTrader_ThomasC  
      Started by f.saeidi, Today, 10:19 AM
      1 response
      5 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Started by kujista, Today, 06:23 AM
      5 responses
      18 views
      0 likes
      Last Post kujista
      by kujista
       
      Working...
      X