I am having some trouble with the OnBarUpdate method running multiple times on the same bar, even though Calculate is set to OnBarClose.
protected override void OnBarUpdate()
{
if (CurrentBar < 1)
return;
//Add your custom indicator logic here.
if(TagFlag == true)
{
if(High[0] > Diamante)
{
PriceVar = High[0];
Draw.Line(this, "HF400" + TagVar, false, 2, PriceVar, -10, PriceVar, Brushes.DarkRed, DashStyleHelper.Solid, 1);
TagFlag = false;
return;
}
return;
}
else
{
BarLow = Low[1];
Diamante = BarLow - 3;
if(Low[0] < Diamante)
{
// Draws a green line from 1 bar back through the following bar.
Draw.Line(this, "725400" + TagVar, false, 2, Diamante, -1, Diamante, Brushes.Green, DashStyleHelper.Solid, 2);
// Draws a thin green line at the level of the previous bar's low.
Draw.Line(this, "391400" + TagVar, false, 2, BarLow, -10, BarLow, Brushes.Green, DashStyleHelper.Solid, 1);
// Paints a yellow diamond on the current bar at the level of the condition being met.
Draw.Diamond(this, "tag" + TagVar, true, 0, Diamante, Brushes.Yellow);
TagVar++;
TagFlag = true;
return;
}
return;
}
}
The behavior that I am seeing, however, is that it sets the variable to true, but instead of exiting the method, it runs again on the same bar and executes the true commands, which includes setting the variable back to false. So, what is meant to be a global variable to control whether only one or the other set of instructions is executed becomes useless, and both sets of instructions are executed each time the first one is triggered.
I have, at one point, gotten it to draw the red line on a subsequent bar which triggered the true condition, but it still drew the red line on the first bar. And in any case, whatever I changed since then has now made it so even that doesn't work.
I thought OnBarClose meant that OnBarUpdate would only run once per bar. What am I doing wrong?
Thank you in advance!

Comment