Problem Description
Solution
Here is a complete MQL4 script designed to analyze your account history.
Script Logic
- Inputs: It allows you to specify a symbol. If you leave it blank, it defaults to the symbol of the chart where you drop the script.
- Filtering: It iterates through the
MODE_HISTORYpool. It filters out non-trading operations (like deposits or cancelled pending orders) and only calculates based on actual Buy (OP_BUY) and Sell (OP_SELL) positions. - Net Profit: It calculates the Net Profit (Order Profit + Swap + Commission).
- Output: It displays the result in a pop-up Message Box.
MQL4 Code
//+------------------------------------------------------------------+
//| AverageProfitAnalyzer.mq4 |
//| Copyright 2023, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Quantitative Analysis"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property script_show_inputs
//--- Input Parameters
input string InpSymbol = ""; // Symbol to analyze (Leave empty for current chart)
input int InpMagicNumber = 0; // Magic Number filter (0 for all manual/EA trades)
input bool InpFilterMagic = false; // Set to true to filter by Magic Number
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 1. Determine the symbol to analyze
string symbolToAnalyze = InpSymbol;
if(symbolToAnalyze == "")
{
symbolToAnalyze = Symbol(); // Use current chart symbol if input is empty
}
//--- 2. Initialize variables
double totalNetProfit = 0.0;
int tradeCount = 0;
int historyTotal = OrdersHistoryTotal();
//--- 3. Iterate through closed trades
for(int i = 0; i < historyTotal; i++)
{
// Select the order from history
if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
// Filter by Symbol
if(OrderSymbol() == symbolToAnalyze)
{
// Filter by Magic Number (if enabled)
if(!InpFilterMagic || OrderMagicNumber() == InpMagicNumber)
{
// Filter by Order Type (Only Buy and Sell, ignore Balance/Credit)
if(OrderType() == OP_BUY || OrderType() == OP_SELL)
{
// Calculate Net Profit: Profit + Commission + Swap
double netProfit = OrderProfit() + OrderCommission() + OrderSwap();
totalNetProfit += netProfit;
tradeCount++;
}
}
}
}
}
//--- 4. Calculate Average and Display Results
if(tradeCount > 0)
{
double averageProfit = totalNetProfit / tradeCount;
string message = "Analysis for: " + symbolToAnalyze + "\n" +
"--------------------------------" + "\n" +
"Total Trades: " + IntegerToString(tradeCount) + "\n" +
"Total Net Profit: " + DoubleToString(totalNetProfit, 2) + " " + AccountCurrency() + "\n" +
"--------------------------------" + "\n" +
"AVERAGE PROFIT: " + DoubleToString(averageProfit, 2) + " " + AccountCurrency();
MessageBox(message, "Trade Analysis Result", MB_OK | MB_ICONINFORMATION);
}
else
{
MessageBox("No closed Buy/Sell trades found for " + symbolToAnalyze, "Analysis Result", MB_OK | MB_ICONWARNING);
}
}
//+------------------------------------------------------------------+
How to use this script:
- Open MetaEditor (F4 in MT4).
- Create a new Script (File -> New -> Script) and name it
AverageProfitAnalyzer. - Paste the code above into the file, replacing everything else.
- Click Compile (F7).
- Go back to the MT4 Terminal.
- Find the script in the Navigator panel under Scripts.
- Drag and drop it onto a chart.
- A window will pop up allowing you to specify a symbol (optional). If you leave it blank, it calculates for the chart you dropped it on.
Q&A
Q: Does this script include commissions and swaps?
A: Yes. The script calculates "Net Profit" by summing OrderProfit(), OrderCommission(), and OrderSwap().
Q: Can I use this to analyze trades made by a specific Expert Advisor?
A: Yes. Set InpFilterMagic to true and enter the specific Magic Number of your EA in the InpMagicNumber input field.
Q: Why does the script ignore deposits and withdrawals?
A: The code specifically checks if(OrderType() == OP_BUY || OrderType() == OP_SELL). This ensures that balance operations (which appear in history as OP_BALANCE) do not skew the average trade profit calculation.