• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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()
          {
          
          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;
          
          }
          // 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 rontronimous, Today, 09:41 PM
                      0 responses
                      1 view
                      0 likes
                      Last Post rontronimous  
                      Started by 400396, Today, 09:34 PM
                      0 responses
                      2 views
                      0 likes
                      Last Post 400396
                      by 400396
                       
                      Started by sandman55, Today, 08:00 PM
                      0 responses
                      3 views
                      0 likes
                      Last Post sandman55  
                      Started by Oracletrades, Yesterday, 02:56 AM
                      1 response
                      10 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Started by Bbrandon5567, Today, 04:56 PM
                      1 response
                      8 views
                      0 likes
                      Last Post Bbrandon5567  
                      Working...
                      X