Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error with Draw.Line or .Ray but .HorizontalLine works fine

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

    Error with Draw.Line or .Ray but .HorizontalLine works fine

    Hey team,
    I'm getting an error when using Draw.Line or Draw.Ray. But, the indicator works great when using Draw.HorizontalLine. I have been using the indicator for a long time with no issue, because it was using Draw.HorizontalLine. When I decided to change it to use Draw.Ray it would always throw a System.NullReferenceException: error. It seems the issue is with any Draw command that uses int ...BarsAgo (X coor. inputs).

    One thing to note, it is being used inside the OnPositionUpdate(object sender, PositionEventArgs e) event.

    Code:
    private void OnPositionUpdate(object sender, PositionEventArgs e)
    {
    if(e.Position.Instrument.FullName != this.Instrument.FullName) return;
    
    int newPositionCount = GetPositionCount();
    
    if(newPositionCount == 0) // THIS IS BROKEN. WAIT FOR NT TO FIX if (e.MarketPosition == MarketPosition.Flat)
    {
    CheckAccountRealizedProfitLoss(accountCurrent);
    if(ShowBreakevenLine)
    {
    breakEven = 0;
    RemoveDrawObject(BreakevenLineName);
    }
    }
    else if(ShowBreakevenLine)
    {
    SetBreakEven(newPositionCount);
    double per = (numberBarsOnChart * PercentageLengthChart/100);
    int startBarsAgo = (int)Math.Round( per, 0);
    Print(String.Format("numberBarsOnChart = {0} \t % of bars = {1} \tstartBarsAgo = {2}", numberBarsOnChart, per, startBarsAgo));
    
    try
    { [B]Draw.Ray(this, BreakevenLineName, true, startBarsAgo, breakEven, 0, breakEven, Brushes.Gold); }
    catch(Exception e)[/B]
    { Print("FTB: Draw.Ray - "+e.ToString()); }
    }
    }
    FYI, this code works great...
    Draw.HorizontalLine(this, BreakevenLineName, breakEven, Plots[0].Brush, Plots[0].DashStyleHelper, (int)Plots[0].Width);


    What prints out is this...

    BREAKEVEN = 30503
    numberBarsOnChart = 67 % of bars = 33 startBarsAgo = 33
    FTB: Draw.Ray - System.NullReferenceException: Object reference not set to an instance of an object.
    at NinjaTrader.Data.BarsSeries.GetTime(Int32 index)
    at NinjaTrader.Data.BarsSeries.GetSessionEndTime(Int3 2 index)
    at NinjaTrader.Data.Bars.GetSessionEndTime(Int32 index)
    at NinjaTrader.NinjaScript.TimeSeries.get_Item(Int32 barsAgo)
    at NinjaTrader.NinjaScript.DrawingTools.DrawingTool.C reateChartAnchor(NinjaScriptBase ownerNinjaScript, Int32 barsAgo, DateTime time, Double y)
    at NinjaTrader.NinjaScript.DrawingTools.Draw.DrawLine TypeCore[T](NinjaScriptBase owner, Boolean isAutoScale, String tag, Int32 startBarsAgo, DateTime startTime, Double startY, Int32 endBarsAgo, DateTime endTime, Double endY, Brush brush, DashStyleHelper dashStyle, Int32 width, Boolean isGlobal, String templateName)
    at NinjaTrader.NinjaScript.DrawingTools.Draw.<>c__Dis playClass5c.<Ray>b__5b()
    at NinjaTrader.NinjaScript.DrawingTools.DrawingTool.D rawToggledPricePanel[T](NinjaScriptBase owner, Boolean isDrawOnPricePanel, Func`1 drawAction)
    at NinjaTrader.NinjaScript.Indicators.FastTraderButto ns.DrawBreakEven()


    Do Draw commands have to be invoked on the data thread?

    Thanks.


    #2
    Hello zacharydw00,

    From non-data-driven methods, like OnPositionUpdate(), TriggerCustomEvent should be used to update the series before doing anything related to data, including drawing on the chart over specific bars.


    What value is being used for startBarsAgo?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      From non-data-driven methods, like OnPositionUpdate(), TriggerCustomEvent should be used to update the series before doing anything related to data...
      #1 What series are you referring to specifically, because the code is not updating a Series?

      #2 The documentation doesn't mention needing to use TriggerCustomEvent for Draw.Raw, or for any Draw command that I could find... https://ninjatrader.com/support/help...8/draw_ray.htm
      Can you elaborate on this please?

      Originally posted by NinjaTrader_ChelseaB View Post
      What value is being used for startBarsAgo?
      It's in the Print statement I provided above.

      Comment


        #4
        Hello zacharydw00,

        I am referring to the primary data series of the chart on which drawing objects are drawn. Drawing objects have to drawn in relation to a bar. Without the bar series being updated, the drawing objects cannot be placed on the most recent bars.

        The help guide states:
        "Provides a way to use your own custom events (such as a Timer object) so that internal NinjaScript indexes and pointers are correctly set prior to processing user code triggered by your custom event."
        A barsAgo value for a drawing object is an internal NinjaScript index and pointer.

        So for any internal indexes, like series, used in non-data-driven methods must use TriggerCustemEvent.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks for the extra info Chelsea. It's working now.
          I would like to suggest that the various Draw. documentation pages, that require an X coordinate, include this information in the notes to help others.

          Separate question, has the MarketPosition.Flat state been fixed?
          private void OnPositionUpdate(object sender, PositionEventArgs e)
          {
          if (e.MarketPosition == MarketPosition.Flat)
          }
          This was confirmed as broken by NT support in one of my previous posts.



          For others, this is the basics of my solution...
          Code:
          private void OnPositionUpdate(object sender, PositionEventArgs e)
          {[INDENT]if(e.Position.Instrument.FullName != this.Instrument.FullName) return;
          
          if(ShowBreakevenLine)[/INDENT][INDENT=2]TriggerCustomEvent(DrawBreakEvenLine, BarsArray[0]);[/INDENT]
           }
          
          
          private void DrawBreakEvenLine(object _bars)
          {[INDENT]// The try-catch is probably overkill, but I don't want to take any chance of this crashing during a trade.
          try
          {[/INDENT][INDENT=2]Draw.Ray(this, "FT Breakeven Line", true, startBarsAgo, breakEven, 0, breakEven, Plots[0].Brush, Plots[0].DashStyleHelper, (int)Plots[0].Width, true);[/INDENT][INDENT]}[/INDENT][INDENT]catch(Exception e)
          { Print("FTB: Draw.Ray error - "+e.ToString()); }[/INDENT]
           }
          Last edited by zacharydw00; 01-04-2021, 04:42 PM.

          Comment


            #6
            Hello zacharydw00,

            This would have to be added to every page that involves series, which is a lot more than drawing objects.

            Instead, there is a dedicated page for TriggerCustomEvent() which states anything involving indexes and pointers has to use this. This means anything with series as well as these are involving indexes.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Very true. What about my other question?

              Comment


                #8
                Hello zacharydw00,

                Apologies for overlooking this, however, I would recommend following up in that other thread where there is already an open inquiry.

                We respectfully request that you do not open separate threads or send multiple emails on the same subject, to prevent multiple technicians from working on the same issue and limiting our ability to respond to all customers in a timely manner.

                As a heads, up I've tested and have not found any issues.

                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                576 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                334 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                553 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                551 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X