Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Taddypole, 04-26-2024, 02:47 PM
    1 response
    12 views
    0 likes
    Last Post NinjaTrader_Eduardo  
    Started by futtrader, 04-21-2024, 01:50 AM
    6 responses
    58 views
    0 likes
    Last Post futtrader  
    Started by sgordet, Today, 11:48 AM
    0 responses
    4 views
    0 likes
    Last Post sgordet
    by sgordet
     
    Started by Trader146, Today, 11:41 AM
    0 responses
    5 views
    0 likes
    Last Post Trader146  
    Started by jpapa, 04-23-2024, 07:22 AM
    2 responses
    21 views
    0 likes
    Last Post rene69851  
    Working...
    X