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 Mindset, 04-21-2026, 06:46 AM
    0 responses
    58 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    80 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    43 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    101 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    61 views
    0 likes
    Last Post PaulMohn  
    Working...
    X