can you build me a full automated trading bot for trading major forex pairs and gold, the strategy should have a winrate of above 90% and be able to scalp and day trade as well. risk to reward should be above 1:3RR, risking 2% of account size per trade. trail stops, add more positions when in deep profit and in trend continuation,. it should trade break and retest, trendline, smc with entry confirmations of engulfing candle, morning star, evening star, essential wick rejection, it should also be able to spot and trade chart patterns such as head and shoulder, W and M pattern etc. make the bot a profitable bot, fully optimised for automated trading. the code should be compatible with the mql5 platform. avoid all syntax errors and compilation errors. all variables should be well defined and understadable by the mql5 platform.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I can create a comprehensive MQL5 Expert Advisor that incorporates many of the trading strategies you've mentioned. However, I need to be upfront about some important limitations:
Important Disclaimers:
No trading bot can guarantee a 90% win rate or consistent profitability
Market conditions are unpredictable and past performance doesn't guarantee future results
This code provides a framework that you'll need to test, optimize, and refine
Always backtest thoroughly before live trading
Here's a comprehensive MQL5 Expert Advisor that includes the strategies you requested:
mql5
//+------------------------------------------------------------------+
//| AdvancedTradingBot.mq5 |
//| Copyright 2025, Your Company |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, Your Company"
#property link "https://www.mql5.com"
#property version "1.00"
#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\AccountInfo.mqh>
//--- Input parameters
input group "=== Risk Management ==="
input double RiskPercent = 2.0; // Risk per trade (%)
input double MinRiskReward = 3.0; // Minimum Risk:Reward ratio
input int MaxPositions = 3; // Maximum concurrent positions
input group "=== Trading Settings ==="
input bool TradeForex = true; // Trade Forex pairs
input bool TradeGold = true; // Trade Gold (XAUUSD)
input bool EnableScalping = true; // Enable scalping (M1-M5)
input bool EnableDayTrading = true; // Enable day trading (M15-H1)
input group "=== Strategy Settings ==="
input bool UseBreakRetest = true; // Use Break and Retest
input bool UseTrendlines = true; // Use Trendline breaks
input bool UseSMC = true; // Use Smart Money Concepts
input bool UsePatterns = true; // Use Chart Patterns
input group "=== Technical Settings ==="
input int MA_Fast = 20; // Fast Moving Average
input int MA_Slow = 50; // Slow Moving Average
input int RSI_Period = 14; // RSI Period
input int ATR_Period = 14; // ATR Period for volatility
input group "=== Position Management ==="
input bool UseTrailingStop = true; // Use trailing stop
input double TrailingDistance = 20.0; // Trailing stop distance (pips)
input bool AddPositions = true; // Add positions in profit
input double AddPositionProfit = 50.0; // Profit level to add positions (pips)
//--- Global variables
CTrade trade;
CPositionInfo position;
CAccountInfo account;
datetime lastBarTime;
double accountBalance;
string tradedSymbols[] = {"EURUSD", "GBPUSD", "USDJPY", "USDCHF", "AUDUSD", "USDCAD", "NZDUSD", "XAUUSD"};
//--- Pattern recognition structures
struct SupportResistance {
double level;
datetime time;
int touches;
bool broken;
};
struct ChartPattern {
string type;
double entry;
double sl;
double tp;
datetime time;
bool valid;
};
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit() {
// Initialize trade object
trade.SetTypeFilling(ORDER_FILLING_FOK);
trade.SetDeviationInPoints(10);
// Get account balance
accountBalance = account.Balance();
// Set timer for position management
EventSetTimer(10);
Print("Advanced Trading Bot initialized successfully");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick() {
// Check for new bar
if (!IsNewBar()) return;
// Update account balance
accountBalance = account.Balance();
// Manage existing positions
ManagePositions();
// Check for new trading opportunities
CheckTradingOpportunities();
}
//+------------------------------------------------------------------+
//| Timer function for position management |
//+------------------------------------------------------------------+
void OnTimer() {
ManagePositions();
}
//+------------------------------------------------------------------+
//| Check if new bar opened |
//+------------------------------------------------------------------+
bool IsNewBar() {
datetime currentBarTime = iTime(_Symbol, PERIOD_CURRENT, 0);
if (currentBarTime != lastBarTime) {
lastBarTime = currentBarTime;
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| Check for trading opportunities |
//+------------------------------------------------------------------+
void CheckTradingOpportunities() {
// Loop through all symbols
for (int i = 0; i < ArraySize(tradedSymbols); i++) {
string symbol = tradedSymbols[i];
// Skip if symbol not available
if (!SymbolSelect(symbol, true)) continue;
// Check different timeframes based on strategy
if (EnableScalping) {
AnalyzeSymbol(symbol, PERIOD_M1);
AnalyzeSymbol(symbol, PERIOD_M5);
}
if (EnableDayTrading) {
AnalyzeSymbol(symbol, PERIOD_M15);
AnalyzeSymbol(symbol, PERIOD_H1);
}
}
}
//+------------------------------------------------------------------+
//| Analyze symbol for trading opportunities |
//+------------------------------------------------------------------+
void AnalyzeSymbol(string symbol, ENUM_TIMEFRAMES timeframe) {
// Check if we already have max positions
if (CountPositions() >= MaxPositions) return;
// Get market data
double high[], low[], open[], close[];
ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);
ArraySetAsSeries(open, true);
ArraySetAsSeries(close, true);
if (CopyHigh(symbol, timeframe, 0, 100, high) < 100) return;
if (CopyLow(symbol, timeframe, 0, 100, low) < 100) return;
if (CopyOpen(symbol, timeframe, 0, 100, open) < 100) return;
if (CopyClose(symbol, timeframe, 0, 100, close) < 100) return;
// Technical indicators
double ma_fast[], ma_slow[], rsi[], atr[];
ArraySetAsSeries(ma_fast, true);
ArraySetAsSeries(ma_slow, true);
ArraySetAsSeries(rsi, true);
ArraySetAsSeries(atr, true);
int ma_fast_handle = iMA(symbol, timeframe, MA_Fast, 0, MODE_EMA, PRICE_CLOSE);
int ma_slow_handle = iMA(symbol, timeframe, MA_Slow, 0, MODE_EMA, PRICE_CLOSE);
int rsi_handle = iRSI(symbol, timeframe, RSI_Period, PRICE_CLOSE);
int atr_handle = iATR(symbol, timeframe, ATR_Period);
if (CopyBuffer(ma_fast_handle, 0, 0, 10, ma_fast) < 10) return;
if (CopyBuffer(ma_slow_handle, 0, 0, 10, ma_slow) < 10) return;
if (CopyBuffer(rsi_handle, 0, 0, 10, rsi) < 10) return;
if (CopyBuffer(atr_handle, 0, 0, 10, atr) < 10) return;
// Strategy analysis
ChartPattern pattern = {0};
// 1. Break and Retest Strategy
if (UseBreakRetest) {
pattern = AnalyzeBreakRetest(symbol, timeframe, high, low, open, close);
if (pattern.valid) {
ExecuteTrade(symbol, pattern);
return;
}
}
// 2. Smart Money Concepts
if (UseSMC) {
pattern = AnalyzeSMC(symbol, timeframe, high, low, open, close, ma_fast, ma_slow);
if (pattern.valid) {
ExecuteTrade(symbol, pattern);
return;
}
}
// 3. Chart Patterns
if (UsePatterns) {
pattern = AnalyzeChartPatterns(symbol, timeframe, high, low, open, close);
if (pattern.valid) {
ExecuteTrade(symbol, pattern);
return;
}
}
// 4. Candlestick Patterns with confirmations
pattern = AnalyzeCandlestickPatterns(symbol, timeframe, high, low, open, close, ma_fast, ma_slow, rsi);
if (pattern.valid) {
ExecuteTrade(symbol, pattern);
}
}
//+------------------------------------------------------------------+
//| Analyze Break and Retest patterns |
//+------------------------------------------------------------------+
ChartPattern AnalyzeBreakRetest(string symbol, ENUM_TIMEFRAMES timeframe,
double &high[], double &low[], double &open[], double &close[]) {
ChartPattern pattern = {0};
// Find support/resistance levels
SupportResistance levels[];
FindSupportResistanceLevels(high, low, levels);
for (int i = 0; i < ArraySize(levels); i++) {
if (!levels[i].broken) continue;
// Check for retest
bool bullishRetest = false, bearishRetest = false;
// Look for retest in recent bars
for (int j = 1; j <= 5; j++) {
if (low[j] <= levels[i].level + SymbolInfoDouble(symbol, SYMBOL_POINT) * 10 &&
close[j] > levels[i].level) {
bullishRetest = true;
break;
}
if (high[j] >= levels[i].level - SymbolInfoDouble(symbol, SYMBOL_POINT) * 10 &&
close[j] < levels[i].level) {
bearishRetest = true;
break;
}
}
if (bullishRetest && HasBullishConfirmation(high, low, open, close)) {
pattern.type = "Break_Retest_Buy";
pattern.entry = SymbolInfoDouble(symbol, SYMBOL_ASK);
pattern.sl = levels[i].level - SymbolInfoDouble(symbol, SYMBOL_POINT) * 200;
pattern.tp = pattern.entry + (pattern.entry - pattern.sl) * MinRiskReward;
pattern.valid = true;
break;
}
if (bearishRetest && HasBearishConfirmation(high, low, open, close)) {
pattern.type = "Break_Retest_Sell";
pattern.entry = SymbolInfoDouble(symbol, SYMBOL_BID);
pattern.sl = levels[i].level + SymbolInfoDouble(symbol, SYMBOL_POINT) * 200;
pattern.tp = pattern.entry - (pattern.sl - pattern.entry) * MinRiskReward;
pattern.valid = true;
break;
}
}
return pattern;
}
//+------------------------------------------------------------------+
//| Analyze Smart Money Concepts |
//+------------------------------------------------------------------+
ChartPattern AnalyzeSMC(string symbol, ENUM_TIMEFRAMES timeframe,
double &high[], double &low[], double &open[], double &close[],
double &ma_fast[], double &ma_slow[]) {
ChartPattern pattern = {0};
// Look for Order Blocks (OB)
bool bullishOB = false, bearishOB = false;
// Identify order blocks (strong rejection candles)
for (int i = 2; i <= 10; i++) {
// Bullish Order Block
if (close[i] < open[i] && // Bearish candle
(open[i] - close[i]) > (high[i] - low[i]) * 0.7 && // Strong body
close[i-1] > open[i] && // Next candle closes above OB
ma_fast[0] > ma_slow[0]) { // Trend confirmation
bullishOB = true;
pattern.type = "SMC_Buy";
pattern.entry = SymbolInfoDouble(symbol, SYMBOL_ASK);
pattern.sl = low[i] - SymbolInfoDouble(symbol, SYMBOL_POINT) * 100;
pattern.tp = pattern.entry + (pattern.entry - pattern.sl) * MinRiskReward;
break;
}
// Bearish Order Block
if (close[i] > open[i] && // Bullish candle
(close[i] - open[i]) > (high[i] - low[i]) * 0.7 && // Strong body
close[i-1] < open[i] && // Next candle closes below OB
ma_fast[0] < ma_slow[0]) { // Trend confirmation
bearishOB = true;
pattern.type = "SMC_Sell";
pattern.entry = SymbolInfoDouble(symbol, SYMBOL_BID);
pattern.sl = high[i] + SymbolInfoDouble(symbol, SYMBOL_POINT) * 100;
pattern.tp = pattern.entry - (pattern.sl - pattern.entry) * MinRiskReward;
break;
}
}
// Validate with additional confirmations
if ((bullishOB && HasBullishConfirmation(high, low, open, close)) ||
(bearishOB && HasBearishConfirmation(high, low, open, close))) {
pattern.valid = true;
}
return pattern;
}
//+------------------------------------------------------------------+
//| Analyze Chart Patterns (Head & Shoulders, W/M patterns) |
//+------------------------------------------------------------------+
ChartPattern AnalyzeChartPatterns(string symbol, ENUM_TIMEFRAMES timeframe,
double &high[], double &low[], double &open[], double &close[]) {
ChartPattern pattern = {0};
// Head and Shoulders pattern detection
if (DetectHeadAndShoulders(high, low)) {
pattern.type = "Head_Shoulders_Sell";
pattern.entry = SymbolInfoDouble(symbol, SYMBOL_BID);
pattern.sl = high[ArrayMaximum(high, 5, 20)] + SymbolInfoDouble(symbol, SYMBOL_POINT) * 100;
pattern.tp = pattern.entry - (pattern.sl - pattern.entry) * MinRiskReward;
pattern.valid = true;
return pattern;
}
// Inverse Head and Shoulders
if (DetectInverseHeadAndShoulders(high, low)) {
pattern.type = "Inv_Head_Shoulders_Buy";
pattern.entry = SymbolInfoDouble(symbol, SYMBOL_ASK);
pattern.sl = low[ArrayMinimum(low, 5, 20)] - SymbolInfoDouble(symbol, SYMBOL_POINT) * 100;
pattern.tp = pattern.entry + (pattern.entry - pattern.sl) * MinRiskReward;
pattern.valid = true;
return pattern;
}
// Double Top (M pattern)
if (DetectDoubleTop(high, low)) {
pattern.type = "Double_Top_Sell";
pattern.entry = SymbolInfoDouble(symbol, SYMBOL_BID);
pattern.sl = high[ArrayMaximum(high, 5, 30)] + SymbolInfoDouble(symbol, SYMBOL_POINT) * 100;
pattern.tp = pattern.entry - (pattern.sl - pattern.entry) * MinRiskReward;
pattern.valid = true;
return pattern;
}
// Double Bottom (W pattern)
if (DetectDoubleBottom(high, low)) {
pattern.type = "Double_Bottom_Buy";
pattern.entry = SymbolInfoDouble(symbol, SYMBOL_ASK);
pattern.sl = low[ArrayMinimum(low, 5, 30)] - SymbolInfoDouble(symbol, SYMBOL_POINT) * 100;
pattern.tp = pattern.entry + (pattern.entry - pattern.sl) * MinRiskReward;
pattern.valid = true;
return pattern;
}
return pattern;
}
//+------------------------------------------------------------------+
//| Analyze Candlestick Patterns |
//+------------------------------------------------------------------+
ChartPattern AnalyzeCandlestickPatterns(string symbol, ENUM_TIMEFRAMES timeframe,
double &high[], double &low[], double &open[], double &close[],
double &ma_fast[], double &ma_slow[], double &rsi[]) {
ChartPattern pattern = {0};
// Engulfing patterns
if (IsBullishEngulfing(high, low, open, close) &&
ma_fast[0] > ma_slow[0] && rsi[0] > 30 && rsi[0] < 70) {
pattern.type = "Bullish_Engulfing";
pattern.entry = SymbolInfoDouble(symbol, SYMBOL_ASK);
pattern.sl = low[1] - SymbolInfoDouble(symbol, SYMBOL_POINT) * 100;
pattern.tp = pattern.entry + (pattern.entry - pattern.sl) * MinRiskReward;
pattern.valid = true;
return pattern;
}
if (IsBearishEngulfing(high, low, open, close) &&
ma_fast[0] < ma_slow[0] && rsi[0] > 30 && rsi[0] < 70) {
pattern.type = "Bearish_Engulfing";
pattern.entry = SymbolInfoDouble(symbol, SYMBOL_BID);
pattern.sl = high[1] + SymbolInfoDouble(symbol, SYMBOL_POINT) * 100;
pattern.tp = pattern.entry - (pattern.sl - pattern.entry) * MinRiskReward;
pattern.valid = true;
return pattern;
}
// Morning Star
if (IsMorningStar(high, low, open, close) && ma_fast[0] > ma_slow[0]) {
pattern.type = "Morning_Star";
pattern.entry = SymbolInfoDouble(symbol, SYMBOL_ASK);
pattern.sl = low[2] - SymbolInfoDouble(symbol, SYMBOL_POINT) * 150;
pattern.tp = pattern.entry + (pattern.entry - pattern.sl) * MinRiskReward;
pattern.valid = true;
return pattern;
}
// Evening Star
if (IsEveningStar(high, low, open, close) && ma_fast[0] < ma_slow[0]) {
pattern.type = "Evening_Star";
pattern.entry = SymbolInfoDouble(symbol, SYMBOL_BID);
pattern.sl = high[2] + SymbolInfoDouble(symbol, SYMBOL_POINT) * 150;
pattern.tp = pattern.entry - (pattern.sl - pattern.entry) * MinRiskReward;
pattern.valid = true;
return pattern;
}
// Wick Rejection
if (HasBullishWickRejection(high, low, open, close) && ma_fast[0] > ma_slow[0]) {
pattern.type = "Bullish_Wick_Rejection";
pattern.entry = SymbolInfoDouble(symbol, SYMBOL_ASK);
pattern.sl = low[0] - SymbolInfoDouble(symbol, SYMBOL_POINT) * 100;
pattern.tp = pattern.entry + (pattern.entry - pattern.sl) * MinRiskReward;
pattern.valid = true;
return pattern;
}
if (HasBearishWickRejection(high, low, open, close) && ma_fast[0] < ma_slow[0]) {
pattern.type = "Bearish_Wick_Rejection";
pattern.entry = SymbolInfoDouble(symbol, SYMBOL_BID);
pattern.sl = high[0] + SymbolInfoDouble(symbol, SYMBOL_POINT) * 100;
pattern.tp = pattern.entry - (pattern.sl - pattern.entry) * MinRiskReward;
pattern.valid = true;
return pattern;
}
return pattern;
}
//+------------------------------------------------------------------+
//| Execute trade based on pattern |
//+------------------------------------------------------------------+
void ExecuteTrade(string symbol, ChartPattern &pattern) {
if (!pattern.valid) return;
// Calculate position size based on risk percentage
double lotSize = CalculateLotSize(symbol, pattern.entry, pattern.sl);
if (lotSize <= 0) return;
// Determine trade type
bool isBuy = (StringFind(pattern.type, "Buy") >= 0 ||
StringFind(pattern.type, "Bullish") >= 0 ||
StringFind(pattern.type, "Morning") >= 0);
// Execute trade
bool result = false;
if (isBuy) {
result = trade.Buy(lotSize, symbol, pattern.entry, pattern.sl, pattern.tp,
"Bot: " + pattern.type);
} else {
result = trade.Sell(lotSize, symbol, pattern.entry, pattern.sl, pattern.tp,
"Bot: " + pattern.type);
}
if (result) {
Print("Trade executed: ", pattern.type, " on ", symbol,
" Lot: ", lotSize, " Entry: ", pattern.entry,
" SL: ", pattern.sl, " TP: ", pattern.tp);
} else {
Print("Trade failed: ", trade.ResultRetcode(), " - ", trade.ResultRetcodeDescription());
}
}
//+------------------------------------------------------------------+
//| Calculate lot size based on risk percentage |
//+------------------------------------------------------------------+
double CalculateLotSize(string symbol, double entry, double stopLoss) {
double riskAmount = accountBalance * RiskPercent / 100.0;
double pipValue = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_VALUE);
double stopLossPips = MathAbs(entry - stopLoss) / SymbolInfoDouble(symbol, SYMBOL_POINT);
double lotSize = riskAmount / (stopLossPips * pipValue);
// Normalize lot size
double minLot = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MAX);
double lotStep = SymbolInfoDouble(symbol, SYMBOL_VOLUME_STEP);
lotSize = MathMax(minLot, MathMin(maxLot, MathRound(lotSize / lotStep) * lotStep));
return lotSize;
}
//+------------------------------------------------------------------+
//| Manage existing positions |
//+------------------------------------------------------------------+
void ManagePositions() {
for (int i = PositionsTotal() - 1; i >= 0; i--) {
if (!position.SelectByIndex(i)) continue;
if (position.Magic() != trade.RequestMagic()) continue;
string symbol = position.Symbol();
double currentPrice = (position.PositionType() == POSITION_TYPE_BUY) ?
SymbolInfoDouble(symbol, SYMBOL_BID) :
SymbolInfoDouble(symbol, SYMBOL_ASK);
// Trailing stop
if (UseTrailingStop) {
UpdateTrailingStop(symbol, currentPrice);
}
// Add positions in profit
if (AddPositions) {
CheckAddPosition(symbol, currentPrice);
}
}
}
//+------------------------------------------------------------------+
//| Update trailing stop |
//+------------------------------------------------------------------+
void UpdateTrailingStop(string symbol, double currentPrice) {
double newSL = 0;
double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
double trailDistance = TrailingDistance * point;
if (position.PositionType() == POSITION_TYPE_BUY) {
newSL = currentPrice - trailDistance;
if (newSL > position.StopLoss() && newSL < currentPrice) {
trade.PositionModify(position.Ticket(), newSL, position.TakeProfit());
}
} else {
newSL = currentPrice + trailDistance;
if ((newSL < position.StopLoss() || position.StopLoss() == 0) && newSL > currentPrice) {
trade.PositionModify(position.Ticket(), newSL, position.TakeProfit());
}
}
}
//+------------------------------------------------------------------+
//| Check if we should add to position |
//+------------------------------------------------------------------+
void CheckAddPosition(string symbol, double currentPrice) {
double profit = position.Profit();
double profitPips = 0;
if (position.PositionType() == POSITION_TYPE_BUY) {
profitPips = (currentPrice - position.PriceOpen()) / SymbolInfoDouble(symbol, SYMBOL_POINT);
} else {
profitPips = (position.PriceOpen() - currentPrice) / SymbolInfoDouble(symbol, SYMBOL_POINT);
}
if (profitPips >= AddPositionProfit && CountPositionsSymbol(symbol) < 3) {
// Add smaller position
double addLotSize = position.Volume() * 0.5;
if (position.PositionType() == POSITION_TYPE_BUY) {
trade.Buy(addLotSize, symbol, 0, position.StopLoss(), position.TakeProfit(),
"Bot: Add Buy");
} else {
trade.Sell(addLotSize, symbol, 0, position.StopLoss(), position.TakeProfit(),
"Bot: Add Sell");
}
}
}
//+------------------------------------------------------------------+
//| Count total positions |
//+------------------------------------------------------------------+
int CountPositions() {
int count = 0;
for (int i = 0; i < PositionsTotal(); i++) {
if (position.SelectByIndex(i) && position.Magic() == trade.RequestMagic()) {
count++;
}
}
return count;
}
//+------------------------------------------------------------------+
//| Count positions for specific symbol |
//+------------------------------------------------------------------+
int CountPositionsSymbol(string symbol) {
int count = 0;
for (int i = 0; i < PositionsTotal(); i++) {
if (position.SelectByIndex(i) &&
position.Magic() == trade.RequestMagic() &&
position.Symbol() == symbol) {
count++;
}
}
return count;
}
//--- Pattern Detection Functions ---
//+------------------------------------------------------------------+
//| Find Support and Resistance levels |
//+------------------------------------------------------------------+
void FindSupportResistanceLevels(double &high[], double &low[], SupportResistance &levels[]) {
ArrayResize(levels, 0);
for (int i = 10; i < 90; i++) {
bool isResistance = true, isSupport = true;
// Check if it's a resistance level
for (int j = i - 5; j <= i + 5; j++) {
if (j != i && high[j] >= high[i]) {
isResistance = false;
break;
}
}
// Check if it's a support level
for (int j = i - 5; j <= i + 5; j++) {
if (j != i && low[j] <= low[i]) {
isSupport = false;
break;
}
}
if (isResistance) {
int size = ArraySize(levels);
ArrayResize(levels, size + 1);
levels[size].level = high[i];
levels[size].time = iTime(_Symbol, PERIOD_CURRENT, i);
levels[size].touches = 1;
levels[size].broken = false;
}
if (isSupport) {
int size = ArraySize(levels);
ArrayResize(levels, size + 1);
levels[size].level = low[i];
levels[size].time = iTime(_Symbol, PERIOD_CURRENT, i);
levels[size].touches = 1;
levels[size].broken = false;
}
}
}
//+------------------------------------------------------------------+
//| Detect Head and Shoulders pattern |
//+------------------------------------------------------------------+
bool DetectHeadAndShoulders(double &high[], double &low[]) {
// Look for three peaks with middle one being highest
for (int i = 10; i < 40; i++) {
if (high[i+10] > high[i+5] && high[i+10] > high[i+15] && // Left shoulder
high[i] > high[i+10] && high[i] > high[i-10] && // Head
high[i-10] > high[i-5] && high[i-10] > high[i-15]) { // Right shoulder
// Check if shoulders are roughly equal
if (MathAbs(high[i+10] - high[i-10]) < (high[i] - high[i+10]) * 0.3) {
return true;
}
}
}
return false;
}
//+------------------------------------------------------------------+
//| Detect Inverse Head and Shoulders pattern |
//+------------------------------------------------------------------+
bool DetectInverseHeadAndShoulders(double &high[], double &low[]) {
// Look for three valleys with middle one being lowest
for (int i = 10; i < 40; i++) {
if (low[i+10] < low[i+5] && low[i+10] < low[i+15] && // Left shoulder
low[i] < low[i+10] && low[i] < low[i-10] && // Head
low[i-10] < low[i-5] && low[i-10] < low[i-15]) { // Right shoulder
// Check if shoulders are roughly equal
if (MathAbs(low[i+10] - low[i-10]) < (low[i+10] - low[i]) * 0.3) {
return true;
}
}
}
return false;
}
//+------------------------------------------------------------------+
//| Detect Double Top pattern |
//+------------------------------------------------------------------+
bool DetectDoubleTop(double &high[], double &low[]) {
for (int i = 5; i < 25; i++) {
for (int j = i + 10; j < 45; j++) {
if (MathAbs(high[i] - high[j]) < (high[i] * 0.01) && // Peaks are similar
high[i] > high[(i+j)/2] && high[j] > high[(i+j)/2]) { // Valley between peaks
return true;
}
}
}
return false;
}
//+------------------------------------------------------------------+
//| Detect Double Bottom pattern |
//+------------------------------------------------------------------+
bool DetectDoubleBottom(double &high[], double &low[]) {
for (int i = 5; i < 25; i++) {
for (int j = i + 10; j < 45; j++) {
if (MathAbs(low[i] - low[j]) < (low[i] * 0.01) && // Bottoms are similar
low[i] < low[(i+j)/2] && low[j] < low[(i+j)/2]) { // Peak between bottoms
return true;
}
}
}
return false;
}
//+------------------------------------------------------------------+
//| Check for bullish engulfing pattern |
//+------------------------------------------------------------------+
bool IsBullishEngulfing(double &high[], double &low[], double &open[], double &close[]) {
return (close[1] < open[1] && // Previous candle bearish
close[0] > open[0] && // Current candle bullish
open[0] < close[1] && // Current open below previous close
close[0] > open[1]); // Current close above previous open
}
//+------------------------------------------------------------------+
//| Check for bearish engulfing pattern |
//+------------------------------------------------------------------+
bool IsBearishEngulfing(double &high[], double &low[], double &open[], double &close[]) {
return (close[1] > open[1] && // Previous candle bullish
close[0] < open[0] && // Current candle bearish
open[0] > close[1] && // Current open above previous close
close[0] < open[1]); // Current close below previous open
}
//+------------------------------------------------------------------+
//| Check for Morning Star pattern |
//+------------------------------------------------------------------+
bool IsMorningStar(double &high[], double &low[], double &open[], double &close[]) {
return (close[2] < open[2] && // First candle bearish
MathAbs(close[1] - open[1]) < MathAbs(close[2] - open[2]) * 0.3 && // Small middle candle
close[0] > open[0] && // Third candle bullish
close[0] > (open[2] + close[2]) / 2); // Third candle closes above midpoint of first
}
//+------------------------------------------------------------------+
//| Check for Evening Star pattern |
//+------------------------------------------------------------------+
bool IsEveningStar(double &high[], double &low[], double &open[], double &close[]) {
return (close[2] > open[2] && // First candle bullish
MathAbs(close[1] - open[1]) < MathAbs(close[2] - open[2]) * 0.3 && // Small middle candle
close[0] < open[0] && // Third candle bearish
close[0] < (open[2] + close[2]) / 2); // Third candle closes below midpoint of first
}
//+------------------------------------------------------------------+
//| Check for bullish wick rejection |
//+------------------------------------------------------------------+
bool HasBullishWickRejection(double &high[], double &low[], double &open[], double &close[]) {
double bodySize = MathAbs(close[0] - open[0]);
double lowerWick = MathMin(open[0], close[0]) - low[0];
double upperWick = high[0] - MathMax(open[0], close[0]);
return (lowerWick > bodySize * 2 && // Long lower wick
upperWick < bodySize * 0.5 && // Small upper wick
close[0] > open[0]); // Bullish candle
}
//+------------------------------------------------------------------+
//| Check for bearish wick rejection |
//+------------------------------------------------------------------+
bool HasBearishWickRejection(double &high[], double &low[], double &open[], double &close[]) {
double bodySize = MathAbs(close[0] - open[0]);
double lowerWick = MathMin(open[0], close[0]) - low[0];
double upperWick = high[0] - MathMax(open[0], close[0]);
return (upperWick > bodySize * 2 && // Long upper wick
lowerWick < bodySize * 0.5 && // Small lower wick
close[0] < open[0]); // Bearish candle
}
//+------------------------------------------------------------------+
//| Check for bullish confirmation |
//+------------------------------------------------------------------+
bool HasBullishConfirmation(double &high[], double &low[], double &open[], double &close[]) {
return (close[0] > open[0] && // Current candle bullish
close[0] > close[1] && // Higher close
low[0] > low[1]); // Higher low
}
//+------------------------------------------------------------------+
//| Check for bearish confirmation |
//+------------------------------------------------------------------+
bool HasBearishConfirmation(double &high[], double &low[], double &open[], double &close[]) {
return (close[0] < open[0] && // Current candle bearish
close[0] < close[1] && // Lower close
high[0] < high[1]); // Lower high
}
Key Features Implemented:
Risk Management: 2% risk per trade with proper lot sizing