PDA

View Full Version : Reference to paste bar


MAX
10-29-2006, 12:42 AM
I'm new in programming in NinjaScript and I'm facing some problem to make reference to paste bars in chart.
I read on the Help Guide that if I want to reference to the close one bar ago I must write Close[1].

I made a simple Indicator where I print on the output window the last close or the close one bar ago.This is the last portion of my 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.


Print(Close[0].ToString());

}

With the code above I regularly see the last close on the output window.

If, I write the code below instead I don't see anything on the output window:

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.


Print(Close[1].ToString());

}

Am I missing something?

Thanks for any help.;)

NinjaTrader_Ray
10-29-2006, 03:49 AM
The problem you are facing is a simple one yet sometimes overlooked.

When you write Close[0], you"0" is the index reference to the current bar. Indicators process data from left to right on the chart (or oldest bars to newest bars) and therefore, when on the very first bar of a data series (the left most bar on the chart) there is no prior bar. So, Close[1], is invalid since there is no prior bar. If you look in your Control Center log window, you will likely see an error. To get around this, you need to add the following line of code:

if (CurrentBar < 1)
return;

This ensures there is at least one bar in the data series so that Close[1] is valid. If you wanted to access Close[5] then you needto have:

if (CurrentBar < 5)
return;

Ray

MAX
10-29-2006, 09:36 PM
Thanks very much now is working fine.:D