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

Indicator Error - not plotting when applied.

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

    Indicator Error - not plotting when applied.

    I applied a new indicator I built to the chart and once applied the indicator is blank with nothing showing. It compiles correctly without any errors and when I check the log I get the following error.


    Indicator ‘DeltaVolume’: Error on calling ‘OnBarUpdate’ method on bar 0: ‘AddPlot’ cannot be called from this state. Please see the Help Guide Article.


    I have tried a number of solutions from the help guid and forum and nothing seems to work.

    ###########

    namespace NinjaTrader.NinjaScript.Indicators
    {
          public class DeltaVolume : Indicator
          {
                protected override void OnStateChange()
                {
                      if (State == State.SetDefaults)
                      {
                            Description                                                  = @"Enter the description for your new custom Indicator here.";
                            Name                                                          = "DeltaVolume";
                            Calculate                                                     = Calculate.OnEachTick;
                            IsOverlay                                                     = false;
                            DisplayInDataBox                                        = true;
                            DrawOnPricePanel                                      = true;
                            DrawHorizontalGridLin es                           = true;
                            DrawVerticalGridLines                                 = true;
                            PaintPriceMarkers                                       = true;
                            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.
                      
                      }
                      else if (State == State.Configure)
                      {
                      }
                }

                protected override void OnBarUpdate()
                {
                                 
                      //Add your custom indicator logic here.

                      
          double currentBidVolume;
          double currentAskVolume;

    currentBidVolume = GetCurrentBidVolume();

    currentAskVolume = GetCurrentAskVolume();
                      
    // Calculate the bid/ask volume delta //

    double volumeDelta = (currentBidVolume - currentAskVolume) / (currentBidVolume + currentAskVolume);
                      
          
    // Check if the volume delta is over 25% or below -25% //

    {
                
    // Check if the volume delta is over 25% or below -25% //

    if (volumeDelta > 0.25)

    {
          
          // Plot a green bar //

    AddPlot(new Stroke(Brushes.LawnGreen, 1), PlotStyle.Bar, "Green");

    }



          else if (volumeDelta < -0.25)

    {
                
                

    AddPlot(new Stroke(Brushes.Red, 1), PlotStyle.Bar, "Red");

    }
          
          else

    {

    // Don't plot a bar //

    AddPlot(new Stroke(Brushes.Transparent, 1), PlotStyle.Bar, "None");

    }

                }     
                      }​




    #2
    Hello cogtrade,

    Welcome to the NinjaTrader forums!

    The error message is absolutely correct. You cannot call AddPlot() in OnBarUpdate().

    Below is a link to the help guide on AddPlot().
    https://ninjatrader.com/support/help...t8/addplot.htm

    Note from the help guide:
    "Warning: This method should ONLY be called within the OnStateChange() method during State.SetDefaults or State.Configure"

    Once a plot is added in State.SetDefaults or State.Configure, you can choose whether to set a plot value to the Values[plot index][barsAgo index] collection. If no plot value is set, no plot will appear. If a plot value is set a plot will appear.
    https://ninjatrader.com/support/help...nt8/values.htm

    If you are trying to set the color of a plot for a specific bar, this is done by setting PlotBrushes[plot index][barsAgo index].
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by GussJ, 03-04-2020, 03:11 PM
    16 responses
    3,281 views
    0 likes
    Last Post Leafcutter  
    Started by WHICKED, Today, 12:45 PM
    2 responses
    19 views
    0 likes
    Last Post WHICKED
    by WHICKED
     
    Started by Tim-c, Today, 02:10 PM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by Taddypole, Today, 02:47 PM
    0 responses
    5 views
    0 likes
    Last Post Taddypole  
    Started by chbruno, 04-24-2024, 04:10 PM
    4 responses
    53 views
    0 likes
    Last Post chbruno
    by chbruno
     
    Working...
    X