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:	136
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 CarlTrading, 03-31-2026, 09:41 PM
            1 response
            46 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            22 views
            0 likes
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            31 views
            1 like
            Last Post CaptainJack  
            Started by CarlTrading, 03-30-2026, 11:51 AM
            0 responses
            50 views
            0 likes
            Last Post CarlTrading  
            Started by CarlTrading, 03-30-2026, 11:48 AM
            0 responses
            42 views
            0 likes
            Last Post CarlTrading  
            Working...
            X