Code: Select all
//+------------------------------------------------------------------+
//| Expectancy_Tracker_MT4.mq4 |
//+------------------------------------------------------------------+
#property copyright "Expectancy Framework"
#property version "1.00"
#property strict
extern int FastEmaLength = 9;
extern int SlowEmaLength = 21;
extern double RiskAtrMultiplier = 1.5;
extern double RewardAtrMultiplier = 3.0;
extern int MagicNumber = 123456;
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(OrdersTotal() == 0)
{
double atr = iATR(Symbol(), 0, 14, 1);
double fastEma1 = iMA(Symbol(), 0, FastEmaLength, 0, MODE_EMA, PRICE_CLOSE, 1);
double slowEma1 = iMA(Symbol(), 0, SlowEmaLength, 0, MODE_EMA, PRICE_CLOSE, 1);
double fastEma2 = iMA(Symbol(), 0, FastEmaLength, 0, MODE_EMA, PRICE_CLOSE, 2);
double slowEma2 = iMA(Symbol(), 0, SlowEmaLength, 0, MODE_EMA, PRICE_CLOSE, 2);
bool longSweep = (fastEma1 > slowEma1 && fastEma2 <= slowEma2);
bool shortSweep = (fastEma1 < slowEma1 && fastEma2 >= slowEma2);
if(longSweep)
{
double sl = Bid - (atr * RiskAtrMultiplier);
double tp = Ask + (atr * RewardAtrMultiplier);
double riskDist = Ask - sl;
string comment = "1R:" + DoubleToString(riskDist, Digits);
OrderSend(Symbol(), OP_BUY, 0.1, Ask, 3, sl, tp, comment, MagicNumber, 0, clrGreen);
}
else if(shortSweep)
{
double sl = Ask + (atr * RiskAtrMultiplier);
double tp = Bid - (atr * RewardAtrMultiplier);
double riskDist = sl - Bid;
string comment = "1R:" + DoubleToString(riskDist, Digits);
OrderSend(Symbol(), OP_SELL, 0.1, Bid, 3, sl, tp, comment, MagicNumber, 0, clrRed);
}
}
UpdateExpectancyDashboard();
}
//+------------------------------------------------------------------+
//| Calculate and Render Expectancy |
//+------------------------------------------------------------------+
void UpdateExpectancyDashboard()
{
int trades = 0, wins = 0, losses = 0;
double sumWinR = 0, sumLossR = 0;
int historyTotal = OrdersHistoryTotal();
for(int i = 0; i < historyTotal; i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
string comment = OrderComment();
int idx = StringFind(comment, "1R:");
if(idx >= 0)
{
double initialRiskPoints = StringToDouble(StringSubstr(comment, idx + 3));
double profit = OrderProfit() + OrderCommission() + OrderSwap();
double tickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
double tickSize = MarketInfo(Symbol(), MODE_TICKSIZE);
double monetaryRisk = (initialRiskPoints / tickSize) * tickValue * OrderLots();
if(monetaryRisk == 0) continue;
double rMultiple = profit / monetaryRisk;
trades++;
if(rMultiple > 0) {
wins++;
sumWinR += rMultiple;
} else {
losses++;
sumLossR += MathAbs(rMultiple);
}
}
}
}
}
double winRate = trades > 0 ? (double)wins / trades : 0.0;
double lossRate = 1.0 - winRate;
double avgWin = wins > 0 ? sumWinR / wins : 0.0;
double avgLoss = losses > 0 ? sumLossR / losses : 0.0;
double expectancy = (winRate * avgWin) - (lossRate * avgLoss);
string dash = "--- EXPECTANCY FRAMEWORK ---\n";
dash += "Total Trades: " + IntegerToString(trades) + "\n";
dash += "Win Rate: " + DoubleToString(winRate * 100, 1) + "%\n";
dash += "Avg Win: +" + DoubleToString(avgWin, 2) + "R\n";
dash += "Avg Loss: -" + DoubleToString(avgLoss, 2) + "R\n";
dash += "Expectancy per Trade: " + DoubleToString(expectancy, 2) + "R\n";
Comment(dash);
}