PDA

View Full Version : indicator: onbarupdate


Danedo
03-13-2009, 08:43 AM
Hi.

Made an indicator.

Does not work. Apparently nothing is being calculated.

Put in Print("HELLO?!?!?!?") statements and other obnoxious print messages inside OnBarUpdate().

Only the Print statements above my logic get triggered but only 2-3 times, according to output window.

Questions:
1) Why are my print statements only being triggered 2-3 times?
2) Why are none of my other print statements being triggered?

Code example:

protected override void OnBarUpdate()
{

Print("I will slay a goat if this fails");

if(Close[0]<High[1])
{
A = Close[0];
}
else
{
A = High[1];
}

Print("Is the goat alive or will I need to feed it to a shark and punish a thousand generations?");

C = (add and subtract stuffs);

Plot.Set(C);
Print("The land seal needs to be slayed, and its innards eaten to appease my computer's internal clock's battery");

}


Any idea why my output shows "I will slay a goat if this fails" a few times, then nothing, and why my indicator is blank?

Regards,

Dane

NinjaTrader_Josh
03-13-2009, 08:49 AM
Dane,

You are likely running into the error discussed in this tip: http://www.ninjatrader-support2.com/vb/showthread.php?t=3170

Danedo
03-13-2009, 09:40 AM
Hi.

I have been running a chart with the indicator for a few hours now, and I still only see two sets of print statements.

I only need the current bar and the previous bar.

If I remove the indicator, clear the log, and restart it, the same thing occurs. The curious thing is I have the following:

Print("Close[0] : "+Close[0]);
Print("Close[1] : "+Close[1]);
Print("Volume[0] : "+Volume[0]);

The only print statement that triggers is the first one, and always with an apparently outdated close price.

I changed the second line,

Print("Close[1] : "+Close[1]);

to

Print("Close[-1] : "+Close[-1]);

thinking I might have messed up trying to get the previous bar's close.

The output now shows all three print statements, though they are all the same:

Close[0] : 5.3
Close[-1] : 5.3
Volume[0] : 1000

I feel as if I am missing something very basic here, besides that Close[-1] is probably erroneous.

NinjaTrader_Josh
03-13-2009, 09:47 AM
Danedo,

You cannot use a negative index value. As mentioned in the tip, you cannot just call [1] until you have at least 1 bar on the chart. Please amend your code with one of the techniques shown in the tip.

Danedo
03-13-2009, 10:34 AM
Hi.

Maybe I need to ask a simpler question to fill in some knowledge:

Using a minute chart, how do I compare the previous bar's close to the current high?

I thought it was as simple as

protected override void OnBarUpdate()
{
if(High[0] > Close[1])
{
Print ("The high is greater than the previous close");
}


You keep telling me I don't have enough bars. I don't quite understand. I want to compare the current bar to a bar a minute ago. Don't I need only 1 minute of data, as well as the current bar? I have been running this thing for hours.

I do notice that everything runs if I don't try to use a previous bar.

I could simply just store the current open, high, close, low in a variable and compare those, but I was hoping there was a more direct way of... referencing a previous bar's open high close low.

Thank you for your great patience.

TAJTrades
03-13-2009, 10:39 AM
protected override void OnBarUpdate()
{
if(High[0] > Close[1])
{
Print ("The high is greater than the previous close");
}





On the very first bar of the chart, all the way to the extreme left of your screen, Close[1] DOES NOT EXIST causing an error.
Therefore you need something like:

if(CurrentBar < 2) return;

Its in the docs

NinjaTrader_Josh
03-13-2009, 10:40 AM
Danedo,

You cannot do that. Please see the tip. You need to first ensure you have enough bars on the chart. Remember your indicators start processing on the very first bar of the chart. From the very first bar there are no previous bars. This is why an index of [1] throws an error. See the tip for how to address this issue.

mrlogik
03-13-2009, 10:41 AM
Danedo,

The indicator starts to calculate on the very first bar of the chart.

If you are on the very first bar calculation, and you try to reference the previous bar that is not on the chart, you will get an error. Check the LOG tab for errors in yellow.

Even though you have many hours of 1 minute data on the chart, the indicator starts on the very first bar, looking back to a bar that doesnt exist. If this occurs, the indicator turns off and will not continue further.

put this in the OnBarUpdate() as the first statement

if(CurrentBar < 20)
return;

your code for high > previous close is correct

Danedo
03-13-2009, 10:44 AM
-.-;;;;;;;

:mad:OK, I FINALLY see.

Thought something was wrong with the logic.

Embarrassed.

Have a great weekend.

mrlogik
03-13-2009, 04:33 PM
Danedo,

Remember,


if(CurrentBar < n)
return;


Where n is the furthest back you plan to look.
For example, if you're using moving averages, whatever the largest period of the moving averages is, you use that period.

hope this helps.
Always check the Log tab when things don't seem as they should.

roonius
03-14-2009, 01:56 AM
Danedo,

Remember,


if(CurrentBar < n)
return;
Where n is the furthest back you plan to look.
For example, if you're using moving averages, whatever the largest period of the moving averages is, you use that period.

hope this helps.
Always check the Log tab when things don't seem as they should.

mrlogik,

if you use NT built in moving averages, you don't need to check CurrentBar condition since it's already done in MA code