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 for simple indicator

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

    Help for simple indicator

    Hi,
    i have got a little problem with a simple indicator that colour the last bar if it's an ENGULFING bar, but some day it dosen't work and in the output window i read this error :

    "Error on calling 'OnBarUpdate' method for indicator 'Engulfing' on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

    The code is this :
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    [Description("Enter the description of your new custom indicator here")]
    public class Engulfing : Indicator
    {
    #region Variables
    // Wizard generated variables
    private int myInput0 = 1; // Default setting for MyInput0
    // User defined variables (add any user defined variables below)
    #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()
    {
    CalculateOnBarClose = false;
    }

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

    // Condition Long
    if (High[0] > High[1]
    && Low[0] < Low[1]
    && Close[0] > Open[0])
    {
    BarColor = Color.Green;
    CandleOutlineColor = Color.Green;

    }
    // Condition Short
    if (High[0] > High[1]
    && Low[0] < Low[1]
    && Close[0] < Open[0])
    {
    BarColor = Color.Red;
    CandleOutlineColor = Color.Red;
    }
    }

    #region Properties

    #endregion
    }
    }


    Can someone help me ? Where the error is ?
    Thanks

    #2
    Hello,

    Since you're checking the current bars high to the previous bars high, you will need to ensure there is enough bars on the chart to calculate.

    You can do this by adding the following at the beginning of your OnBarUpdate section of the code:

    Code:
    if (CurrentBar < 1)
    return;
    This will ensure that starting from the very first bar on the chart, it can look back at least 1 bar to calculate.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Sorry, can you tell me where exacly must I add the code ? Thanks .
      if (CurrentBar < 1) return;

      Comment


        #4
        Originally posted by sniper View Post
        Sorry, can you tell me where exacly must I add the code ? Thanks .
        if (CurrentBar < 1) return;
        I located the code in the position posted below but dosen't work yet. No error code.

        CODE:

        protected override void OnBarUpdate()
        {
        if (CurrentBar <1) return;
        // Condition Long
        if (High[0] > High[1]
        && Low[0] < Low[1]
        && Close[0] > Open[0])
        {
        BarColor = Color.Green;
        CandleOutlineColor = Color.Green;

        Comment


          #5
          at the beginning of your OnBarUpdate section of the code:

          Code:
          protected override void OnBarUpdate()
          {
          
          [B]if(CurrentBar < 1)
          return;[/B]
          
          // Condition Long
          if (High[0] > High[1]
          && Low[0] < Low[1]
          && Close[0] > Open[0])
          {
          BarColor = Color.Green;
          CandleOutlineColor = Color.Green;
          
          }
          // Condition Short
          if (High[0] > High[1]
          && Low[0] < Low[1]
          && Close[0] < Open[0])
          {
          BarColor = Color.Red;
          CandleOutlineColor = Color.Red;
          }
          }
          MatthewNinjaTrader Product Management

          Comment


            #6
            just done, but it dosen't work.

            Comment


              #7
              This works here, but there aren't a lot of instances where your condition is true.

              I'd suggest adding some Print() statements to print the time the condition is true. You can view the output from these statements by going to Tools--> Output window

              Code:
                      protected override void OnBarUpdate()
                      {
              			
              			if(CurrentBar <1)
              				return;
                       
              			// Condition Long
              			if (High[0] > High[1]
              			&& Low[0] < Low[1]
              			&& Close[0] > Open[0])
              			{
              				BarColor = Color.Green;
              				CandleOutlineColor = Color.Green;
              				Print(Time[0]);
              
              			}
              			// Condition Short
              			if (High[0] > High[1]
              			&& Low[0] < Low[1]
              			&& Close[0] < Open[0])
              			{
              				BarColor = Color.Red;
              				CandleOutlineColor = Color.Red;
              				Print(Time[0]);
              			}
              		}
              Please note and add the Print(Time[0]); I had put and then reload your script. This should produce results to the Tools--> Output window and you can check when the conditions are true.
              MatthewNinjaTrader Product Management

              Comment


                #8
                Done but the output give me the same error. I'm waithing for the right condition to see the check.

                Comment


                  #9
                  Originally posted by sniper View Post
                  Done but the output give me the same error. I'm waithing for the right condition to see the check.
                  Nothing happned when the condition are true. Just happned 1 of that.

                  Comment


                    #10
                    Hello,

                    You're still getting the OnBarUpdate error after adding if(CurrentBar < 1) return?

                    Did you recompile the script after you made this change?
                    MatthewNinjaTrader Product Management

                    Comment


                      #11
                      Sorry ! I Didn't recompile !

                      NOW IT WORK FINE !

                      Great support.

                      Thanks a lot.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by usazencort, Today, 01:16 AM
                      0 responses
                      1 view
                      0 likes
                      Last Post usazencort  
                      Started by kaywai, 09-01-2023, 08:44 PM
                      5 responses
                      603 views
                      0 likes
                      Last Post NinjaTrader_Jason  
                      Started by xiinteractive, 04-09-2024, 08:08 AM
                      6 responses
                      22 views
                      0 likes
                      Last Post xiinteractive  
                      Started by Pattontje, Yesterday, 02:10 PM
                      2 responses
                      21 views
                      0 likes
                      Last Post Pattontje  
                      Started by flybuzz, 04-21-2024, 04:07 PM
                      17 responses
                      230 views
                      0 likes
                      Last Post TradingLoss  
                      Working...
                      X