mql5/Include/Example1/CRiskManager.mqh
SoloTradeOfficial f20bb89655 new files added
2025-08-30 11:40:16 +00:00

75 lines
3 KiB
MQL5

//+------------------------------------------------------------------+
//| CRiskManager.mqh |
//| Copyright 2025, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "SoloTrade Official, Solomon"
#property link "https://www.mql5.com/en/users/SoloTradeOfficial"
#property version "1.00"
#property description "THE WORLD MOST INTELLIGENT FOREX AI/ALGO BOTS EVER IN"
"HUMAN HISTORY OF FINTECH, BY SOLOMON ESHUN"
"\nTrading Bots in MQL5"
// CRiskManager.mqh
class CRiskManager {
// ---------------- PUBLIC INTERFACE ----------------
// The simple controls that the outside world can use.
public:
// Constructor: How we set up our risk rules.
void CRiskManager(double maxRiskPercent, int maxOpenTrades) {
m_max_risk_percent = maxRiskPercent;
m_max_open_trades = maxOpenTrades;
}
// The main, simple question anyone can ask this class.
bool isTradeAllowed(double stopLossPips) {
// This public function calls its own private, complex helper functions.
if (isMaxTradesExceeded()) {
Print("Risk check failed: Maximum number of trades exceeded.");
return false;
}
if (isRiskTooHigh(stopLossPips)) {
Print("Risk check failed: Potential loss is too high for this trade.");
return false;
}
Print("Risk check passed. Trade is allowed.");
return true;
}
// ---------------- PRIVATE IMPLEMENTATION ----------------
// The hidden "engine" that the outside world cannot see or touch.
private:
// Member variables to store our rules and state.
double m_max_risk_percent;
int m_max_open_trades;
// A private helper function to check trade count.
bool isMaxTradesExceeded() {
// In a real EA, this would check PositionsTotal()
int currentTrades = PositionsTotal(); // Assuming we are checking current open positions
return (currentTrades >= m_max_open_trades);
}
// A private helper function for the complex risk calculation.
bool isRiskTooHigh(double stopLossPips) {
double account_balance = AccountInfoDouble(ACCOUNT_BALANCE);
double tick_value = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double tick_size = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
// This is the kind of complex logic you want to hide!
double potential_loss_per_lot = stopLossPips * (tick_value / tick_size);
double max_allowed_loss = account_balance * (m_max_risk_percent / 100.0);
// For this example, we'll check based on a standard lot size of 1.0.
// A more advanced version would calculate the lot size itself.
if (potential_loss_per_lot > max_allowed_loss) {
return true; // Risk is too high
}
return false; // Risk is acceptable
}
};
//+------------------------------------------------------------------+