Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exporting Strategy back test trade data to csv or xlsx

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

    Exporting Strategy back test trade data to csv or xlsx

    Dear NinjaTrader Support,

    I am currently developing a statistical tool in NinjaTrader 8 to compare trades between different variations of my strategy. For this purpose, I am utilizing a slightly modified version of the SampleMaCrossOver strategy, where I've added print statements to monitor the strategy states during execution.

    While running a single backtest and analysing the lifecycle of the strategy through the Strategy Analyzer, I noticed some peculiarities in the sequence of states. The sequence I observed was as follows: the strategy switches to the SetDefaults state, then to the Terminated state three times, back to SetDefaults two times, and finally terminates. I have attached a text file (SampleMaCrossOverStrategyAnalysis10.txt) that shows this sequence as output from my print statements.

    Next, I conducted an optimization test by adjusting the 'Fast' parameter from 10 to 15 in increments of 5, with IsInstantiatedOnEachOptimizationIteration set to false. When examining the print outputs, it appears that the first and second iterations of the strategy run concurrently. This concurrency poses a challenge for data collection as trades from the first iteration intermingle with those from the second in my csv output files. You can observe this behavior in the attached text file (SampleMaCrossOverStrategyAnalysis10&15.txt).

    I am seeking answers and guidance for the following:
    1. Why does the strategy cycle through the SetDefaults and Terminated states multiple times at the end of a backtest? Is this the expected behavior? If not, what might be causing this in my code?
    2. In the optimization test, how can I prevent iterations of the strategy from running concurrently and avoid intermingling of data in my csv files? Is there a recommended method for cleanly extracting this data?
    3. Is it possible to directly export the strategy data to an .xlsx file instead of a .csv? If so, could you provide some guidance or refer me to relevant documentation/examples on how to accomplish this?
    4. Given that my primary goal is to export the strategy data for further analysis in Excel, do you have any additional recommendations on how to optimize my data extraction process or any features of NinjaTrader that may assist in this endeavor?

    I have also attached the modified SampleMaCrossOver strategy code (SampleMaCrossOverStrategyAnalysis.cs) for reference. The start and end dates for all the backtests are 01/05/2023 and 23/05/2023 respectively.

    Thank you for your time and assistance.

    Best Regards,
    ​Aviram Y
    Attached Files
    Aviram Y
    NinjaTrader Ecosystem Vendor - Aviram Y

    #2
    Hello Aviram Y,

    It is normal for a scripts OnStateChange to run multiple times, that is part of how the NinjaScript lifecycle works. SetDefaults is called any time the strategies default parameters are needed which could include menus or other actions like collecting information for performance. Terminated is called when a strategy terminates but also may be called other times if sub instances of the strategy were created to collect parameters. You can read about that here: https://ninjatrader.com/support/help...tsub=lifecycle

    There is not a way to change how the optimizer runs the strategy iterations, that is controlled by internal logic and your PC resources. Generally writing to a single file from an optimization is not possible because the data won't have a specific order and you may observe file I/O errors.

    You can export any grid in the platform by going to Right click -> Export, to use excel format you can select that from the dropdown where .csv may be selected. The dropdown has both csv and excel format.

    I wouldn't have any suggestions for automating exporting optimizer data to excel, I would likely suggest running backtests instead if you wanted to write results to a file from the strategy. That would prevent any read/write errors. Alternatively you could have each strategy write to a unique file and then after the test combine the data into one file and import it into excel.

    Comment


      #3
      Hello Jesse,

      Thank you for your comprehensive response, it provided important insights into the functioning of NinjaScript and the strategy analyzer. I understand the challenges in writing to a single file from an optimization due to internal logic and potential I/O errors.

      I'm contemplating the possibility of creating my own strategy analyzer and optimizer, which could serve my specific requirements more effectively. I anticipate that this could entail a significant learning curve, so I'm hoping you could provide some guidance.
      1. Are there any NinjaTrader specific tools or features that would aid in the creation of a custom strategy analyzer?
      2. What potential obstacles or limitations might I encounter while building this in NinjaTrader?
      3. Would it be possible to interface directly with the NinjaTrader platform for real-time or historical price data from my custom analyzer?
      4. Is there a way to integrate my custom analyzer into the NinjaTrader platform as an add-on?

      Finally, I'm aware that NinjaTrader uses an older version of C# and .NET Framework. Would you recommend working with newer versions of these technologies and then exporting the scripts into an assembly that can be utilized in NinjaTrader?

      I greatly appreciate any advice or insights you can provide to help me embark on this project more confidently.

      Best regards, Aviram Y
      Aviram Y
      NinjaTrader Ecosystem Vendor - Aviram Y

      Comment


        #4
        Hello Aviram Y,

        There are not any tools to help with that kind of development, you can use the NinjaScript editor to view the existing Optimizers code.

        I wouldn't have any specific items that I could note for obstacles or limitations, you would have to write the code first and see what kind of issues arise from whatever was coded.

        Custom optimizers are only for the strategy analyzer tool, that is strictly a historical based tool.

        An addon could be a lot of different items like a custom window or something which affects other windows. An optimizer is only used in the strategy analyzer for running tests on strategies in historical data. The two are not at all the same so an optimizer would not be able to be converted to an addon in the sense of generating performance reports. An addon could do custom trading similar to a strategy but in that situation you would be overcomplicating the use case, a strategy can be used for trading in both historical and realtime, a strategy can also be used with a custom optimizer.

        I would not suggest using other versions of C# that are newer, matching the platform would be the best practice because that is known to work right.


        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        633 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        364 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        105 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        567 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        568 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X