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

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

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Stanfillirenfro, Yesterday, 09:19 AM
    7 responses
    51 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by TraderCro, 04-12-2024, 11:36 AM
    4 responses
    69 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Mindset, Yesterday, 02:04 AM
    1 response
    15 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by agclub, 04-21-2024, 08:57 PM
    4 responses
    18 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by Irukandji, Today, 04:58 AM
    0 responses
    5 views
    0 likes
    Last Post Irukandji  
    Working...
    X