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

Help : indicator not plotting

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

    Help : indicator not plotting

    Hi,

    my indicator does not plot. I get the following error message when i try to plot it :
    Error on calling the 'OnBarUpdate' method for indicator 'test2' on bar 10 : Object reference not set to an instance of an object.

    Here is the code :

    [Description("Enter the description of your new custom indicator here")]
    public class test2 : Indicator
    {
    #region Variables
    // Wizard generated variables
    private int period = 10; // Default setting for Period
    // User defined variables (add any user defined variables below)
    private DataSeries Var50;
    private DataSeries Var51;
    private int Var49 = 1;
    private int Var52 =10;
    private int Var53 =10;
    private double Var54 = 0.0;
    private double Var55 = 0.0;
    private double Var56 = 0.0;
    private double Var57 = 0.0;
    #endregion

    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Hash, "Var38"));
    Add(new Plot(Color.FromKnownColor(KnownColor.LimeGreen), PlotStyle.Hash, "Var39"));
    Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Hash, "Var40"));
    Add(new Plot(Color.FromKnownColor(KnownColor.LimeGreen), PlotStyle.Hash, "Var41"));
    Add(new Plot(Color.FromKnownColor(KnownColor.Magenta), PlotStyle.Hash, "Var59"));
    CalculateOnBarClose = true;
    Overlay = true;
    PriceTypeSupported = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {

    if (CurrentBar<period)
    return;

    Var50.Set(AverageFC(Low,period)[0]);
    Var51.Set(AverageFC(High,period)[0]);

    if (Var49 == -1 && CrossAbove(Close,Var51[0],1)) {Var49 = 1;}
    if (Var49 == 1 && CrossBelow(Close,Var50[0],1)) {Var49 = -1;}

    if (CurrentBar>150) {
    double Var37 = MAX(High,150)[0];
    double Var36 = MIN(Low,150)[0];

    if (Var37 != Var54 && Var52 >= 10)
    {
    Var54 = Var37;
    Var52=0;
    Var56= ATR(Close,period)[0];
    }

    if (Var36 != Var55 && Var53 >= 10)
    {
    Var55 = Var37;
    Var53=0;
    Var57= ATR(Close,period)[0];
    }

    Var52++;
    Var53++;


    Var38.Set(Var54-Var56*0.50);
    Var39.Set(Var54+Var56*0.50);
    Var40.Set(Var55-Var57*0.50);
    Var41.Set(Var55+Var57*0.50);
    if (Var49 ==1) { Var59[0]=Var50[0]; }
    if (Var49 ==-1) { Var59[0]=Var51[0]; }
    }
    }

    May someone help ?

    Another question : I would like to plot only the last value on the right of the chart (some kind of S/R but not overlaid on the price). Is there a way to set a right margin to the chart ? If so, I would really appreciate some advice I can start from.

    #2
    kuroro, everything looks fine, but how is AverageFC defined? I feel like that is where the problem lies. You can add Print() statements sprinkled throughout your code and watch the output window to determine exactly where the error occurs.
    AustinNinjaTrader Customer Service

    Comment


      #3
      the AverageFC is just a more efficient way to calculate a SMA. I have replaced this function by the default SMA but it does not change anything.

      I also tried to print the first calculations (Var50) but the output window is completely blank.

      This is really confusing because as you already point out, everything looks fine...I'm a little bit lost there....

      Comment


        #4
        kuroro,

        You need to instantiate the DataSeries variables in the Initialize() section of the code.

        Code:
        Initialize()
        {
        Var50 = new DataSeries(this);
        Var51 = new DataSeries(this);
        }
        mrlogik
        NinjaTrader Ecosystem Vendor - Purelogik Trading

        Comment


          #5
          Of course !!!

          what an idiot I completely forgot :-)

          Thank you mrlogik.

          I still need an answer to my other question and I can close this topic :

          Another question : I would like to plot only the last value on the right of the chart (some kind of S/R but not overlaid on the price). Is there a way to set a right margin to the chart ? If so, I would really appreciate some advice I can start from.

          Comment


            #6
            Unfortunately plotting into the future is not supported per default - you could look into overriding the Plot method via the CustomPlotSample.
            BertrandNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by ETFVoyageur, Today, 02:08 AM
            1 response
            7 views
            0 likes
            Last Post ETFVoyageur  
            Started by kujista, 04-22-2024, 07:46 AM
            3 responses
            12 views
            0 likes
            Last Post kujista
            by kujista
             
            Started by kujista, 04-23-2024, 06:23 AM
            7 responses
            57 views
            0 likes
            Last Post kujista
            by kujista
             
            Started by SentientDavid, Today, 01:34 AM
            0 responses
            8 views
            0 likes
            Last Post SentientDavid  
            Started by MrForgetful, Today, 01:28 AM
            0 responses
            6 views
            0 likes
            Last Post MrForgetful  
            Working...
            X