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

How to export net profit from Execution without order?

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

    How to export net profit from Execution without order?

    I wrote some code to export a .csv of all trades that took place during a session using SystemPerformance.AllTrades and everything seemed like it worked. I tested my strategy over 2017 and it exported all the trades for the year. Great!

    However, when I compared my total net profit, as well as number of trades to what was shown in the Strategy Analyzer 'Summary' section for the same period, the numbers were off by about 15%.

    I've discovered that the Strategy Analyzer shows not just the net results of the Trades but also of the Executions. After digging through the help guide, I found an important note:

    "Note: Not all executions will have associated Order objects (e.g ExitOnSessionClose executions or AtmStrategyCreate() executions)"

    So, I'm guessing the discrepancy is due to trades that were canceled on SessionClose. This means that I can't iterate through SystemPerformance.AllTrades to get a true result of the performance, right?

    Looking at the available properties of Execution, here:


    I don't see any way to get the net profit when there's no order available.

    What is the best way to export all the executions to a .csv file that includes at least their timestamp and net profit?

    Thanks!

    #2
    Hello hillborne,

    Thank you for your note.

    If you disable exit on session close, then compare the SA to the results from your export, do they match?

    The Strategy Analyzer trades report is made up of executions. Could you provide more information on what you mean that the net results include both trades and executions?

    If you test your same strategy in the strategy analyzer then go to trades tab and export these results by right clicking>export, then comparing this to your csv file, do you notice which trades are missing?

    In your code, rather than writing the trades to a file, if you write the executions to a file, do the results match up? For example at the following link in the first example, you could write logic to write all executions in OnExecutionUpdate to file if they are filled.
    See, https://ninjatrader.com/support/help...?execution.htm

    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hi Alan,

      Thanks for the quick reply!

      If you disable exit on session close, then compare the SA to the results from your export, do they match?

      The Strategy Analyzer trades report is made up of executions. Could you provide more information on what you mean that the net results include both trades and executions?
      Ah, I misspoke. I didn't know the SA results were based only on Executions. I can't easily disable exit on session close because all my data structures are reset on session close and performance is much worse if I don't exit...but I can answer your next question:

      If you test your same strategy in the strategy analyzer then go to trades tab and export these results by right clicking>export, then comparing this to your csv file, do you notice which trades are missing?
      Yes, this is what led me to see there were missing trades. All the missing ones were 'Exit on session close'.

      In your code, rather than writing the trades to a file, if you write the executions to a file, do the results match up? For example at the following link in the first example, you could write logic to write all executions in OnExecutionUpdate to file if they are filled.
      See, https://ninjatrader.com/support/help...?execution.htm
      Right, this is probably what I have to do. My confusion and final question is based on the fact that Executions created by 'Exit on Session Close' do not have associated orders. Can you confirm my understanding of how to use the example 1 you referenced?

      I have several different signal names so rather than searching for a match on the name, I think I can just search for non-null Order.Name?

      If so, how do I get the final net profit from that execution.Order? I see no properties in either Execution or Order that might help.

      Code:
      // Example #1
      private Order entryOrder = null;
        
      protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
      {
        // Assign entryOrder in OnExecutionUpdate() to ensure the assignment occurs when expected.
        // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not guaranteed to be complete if it is referenced immediately after submitting
        if (execution.Order.Name != null && execution.Order.OrderState == OrderState.Filled)
            entryOrder = order;
       
          if (entryOrder != null && entryOrder == execution.Order)
            Print(???????????);
      }

      Also, please tell me if I'm going in a completely wrong direction. I feel like I'm missing something because I'm sure other people have wanted to automatically export their final daily performance which includes results based on trades that Exit on Session Close.

      Thanks, again!

      Comment


        #4
        Hello hillborne,

        If you test the SampleMaCrossover strategy, are you seeing the same, where exit on close is not included?

        When I test the samplemacross strategy, I see trades associated with ExitOnClose.

        To get a specific order in OnExecutionupdate, you could check the order name with,
        Code:
        if (execution.Order.Name == "myEntryOrder")
        See,



        You could get the net profit by subtracting execution.Price for the entry with the exit, and adding this difference to a variable of total net profit.

        I would suggest simplifying your strategy to the point where you can get what you’re looking for with only 1 signal name.

        I look forward to your reply.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Thanks, Alan. I guess I need to re-base my method to use OnExecutionUpdate since .AllTrades don't hold the ExitOnClose Executions.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by WeyldFalcon, 08-07-2020, 06:13 AM
          11 responses
          1,422 views
          0 likes
          Last Post jculp
          by jculp
           
          Started by RubenCazorla, Today, 09:07 AM
          0 responses
          4 views
          0 likes
          Last Post RubenCazorla  
          Started by BarzTrading, Today, 07:25 AM
          2 responses
          29 views
          1 like
          Last Post BarzTrading  
          Started by devatechnologies, 04-14-2024, 02:58 PM
          3 responses
          21 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by tkaboris, Today, 08:01 AM
          0 responses
          6 views
          0 likes
          Last Post tkaboris  
          Working...
          X