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 Debugging my indicator

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

    Help Debugging my indicator

    Hi,

    I have compiled and added my indicator to my chart but dont see any plot - trying to debug it using print statements to the output window yet dont see anything there either.. This is my first custom script....

    Another question - when you recompile the indicator does the chart get automatically updated? or do you have to remove and add again?

    Here are my variables

    #region Variables:
    // Wizard generated variables
    private int length = 10; // Default setting for Length
    private int length1 = 14; // Default setting for Length1
    // User defined variables (add any user defined variables below)
    private int Trend_flo =0;
    private DataSeries PercentR; // Define a DataSeries variable
    private int PerR =0;
    private double StDev =0;
    private int LastBar =0;
    private DataSeries TrStop;
    #endregion


    Here is my initialize region:

    protected override void Initialize()
    {
    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Trend"));
    CalculateOnBarClose = true;
    Overlay = false;
    PriceTypeSupported = true;
    }

    Here is main code:

    protected override void OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.
    if(Time[0].Date != Time[1].Date)
    {
    Trend_flo =0;
    Print("Trend_flo initial condition: "+ Trend_flo);

    }
    double StDev = StdDev(length1)[0];
    //double PerR = PercentR(length);
    double HH =0.0;
    double Divisor = 0.0;
    HH = MAX(High, length)[0];
    Divisor = HH - MIN(Low, length)[0];
    if (Divisor != 0)
    PercentR.Set(100 - ( ( HH - Close[0] ) / Divisor ) * 100);
    else
    PercentR.Set(0.0);

    if ((PercentR[0] >= 70) && (PercentR[1] < 70) && (Trend_flo < 1) && (Close[0] > TrStop[0]))
    {
    TrStop.Set(Low[0] - StDev*1.382);
    Trend_flo = 1;
    Print("Trend_flo Long: "+ Trend_flo);
    LastBar = CurrentBar;
    }

    if ((PercentR[0] <= 30) && (PercentR[1] > 30) && (Trend_flo > -1) && (Close[0] < TrStop[0]))
    {
    TrStop.Set(High[0] + StDev*1.382);
    Trend_flo = -1;
    Print("Trend_flo Short: "+ Trend_flo);
    LastBar = CurrentBar;
    }

    if (Trend_flo == 1)
    {
    if ((Open[0] <= TrStop[0]) | (High[0] <= TrStop[0]) | (Low[0] <= TrStop[0]) | (Close[0] <= TrStop[0]))
    {
    Trend_flo =0;
    Print("End of Trend_flo Long: "+ Trend_flo);
    }
    else
    {
    if (LastBar < CurrentBar)
    {
    if (TrStop[0] == 0) TrStop.Set(Close[0]);
    if (Close[0] > Close[1]) TrStop.Set(Low[0] - StDev * 1.382);
    if (Low[0] - High[1] > StDev) TrStop.Set(Low[0] - StDev * 0.618);
    if (TrStop[0] < TrStop[1]) TrStop.Set(TrStop[1]);
    }

    }
    }

    if (Trend_flo == -1)
    {
    if ((Open[0] >= TrStop[0]) | (High[0] >= TrStop[0]) | (Low[0] >= TrStop[0]) | (Close[0] >= TrStop[0]))
    {
    Trend_flo =0;
    Print("End of Trend_flo Short: "+ Trend_flo);
    }
    else
    {
    if (LastBar < CurrentBar)
    {
    if (TrStop[0] == 0) TrStop.Set(Close[0]);
    if (Close[0] < Close[1]) TrStop.Set(High[0] + StDev * 1.382);
    if (Low[1] - High[0] > StDev) TrStop.Set(High[0] + StDev * 1.382);
    if (TrStop[0] > TrStop[1]) TrStop.Set(TrStop[1]);
    }

    }
    }

    // Set the plot value
    Trend.Set(Trend_flo);
    //Trend.Set(1);

    }

    #2
    Check the log tab for any errors.
    Yes, you must refresh (F5) the chart after you compile
    mrlogik
    NinjaTrader Ecosystem Vendor - Purelogik Trading

    Comment


      #3
      Checked the Logfile after doing a F5 and got the following message

      "Error on calling the 'OnBarUpdate' method for indicator 'mfPercentRBrkout' on bar 0: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name:index

      I presume it is referring to PercentR[1] in the following command ?

      if ((PercentR[0] >= 70) && (PercentR[1] < 70) && (Trend_flo < 1) && (Close[0] > TrStop[0]))
      Do I have initialiase a data series ? How does one do that?

      Comment


        #4
        I have changed the start of the main code to

        protected override void OnBarUpdate()
        {
        // Use this method for calculating your indicator values. Assign a value to each
        // plot below by replacing 'Close[0]' with your own formula.
        if (CurrentBar < length1)
        {
        //PercentR.Set(0,0.0);
        //PercentR.Set(1,0.0);
        return;
        }
        if(Time[0].Date != Time[1].Date)
        {
        Trend_flo =0;
        Print("This is a message");
        Print("Trend_flo initial condition: "+ Trend_flo);

        }
        double StDev = StdDev(length1)[0];
        Print("StDev: "+ StDev);
        //double PerR = PercentR(length);
        double HH =0.0;
        double Divisor = 0.0;
        HH = MAX(High, length)[0];
        Print("HH: "+ HH);
        Divisor = HH - MIN(Low, length)[0];
        Print("Divisor: "+ Divisor);
        if (Divisor != 0)
        PercentR.Set(100 - ( ( HH - Close[0] ) / Divisor ) * 100);
        else
        PercentR.Set(0.0);
        Print("PercentR[0]: "+ PercentR[0].ToString());

        if ((PercentR[0] >= 70) && (PercentR[1] < 70) && (Trend_flo < 1) && (Close[0] > TrStop[0]))
        {
        TrStop.Set(Low[0] - StDev*1.382);
        Trend_flo = 1;
        Print("This is a message");
        Print("Trend_flo Long: "+ Trend_flo);
        LastBar = CurrentBar;
        }


        It gets evaluated down as far as the highlighted in the red last line but I get the following error?

        Error on calling the 'OnBarUpdate' method for indicator 'mfPercentRBrkout' on bar 14: Object reference not set to an instance of an object.

        length1 is set to 14 so that makes sense but what does ' Object reference not set to an instance of an object." mean?

        Where can one get a detailed explanation of possible Log Errors?

        Comment


          #5
          seems to be complaining about the following line

          PercentR.Set(100 - ( ( HH - Close[0] ) / Divisor ) * 100);

          Comment


            #6
            The error message 'Object reference not set...' usually refers to accessing an object without first performing a null check (if (someObject !=null) {...}). If you post the complete code, I'll take a look and see if anything else looks out of place.
            AustinNinjaTrader Customer Service

            Comment


              #7
              mefTrader,

              Is PercentR a plot, or a DataSeries you create for use internally? If you create it for internal use you have to instantiate it in the Initialize() function

              Code:
              PercentR = new DataSeries(this);
              If its a plot, you have to add the plot at the bottom of the indicator. If you need more help please post the full code, or send me a PM if you don't want it on the forum.

              mrLogik
              mrlogik
              NinjaTrader Ecosystem Vendor - Purelogik Trading

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by bortz, 11-06-2023, 08:04 AM
              47 responses
              1,607 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
              16 views
              0 likes
              Last Post Javierw.ok  
              Working...
              X