//+------------------------------------------------------------------+
//| AggroCompounder.mq4 |
//| Super-high-risk educational EA |
//| (c) 2024 – use at your own peril |
//+------------------------------------------------------------------+
#property copyright "Released for educational purposes only"
#property version "1.00"
#property strict
#property indicator_chart_window
//––––––––– USER INPUTS ––––––––––––––––––––––––––––––––––––––––––––
input double RiskPercent = 10.0; // % of equity per trade
input bool UseMartingale = false; // Double risk after loss?
input double ATR_Min = 10; // Minimum ATR (points) to trade
input int MagicNumber = 70001; // For tracking orders
input bool ShowDashboard = true; // Paint info panel
input color PanelBGColor = clrBlack;
input color PanelTextColor = clrLime;
input ENUM_TIMEFRAMES TF = PERIOD_M15;
//––––––––– GLOBALS ––––––––––––––––––––––––––––––––––––––––––––––––
double LastRiskPercent = RiskPercent;
string dashboardName = "AggroDashBox";
datetime lastBarTime = 0;
//+------------------------------------------------------------------+
int OnInit()
{
ObjectsDeleteAll(0,OBJ_LABEL); // Clean any leftovers
if(ShowDashboard)
InitPanel();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0,OBJ_LABEL);
}
//+------------------------------------------------------------------+
void OnTick()
{
// Operate only on chosen timeframe's first tick of new candle
if(Period() != TF) return;
datetime currentBarTime = iTime(_Symbol, TF, 0);
if(currentBarTime == lastBarTime) return; // same candle
lastBarTime = currentBarTime;
// 1. Market conditions
double atr = iATR(_Symbol, TF, 14, 1);
double ema = iMA(_Symbol, TF, 50, 0, MODE_EMA, PRICE_CLOSE, 1);
double closePrev = iClose(_Symbol, TF, 1);
bool uptrend = closePrev > ema;
bool downtrend = closePrev < ema;
double breakoutBuy = iHigh(_Symbol, TF, 1) + atr * 0.2;
double breakoutSell = iLow (_Symbol, TF, 1) - atr * 0.2;
// 2. Manage existing orders
ManageOpenTrades();
// 3. Entry logic – only if no positions on this symbol/magic
if(!HasOpenPosition())
{
if(atr > ATR_Min)
{
double lots = CalcLots(LastRiskPercent, atr);
double sl, tp;
if(uptrend)
{
sl = breakoutBuy - atr;
tp = breakoutBuy + 2*atr;
PlacePending(OP_BUYSTOP, breakoutBuy, sl, tp, lots);
}
else if(downtrend)
{
sl = breakoutSell + atr;
tp = breakoutSell - 2*atr;
PlacePending(OP_SELLSTOP, breakoutSell, sl, tp, lots);
}
}
}
// 4. Refresh dashboard
if(ShowDashboard)
UpdatePanel(atr, ema, uptrend, downtrend);
}
//––––––––– UTILITIES –––––––––––––––––––––––––––––––––––––––––––––––
bool HasOpenPosition()
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)
&& OrderSymbol()==_Symbol
&& OrderMagicNumber()==MagicNumber)
return true;
}
return false;
}
//--------------------------------------------------------------
double CalcLots(double riskPerc, double atrPoints)
{
// Risk = Equity * riskPerc%
double riskDollars = AccountEquity() * riskPerc/100.0;
// StopDistance in points => convert to price * lot value
double slPoints = atrPoints; // 1×ATR
double tickValue = MarketInfo(_Symbol, MODE_TICKVALUE);
double lot = NormalizeDouble((riskDollars / (slPoints * tickValue)),2);
double minLot = MarketInfo(_Symbol, MODE_MINLOT);
double maxLot = MarketInfo(_Symbol, MODE_MAXLOT);
lot = MathMax(minLot, MathMin(lot, maxLot));
return(lot);
}
//--------------------------------------------------------------
void PlacePending(int type, double price, double sl, double tp, double lots)
{
int ticket = OrderSend(_Symbol, type, lots, price, 10, sl, tp,
"AggroBreak", MagicNumber, 0, clrDeepSkyBlue);
if(ticket < 0)
Print("OrderSend error: ",GetLastError());
}
//--------------------------------------------------------------
void ManageOpenTrades()
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)
&& OrderMagicNumber()==MagicNumber
&& OrderSymbol()==_Symbol)
{
// If trade closed? Check state in OnTick? (Simplified)
}
}
// If last closed order loss -> martingale
int totalHist = OrdersHistoryTotal();
if(totalHist>0 &&
OrderSelect(totalHist-1, SELECT_BY_POS, MODE_HISTORY) &&
OrderMagicNumber()==MagicNumber)
{
double profit = OrderProfit() + OrderSwap() + OrderCommission();
if(profit < 0)
{
if(UseMartingale) LastRiskPercent = MathMin(LastRiskPercent*2, 90);
}
else
LastRiskPercent = RiskPercent; // reset
}
}
//––––––––– DASHBOARD –––––––––––––––––––––––––––––––––––––––––––––––
void InitPanel()
{
if(ObjectFind(0,dashboardName)<0)
{
ObjectCreate(0,dashboardName,OBJ_LABEL,0,0,0);
ObjectSetInteger(0,dashboardName,OBJPROP_CORNER,0);
ObjectSetInteger(0,dashboardName,OBJPROP_XDISTANCE,10);
ObjectSetInteger(0,dashboardName,OBJPROP_YDISTANCE,20);
ObjectSetInteger(0,dashboardName,OBJPROP_COLOR,PanelTextColor);
ObjectSetInteger(0,dashboardName,OBJPROP_FONTSIZE,10);
ObjectSetString(0,dashboardName,OBJPROP_FONT,"Consolas");
ObjectSetString(0,dashboardName,OBJPROP_BACK,true);
ObjectSetInteger(0,dashboardName,OBJPROP_BGCOLOR,PanelBGColor);
}
}
void UpdatePanel(double atr, double ema, bool uptrend, bool downtrend)
{
string txt;
txt = "Aggro Compounder EA\n";
txt += "--------------------------\n";
txt += "Symbol: " + _Symbol + "\n";
txt += "Trend : " + (uptrend ? "UP" : downtrend ? "DOWN" : "FLAT") + "\n";
txt += "EMA(50): " + DoubleToString(ema,Digits) + "\n";
txt += "ATR(14): " + DoubleToString(atr,1) + "\n";
txt += "Next Risk%: " + DoubleToString(LastRiskPercent,1) + "\n";
txt += "Equity : " + DoubleToString(AccountEquity(),2) + "\n";
txt += "FreeMargin: " + DoubleToString(AccountFreeMargin(),2) + "\n";
ObjectSetString(0,dashboardName,OBJPROP_TEXT,txt);
}
//+------------------------------------------------------------------+