• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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

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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by cstern71, 09-02-2019, 10:30 AM
        4 responses
        54 views
        0 likes
        Last Post Lancer
        by Lancer
         
        Started by Hanstanz, Today, 04:44 AM
        0 responses
        9 views
        0 likes
        Last Post Hanstanz  
        Started by juniortut, Today, 04:38 AM
        0 responses
        5 views
        0 likes
        Last Post juniortut  
        Started by rojiinko, Today, 03:58 AM
        0 responses
        5 views
        0 likes
        Last Post rojiinko  
        Started by mreanuzlazx, Today, 01:30 AM
        0 responses
        4 views
        0 likes
        Last Post mreanuzlazx  
        Working...
        X