Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnBarUpdate Error

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

    OnBarUpdate Error

    Hi

    Error on calling 'OnBarUpdate' method on bar 27431: Object reference not set to an instance of an object.

    I sometimes get this error and my strategy becomes disable. Is there a way to trap this error in on OnBarUpdate using an error variable rather than inserting Print statement on every line of the code. I have multiple indicator on in the OnBarUpdate method.

    I want to include code in OnBarUpdate similar to what I have in OnOrderUpdate as follows:

    Code:
    if (order.Name.Contains("StopLoss") == true)
    {
    stopOrder = order;
    if (error == ErrorCode.OrderRejected || error == ErrorCode.UnableToChangeOrder)
    {
    Print("Unable to Change Order. Submitting New Order.");
    Trail_Stop = Position.GetMarketPrice() + 5*TickSize;
    ChangeOrder(stopOrder, Position.Quantity, 0, Trail_Stop);
    
    }
    
    }
    Thanks,
    Last edited by bjunaid; 11-04-2020, 07:30 AM.

    #2
    Hello bjunaid,

    Thanks for your post.

    You will still need to use prints throughout your strategy's OnBarUpdate method. If prints are before/after each line of code, you can easily identify where the error was received by looking for the last print from your strategy.

    Once the line of code is identified, you will need to see which object on that line is null. You can then protect that code with a null check. If the object should not be null, you will need to look into how that object gets created.

    If you do not want to use prints, then you could consider attaching the Visual Studio Debugger (if you have Visual Studio) and then reproduce the error. The Debugger will show you the line of code that threw the error.

    Attaching Visual Studio Debugger - https://ninjatrader.com/support/help..._debugging.htm

    Publicly available resource on using Visual Studio Debugger - https://docs.microsoft.com/en-us/vis.../?view=vs-2019

    If you are working with Order objects, I recommend viewing our SampleOnOrderUpdate strategy for further direction on working with Order objects. Order objects should be assigned in OnOrderUpdate as a best practice, and assigned back to null in OnOrderUpdate when the order is terminal (Filled, Cancelled, Rejected.) If the Order object is used in OnExecutionUpdate, it can be assigned back to null at the end of OnExecutuionUpdate when that order fills. (Demonstrated in SampleOnOrderUpdate)

    SampleOnOrderUpdate also demonstrates adding breakeven behaviors in OnBarUpdate. That section of code could be easily modified to make an auto trail.

    SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

    We look forward to assisting.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    38 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    124 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    64 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    41 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X