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

Object reference not set to an instance of an object

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

    Object reference not set to an instance of an object

    Hello again with the same question and still don't have the answer.

    I have simplified the code the maximum so maybe someone can troubleshoot it.

    I am still getting the error Object reference not set to an instance of an object. I understand this is has to do with the init of the order and more precisely the Name of the order.

    any comment will help.



    Code:
     
    
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
     public class testordermanagement : Strategy
     {
      private Order entryOrder = null; // This variable holds an object representing our entry order
    
      protected override void OnStateChange()
      {
       if (State == State.SetDefaults)
       {
        Description         = @"Enter the description for your new custom Strategy here.";
        Name          = "testordermanagement";
        Calculate         = Calculate.OnBarClose;
        EntriesPerDirection       = 1;
        EntryHandling        = EntryHandling.AllEntries;
        IsExitOnSessionCloseStrategy    = true;
        ExitOnSessionCloseSeconds     = 30;
        IsFillLimitOnTouch       = false;
        MaximumBarsLookBack       = MaximumBarsLookBack.TwoHundredFiftySix;
        OrderFillResolution       = OrderFillResolution.Standard;
        Slippage         = 0;
        StartBehavior        = StartBehavior.WaitUntilFlat;
        TimeInForce         = TimeInForce.Gtc;
        TraceOrders         = false;
        RealtimeErrorHandling      = RealtimeErrorHandling.StopCancelClose;
        StopTargetHandling       = StopTargetHandling.PerEntryExecution;
        BarsRequiredToTrade       = 20;
        // Disable this property for performance gains in Strategy Analyzer optimizations
        // See the Help Guide for additional information
        IsInstantiatedOnEachOptimizationIteration = true;
       }
       else if (State == State.Configure)
       {
        if (entryOrder != null)
                        entryOrder = GetRealtimeOrder(entryOrder);
    
       }
      }
    
      protected override void OnBarUpdate()
      {
    
       if (CurrentBar < BarsRequiredToTrade)
        return;
    
    
    
       if ( Close[0] > Open[0])
       {
        EnterLongLimit(0, true, 1, Close[0] , "LongEntry");
       }
    
    
       if (entryOrder.Name == "LongEntry" && Close[0] < Open[0])
        {
         CancelOrder(entryOrder);
        }
    
      }
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
            {
    
                if (order.Name == "LongEntry")
                {
                    entryOrder = order;
                    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                    {
                        entryOrder = null;
                    }
                }
            }
    
     protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
            {
    
                if (entryOrder != null && entryOrder == execution.Order)
                {
                        if (execution.Order.OrderState != OrderState.PartFilled)
                        {
                            entryOrder = null;
                        }
    
                }
    
               
            }
     }
    }

    #2
    Code:
     
     if (entryOrder.Name == "LongEntry" && Close[0] < Open[0])
    should be

    Code:
     
     if (entryOrder != null && entryOrder.Name == "LongEntry" && Close[0] < Open[0])
    Maybe there are other issues, but this one was easy to identify

    When looking for an error, one helpful method is to surround your code with a try/catch block, if the error gets caught have the block cover fewer lines and try again.

    E.g.:
    Code:
        protected override void OnBarUpdate()
        {
            try
            {
                if (CurrentBar < BarsRequiredToTrade)
                   return;
    
    
    
                if ( Close[0] > Open[0])
                {
                    EnterLongLimit(0, true, 1, Close[0] , "LongEntry");
                }
    
    
                if (entryOrder.Name == "LongEntry" && Close[0] < Open[0])
                {
                    CancelOrder(entryOrder);
                }
            }
            catch(Exception ex)
            {
                Print(ex.StackTrace);
            }
        }
    Unfortunately no line numbers are shown in the stack trace.

    Comment


      #3
      Hello madams212121,

      Thanks for your post.

      From observance, I can say that GetRealtimeOrder should be used in State.Realtime and not in State.Configure, but I do not think this is the source of your troubles.

      You will need to take debugging steps to see which line of code is throwing the error.

      The log tab of the Control Center will tell you which method the error came from.

      You can then add prints within that method to identify which line of code is throwing the error. I recommend using the Playback Connection to reproduce behavior that happened with realtime processing so you can repeat the occurrence and take debugging steps.

      MojoJojo's point that checking entryOrder's name without first checking if entryOrder is null could hit this error. Adding try catches as he mentions would be another way to identify which part of your code is throwing the error.

      Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

      Using try/catches - https://ninjatrader.com/support/help...tch_blocks.htm

      The SampleOnOrderUpdate strategy can be used as a reference for using GetRealtimeOrder, Order objects, and the Advanced Order Handling methods.



      Playback Connection - https://ninjatrader.com/support/help.../?playback.htm

      Please let us know if we can be of further assistance.
      JimNinjaTrader Customer Service

      Comment


        #4
        ok thank you. Yes checking for the Not null help with the code

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by WHICKED, Today, 12:45 PM
        2 responses
        18 views
        0 likes
        Last Post WHICKED
        by WHICKED
         
        Started by GussJ, 03-04-2020, 03:11 PM
        15 responses
        3,276 views
        0 likes
        Last Post xiinteractive  
        Started by Tim-c, Today, 02:10 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by Taddypole, Today, 02:47 PM
        0 responses
        5 views
        0 likes
        Last Post Taddypole  
        Started by chbruno, 04-24-2024, 04:10 PM
        4 responses
        51 views
        0 likes
        Last Post chbruno
        by chbruno
         
        Working...
        X