The indicator I am using is somewhat complex, to I splitted the code in a few classes. This way I can simplify the task of plotting and controlling data. So far, so good.
My problem is the error in the title: "Error on calling 'OnBarUpdate' method on bar [nnn]", where [nnn] is first the value 16. Then, if I filter the CurrentBar to be above 16, the error moves to the bar number 22. And keeps changing! Also, the error only happens when I try to draw a rectangle (which is called in another class). Here is the relevant code:
public class Fvg : GraphicObject
{
private Rectangle rectangle;
private int barIndex;
private double top;
private double bottom;
private Direction direction;
public Fvg(NinjaScriptBase owner, int barIndex, double top, double bottom, Direction direction)
: base(owner)
{
this.barIndex = barIndex;
this.top = top;
this.bottom = bottom;
this.direction = direction;
}
~Fvg()
{
Clear();
}
private void Clear()
{
if (rectangle != null)
{
rectangle.Dispose();
rectangle = null;
}
}
public bool IsMitigated()
{
return (top <= bottom);
}
public void Paint()
{
Clear();
rectangle = Draw.Rectangle(Owner, "FVG-" + barIndex, barIndex + 2, top, barIndex - 100, bottom, Brushes.Purple);
}
public void Update(double currentHigh, double currentLow)
{
if (direction == Direction.Up)
top = Math.Min(top, currentLow);
else if (direction == Direction.Down)
bottom = Math.Max(bottom, currentHigh);
Paint();
}
}
Each FVG object is managed by a specific class:
public class FvgManager
{
private NinjaScriptBase owner;
private List<Fvg> items;
public FvgManager(NinjaScriptBase owner)
{
this.owner = owner;
items = new List<Fvg>();
}
public void AddIfValid(int barIndex, double currentHigh, double currentLow, double previousHigh, double previousLow)
{
var direction = (previousLow > currentHigh) ? Direction.Down : (previousHigh < currentLow) ? Direction.Up : Direction.None;
var fvg =
(direction == Direction.Up) ? new Fvg(owner, barIndex, currentLow, previousHigh, direction) :
(direction == Direction.Down) ? new Fvg(owner, barIndex, previousHigh, currentLow, direction) :
null;
if (fvg != null)
{
items.Add(fvg);
fvg.Paint();
}
}
}
protected override void OnBarUpdate()
{
var barIndex = CurrentBar;
var barDateTime = Bars.GetTime(barIndex);
var barDate = barDateTime.Date;
inDaysBack = (DateTime.Today.Subtract(barDate).Days <= MaxDaysBack);
if (barIndex > 1000)
{
Print("barIndex: " + barIndex);
Print("High[1]: " + High[1]);
Print("Low[1]: " + Low[1]);
Print("High[3]: " + High[3]);
Print("Low[3]: " + Low[3]);
fvgManager.AddIfValid(barIndex, High[1], Low[1], High[3], Low[3]);
}

Comment