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 bortz, 11-06-2023, 08:04 AM
      47 responses
      1,611 views
      0 likes
      Last Post aligator  
      Started by jaybedreamin, Today, 05:56 PM
      0 responses
      9 views
      0 likes
      Last Post jaybedreamin  
      Started by DJ888, 04-16-2024, 06:09 PM
      6 responses
      19 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by Jon17, Today, 04:33 PM
      0 responses
      6 views
      0 likes
      Last Post Jon17
      by Jon17
       
      Started by Javierw.ok, Today, 04:12 PM
      0 responses
      22 views
      0 likes
      Last Post Javierw.ok  
      Working...
      X