Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problems with data series drawing lines

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

    Problems with data series drawing lines

    Hi, I've created this basic indicator, this works perfectly in a 5-minute chart, it creates the line at each new bar and removes the previous one, so at any time you will just see the line on the last bar, my issue is that when I add this to a chart with 2 data series, for example, 5-minute and 1-minute I keep getting this error: Chart rendering failed. There is likely a problem with a chart object's OnRender method. D2D error = 'An item with the same key has already been added.'

    Code:
    public class TestInd : Indicator
    {
    private int previousBar = -1;
    private double upperLineValue;
    private double lowerLineValue;
    private string upperTag;
    private int StopDistance = 50;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Draws a line 50 ticks above and below the open for each new candle.";
    Name = "TestInd";
    Calculate = Calculate.OnEachTick;
    IsOverlay = true;
    }
    else if (State == State.DataLoaded)
    {
    
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 1)
    return;
    
    if (CurrentBar != previousBar)
    {
    // Remove old lines
    if (upperTag != null)
    {
    RemoveDrawObject(upperTag);
    }
    
    // Calculate new line values
    upperLineValue = Open[0] + (StopDistance * TickSize);
    
    
    // Draw new lines
    upperTag = "UpperLine" + CurrentBar;
    Draw.Line(this, upperTag, false, 0, upperLineValue, -3, upperLineValue, Brushes.Red, DashStyleHelper.Solid, 2);
    
    // Update previous bar
    previousBar = CurrentBar;
    }
    }
    }​

    #2
    Hello xtremel,

    To do what you are asking you don't actually need to use RemoveDrawObject, you can just call Draw.Line again with the same tag and that will update the object. It is much more resource usage to call RemoveDrawObject on every bar so that should be avoided. If you change to using the tag "UpperLine" without amending the currentbar that will let you update the existing object.

    I would suggest trying that change first and see if you still get a problem, if so post the modified code along with the specific steps you used to see the problem and I can try that on my end.

    Comment

    Latest Posts

    Collapse

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