Code pour créer un robot
//+------------------------------------------------------------------+
#include <Trade/Trade.mqh>
CTrade trade;
// SYMBOLS
string symbols[3] = {"Volatility 10 Index","Volatility 25 Index","Volatility 75 Index"};
string bestSymbol = "";
// VARIABLES
double startBalance;
int tradeCount = 0;
int lossStreak = 0;
bool tradingStopped = false;
bool profitMode = false;
datetime lastDay;
// PARAMÈTRES
double lotSize;
int stopLoss;
int takeProfit;
int trailingStop = 80;
//+------------------------------------------------------------------+
// RESET JOUR
void ResetDaily()
{
datetime now = TimeCurrent();
if(TimeDay(now) != TimeDay(lastDay))
{
startBalance = AccountInfoDouble(ACCOUNT_BALANCE);
tradeCount = 0;
lossStreak = 0;
tradingStopped = false;
profitMode = false;
lastDay = now;
}
}
//+------------------------------------------------------------------+
double GetProfitPercent()
{
double balance = AccountInfoDouble(ACCOUNT_BALANCE);
return ((balance - startBalance) / startBalance) * 100.0;
}
//+------------------------------------------------------------------+
// IA MEILLEUR INDICE
string GetBestSymbol()
{
double bestScore = 0;
string selected = symbols[0];
for(int i=0; i<3; i++)
{
string sym = symbols[i];
double range = iHigh(sym, PERIOD_M5, 0) - iLow(sym, PERIOD_M5, 0);
double ema10 = iMA(sym, PERIOD_M5, 10, 0, MODE_EMA, PRICE_CLOSE, 0);
double ema20 = iMA(sym, PERIOD_M5, 20, 0, MODE_EMA, PRICE_CLOSE, 0);
double trend = MathAbs(ema10 - ema20);
double score = range + trend;
if(score > bestScore)
{
bestScore = score;
selected = sym;
}
}
return selected;
}
//+------------------------------------------------------------------+
// SCALPING V75
bool IsScalpingMode(string sym)
{
if(sym == "Volatility 75 Index")
{
double range = iHigh(sym, PERIOD_M1, 0) - iLow(sym, PERIOD_M1, 0);
return (range > 200 * _Point);
}
return false;
}
//+------------------------------------------------------------------+
// ANTI FAKE BREAKOUT
bool IsValidEntry(string sym)
{
double rsi = iRSI(sym, PERIOD_M1, 14, PRICE_CLOSE, 0);
double high0 = iHigh(sym, PERIOD_M1, 0);
double low0 = iLow(sym, PERIOD_M1, 0);
double range = high0 - low0;
if(range < 100 * _Point) return false;
if(range > 400 * _Point) return false;
if(rsi > 75 || rsi < 25) return false;
return true;
}
//+------------------------------------------------------------------+
// MARKET MAKER TRAP
bool IsMarketTrap(string sym)
{
double high0 = iHigh(sym, PERIOD_M1, 0);
double low0 = iLow(sym, PERIOD_M1, 0);
double close0 = iClose(sym, PERIOD_M1, 0);
double open0 = iOpen(sym, PERIOD_M1, 0);
double range = high0 - low0;
double body = MathAbs(close0 - open0);
if(range > 350 * _Point && body < (range * 0.3))
return true;
return false;
}
//+------------------------------------------------------------------+
// SUPPORT / RESISTANCE
bool NearSupportResistance(string sym)
{
double high = iHigh(sym, PERIOD_M15, iHighest(sym, PERIOD_M15, MODE_HIGH, 20, 0));
double low = iLow(sym, PERIOD_M15, iLowest(sym, PERIOD_M15, MODE_LOW, 20, 0));
double price = SymbolInfoDouble(sym, SYMBOL_BID);
double zone = 150 * _Point;
if(MathAbs(price - high) < zone || MathAbs(price - low) < zone)
return true;
return false;
}
//+------------------------------------------------------------------+
// MULTI TF
bool MultiTFConfirm(string sym)
{
double ema10_M1 = iMA(sym, PERIOD_M1, 10, 0, MODE_EMA, PRICE_CLOSE, 0);
double ema20_M1 = iMA(sym, PERIOD_M1, 20, 0, MODE_EMA, PRICE_CLOSE, 0);
double ema10_M5 = iMA(sym, PERIOD_M5, 10, 0, MODE_EMA, PRICE_CLOSE, 0);
double ema20_M5 = iMA(sym, PERIOD_M5, 20, 0, MODE_EMA, PRICE_CLOSE, 0);
double ema10_M15 = iMA(sym, PERIOD_M15, 10, 0, MODE_EMA, PRICE_CLOSE, 0);
double ema20_M15 = iMA(sym, PERIOD_M15, 20, 0, MODE_EMA, PRICE_CLOSE, 0);
bool buyTrend =
(ema10_M1 > ema20_M1) &&
(ema10_M5 > ema20_M5) &&
(ema10_M15 > ema20_M15);
bool sellTrend =
(ema10_M1 < ema20_M1) &&
(ema10_M5 < ema20_M5) &&
(ema10_M15 < ema20_M15);
return (buyTrend || sellTrend);
}
//+------------------------------------------------------------------+
// TRAILING
void ManageTrailing(string sym)
{
if(PositionSelect(sym))
{
double tp = PositionGetDouble(POSITION_TP);
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
double price = SymbolInfoDouble(sym, SYMBOL_BID);
trade.PositionModify(sym, price - trailingStop * _Point, tp);
}
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
double price = SymbolInfoDouble(sym, SYMBOL_ASK);
trade.PositionModify(sym, price + trailingStop * _Point, tp);
}
}
}
Code pour créer un robot suite
//+------------------------------------------------------------------+
bool StopConditions()
{
double profit = GetProfitPercent();
if(profit <= -5.0) return true;
if(tradeCount >= 3) return true;
if(lossStreak >= 2) return true;
return false;
}
//+------------------------------------------------------------------+
void OnTick()
{
ResetDaily();
if(tradingStopped) return;
double balance = AccountInfoDouble(ACCOUNT_BALANCE);
lotSize = 0.01;
stopLoss = 120;
takeProfit = 140;
double profit = GetProfitPercent();
if(profit >= 2.0 && !profitMode)
{
profitMode = true;
SendNotification("💰 Profit sécurisé");
}
if(StopConditions())
{
tradingStopped = true;
SendNotification("🚫 STOP");
return;
}
bestSymbol = GetBestSymbol();
string sym = bestSymbol;
ManageTrailing(sym);
double lot = profitMode ? lotSize * 0.5 : lotSize;
if(IsScalpingMode(sym))
{
stopLoss = 80;
takeProfit = 100;
}
double ema10 = iMA(sym, PERIOD_M1, 10, 0, MODE_EMA, PRICE_CLOSE, 0);
double ema20 = iMA(sym, PERIOD_M1, 20, 0, MODE_EMA, PRICE_CLOSE, 0);
double rsi = iRSI(sym, PERIOD_M1, 14, PRICE_CLOSE, 0);
double ask = SymbolInfoDouble(sym, SYMBOL_ASK);
double bid = SymbolInfoDouble(sym, SYMBOL_BID);
if(!PositionSelect(sym)
&& IsValidEntry(sym)
&& !IsMarketTrap(sym)
&& NearSupportResistance(sym)
&& MultiTFConfirm(sym))
{
if(ema10 > ema20 && rsi > 55)
{
trade.Buy(lot, sym, ask,
bid - stopLoss * _Point,
bid + takeProfit * _Point);
tradeCount++;
SendNotification("📈 BUY " + sym);
}
if(ema10 < ema20 && rsi < 45)
{
trade.Sell(lot, sym, bid,
ask + stopLoss * _Point,
ask - takeProfit * _Point);
tradeCount++;
SendNotification("📉 SELL " + sym);
}
}
}
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
const MqlTradeRequest &request,
const MqlTradeResult &result)
{
if(trans.type == TRADE_TRANSACTION_DEAL_ADD)
{
if(trans.profit < 0)
lossStreak++;
else
lossStreak = 0;
}
}