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 algospoke, 04-17-2024, 06:40 PM
    6 responses
    49 views
    0 likes
    Last Post algospoke  
    Started by arvidvanstaey, Today, 02:19 PM
    4 responses
    11 views
    0 likes
    Last Post arvidvanstaey  
    Started by samish18, 04-17-2024, 08:57 AM
    16 responses
    61 views
    0 likes
    Last Post samish18  
    Started by jordanq2, Today, 03:10 PM
    2 responses
    11 views
    0 likes
    Last Post jordanq2  
    Started by traderqz, Today, 12:06 AM
    10 responses
    21 views
    0 likes
    Last Post traderqz  
    Working...
    X