Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Issue with getting close price

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

    Issue with getting close price


    I am trying to use the StartAnchor.Time to print out the price of the bar that the StartAnchor is drawn on a bar it does return the bar number and when querying the startAnchor.time it returns the correct time

    But when doing Close[Bars.GetBar(StartAnchor.Time)] it fails and says that OnBarUpdate method on bar 0 even though it returns a value when just querying the bar number.

    Code:
    foreach(DrawingTool draw in DrawObjects.ToList())
                {
                    if(draw is DrawingTools.RegionHighlightX)
                    {
                        if(draw.Tag == r.Tag)
                        {
                            DrawingTools.RegionHighlightX rec = draw as DrawingTools.RegionHighlightX;
                            Log(Bars.GetBar(rec.StartAnchor.Time).ToString(), LogLevel.Information);
                            Log(Close[Bars.GetBar(rec.StartAnchor.Time)].ToString(), LogLevel.Information); <--- issue here
                            return;
                        }
                    }
                }​
                }​


    Last edited by WHICKED; 07-24-2024, 10:06 PM.

    #2
    Hello WHICKED,

    You would need to add error checking to that syntax, you can only use a bars ago once that many bars have elapsed.

    Code:
    int barsAgo = Bars.GetBar(rec.StartAnchor.Time);
    
    if(CurrentBar >= BarsAgo)
    {
    Log(Close[barsAgo].ToString(), LogLevel.Information); <--- issue here
    
    }

    Comment


      #3
      Basically this is drawing a Region Highlight X during historical data.

      int barsAgo = Bars.GetBar(rec.StartAnchor.Time); will return 0.

      But using Close[barsAgo] <---- barsAgo = 0

      It will go to the very first bar on the bar (all the way to the left) and get the Close value of that bar - not the bar I am wanting to look at.

      Comment


        #4
        Hello WHICKED,

        The collection of drawing objects that you are using will only be available in realtime so you could disable that logic until realtime to avoid the error. The error specifically means an invalid bars ago was used at the time that logic was called, the way to avoid that is to make sure the bars ago is valid based on the CurrentBar variable.

        If you are trying to work with the objects that you script has drawn and not other objects on the chart it would be better to avoid using that specific collection and instead make your own List<RegionHighlightX>. This would let you work with the historical objects as your script processes through historical data. When you draw an object it produces an instance which you can use to get values from it just like you are now.

        RegionHighlightX myVariable =Draw.RegionHighlightX(this, "tag1", 10, 0, Brushes.Blue);


        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        596 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        343 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        103 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        556 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        554 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X