Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exporting trades data to CSV in BackTest and Playback modes

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

    Exporting trades data to CSV in BackTest and Playback modes


    Hi !

    I’m trying to export all of the trades data to a CSV file using Trade object fields during both backtesting and playback mode.I want to use additional Python to perform more analysis on the trades. Ive tried to implement the following but it didn't worked well:


    Code:
    else if (State == State.Terminated)
    {
    if (SystemPerformance.AllTrades == null || SystemPerformance.AllTrades.Count == 0){
     Print("No trades found to export.");
     return;
    }
    mode = State == State.Realtime ? "Playback" : "Backtest";
    string filePath = @"C:\NinjaTrader_Trades_" + Instrument.FullName + "_" + mode + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".csv";
    try
    {
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath)) {
    writer.WriteLine("Trade #,Entry Time,Exit Time,Profit Currency"); foreach (Trade trade in SystemPerformance.AllTrades) { writer.WriteLine( $"{trade.TradeNumber}," + $"{trade.Entry.Time}," + $"{trade.Exit.Time}," + $"{trade.ProfitCurrency}" )
    };​
    How correctly implement this ?

    #2
    Hello Caspersky_,

    I have a few examples you can copy code out of.
    Then change the string being written and filename + extension.

    You might also consider writing the file to the Documents directory. Some computers may not have permissions to write a file to a directory under the C:\ .

    First is the reference sample in the help guide.


    And a script that also writes information to file.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3

      Great to see you still part of the NT family, Chelsea!
      I want to challenge you a bit. Using the Strategy Optimizer, is there a way to fetch the parameters used for each iteration (let's say I am using the Standard Optimizer)?

      My main goal is to create an Excel sheet that includes:
      1. The optimized parameters,
      2. The associated metrics,
      3. And the trades themselves for each iteration

      Comment


        #4
        Hello Caspersky_,

        The values of each property (parameter) could be printed or written to a file once the script has reached State.DataLoaded or later.

        Try printing these to have an understanding of when the values can be accessed.

        For example if you had a property named Slow and one named Fast:

        Print("Slow: " + Slow + " Fast: " + Fast);
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks for the answer ChelseaB​, but lets say I want to make it more general without targeting specific parameter names, How can I fetch the Parameters for exmaple as seen here:

          Click image for larger version

Name:	image.png
Views:	127
Size:	5.1 KB
ID:	1326166

          Comment


            #6
            Hello Caspersky_,

            You'll need to print (or write to file) the variables in the script using the variables themselves.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            39 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            124 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            64 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            41 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            46 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X