Generate code in 3 minutes
Enter your idea and AI generates runnable code.
Here is a complete MQL4 script designed to analyze your account history.
MODE_HISTORY pool. 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.//+------------------------------------------------------------------+
//| 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);
}
}
//+------------------------------------------------------------------+
AverageProfitAnalyzer.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.