Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem using Draw.Text from OnOrderUpdate in strategy...

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

    Problem using Draw.Text from OnOrderUpdate in strategy...

    I am using ChartTrader and trying to print profit/loss potential with the target and stop loss order lines. The most logical and useful time to display this information is after a stop loss or target order has been updated or changed.

    I am using Draw.Text from the OnOrderUpdate procedure in a strategy, but when I do I get errors in the log like:
    Error in 'OnOpenOrder': 'Object reference not set to an instance of an object.'
    Error in 'OnOrderStatus': 'Object reference not set to an instance of an object.'
    I believe the problem is that when using 'this' from OnOrderUpdate, it is a null reference. To try and fix this, I have tried to capture the 'this' reference from the OnBarUpdate procedure:


    Code:
    // class variables:
    NinjaTrader.NinjaScript.NinjaScriptBase ThisChartWindow;
    Code:
    protected override void OnBarUpdate()
            {
                if (ThisChartWindow == null)
                    if (State == State.Realtime)            
                        ThisChartWindow = this;                
            }
    But I still get the errors. Here is the code. It works fine from OnBarUpdate, but it would be much more helpful if it could work when the order is updated.

    Code:
    if (ThisChartWindow != null)
                               {
                                    Draw.Text(ThisChartWindow, cp + "-Target", ProfitPotential.ToString("#,##0.00"), 5, TargetPrice, Brushes.White);
                                      Draw.Text(ThisChartWindow, cp + "-StopLoss", LossPotential.ToString("#,##0.00"), 5, StopLossPrice, Brushes.White);
                               }
    Is there a way to get Draw.Text to work from OnOrderUpdate?

    Thanks.

    #2
    Hello RogBear,

    Thank you for your post.

    In this case, something you are using is null while it is being used however it should not be the object "this" as that is the current class, it is likely one of the other objects you are using.

    I attached a simple test SampleMACrossOver that uses OnOrderUpdate with Draw.Text to draw the orders prices. I could suggest using a small sample like the following to identify further where the break in your logic is. You may also try using a print like the following with the objects used in OnOrderUpdate to identify what is null:

    Code:
    Print(( myObject == null));
    I look forward to being of further assistance.


    Attached Files

    Comment


      #3
      Thank you for assisting me!

      You are correct about the 'this' keyword, it is not null. I have updated my code accordingly to use 'this' directly:

      Code:
         Print("this:" + (this == null) + " " + this);
                                     Print("cp:"+ (cp == null) + " " + cp);
                                     Print("ProfitPotential:" + (ProfitPotential == null) + " " + ProfitPotential.ToString("#,##0.00"));
                                     Print("TargetPrice:" + (TargetPrice == null) + " " + TargetPrice);
                                     Print("Brushes.White:" + (Brushes.White == null) + " " + Brushes.White);                                                                                            
      
                                       Draw.Text(this, cp + "-Target", ProfitPotential.ToString("#,##0.00"), 5, TargetPrice, Brushes.White);
                                       Draw.Text(this, cp + "-StopLoss", LossPotential.ToString("#,##0.00"), 5, StopLossPrice, Brushes.White);
      However, none of my other variables that are passed to Draw.Text are null either, as you can see from the output:

      this:False aaChartTraderTradeTargetStopValues(ES 03-19 (5 Minute))
      cp:False ES
      ProfitPotential:False 997.95
      TargetPrice:False 2729.5
      Brushes.White:False #FFFFFFFF

      When debugging with Visual Studio, this is the error that is produced:

      Click image for larger version  Name:	2019-03-07_NinjaNull.png Views:	1 Size:	64.7 KB ID:	1050664

      When hovering over the four variables on this line, (owner, barsAgo, time, y) none of them are null and they do appear as expected.

      Also, when I comment out the two Draw.Text lines, there is no error produced, so it doesn't seem like the error is coming from anyplace else.

      I hope some of this helps, thanks for your help!
      Last edited by RogBear; 03-07-2019, 06:01 PM.

      Comment


        #4
        Hello RogBear,

        Thank you for your reply.

        I wanted to check, were you able to test the sample I had created and had seen it work?

        This would be helpful to set a baseline of where the problem is coming from. If your script is more advanced or there are more steps involved than my sample, it is possible it is one of these items which we could explore further.

        Do you have a simplified sample (a .cs file) similar to what I provided that demonstrates the problem? If so, I can take a look at that and see if I also have the error so we can explore that further.


        I look forward to being of further assistance.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        672 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        379 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        111 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        575 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        582 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X