Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

Metatrader 5 Script | Export Closed Trades to CSV with a summary per magic number | Source Code Included

Discover expert developed MetaTrader tools that can complement professional solutions.

The History tab in MetaTrader 5 lists deals and orders, not trades. This script rebuilds each closed trade from its deals and writes one row per trade to a CSV file you can open in Excel, LibreOffice or Google Sheets.

Image for Export Closed Trades to CSV with a summary per magic number

Inputs

Never miss news-driven moves – use the News OCO Expert Advisor to place pending orders safely around economic releases. Find out more.

  • Period: This month, Last month, This year, All history or Custom. A trade counts in the period by its close time.
  • Custom period: From and To. A To date with no time includes that whole day.
  • Symbol: one symbol or empty for all symbols.
  • Magic number: one magic number or -1 for all trades.
  • Column separator: comma, semicolon or tab.

File 1: ClosedTrades_<login>_<period>.csv

One row per closed trade: OpenTime, CloseTime, Symbol, Type, Volume, OpenPrice, ClosePrice, StopLoss, TakeProfit, Commission, Swap, Fee, Profit, NetProfit, Magic, Comment, CloseReason, PositionTicket.

File 2: ClosedTradesByMagic_<login>_<period>.csv

One row per magic number and one row for all trades: Magic, Trades, Wins, Losses, NetProfit, GrossProfit, GrossLoss, LargestLoss, LongestLosingStreak, WinRatePct.

How to use

  1. Copy ExportClosedTrades.mq5 to the MQL5\Scripts folder and compile it in MetaEditor.
  2. Drag the script onto any chart, set the inputs and press OK.
  3. The Experts tab shows where both files were saved and how many rows each has. To find them, use File > Open Data Folder > MQL5\Files.

Notes

  • A trade closed in parts is one row. Its exit price is the average of the exits, weighted by volume.
  • Open positions are not exported.
  • Files are UTF-8 with a BOM. Prices and money use a decimal point.
  • The script only reads the account history. It doesn't place, change or close orders.
  • If the terminal isn't connected, the journal says so, because the history may be incomplete.
  • If a file is open in another program, the script can't overwrite it and writes the reason to the journal.

Complement community code with advanced automation from RobotFX.

80136

Best MetaTrader Indicators + Profitable Expert Advisors