Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on calling 'OnBarUpdate' method on bar 16, then 22, then 76, then...???

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

    Error on calling 'OnBarUpdate' method on bar 16, then 22, then 76, then...???

    Greetings everyone. I am learning to develop in NinjaScript, using my background in other platforms.

    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:

    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();
    }
    }
    The line "rectangle = Draw.Rectangle(Owner, "FVG-" + barIndex, barIndex + 2, top, barIndex - 100, bottom, Brushes.Purple);" is the one that causes the error in some weird bar numbers.

    Each FVG object is managed by a specific class:

    Code:
        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();
                }
            }
        }
    By the way, each FVG object is created by passing my indicator as the owner, where I also filter the CurrentBar value:

    Code:
    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]);
    }
    Any help would be greatly appreciated. Thank you in advance.

    #2
    Originally posted by italogomes View Post
    Greetings everyone. I am learning to develop in NinjaScript, using my background in other platforms.

    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:

    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();
    }
    }
    The line "rectangle = Draw.Rectangle(Owner, "FVG-" + barIndex, barIndex + 2, top, barIndex - 100, bottom, Brushes.Purple);" is the one that causes the error in some weird bar numbers.

    Each FVG object is managed by a specific class:

    Code:
    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();
    }
    }
    }
    By the way, each FVG object is created by passing my indicator as the owner, where I also filter the CurrentBar value:

    Code:
    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]);
    }
    Any help would be greatly appreciated. Thank you in advance.
    Never mind. I just figured out the silly mistake.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    571 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    330 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    548 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    549 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X