//+------------------------------------------------------------------+
//|                                      TradeStatementExporter.mq5  |
//+------------------------------------------------------------------+

#property script_show_inputs

#include <Xlsx/TradeRecord.mqh>
#include <Xlsx/TradeLoader.mqh>
#include <Xlsx/XlsxWriter.mqh>

//--- Inputs shown in the script dialog
input string   InpSymbol         = "";                     // Symbol filter (empty = all)
input long     InpMagic          = 0;                      // Magic number filter (0 = all)
input datetime InpFromDate       = 0;                      // History start date (0 = earliest)
input datetime InpToDate         = 0;                      // History end date (0 = now)
input string   InpOutputFilename = "trade_statement.xlsx"; // Output filename in MQL5/Files/

//+------------------------------------------------------------------+
//| Script entry point: load trades and write the XLSX file.         |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- resolve date range; zero means "use the full available range"
   datetime from = (InpFromDate == 0) ? 0             : InpFromDate;
   datetime to   = (InpToDate   == 0) ? TimeCurrent() : InpToDate;

   uint t_start = GetTickCount(); // for elapsed-time logging

//--- load trade history from the terminal
   CTradeLoader loader;
   CTradeRecord trades[];

   int count = loader.Load(from, to, InpSymbol, InpMagic, trades);

   if(count == 0)
     {
      Print("TradeStatementExporter: no trades found for the given filters.");
      return;
     }

   PrintFormat("TradeStatementExporter: %d trades loaded.", count);

//--- write the XLSX file
   CXlsxWriter writer;
   bool ok = writer.Write(trades, count, InpOutputFilename);

   if(!ok)
     {
      Print("TradeStatementExporter: write failed. Check the journal for details.");
      return;
     }

   uint elapsed = GetTickCount() - t_start;
   PrintFormat("TradeStatementExporter: export complete. File: MQL5/Files/%s  (%d ms)",
               writer.GetOutputPath(), elapsed);
  }
//+------------------------------------------------------------------+