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 funk10101, Today, 11:35 AM
    0 responses
    0 views
    0 likes
    Last Post funk10101  
    Started by samish18, Today, 11:26 AM
    0 responses
    1 view
    0 likes
    Last Post samish18  
    Started by Trader146, 03-29-2024, 01:22 PM
    2 responses
    14 views
    0 likes
    Last Post Trader146  
    Started by tsantospinto, 04-12-2024, 07:04 PM
    7 responses
    127 views
    0 likes
    Last Post aligator  
    Started by futtrader, 04-21-2024, 01:50 AM
    5 responses
    56 views
    0 likes
    Last Post NinjaTrader_Eduardo  
    Working...
    X