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 Barry Milan, Yesterday, 10:35 PM
    5 responses
    16 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by DanielSanMartin, Yesterday, 02:37 PM
    2 responses
    13 views
    0 likes
    Last Post DanielSanMartin  
    Started by DJ888, 04-16-2024, 06:09 PM
    4 responses
    12 views
    0 likes
    Last Post DJ888
    by DJ888
     
    Started by terofs, Today, 04:18 PM
    0 responses
    11 views
    0 likes
    Last Post terofs
    by terofs
     
    Started by nandhumca, Today, 03:41 PM
    0 responses
    8 views
    0 likes
    Last Post nandhumca  
    Working...
    X