Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Object Reference Error

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

    Object Reference Error

    I seem to be having a problem with this part of my code:

    s_end = ToTime(Time[0]) + 4100;
    if (s_end == ToTime(sessionEnd))
    {
    if (myOrder.OrderState == OrderState.Accepted)
    {
    CancelOrder(myOrder);
    myOrder = null;
    }
    }

    The problem I was trying to solve is when I start the strategy, the strategy will enter a limit order based on the closing of the previous bar, which happens to be the previous session. If I don't cancel that order, then the strategy will enter a limit order at the time I start the strategy, and if that is pre-market and the market is trading at or below this price, then I get an unintended fill. But if I use this code, then sometimes I will get the following error:

    Error on calling 'OnBarUpdate' method for strategy 'Pulse300LDev/bda624da93134bb8855e699a548e0674': Object reference not set to an instance of an object.

    I'm thinking this is happening when my strategy has determined that there shouldn't be any limit orders, but I'm not totally sure. Help?

    Rich
    tradethepulse.com

    #2
    Correct Rich, the error likely comes from not checking if the myOrder IOrder is null before attempting to CancelOrder() it -

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    68 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    36 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    61 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    62 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    53 views
    0 likes
    Last Post CarlTrading  
    Working...
    X