Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Onbar Update Syntax issue.

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

    Onbar Update Syntax issue.

    My first attempt at coding and indicator and i am having an issue with two variables. The indicator compiles but doesnt show on chart so i know there i a syntax issue. The main issue is the math associated with formula below. Any help will be appreciated'

    "def s2 = expAverage(close,6) - expAverage(close,9);"





    private Series<Double> ema1;
    private Series<Double> ema2;
    private Series<Double> S2;
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "EZTrend";
    Calculate = Calculate.OnBarClose;
    IsOverlay = false;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = false;
    DrawVerticalGridLines = false;
    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.
    IsSuspendedWhileInactive = true;
    AddPlot(Brushes.Gold, "S2");
    AddLine(Brushes.PapayaWhip, 0, "ZeroLine");

    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    ema1 = new Series<double>(this);
    ema2 = new Series<double>(this);

    }

    }

    protected override void OnBarUpdate()
    {
    //Add your custom indicator logic here.
    double S2 = ema1[6] - ema2[9];

    #2
    Hello Teebone21,

    Thanks for your post.

    Please first check the Log tab of the Control Center for any errors that are thrown by your script that would need to be resolved.

    Typically, you will then use debugging prints to see how far your code gets before the error is received. Once you know the line of code that is throwing the error, you can then make changes to correct the error.

    In your snippet, I see that you are making bars ago references without a CurrentBar check (ensuring the script has processed X number of bars before referencing X bars ago) and this can throw an error.

    Please see the documentation below which can be helpful to resolve these errors.

    Debugging tips - https://ninjatrader.com/support/help...script_cod.htm

    Making sure enough bars have been processed - https://ninjatrader.com/support/help...nough_bars.htm

    If you are new to programming, it can be helpful to create some logic in the Strategy Builder, and then click the View Code button to see the generated syntax.

    We look forward to assisting.

    Comment


      #3
      Hello,
      You've 2 series ema1 & ema2 as double series but there is no definition for both & then you're negating them.
      Suggestion :
      Don't use double series for ema1 & ema2, instead you should use private EMA ema1 & private EMA ema2 & then in Data Loaded: ema1 = EMA(Period1); ema2 = EMA(Period2);
      On Bar Update:
      if(CurrentBar<9)
      return;
      S2[0] = ema1[6] - ema2[9];

      Hope it helps!
      Last edited by s.kinra; 10-13-2020, 11:43 PM.

      Comment


        #4
        Originally posted by s.kinra View Post
        Hello,
        You've 2 series ema1 & ema2 as double series but there is no definition for both & then you're negating them.
        Suggestion :
        Don't use double series for ema1 & ema2, instead you should use private EMA ema1 & private EMA ema2 & then in Data Loaded: ema1 = EMA(Period1); ema2 = EMA(Period2);
        On Bar Update:
        if(CurrentBar<9)
        return;
        S2[0] = ema1[6] - ema2[9];

        Hope it helps!
        Thank you that was exactly what I needed !

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        648 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        369 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        108 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        572 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        574 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X