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
              1 response
              8 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by Aviram Y, Today, 05:29 AM
              1 response
              6 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by cls71, Today, 04:45 AM
              1 response
              7 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by TradeForge, Today, 02:09 AM
              1 response
              22 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by elirion, Today, 01:36 AM
              2 responses
              14 views
              0 likes
              Last Post elirion
              by elirion
               
              Working...
              X