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

High[i] & Low[i] in method. Please help.

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

    High[i] & Low[i] in method. Please help.

    Hello guys.

    Can some one say me, why i can't get history bars in my method "priceUpOrDownORFlat()"?
    I'm always recieve exception "ArgumetOutOfRangeException".
    Here my indicator code:

    Code:
    protected override void OnBarUpdate()
            {
                DataSeries inp = new DataSeries(this);
    			// Use this method for calculating your indicator values. Assign a value to each
                // plot below by replacing 'Close[0]' with your own formula.
                String trend = priceUpOrDownORFlat(inp);
    			if (trend.IndexOf('u')!=-1) { Plot0.Set(High[0]); }
    			else if (trend.IndexOf('d')!=-1) { Plot0.Set(Low[0]); }
    			else { Plot0.Set((High[0]+Low[0])/2); }
    			Plot0.Set(Close[0]);
            }
    		
    		private String priceUpOrDownORFlat(DataSeries input) {
    			// u- up, d- down, f- flat
    			String tr = "f";
    			double[] arr = new double[3];
    			for (int i = 3; i >= 1; i--) {
    				arr.SetValue(((input.Indicator.High[i] + input.Indicator.Low[i])/2),(int)(i-1));	
    			}
    			if((double)arr[2]>=(double)arr[1] && (double)arr[1]>=(double)arr[0]) { tr = "d"; }
    			else if((double)arr[2]<=(double)arr[1] && (double)arr[1]<=(double)arr[0]) { tr = "u"; }
    			else { tr = "f"; }
    			return tr;
    		}
    Last edited by nordseven; 02-13-2016, 11:20 AM.

    #2
    Your code shows you have not studied the examples here:



    Code:
    protected override void OnBarUpdate()
            {
                DataSeries inp = new DataSeries(this);
                ....
    This code should be in Initialize, not OnBarUpdate. Study the example.
    And, you'll want to define the inp variable outside of Initialize. Again, study the example in the above link.

    But, wait, what is the purpose of the inp DataSeries?
    I mean, you have not used Set to add any values into inp.

    That means this code,

    Code:
    String trend = priceUpOrDownORFlat(inp);
    is passing an empty DataSeries as its argument. (The exception you receive is most likely related to this.)

    Are you sure you need a brand new DataSeries here?

    What is a DataSeries?
    Every price point of each individual candlestick (aka, the bar) is stored in an ever growing "backwards" style array -- this special backwards style array is called a DataSeries.

    [In a normal array, the 0th index is the first element. But in a DataSeries, the 0th element is the last element, or more rightfully, the most recent element added to the DataSeries using the Set method. A DataSeries seems like just another array of doubles, but it has very special semantics around how it is built and how it is accessed.]

    Let's expand: you know that every individual bar has these 4 price values: open, high, low, close (commonly abbreviated OHLC) and NinjaTader stores these values for all bars in 4 separate arrays ... er, I mean, DataSeries.

    A DataSeries holds values of type double such that the 0th element is always from the most recently closed bar.

    Thus, to access the OHLC of the most recently closed bar requires inspecting the 4 separate arrays (oops, I mean, DataSeries) for the 0th element,

    Code:
    Open[0]
    High[0]
    Low[0]
    Close[0]
    The next previously closed bar is at index 1, and so on. As each newly closed bar forms on the chart, NinjaTrader behind the scenes shifts all values in each of the 4 arrays (er, I mean, DataSeries) one index higher, which frees up the 0th element again.

    Why does it do this? Well, because, like I said, the value at Close[0] is always the close price of the most recently closed bar. So everything is shifted higher by 1, to free up the 0th spot for the newly closed bar.

    The index value used in a DataSeries has a special name because it helps with understanding what the DataSeries is storing. We call the index BarsAgo because Close[x] literally represents the close price from x bars ago, so sure x is an index, but think of x as the BarsAgo index.

    So, I ask again, what is the purpose of inp?

    You have 4 perfectly good DataSeries created for you: Open, High, Low, Close. (And there are 3 others, such as Median, Typical and Weighted, but I digress.) Could you use one of these?

    For example, have you tried replacing inp with any of these?

    Code:
    String trend = priceUpOrDownORFlat(Open);
    String trend = priceUpOrDownORFlat(High);
    String trend = priceUpOrDownORFlat(Low);
    String trend = priceUpOrDownORFlat(Close);
    Are you sure you really need your inp DataSeries?
    Last edited by bltdavid; 02-13-2016, 12:14 PM.

    Comment


      #3
      Hello nordseven,

      Thank you for writing in and welcome to the NinjaTrader Support Forum!

      In reference to the error you are getting, you need to check that you have at least three previous bars before calling your priceUpOrDownORFlat() method.

      On the first iteration of the for loop, it's checking for High[3] and Low[3]. What if your script is evaluating on the first bar? Three bars ago wouldn't exist, and it would throw that error.

      You'll need to check if you have at least four bars before calling your method.

      For example:
      Code:
      // current bar starts at 0 for the first bar, 1 for the second, etc.
      if (CurrentBar < 3)
           return;
      For more information about CurrentBar, please take a look at this help guide link: https://ninjatrader.com/support/help...currentbar.htm

      For more information about checking to see if you have enough bars in the data series you are trying to access, please take a look at this forum link: http://ninjatrader.com/support/forum...ead.php?t=3170

      Please, let us know if we may be of further assistance.
      Zachary G.NinjaTrader Customer Service

      Comment


        #4
        Thank you NinjaTrader_ZacharyG, exactly what i need!

        Comment


          #5
          bltdavid thank you for the post, but what you write me here? This is not answer on my question. I think this is judgement about my knowledges. You completely wrong in everything. This code is part of my experiments with the error. But this works too with NinjaTrader_ZacharyG answer.
          Before answer carefully read the question, best regards.

          Comment


            #6
            You're welcome.

            You cannot escape the fact your code clearly showed the errors I pointed out.

            And those errors appeared (to me) relevant to the question you were asking.

            Perhaps you posted incomplete code with regards to your inp DataSeries, or you simply posted erroneous code. How am I to know the difference?

            Like I said, the code you posted clearly has errors, so I started with that.

            Good to hear that the issue with CurrentBar pointed out by Zachary solved your problem.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Perr0Grande, Today, 08:16 PM
            0 responses
            2 views
            0 likes
            Last Post Perr0Grande  
            Started by elderan, Today, 08:03 PM
            0 responses
            3 views
            0 likes
            Last Post elderan
            by elderan
             
            Started by algospoke, Today, 06:40 PM
            0 responses
            10 views
            0 likes
            Last Post algospoke  
            Started by maybeimnotrader, Today, 05:46 PM
            0 responses
            9 views
            0 likes
            Last Post maybeimnotrader  
            Started by quantismo, Today, 05:13 PM
            0 responses
            7 views
            0 likes
            Last Post quantismo  
            Working...
            X