Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Getting an error on 'OnExecution' method during backtesting

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

    Getting an error on 'OnExecution' method during backtesting

    Hello Guys,

    I'm backtesting a strategy and I'm using OnExecution to identify when a stop order is executed so I can calculate Profit/Loss for the day.

    When it gets to that method during the backtest I'm getting the following error:

    **NT** Error on calling 'OnExecution' method for strategy 'XStrategy12/cf4b8224c9a14ff4a18b365c4a752af3': Object reference not set to an instance of an object.

    Wonder if that has anything to do to the fact that I'm simulating/backtesting? Can you please confirm?


    Thanks,

    Rodrigo

    #2
    Hello rgaleote,

    Thank you for your post.

    You can run OnExecution() in backtesting.

    Can you provide the full code used in OnExecution()?

    Comment


      #3
      Hi Patrick,

      Thanks for the prompt repply, there you go:

      protected override void OnExecution(IExecution execution)
      {

      if (execution.Order.OrderType == OrderType.Stop)
      {
      cumProfit += LucroPreju(Close[0]);
      }

      }


      Thanks,

      Rodrigo

      Comment


        #4
        Originally posted by rgaleote View Post
        Hi Patrick,

        Thanks for the prompt repply, there you go:

        protected override void OnExecution(IExecution execution)
        {

        if (execution.Order.OrderType == OrderType.Stop)
        {
        cumProfit += LucroPreju(Close[0]);
        }

        }


        Thanks,

        Rodrigo
        Look into your LucroPreju() code. What object(s) is it accessing?

        Comment


          #5
          Hello,

          This could be in the execution object as well per example #2 in the help guide, I don't see any sort of null check on the IOrder here.

          You may try the following instead to see if this resolved the error:

          Code:
          if (execution.Order != null && execution.Order.OrderType == OrderType.Stop)
          If this is not the case, this would likely be as koganam had mentioned in the LucroPreju() method.

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_Jesse View Post
            Hello,

            This could be in the execution object as well per example #2 in the help guide, I don't see any sort of null check on the IOrder here.

            You may try the following instead to see if this resolved the error:

            Code:
            if (execution.Order != null && execution.Order.OrderType == OrderType.Stop)
            If this is not the case, this would likely be as koganam had mentioned in the LucroPreju() method.

            I look forward to being of further assistance.
            I get your point about the null-check as a matter of principle, but it seems redundant to me. An execution will always have an order, no? After all, the only things that do get executed are orders, no? What am I misunderstanding about that?

            Comment


              #7
              Hello,

              I also feel this is a redundant check but I have seen cases where this does resolve errors in OnExecution related to "Object reference not set to an instance of an object" when using the IOrder. I had overlooked in the past cases if this was specific to backtest or realtime causing this.I am uncertain of what case that would be aside from the IOrder object being null.

              In the meantime I suppose another simple test would be to comment out the method and leave the following in case the logic requires this to have a value:

              Code:
              if (execution.Order.OrderType == OrderType.Stop)
              {
              //cumProfit += LucroPreju(Close[0]);
              cumProfit += Close[0];
              }
              If the error exists, check for null in the if statement and test again. I would then verify what the case is that causes that for clarity.

              If the error does not exist, Koganam called it and there is error in the logic of the method.

              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by gentlebenthebear, Today, 01:30 AM
              2 responses
              13 views
              0 likes
              Last Post gentlebenthebear  
              Started by Kaledus, Today, 01:29 PM
              2 responses
              7 views
              0 likes
              Last Post Kaledus
              by Kaledus
               
              Started by frankthearm, Yesterday, 09:08 AM
              13 responses
              45 views
              0 likes
              Last Post frankthearm  
              Started by PaulMohn, Today, 12:36 PM
              2 responses
              16 views
              0 likes
              Last Post PaulMohn  
              Started by Conceptzx, 10-11-2022, 06:38 AM
              2 responses
              56 views
              0 likes
              Last Post PhillT
              by PhillT
               
              Working...
              X