//+------------------------------------------------------------------+
//| Combined 2ES & 2EL Trading System.mq5 |
//| |
//+------------------------------------------------------------------+
#property copyright "2025"
#property link ""
#property version "1.00"
#property strict
#property indicator_chart_window
// Mode selection
enum SYSTEM_MODE {
MODE_ES, // Entry Short (2ES) Only
MODE_EL, // Entry Long (2EL) Only
MODE_BOTH // Both Systems (2ES & 2EL)
};
// Input parameters
input SYSTEM_MODE SystemMode = MODE_BOTH; // Trading System Mode
input int EmaPeriod = 21; // EMA Period
input bool EnableTradingSignals = true; // Enable BUY/SELL STOP signals
input bool ShowRiskManagement = true; // Show Stop Loss and Take Profit lines
input bool EnableRealTimeSignals = true; // Enable real-time signal detection
// Enum for tracking the current state
enum SystemState {
STATE_LOOKING_FOR_BAR0,
STATE_LOOKING_FOR_PB1,
STATE_LOOKING_FOR_1E, // Generic 1E (1ES or 1EL)
STATE_LOOKING_FOR_PB2,
STATE_LOOKING_FOR_2E, // Generic 2E (2ES or 2EL)
STATE_FOUND_2E, // Generic Found (2ES or 2EL)
STATE_AFTER_2EF // Generic After Failure (2ESF or 2ELF)
};
// Custom chart colors
color backgroundColor = clrWhite;
color foregroundColor = clrBlack;
color gridColor = C'241,236,242';
color barUpColor = C'38,166,154';
color barDownColor = C'239,83,80';
color bullCandleColor = C'38,166,154';
color bearCandleColor = C'239,83,80';
color lineChartColor = C'86,186,132';
color volumesColor = C'38,166,154';
color bidLineColor = C'38,166,154';
color askLineColor = C'239,83,80';
color lastPriceColor = C'156,186,240';
color stopLevelsColor = C'239,83,80';
// Signal colors
color buySignalColor = clrLime;
color sellSignalColor = clrRed;
color sellStopColor = clrRed;
color stopLossColor = clrOrangeRed;
color takeProfitColor = clrDodgerBlue;
// EMA settings
int emaHandle;
color emaColor = clrBlue;
double emaBuffer[];
// 2ES (Entry Short) global variables
SystemState ES_currentState = STATE_LOOKING_FOR_BAR0;
datetime ES_bar0Time;
double ES_bar0Low;
datetime ES_pb1Time;
double ES_pb1High, ES_pb1Low;
datetime ES_es1Time;
double ES_es1High, ES_es1Low;
datetime ES_pb2Time;
double ES_pb2High, ES_pb2Low;
datetime ES_es2Time;
double ES_es2High, ES_es2Low;
int ES_pb1Count = 0;
int ES_pb2Count = 0;
int ES_currentSetupID = 0;
bool ES_isCompletedSetup = false;
string ES_objectPrefix = "2ES_";
// Real-time signal tracking for 2ES
bool ES_realTimeSignalGenerated = false;
bool ES_currentBarProcessed = false;
// Signal bar variables for 2ES
datetime ES_signalBarTime;
double ES_signalBarHigh, ES_signalBarLow;
double ES_signalBarOpen, ES_signalBarClose; // Added for candle confirmation
// 2EL (Entry Long) global variables
SystemState EL_currentState = STATE_LOOKING_FOR_BAR0;
datetime EL_bar0Time;
double EL_bar0High;
datetime EL_pb1Time;
double EL_pb1High, EL_pb1Low;
datetime EL_el1Time;
double EL_el1High, EL_el1Low;
datetime EL_pb2Time;
double EL_pb2High, EL_pb2Low;
datetime EL_el2Time;
double EL_el2High, EL_el2Low;
int EL_pb1Count = 0;
int EL_pb2Count = 0;
int EL_currentSetupID = 0;
bool EL_isCompletedSetup = false;
string EL_objectPrefix = "2EL_";
// Real-time signal tracking for 2EL
bool EL_realTimeSignalGenerated = false;
bool EL_currentBarProcessed = false;
// Signal bar variables for 2EL
datetime EL_signalBarTime;
double EL_signalBarHigh, EL_signalBarLow;
double EL_signalBarOpen, EL_signalBarClose; // Added for candle confirmation
// Color schemes for each system
// 2ES colors - Red theme (bearish)
color ES_bar0Color = C'239,83,80'; // Bar0 - RED for 2ES
color ES_pb1Color = C'156,186,240'; // PB1 - Less prominent blue
color ES_es1Color = C'239,83,80'; // 1ES - Standard red
color ES_pb2Color = C'156,186,240'; // PB2 - Less prominent blue
color ES_es2Color = C'180,0,0'; // 2ES - Brighter red for importance
color ES_esfColor = C'0,255,0'; // 2ESF - Bright green for failures
color ES_signalColor = clrMagenta; // Signal bar color for 2ES
// 2EL colors - Green theme (bullish)
color EL_bar0Color = C'239,83,80'; // Bar0 - Red
color EL_pb1Color = C'156,186,240'; // PB1 - Less prominent blue
color EL_el1Color = C'38,166,154'; // 1EL - Standard green
color EL_pb2Color = C'156,186,240'; // PB2 - Less prominent blue
color EL_el2Color = C'0,180,0'; // 2EL - Brighter green for importance
color EL_elfColor = C'255,0,0'; // 2ELF - Bright red for failures
color EL_signalColor = clrMagenta; // Signal bar color for 2EL
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit() {
// Clear ALL previous objects from the chart
ObjectsDeleteAll(0);
// Initialize EMA indicator
emaHandle = iMA(_Symbol, PERIOD_CURRENT, EmaPeriod, 0, MODE_EMA, PRICE_CLOSE);
if(emaHandle == INVALID_HANDLE) {
Print("Failed to create EMA indicator");
return(INIT_FAILED);
}
// Allocate buffer for EMA values
ArraySetAsSeries(emaBuffer, true);
// Set up initial states
ES_currentState = STATE_LOOKING_FOR_BAR0;
EL_currentState = STATE_LOOKING_FOR_BAR0;
// Apply custom chart colors
ApplyChartColors();
// Create combined dashboard for displaying information
CreateCombinedDashboard();
// Start with new setup IDs
ES_currentSetupID = 1;
ES_isCompletedSetup = false;
EL_currentSetupID = 1;
EL_isCompletedSetup = false;
// Reset real-time signal flags
ES_realTimeSignalGenerated = false;
EL_realTimeSignalGenerated = false;
ES_currentBarProcessed = false;
EL_currentBarProcessed = false;
// Draw initial EMA line
DrawEMALine();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
// Release the EMA indicator handle
if(emaHandle != INVALID_HANDLE) {
IndicatorRelease(emaHandle);
}
// Clean up all objects when EA is removed
ObjectsDeleteAll(0, ES_objectPrefix);
ObjectsDeleteAll(0, EL_objectPrefix);
ObjectDelete(0, "Combined_Dashboard");
ObjectDelete(0, "Dashboard_Divider");
ObjectDelete(0, "ES_Title");
ObjectDelete(0, "EL_Title");
ObjectDelete(0, "EMA_Info");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick() {
// Check if a new bar has formed
bool newBar = isNewBar();
if(newBar) {
// Reset real-time signal flags at the start of a new bar
ES_realTimeSignalGenerated = false;
EL_realTimeSignalGenerated = false;
ES_currentBarProcessed = false;
EL_currentBarProcessed = false;
// Update EMA on new bar
DrawEMALine();
// Process completed bar for each system based on selected mode
if(SystemMode == MODE_ES || SystemMode == MODE_BOTH) {
ProcessBarES();
UpdateDashboardES();
}
if(SystemMode == MODE_EL || SystemMode == MODE_BOTH) {
ProcessBarEL();
UpdateDashboardEL();
}
}
// Real-time pattern detection
if(EnableRealTimeSignals) {
// Get current price and time
double currentBid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double currentAsk = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
datetime currentTime = TimeCurrent();
// Process real-time signal for 2ES (Entry Short) system
if((SystemMode == MODE_ES || SystemMode == MODE_BOTH) &&
ES_currentState == STATE_LOOKING_FOR_PB2 &&
ES_pb2Count > 0 &&
!ES_realTimeSignalGenerated &&
!ES_currentBarProcessed) {
// Check if current bid price has moved below PB2 low
if(currentBid < ES_pb2Low) {
// Signal a real-time 2ES pattern detection
ProcessRealTime2ES(currentTime, currentBid);
ES_realTimeSignalGenerated = true;
}
}
// Process real-time signal for 2EL (Entry Long) system
if((SystemMode == MODE_EL || SystemMode == MODE_BOTH) &&
EL_currentState == STATE_LOOKING_FOR_PB2 &&
EL_pb2Count > 0 &&
!EL_realTimeSignalGenerated &&
!EL_currentBarProcessed) {
// Check if current ask price has moved above PB2 high
if(currentAsk > EL_pb2High) {
// Signal a real-time 2EL pattern detection
ProcessRealTime2EL(currentTime, currentAsk);
EL_realTimeSignalGenerated = true;
}
}
}
}
//+------------------------------------------------------------------+
//| Process real-time 2ES signal when price crosses below PB2 low |
//+------------------------------------------------------------------+
void ProcessRealTime2ES(datetime time, double price) {
// Get open price of current forming bar (to check if it's bearish)
double currentOpen = iOpen(_Symbol, PERIOD_CURRENT, 0);
// Get previous completed bar data for signal bar information
ES_signalBarTime = iTime(_Symbol, PERIOD_CURRENT, 1);
ES_signalBarHigh = iHigh(_Symbol, PERIOD_CURRENT, 1);
ES_signalBarLow = iLow(_Symbol, PERIOD_CURRENT, 1);
ES_signalBarOpen = iOpen(_Symbol, PERIOD_CURRENT, 1);
ES_signalBarClose = iClose(_Symbol, PERIOD_CURRENT, 1);
// Check if signal bar is bearish (close < open)
bool isValidSignalBar = (ES_signalBarClose < ES_signalBarOpen);
// Mark signal bar on chart
MarkSignalBarES(ES_signalBarTime, ES_signalBarHigh, ES_signalBarLow);
// Update state
ES_es2Time = time;
ES_es2High = price; // Using current price as initial high
ES_es2Low = price; // Using current price as initial low
ES_currentState = STATE_FOUND_2E;
ES_isCompletedSetup = true;
// First clear all other objects, keeping only dashboard
ClearAllExceptDashboardES();
// Mark 2ES on chart - use current price for both
MarkBarOnChartES(ES_es2Time, price, ES_es2Color, "2ES", 1);
// Add SELL STOP order visualization and risk management
if(EnableTradingSignals) {
double sellStopLevel = ES_pb2Low - _Point; // Just below PB2 low
// Create the sell stop visualization
CreateSellStopSignal(ES_es2Time, sellStopLevel, ES_pb2Low, isValidSignalBar);
// Add take profit visualization if risk management is enabled
if(ShowRiskManagement) {
// Take profit at Bar0 low
double takeProfitLevel = ES_bar0Low;
CreateTakeProfitLine(ES_es2Time, takeProfitLevel, "TP-2ES", sellStopColor);
// Risk to reward calculation
double rr = (sellStopLevel - takeProfitLevel) / (ES_signalBarHigh - sellStopLevel);
// Display risk:reward ratio
string rrText = "R:R = 1:" + DoubleToString(rr, 2);
DisplayRiskRewardRatio(ES_es2Time, sellStopLevel - 50*_Point, rrText, sellSignalColor);
}
}
// Update dashboard
UpdateDashboardES();
// Alert about real-time detection
Alert("REAL-TIME 2ES Pattern Detected! SELL STOP @ ", DoubleToString(ES_pb2Low - _Point, _Digits));
}
//+------------------------------------------------------------------+
//| Process real-time 2EL signal when price crosses above PB2 high |
//+------------------------------------------------------------------+
void ProcessRealTime2EL(datetime time, double price) {
// Get open price of current forming bar (to check if it's bullish)
double currentOpen = iOpen(_Symbol, PERIOD_CURRENT, 0);
// Get previous completed bar data for signal bar information
EL_signalBarTime = iTime(_Symbol, PERIOD_CURRENT, 1);
EL_signalBarHigh = iHigh(_Symbol, PERIOD_CURRENT, 1);
EL_signalBarLow = iLow(_Symbol, PERIOD_CURRENT, 1);
EL_signalBarOpen = iOpen(_Symbol, PERIOD_CURRENT, 1);
EL_signalBarClose = iClose(_Symbol, PERIOD_CURRENT, 1);
// Check if signal bar is bullish (close > open)
bool isValidSignalBar = (EL_signalBarClose > EL_signalBarOpen);
// Mark signal bar on chart
MarkSignalBarEL(EL_signalBarTime, EL_signalBarHigh, EL_signalBarLow);
// Update state
EL_el2Time = time;
EL_el2High = price; // Using current price as initial high
EL_el2Low = price; // Using current price as initial low
EL_currentState = STATE_FOUND_2E;
EL_isCompletedSetup = true;
// First clear all other objects, keeping only dashboard
ClearAllExceptDashboardEL();
// Mark 2EL on chart - use current price for both
MarkBarOnChartEL(EL_el2Time, price, EL_el2Color, "2EL", 1);
// Add BUY STOP order visualization and risk management
if(EnableTradingSignals) {
double buyStopLevel = EL_pb2High + _Point; // Just above PB2 high
// Create the buy stop visualization
CreateBuyStopSignal(EL_el2Time, buyStopLevel, EL_pb2High, isValidSignalBar);
// Add take profit visualization if risk management is enabled
if(ShowRiskManagement) {
// Take profit at Bar0 high
double takeProfitLevel = EL_bar0High;
CreateTakeProfitLine(EL_el2Time, takeProfitLevel, "TP-2EL", buySignalColor);
// Risk to reward calculation
double rr = (takeProfitLevel - buyStopLevel) / (buyStopLevel - EL_signalBarLow);
// Display risk:reward ratio
string rrText = "R:R = 1:" + DoubleToString(rr, 2);
DisplayRiskRewardRatio(EL_el2Time, buyStopLevel + 50*_Point, rrText, buySignalColor);
}
}
// Update dashboard
UpdateDashboardEL();
// Alert about real-time detection
Alert("REAL-TIME 2EL Pattern Detected! BUY STOP @ ", DoubleToString(EL_pb2High + _Point, _Digits));
}
//+------------------------------------------------------------------+
//| Draw EMA line on the chart |
//+------------------------------------------------------------------+
void DrawEMALine() {
// Get enough EMA values to draw a line across visible chart
long bars = ChartGetInteger(0, CHART_VISIBLE_BARS);
// Ensure we have at least a few bars
if(bars < 2) bars = 100;
// Copy EMA values - explicitly cast long to int for CopyBuffer
if(CopyBuffer(emaHandle, 0, 0, (int)bars, emaBuffer) < bars) {
Print("Failed to copy EMA buffer data");
return;
}
// Create or update EMA line
string emaName = "Combined_EMA_Line";
// Remove any existing EMA line
ObjectDelete(0, emaName);
// Draw the EMA line as individual trend line segments
for(long i = 1; i < bars; i++) {
string segmentName = emaName + "_" + IntegerToString(i);
datetime time1 = iTime(_Symbol, PERIOD_CURRENT, (int)i);
datetime time2 = iTime(_Symbol, PERIOD_CURRENT, (int)(i-1));
ObjectCreate(0, segmentName, OBJ_TREND, 0, time1, emaBuffer[i], time2, emaBuffer[i-1]);
ObjectSetInteger(0, segmentName, OBJPROP_COLOR, emaColor);
ObjectSetInteger(0, segmentName, OBJPROP_WIDTH, 2);
ObjectSetInteger(0, segmentName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, segmentName, OBJPROP_RAY_RIGHT, false);
ObjectSetInteger(0, segmentName, OBJPROP_BACK, false);
ObjectSetInteger(0, segmentName, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, segmentName, OBJPROP_SELECTED, false);
ObjectSetInteger(0, segmentName, OBJPROP_HIDDEN, true);
}
// Force chart redraw
ChartRedraw(0);
}
//+------------------------------------------------------------------+
//| Apply custom color scheme to the chart |
//+------------------------------------------------------------------+
void ApplyChartColors() {
// Set chart colors for both real-time and strategy tester
ChartSetInteger(0, CHART_COLOR_BACKGROUND, backgroundColor);
ChartSetInteger(0, CHART_COLOR_FOREGROUND, foregroundColor);
ChartSetInteger(0, CHART_COLOR_GRID, gridColor);
ChartSetInteger(0, CHART_COLOR_CHART_UP, barUpColor);
ChartSetInteger(0, CHART_COLOR_CHART_DOWN, barDownColor);
ChartSetInteger(0, CHART_COLOR_CANDLE_BULL, bullCandleColor);
ChartSetInteger(0, CHART_COLOR_CANDLE_BEAR, bearCandleColor);
ChartSetInteger(0, CHART_COLOR_CHART_LINE, lineChartColor);
ChartSetInteger(0, CHART_COLOR_VOLUME, volumesColor);
ChartSetInteger(0, CHART_COLOR_BID, bidLineColor);
ChartSetInteger(0, CHART_COLOR_ASK, askLineColor);
ChartSetInteger(0, CHART_COLOR_LAST, lastPriceColor);
ChartSetInteger(0, CHART_COLOR_STOP_LEVEL, stopLevelsColor);
// Force chart redraw
ChartRedraw(0);
}
//+------------------------------------------------------------------+
//| Check if we have a new bar |
//+------------------------------------------------------------------+
bool isNewBar() {
static datetime lastBarTime = 0;
datetime currentBarTime = iTime(_Symbol, PERIOD_CURRENT, 0);
if(lastBarTime != currentBarTime) {
lastBarTime = currentBarTime;
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| Create unified dashboard with both systems |
//+------------------------------------------------------------------+
void CreateCombinedDashboard() {
int dashboardWidth = 560; // Twice the original width
int dashboardHeight = 190;
int xPosition = 10;
int yPosition = 10;
int dividerX = xPosition + dashboardWidth/2; // Middle of dashboard
// Create a single large dashboard
string dashName = "Combined_Dashboard";
ObjectCreate(0, dashName, OBJ_RECTANGLE_LABEL, 0, 0, 0);
ObjectSetInteger(0, dashName, OBJPROP_XDISTANCE, xPosition);
ObjectSetInteger(0, dashName, OBJPROP_YDISTANCE, yPosition);
ObjectSetInteger(0, dashName, OBJPROP_XSIZE, dashboardWidth);
ObjectSetInteger(0, dashName, OBJPROP_YSIZE, dashboardHeight);
ObjectSetInteger(0, dashName, OBJPROP_BGCOLOR, backgroundColor);
ObjectSetInteger(0, dashName, OBJPROP_BORDER_TYPE, BORDER_FLAT);
ObjectSetInteger(0, dashName, OBJPROP_COLOR, foregroundColor);
ObjectSetInteger(0, dashName, OBJPROP_WIDTH, 2);
// Create visual divider
string dividerName = "Dashboard_Divider";
// Can't use VLINE here as it spans the whole chart, so we'll create a vertical line
ObjectCreate(0, dividerName, OBJ_TREND, 0, 0, 0);
ObjectSetInteger(0, dividerName, OBJPROP_COLOR, gridColor);
ObjectSetInteger(0, dividerName, OBJPROP_STYLE, STYLE_DOT);
ObjectSetInteger(0, dividerName, OBJPROP_WIDTH, 1);
ObjectSetInteger(0, dividerName, OBJPROP_BACK, true);
// Set divider coordinates
datetime time = TimeCurrent(); // Use current time
ObjectSetInteger(0, dividerName, OBJPROP_XDISTANCE, dividerX);
ObjectSetInteger(0, dividerName, OBJPROP_YDISTANCE, yPosition);
// Make a vertical line by setting same X position for both points
ObjectSetInteger(0, dividerName, OBJPROP_XDISTANCE, dividerX);
ObjectSetInteger(0, dividerName, OBJPROP_YDISTANCE, yPosition + dashboardHeight);
// Create section headers
// 2ES Title (left side)
string esTitle = "ES_Title";
ObjectCreate(0, esTitle, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, esTitle, OBJPROP_XDISTANCE, xPosition + 10);
ObjectSetInteger(0, esTitle, OBJPROP_YDISTANCE, yPosition + 15);
ObjectSetInteger(0, esTitle, OBJPROP_COLOR, ES_es2Color); // Use strong ES color
ObjectSetInteger(0, esTitle, OBJPROP_FONTSIZE, 12);
ObjectSetString(0, esTitle, OBJPROP_TEXT, "2ES Trading System");
// 2EL Title (right side)
string elTitle = "EL_Title";
ObjectCreate(0, elTitle, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, elTitle, OBJPROP_XDISTANCE, dividerX + 10);
ObjectSetInteger(0, elTitle, OBJPROP_YDISTANCE, yPosition + 15);
ObjectSetInteger(0, elTitle, OBJPROP_COLOR, EL_el2Color); // Use strong EL color
ObjectSetInteger(0, elTitle, OBJPROP_FONTSIZE, 12);
ObjectSetString(0, elTitle, OBJPROP_TEXT, "2EL Trading System");
// Create EMA info (centered at top)
string emaInfoName = "EMA_Info";
ObjectCreate(0, emaInfoName, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, emaInfoName, OBJPROP_XDISTANCE, xPosition + dashboardWidth/2 - 40);
ObjectSetInteger(0, emaInfoName, OBJPROP_YDISTANCE, yPosition + 15);
ObjectSetInteger(0, emaInfoName, OBJPROP_COLOR, emaColor);
ObjectSetInteger(0, emaInfoName, OBJPROP_FONTSIZE, 10);
ObjectSetString(0, emaInfoName, OBJPROP_TEXT, "EMA(" + IntegerToString(EmaPeriod) + ")");
// Create ES info elements (left side)
CreateESInfoElements(xPosition + 10, yPosition + 35);
// Create EL info elements (right side)
CreateELInfoElements(dividerX + 10, yPosition + 35);
}
//+------------------------------------------------------------------+
//| Create 2ES information elements within dashboard |
//+------------------------------------------------------------------+
void CreateESInfoElements(int x, int y) {
// Create state label
string stateName = ES_objectPrefix + "State";
ObjectCreate(0, stateName, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, stateName, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, stateName, OBJPROP_YDISTANCE, y);
ObjectSetInteger(0, stateName, OBJPROP_COLOR, foregroundColor);
ObjectSetInteger(0, stateName, OBJPROP_FONTSIZE, 10);
// Create Bar0 info
string bar0Name = ES_objectPrefix + "Bar0Info";
ObjectCreate(0, bar0Name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, bar0Name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, bar0Name, OBJPROP_YDISTANCE, y + 20);
ObjectSetInteger(0, bar0Name, OBJPROP_COLOR, ES_bar0Color);
ObjectSetInteger(0, bar0Name, OBJPROP_FONTSIZE, 10);
// Create PB1 info
string pb1Name = ES_objectPrefix + "PB1Info";
ObjectCreate(0, pb1Name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, pb1Name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, pb1Name, OBJPROP_YDISTANCE, y + 40);
ObjectSetInteger(0, pb1Name, OBJPROP_COLOR, ES_pb1Color);
ObjectSetInteger(0, pb1Name, OBJPROP_FONTSIZE, 10);
// Create 1ES info
string es1Name = ES_objectPrefix + "1ESInfo";
ObjectCreate(0, es1Name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, es1Name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, es1Name, OBJPROP_YDISTANCE, y + 60);
ObjectSetInteger(0, es1Name, OBJPROP_COLOR, ES_es1Color);
ObjectSetInteger(0, es1Name, OBJPROP_FONTSIZE, 10);
// Create PB2 info
string pb2Name = ES_objectPrefix + "PB2Info";
ObjectCreate(0, pb2Name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, pb2Name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, pb2Name, OBJPROP_YDISTANCE, y + 80);
ObjectSetInteger(0, pb2Name, OBJPROP_COLOR, ES_pb2Color);
ObjectSetInteger(0, pb2Name, OBJPROP_FONTSIZE, 10);
// Create 2ES info
string es2Name = ES_objectPrefix + "2ESInfo";
ObjectCreate(0, es2Name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, es2Name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, es2Name, OBJPROP_YDISTANCE, y + 100);
ObjectSetInteger(0, es2Name, OBJPROP_COLOR, ES_es2Color);
ObjectSetInteger(0, es2Name, OBJPROP_FONTSIZE, 10);
// Create 2ESF info
string esfName = ES_objectPrefix + "2ESFInfo";
ObjectCreate(0, esfName, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, esfName, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, esfName, OBJPROP_YDISTANCE, y + 120);
ObjectSetInteger(0, esfName, OBJPROP_COLOR, ES_esfColor);
ObjectSetInteger(0, esfName, OBJPROP_FONTSIZE, 10);
}
//+------------------------------------------------------------------+
//| Create 2EL information elements within dashboard |
//+------------------------------------------------------------------+
void CreateELInfoElements(int x, int y) {
// Create state label
string stateName = EL_objectPrefix + "State";
ObjectCreate(0, stateName, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, stateName, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, stateName, OBJPROP_YDISTANCE, y);
ObjectSetInteger(0, stateName, OBJPROP_COLOR, foregroundColor);
ObjectSetInteger(0, stateName, OBJPROP_FONTSIZE, 10);
// Create Bar0 info
string bar0Name = EL_objectPrefix + "Bar0Info";
ObjectCreate(0, bar0Name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, bar0Name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, bar0Name, OBJPROP_YDISTANCE, y + 20);
ObjectSetInteger(0, bar0Name, OBJPROP_COLOR, EL_bar0Color);
ObjectSetInteger(0, bar0Name, OBJPROP_FONTSIZE, 10);
// Create PB1 info
string pb1Name = EL_objectPrefix + "PB1Info";
ObjectCreate(0, pb1Name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, pb1Name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, pb1Name, OBJPROP_YDISTANCE, y + 40);
ObjectSetInteger(0, pb1Name, OBJPROP_COLOR, EL_pb1Color);
ObjectSetInteger(0, pb1Name, OBJPROP_FONTSIZE, 10);
// Create 1EL info
string el1Name = EL_objectPrefix + "1ELInfo";
ObjectCreate(0, el1Name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, el1Name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, el1Name, OBJPROP_YDISTANCE, y + 60);
ObjectSetInteger(0, el1Name, OBJPROP_COLOR, EL_el1Color);
ObjectSetInteger(0, el1Name, OBJPROP_FONTSIZE, 10);
// Create PB2 info
string pb2Name = EL_objectPrefix + "PB2Info";
ObjectCreate(0, pb2Name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, pb2Name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, pb2Name, OBJPROP_YDISTANCE, y + 80);
ObjectSetInteger(0, pb2Name, OBJPROP_COLOR, EL_pb2Color);
ObjectSetInteger(0, pb2Name, OBJPROP_FONTSIZE, 10);
// Create 2EL info
string el2Name = EL_objectPrefix + "2ELInfo";
ObjectCreate(0, el2Name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, el2Name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, el2Name, OBJPROP_YDISTANCE, y + 100);
ObjectSetInteger(0, el2Name, OBJPROP_COLOR, EL_el2Color);
ObjectSetInteger(0, el2Name, OBJPROP_FONTSIZE, 10);
// Create 2ELF info
string elfName = EL_objectPrefix + "2ELFInfo";
ObjectCreate(0, elfName, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, elfName, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, elfName, OBJPROP_YDISTANCE, y + 120);
ObjectSetInteger(0, elfName, OBJPROP_COLOR, EL_elfColor);
ObjectSetInteger(0, elfName, OBJPROP_FONTSIZE, 10);
}
//+------------------------------------------------------------------+
//| Process 2ES - Entry Short system |
//+------------------------------------------------------------------+
void ProcessBarES() {
// Mark current bar as processed to avoid duplicate real-time signals
ES_currentBarProcessed = true;
// Get current bar data
datetime time = iTime(_Symbol, PERIOD_CURRENT, 1); // Use previous completed bar
double high = iHigh(_Symbol, PERIOD_CURRENT, 1);
double low = iLow(_Symbol, PERIOD_CURRENT, 1);
double open = iOpen(_Symbol, PERIOD_CURRENT, 1); // Added for signal bar confirmation
double close = iClose(_Symbol, PERIOD_CURRENT, 1); // Added for signal bar confirmation
// State machine for tracking 2ES setup
switch(ES_currentState) {
case STATE_LOOKING_FOR_BAR0:
// Set the first candle as Bar0
ES_bar0Time = time;
ES_bar0Low = low;
ES_currentState = STATE_LOOKING_FOR_PB1;
ES_pb1Count = 0; // Ensure pullback counter is reset
// Mark Bar0 on chart
MarkBarOnChartES(ES_bar0Time, ES_bar0Low, ES_bar0Color, "Bar0", 0);
break;
case STATE_AFTER_2EF:
// After 2ESF, the next bar is always marked as Bar0 regardless of price
ES_bar0Time = time;
ES_bar0Low = low;
ES_currentState = STATE_LOOKING_FOR_PB1;
ES_pb1Count = 0; // Ensure pullback counter is reset
// Mark this candle as Bar0
MarkBarOnChartES(ES_bar0Time, ES_bar0Low, ES_bar0Color, "Bar0", 0);
break;
case STATE_LOOKING_FOR_PB1:
// Check Reset Rule
if(low <= ES_bar0Low) {
// Reset everything - new candle becomes Bar0
ResetSystemES();
ES_bar0Time = time;
ES_bar0Low = low;
// IMPORTANT: Explicitly reset state and counter to ensure proper sequence
ES_currentState = STATE_LOOKING_FOR_PB1;
ES_pb1Count = 0;
// Mark new Bar0 on chart
MarkBarOnChartES(ES_bar0Time, ES_bar0Low, ES_bar0Color, "Bar0", 0);
}
else {
// This is a pullback candle (PB1)
if(ES_pb1Count == 0) {
// First pullback candle - can ONLY be a pullback, never a 1ES
ES_pb1Time = time;
ES_pb1High = high;
ES_pb1Low = low;
ES_pb1Count++;
// Mark first PB1 on chart
MarkBarOnChartES(ES_pb1Time, ES_pb1Low, ES_pb1Color, "PB1", 4);
}
else if(ES_pb1Count == 1) {
// Second pullback candle - now we can check for 1ES
ES_pb1Count++; // Increment the count first
if(high > ES_pb1High) {
// Update highest high of pullback
ES_pb1High = high;
}
if(low < ES_pb1Low) {
// This is 1ES
ES_es1Time = time;
ES_es1High = high;
ES_es1Low = low;
ES_currentState = STATE_LOOKING_FOR_PB2;
// Mark 1ES on chart
MarkBarOnChartES(ES_es1Time, ES_es1Low, ES_es1Color, "1ES", 2);
}
else {
// Still just a pullback
MarkBarOnChartES(time, ES_pb1Low, ES_pb1Color, "PB1-" + IntegerToString(ES_pb1Count), 4);
ES_pb1Low = low; // Update pb1Low to the current candle's low
}
}
else {
// Third or later pullback - same logic as before
if(high > ES_pb1High) {
// Update highest high of pullback
ES_pb1High = high;
// Check if this candle also forms a 1ES
if(low < ES_pb1Low) {
// This candle is both a pullback and 1ES
ES_es1Time = time;
ES_es1High = high;
ES_es1Low = low;
ES_currentState = STATE_LOOKING_FOR_PB2;
// Mark 1ES on chart
MarkBarOnChartES(ES_es1Time, ES_es1Low, ES_es1Color, "1ES", 2);
}
else {
// Just another pullback
ES_pb1Count++;
MarkBarOnChartES(time, ES_pb1Low, ES_pb1Color, "PB1-" + IntegerToString(ES_pb1Count), 4);
ES_pb1Low = low; // Update pb1Low to the current candle's low
}
}
else if(low < ES_pb1Low) {
// This is 1ES
ES_es1Time = time;
ES_es1High = high;
ES_es1Low = low;
ES_currentState = STATE_LOOKING_FOR_PB2;
// Mark 1ES on chart
MarkBarOnChartES(ES_es1Time, ES_es1Low, ES_es1Color, "1ES", 2);
}
else {
// Still a pullback candle
ES_pb1Count++;
MarkBarOnChartES(time, ES_pb1Low, ES_pb1Color, "PB1-" + IntegerToString(ES_pb1Count), 4);
ES_pb1Low = low; // Update pb1Low to the current candle's low
}
}
}
break;
case STATE_LOOKING_FOR_PB2:
// Apply Reset Rule first - always reset if we hit a new low below Bar0
if(low <= ES_bar0Low) {
// Reset everything - new candle becomes Bar0
ResetSystemES();
ES_bar0Time = time;
ES_bar0Low = low;
ES_currentState = STATE_LOOKING_FOR_PB1;
ES_pb1Count = 0; // Ensure pullback counter is reset
// Mark new Bar0 on chart
MarkBarOnChartES(ES_bar0Time, ES_bar0Low, ES_bar0Color, "Bar0", 0);
}
// First check if we already have a valid PB2 and this could be 2ES
else if(ES_pb2Count > 0 && low < ES_pb2Low && !ES_realTimeSignalGenerated) {
// Record the signal bar (the bar before this one)
ES_signalBarTime = iTime(_Symbol, PERIOD_CURRENT, 2); // Index 2 because we're looking at bar 1 and need the previous one
ES_signalBarHigh = iHigh(_Symbol, PERIOD_CURRENT, 2);
ES_signalBarLow = iLow(_Symbol, PERIOD_CURRENT, 2);
ES_signalBarOpen = iOpen(_Symbol, PERIOD_CURRENT, 2); // Added for signal bar confirmation
ES_signalBarClose = iClose(_Symbol, PERIOD_CURRENT, 2); // Added for signal bar confirmation
// Check if signal bar is bearish (close < open)
bool isValidSignalBar = (ES_signalBarClose < ES_signalBarOpen);
// Mark signal bar on chart
MarkSignalBarES(ES_signalBarTime, ES_signalBarHigh, ES_signalBarLow);
// This is 2ES since we have PB2 and price moved lower
ES_es2Time = time;
ES_es2High = high;
ES_es2Low = low;
ES_currentState = STATE_FOUND_2E;
ES_isCompletedSetup = true; // Mark this as a completed setup
// First clear all other objects, keeping only dashboard
ClearAllExceptDashboardES();
// Mark 2ES on chart
MarkBarOnChartES(ES_es2Time, ES_es2Low, ES_es2Color, "2ES", 1);
// Add SELL STOP order visualization and risk management if signals are enabled
if(EnableTradingSignals) {
double sellStopLevel = ES_pb2Low - _Point; // Just below PB2 low
// Create the sell stop visualization
CreateSellStopSignal(ES_es2Time, sellStopLevel, ES_pb2Low, isValidSignalBar);
// Add take profit visualization if risk management is enabled
if(ShowRiskManagement) {
// Take profit at Bar0 low
double takeProfitLevel = ES_bar0Low;
CreateTakeProfitLine(ES_es2Time, takeProfitLevel, "TP-2ES", sellStopColor);
// Risk to reward calculation
double rr = (sellStopLevel - takeProfitLevel) / (ES_signalBarHigh - sellStopLevel);
// Display risk:reward ratio
string rrText = "R:R = 1:" + DoubleToString(rr, 2);
DisplayRiskRewardRatio(ES_es2Time, sellStopLevel - 50*_Point, rrText, sellSignalColor);
}
}
}
// Only now check for 1ES reset when no valid PB2 yet
else if(ES_pb2Count == 0 && low < ES_es1Low && low > ES_bar0Low) {
// Reset to new 1ES - only if we haven't found PB2 yet
ES_es1Time = time;
ES_es1High = high;
ES_es1Low = low;
ES_pb2Count = 0;
// Mark new 1ES on chart
MarkBarOnChartES(ES_es1Time, ES_es1Low, ES_es1Color, "1ES", 2);
// Stay in the PB2 state
ES_currentState = STATE_LOOKING_FOR_PB2;
}
else if(low > ES_es1Low) {
// This is a second pullback (PB2)
if(ES_pb2Count == 0) {
ES_pb2Time = time;
ES_pb2High = high;
ES_pb2Low = low;
ES_pb2Count++;
// Mark first PB2 on chart
MarkBarOnChartES(ES_pb2Time, ES_pb2Low, ES_pb2Color, "PB2", 4);
// Important: Don't allow 2ES on the very first PB2 bar
return;
}
else {
// For subsequent PB2 candles
if(high > ES_pb2High) {
ES_pb2High = high; // Update highest high of PB2
// Check if this candle also forms a 2ES
if(low < ES_pb2Low && !ES_realTimeSignalGenerated) {
// Record the signal bar (the bar before this one)
ES_signalBarTime = iTime(_Symbol, PERIOD_CURRENT, 2);
ES_signalBarHigh = iHigh(_Symbol, PERIOD_CURRENT, 2);
ES_signalBarLow = iLow(_Symbol, PERIOD_CURRENT, 2);
ES_signalBarOpen = iOpen(_Symbol, PERIOD_CURRENT, 2); // Added for signal bar confirmation
ES_signalBarClose = iClose(_Symbol, PERIOD_CURRENT, 2); // Added for signal bar confirmation
// Check if signal bar is bearish (close < open)
bool isValidSignalBar = (ES_signalBarClose < ES_signalBarOpen);
// Mark signal bar on chart
MarkSignalBarES(ES_signalBarTime, ES_signalBarHigh, ES_signalBarLow);
// This candle is both a pullback and 2ES
ES_es2Time = time;
ES_es2High = high;
ES_es2Low = low;
ES_currentState = STATE_FOUND_2E;
ES_isCompletedSetup = true; // Mark this as a completed setup
// First clear all other objects, keeping only dashboard
ClearAllExceptDashboardES();
// Mark 2ES on chart
MarkBarOnChartES(ES_es2Time, ES_es2Low, ES_es2Color, "2ES", 1);
// Add SELL STOP order visualization and risk management if signals are enabled
if(EnableTradingSignals) {
double sellStopLevel = ES_pb2Low - _Point; // Just below PB2 low
// Create the sell stop visualization
CreateSellStopSignal(ES_es2Time, sellStopLevel, ES_pb2Low, isValidSignalBar);
// Add take profit visualization if risk management is enabled
if(ShowRiskManagement) {
// Take profit at Bar0 low
double takeProfitLevel = ES_bar0Low;
CreateTakeProfitLine(ES_es2Time, takeProfitLevel, "TP-2ES", sellStopColor);
// Risk to reward calculation
double rr = (sellStopLevel - takeProfitLevel) / (ES_signalBarHigh - sellStopLevel);
// Display risk:reward ratio
string rrText = "R:R = 1:" + DoubleToString(rr, 2);
DisplayRiskRewardRatio(ES_es2Time, sellStopLevel - 50*_Point, rrText, sellSignalColor);
}
}
}
else {
// Just another PB2
ES_pb2Count++;
MarkBarOnChartES(time, ES_pb2Low, ES_pb2Color, "PB2-" + IntegerToString(ES_pb2Count), 4);
ES_pb2Low = low; // Update pb2Low to the current candle's low
}
}
else if(low < ES_pb2Low && !ES_realTimeSignalGenerated) {
// Record the signal bar (the bar before this one)
ES_signalBarTime = iTime(_Symbol, PERIOD_CURRENT, 2);
ES_signalBarHigh = iHigh(_Symbol, PERIOD_CURRENT, 2);
ES_signalBarLow = iLow(_Symbol, PERIOD_CURRENT, 2);
ES_signalBarOpen = iOpen(_Symbol, PERIOD_CURRENT, 2); // Added for signal bar confirmation
ES_signalBarClose = iClose(_Symbol, PERIOD_CURRENT, 2); // Added for signal bar confirmation
// Check if signal bar is bearish (close < open)
bool isValidSignalBar = (ES_signalBarClose < ES_signalBarOpen);
// Mark signal bar on chart
MarkSignalBarES(ES_signalBarTime, ES_signalBarHigh, ES_signalBarLow);
// This is 2ES
ES_es2Time = time;
ES_es2High = high;
ES_es2Low = low;
ES_currentState = STATE_FOUND_2E;
ES_isCompletedSetup = true; // Mark this as a completed setup
// First clear all other objects, keeping only dashboard
ClearAllExceptDashboardES();
// Mark 2ES on chart
MarkBarOnChartES(ES_es2Time, ES_es2Low, ES_es2Color, "2ES", 1);
// Add SELL STOP order visualization and risk management if signals are enabled
if(EnableTradingSignals) {
double sellStopLevel = ES_pb2Low - _Point; // Just below PB2 low
// Create the sell stop visualization
CreateSellStopSignal(ES_es2Time, sellStopLevel, ES_pb2Low, isValidSignalBar);
// Add take profit visualization if risk management is enabled
if(ShowRiskManagement) {
// Take profit at Bar0 low
double takeProfitLevel = ES_bar0Low;
CreateTakeProfitLine(ES_es2Time, takeProfitLevel, "TP-2ES", sellStopColor);
// Risk to reward calculation
double rr = (sellStopLevel - takeProfitLevel) / (ES_signalBarHigh - sellStopLevel);
// Display risk:reward ratio
string rrText = "R:R = 1:" + DoubleToString(rr, 2);
DisplayRiskRewardRatio(ES_es2Time, sellStopLevel - 50*_Point, rrText, sellSignalColor);
}
}
}
else {
// Still a PB2 candle
ES_pb2Count++;
MarkBarOnChartES(time, ES_pb2Low, ES_pb2Color, "PB2-" + IntegerToString(ES_pb2Count), 4);
ES_pb2Low = low; // Update pb2Low to the current candle's low
}
}
}
else if(low == ES_es1Low && high == ES_es1High) {
// Candle trades within the range of 1ES - still considered a pullback
ES_pb2Count++;
MarkBarOnChartES(time, ES_pb2Low, ES_pb2Color, "PB2-Range", 4);
ES_pb2Low = low; // Update pb2Low to the current candle's low
}
break;
case STATE_FOUND_2E:
// Check if 2ES failed
if(high > ES_es2High) {
// This is a failed 2ES (2ESF)
// First clear all other objects, keeping only dashboard and 2ES
ClearAllExceptDashboardAnd2ES();
// Mark 2ESF on chart
MarkBarOnChartES(time, high, ES_esfColor, "2ESF", 3); // Bright green for failure
// Reset and look for new setup - but don't clear the 2ES setup markers
// Start a new setup ID for the next attempt
ES_currentSetupID++;
ES_isCompletedSetup = false;
// Change to the special state that will make the next candle Bar0
ES_currentState = STATE_AFTER_2EF;
}
else {
// 2ES is valid, reset to look for new setups
// Need to fully reset all variables from previous setup
ResetSystemES(true);
// Now set up a new Bar0
ES_bar0Time = time;
ES_bar0Low = low;
ES_currentState = STATE_LOOKING_FOR_PB1;
ES_pb1Count = 0; // Ensure pullback counter is reset
// Mark new Bar0 on chart
MarkBarOnChartES(ES_bar0Time, ES_bar0Low, ES_bar0Color, "Bar0", 0);
}
break;
}
}
//+------------------------------------------------------------------+
//| Create a SELL STOP signal visualization |
//+------------------------------------------------------------------+
void CreateSellStopSignal(datetime time, double price, double pb2Low, bool isValidSignalBar) {
// Create unique names for the signal objects
string arrowName = ES_objectPrefix + "SELL_STOP_" + IntegerToString(ES_currentSetupID);
string labelName = ES_objectPrefix + "SELL_LABEL_" + IntegerToString(ES_currentSetupID);
string entryLine = ES_objectPrefix + "ENTRY_LINE_" + IntegerToString(ES_currentSetupID);
// Choose color based on signal bar confirmation
color signalColor = isValidSignalBar ? sellSignalColor : clrDarkGray;
string signalText = isValidSignalBar ? "SELL STOP @ " + DoubleToString(price, _Digits) :
"SELL STOP @ " + DoubleToString(price, _Digits) + " (WEAK)";
// Create down arrow for sell
ObjectCreate(0, arrowName, OBJ_ARROW, 0, time, price);
ObjectSetInteger(0, arrowName, OBJPROP_ARROWCODE, 242); // Thick down arrow
ObjectSetInteger(0, arrowName, OBJPROP_COLOR, signalColor);
ObjectSetInteger(0, arrowName, OBJPROP_WIDTH, 3);
ObjectSetInteger(0, arrowName, OBJPROP_SELECTABLE, false);
// Create text label for sell signal
ObjectCreate(0, labelName, OBJ_TEXT, 0, time, price - 30 * _Point);
ObjectSetString(0, labelName, OBJPROP_TEXT, signalText);
ObjectSetInteger(0, labelName, OBJPROP_COLOR, signalColor);
ObjectSetInteger(0, labelName, OBJPROP_FONTSIZE, 12);
ObjectSetInteger(0, labelName, OBJPROP_SELECTABLE, false);
// Create horizontal line at entry level
ObjectCreate(0, entryLine, OBJ_HLINE, 0, 0, price);
ObjectSetInteger(0, entryLine, OBJPROP_COLOR, signalColor);
ObjectSetInteger(0, entryLine, OBJPROP_STYLE, STYLE_DASH);
ObjectSetInteger(0, entryLine, OBJPROP_WIDTH, 1);
ObjectSetInteger(0, entryLine, OBJPROP_SELECTABLE, false);
// Show alert if enabled and signal is valid and not a real-time signal
if(isValidSignalBar && !ES_realTimeSignalGenerated) {
Alert("2ES Pattern Detected - SELL STOP @ ", DoubleToString(price, _Digits));
}
}
//+------------------------------------------------------------------+
//| Mark signal bar for ES system |
//+------------------------------------------------------------------+
void MarkSignalBarES(datetime time, double high, double low) {
// Create unique name based on current setup ID
string objName = ES_objectPrefix + "SigArrow_" + IntegerToString(ES_currentSetupID);
string txtName = ES_objectPrefix + "SigText_" + IntegerToString(ES_currentSetupID);
string hlName = ES_objectPrefix + "SigSL_" + IntegerToString(ES_currentSetupID);
string slLabel = ES_objectPrefix + "SL_Label_" + IntegerToString(ES_currentSetupID);
// Create arrow at the high of the signal bar (this is the stop loss level)
ObjectCreate(0, objName, OBJ_ARROW, 0, time, high);
ObjectSetInteger(0, objName, OBJPROP_ARROWCODE, 159); // Down arrow
ObjectSetInteger(0, objName, OBJPROP_COLOR, stopLossColor); // Changed to stop loss color
ObjectSetInteger(0, objName, OBJPROP_WIDTH, 3);
// Create text label
ObjectCreate(0, txtName, OBJ_TEXT, 0, time, high + 15 * _Point);
ObjectSetString(0, txtName, OBJPROP_TEXT, "2ES-SIG");
ObjectSetInteger(0, txtName, OBJPROP_COLOR, ES_signalColor);
ObjectSetInteger(0, txtName, OBJPROP_FONTSIZE, 11);
ObjectSetInteger(0, txtName, OBJPROP_WIDTH, 2);
// Create horizontal line at the high of signal bar (stop loss level)
ObjectCreate(0, hlName, OBJ_HLINE, 0, 0, high);
ObjectSetInteger(0, hlName, OBJPROP_COLOR, stopLossColor);
ObjectSetInteger(0, hlName, OBJPROP_STYLE, STYLE_DASH);
ObjectSetInteger(0, hlName, OBJPROP_WIDTH, 2);
// Add Stop Loss label at the right edge of the chart
datetime rightEdgeTime = TimeCurrent() + PeriodSeconds(PERIOD_CURRENT) * 20;
ObjectCreate(0, slLabel, OBJ_TEXT, 0, rightEdgeTime, high);
ObjectSetString(0, slLabel, OBJPROP_TEXT, "STOP LOSS @ " + DoubleToString(high, _Digits));
ObjectSetInteger(0, slLabel, OBJPROP_COLOR, stopLossColor);
ObjectSetInteger(0, slLabel, OBJPROP_FONTSIZE, 10);
ObjectSetInteger(0, slLabel, OBJPROP_ANCHOR, ANCHOR_RIGHT);
}
//+------------------------------------------------------------------+
//| Process 2EL - Entry Long system |
//+------------------------------------------------------------------+
void ProcessBarEL() {
// Mark current bar as processed to avoid duplicate real-time signals
EL_currentBarProcessed = true;
// Get current bar data
datetime time = iTime(_Symbol, PERIOD_CURRENT, 1); // Use previous completed bar
double high = iHigh(_Symbol, PERIOD_CURRENT, 1);
double low = iLow(_Symbol, PERIOD_CURRENT, 1);
double open = iOpen(_Symbol, PERIOD_CURRENT, 1); // Added for signal bar confirmation
double close = iClose(_Symbol, PERIOD_CURRENT, 1); // Added for signal bar confirmation
// State machine for tracking 2EL setup
switch(EL_currentState) {
case STATE_LOOKING_FOR_BAR0:
// Set the first candle as Bar0
EL_bar0Time = time;
EL_bar0High = high;
EL_currentState = STATE_LOOKING_FOR_PB1;
EL_pb1Count = 0; // Ensure pullback counter is reset
// Mark Bar0 on chart
MarkBarOnChartEL(EL_bar0Time, EL_bar0High, EL_bar0Color, "Bar0", 0);
break;
case STATE_AFTER_2EF:
// After 2ELF, the next bar is always marked as Bar0 regardless of price
EL_bar0Time = time;
EL_bar0High = high;
EL_currentState = STATE_LOOKING_FOR_PB1;
EL_pb1Count = 0; // Ensure pullback counter is reset
// Mark this candle as Bar0
MarkBarOnChartEL(EL_bar0Time, EL_bar0High, EL_bar0Color, "Bar0", 0);
break;
case STATE_LOOKING_FOR_PB1:
// Check Reset Rule
if(high >= EL_bar0High) {
// Reset everything - new candle becomes Bar0
ResetSystemEL();
EL_bar0Time = time;
EL_bar0High = high;
// IMPORTANT: Explicitly reset state and counter to ensure proper sequence
EL_currentState = STATE_LOOKING_FOR_PB1;
EL_pb1Count = 0;
// Mark new Bar0 on chart
MarkBarOnChartEL(EL_bar0Time, EL_bar0High, EL_bar0Color, "Bar0", 0);
}
else {
// This is a pullback candle (PB1)
if(EL_pb1Count == 0) {
// First pullback candle - can ONLY be a pullback, never a 1EL
EL_pb1Time = time;
EL_pb1High = high;
EL_pb1Low = low;
EL_pb1Count++;
// Mark first PB1 on chart
MarkBarOnChartEL(EL_pb1Time, EL_pb1High, EL_pb1Color, "PB1", 4);
}
else if(EL_pb1Count == 1) {
// Second pullback candle - now we can check for 1EL
EL_pb1Count++; // Increment the count first
if(low < EL_pb1Low) {
// Update lowest low of pullback
EL_pb1Low = low;
}
if(high > EL_pb1High && high < EL_bar0High) {
// This is 1EL
EL_el1Time = time;
EL_el1High = high;
EL_el1Low = low;
EL_currentState = STATE_LOOKING_FOR_PB2;
// Mark 1EL on chart
MarkBarOnChartEL(EL_el1Time, EL_el1High, EL_el1Color, "1EL", 2);
}
else {
// Still just a pullback
MarkBarOnChartEL(time, EL_pb1High, EL_pb1Color, "PB1-" + IntegerToString(EL_pb1Count), 4);
EL_pb1High = high; // Update pb1High to the current candle's high
}
}
else {
// Third or later pullback - same logic as before
if(low < EL_pb1Low) {
// Update lowest low of pullback
EL_pb1Low = low;
// Check if this candle also forms a 1EL
if(high > EL_pb1High) {
// This candle is both a pullback and 1EL
EL_el1Time = time;
EL_el1High = high;
EL_el1Low = low;
EL_currentState = STATE_LOOKING_FOR_PB2;
// Mark 1EL on chart
MarkBarOnChartEL(EL_el1Time, EL_el1High, EL_el1Color, "1EL", 2);
}
else {
// Just another pullback
EL_pb1Count++;
MarkBarOnChartEL(time, EL_pb1High, EL_pb1Color, "PB1-" + IntegerToString(EL_pb1Count), 4);
EL_pb1High = high; // Update pb1High to the current candle's high
}
}
else if(high > EL_pb1High && high < EL_bar0High) {
// This is 1EL
EL_el1Time = time;
EL_el1High = high;
EL_el1Low = low;
EL_currentState = STATE_LOOKING_FOR_PB2;
// Mark 1EL on chart
MarkBarOnChartEL(EL_el1Time, EL_el1High, EL_el1Color, "1EL", 2);
}
else {
// Still a pullback candle
EL_pb1Count++;
MarkBarOnChartEL(time, EL_pb1High, EL_pb1Color, "PB1-" + IntegerToString(EL_pb1Count), 4);
EL_pb1High = high; // Update pb1High to the current candle's high
}
}
}
break;
case STATE_LOOKING_FOR_PB2:
// Apply Reset Rule first - always reset if we hit a new high above Bar0
if(high >= EL_bar0High) {
// Reset everything - new candle becomes Bar0
ResetSystemEL();
EL_bar0Time = time;
EL_bar0High = high;
EL_currentState = STATE_LOOKING_FOR_PB1;
EL_pb1Count = 0; // Ensure pullback counter is reset
// Mark new Bar0 on chart
MarkBarOnChartEL(EL_bar0Time, EL_bar0High, EL_bar0Color, "Bar0", 0);
}
// First check if we already have a valid PB2 and this could be 2EL
else if(EL_pb2Count > 0 && high > EL_pb2High && high < EL_bar0High && !EL_realTimeSignalGenerated) {
// Record the signal bar (the bar before this one)
EL_signalBarTime = iTime(_Symbol, PERIOD_CURRENT, 2);
EL_signalBarHigh = iHigh(_Symbol, PERIOD_CURRENT, 2);
EL_signalBarLow = iLow(_Symbol, PERIOD_CURRENT, 2);
EL_signalBarOpen = iOpen(_Symbol, PERIOD_CURRENT, 2); // Added for signal bar confirmation
EL_signalBarClose = iClose(_Symbol, PERIOD_CURRENT, 2); // Added for signal bar confirmation
// Check if signal bar is bullish (close > open)
bool isValidSignalBar = (EL_signalBarClose > EL_signalBarOpen);
// Mark signal bar on chart
MarkSignalBarEL(EL_signalBarTime, EL_signalBarHigh, EL_signalBarLow);
// This is 2EL since we have PB2 and price moved higher but is below BAR0
EL_el2Time = time;
EL_el2High = high;
EL_el2Low = low;
EL_currentState = STATE_FOUND_2E;
EL_isCompletedSetup = true; // Mark this as a completed setup
// First clear all other objects, keeping only dashboard
ClearAllExceptDashboardEL();
// Mark 2EL on chart
MarkBarOnChartEL(EL_el2Time, EL_el2High, EL_el2Color, "2EL", 1);
// Add BUY STOP order visualization and risk management if signals are enabled
if(EnableTradingSignals) {
double buyStopLevel = EL_pb2High + _Point; // Just above PB2 high
// Create the buy stop visualization
CreateBuyStopSignal(EL_el2Time, buyStopLevel, EL_pb2High, isValidSignalBar);
// Add take profit visualization if risk management is enabled
if(ShowRiskManagement) {
// Take profit at Bar0 high
double takeProfitLevel = EL_bar0High;
CreateTakeProfitLine(EL_el2Time, takeProfitLevel, "TP-2EL", buySignalColor);
// Risk to reward calculation
double rr = (takeProfitLevel - buyStopLevel) / (buyStopLevel - EL_signalBarLow);
// Display risk:reward ratio
string rrText = "R:R = 1:" + DoubleToString(rr, 2);
DisplayRiskRewardRatio(EL_el2Time, buyStopLevel + 50*_Point, rrText, buySignalColor);
}
}
}
// Only now check for 1EL reset when no valid PB2 yet
else if(EL_pb2Count == 0 && high > EL_el1High && high < EL_bar0High) {
// Reset to new 1EL - only if we haven't found PB2 yet
EL_el1Time = time;
EL_el1High = high;
EL_el1Low = low;
EL_pb2Count = 0;
// Mark new 1EL on chart
MarkBarOnChartEL(EL_el1Time, EL_el1High, EL_el1Color, "1EL", 2);
// Stay in the PB2 state
EL_currentState = STATE_LOOKING_FOR_PB2;
}
else if(high < EL_el1High) {
// This is a second pullback (PB2)
if(EL_pb2Count == 0) {
EL_pb2Time = time;
EL_pb2High = high;
EL_pb2Low = low;
EL_pb2Count++;
// Mark first PB2 on chart
MarkBarOnChartEL(EL_pb2Time, EL_pb2High, EL_pb2Color, "PB2", 4);
// Important: Don't allow 2EL on the very first PB2 bar
return;
}
else {
// For subsequent PB2 candles
if(low < EL_pb2Low) {
EL_pb2Low = low; // Update lowest low of PB2
// Check if this candle also forms a 2EL
if(high > EL_pb2High && high < EL_bar0High && !EL_realTimeSignalGenerated) {
// Record the signal bar (the bar before this one)
EL_signalBarTime = iTime(_Symbol, PERIOD_CURRENT, 2);
EL_signalBarHigh = iHigh(_Symbol, PERIOD_CURRENT, 2);
EL_signalBarLow = iLow(_Symbol, PERIOD_CURRENT, 2);
EL_signalBarOpen = iOpen(_Symbol, PERIOD_CURRENT, 2); // Added for signal bar confirmation
EL_signalBarClose = iClose(_Symbol, PERIOD_CURRENT, 2); // Added for signal bar confirmation
// Check if signal bar is bullish (close > open)
bool isValidSignalBar = (EL_signalBarClose > EL_signalBarOpen);
// Mark signal bar on chart
MarkSignalBarEL(EL_signalBarTime, EL_signalBarHigh, EL_signalBarLow);
// This candle is both a pullback and 2EL
EL_el2Time = time;
EL_el2High = high;
EL_el2Low = low;
EL_currentState = STATE_FOUND_2E;
EL_isCompletedSetup = true; // Mark this as a completed setup
// First clear all other objects, keeping only dashboard
ClearAllExceptDashboardEL();
// Mark 2EL on chart
MarkBarOnChartEL(EL_el2Time, EL_el2High, EL_el2Color, "2EL", 1);
// Add BUY STOP order visualization and risk management if signals are enabled
if(EnableTradingSignals) {
double buyStopLevel = EL_pb2High + _Point; // Just above PB2 high
// Create the buy stop visualization
CreateBuyStopSignal(EL_el2Time, buyStopLevel, EL_pb2High, isValidSignalBar);
// Add take profit visualization if risk management is enabled
if(ShowRiskManagement) {
// Take profit at Bar0 high
double takeProfitLevel = EL_bar0High;
CreateTakeProfitLine(EL_el2Time, takeProfitLevel, "TP-2EL", buySignalColor);
// Risk to reward calculation
double rr = (takeProfitLevel - buyStopLevel) / (buyStopLevel - EL_signalBarLow);
// Display risk:reward ratio
string rrText = "R:R = 1:" + DoubleToString(rr, 2);
DisplayRiskRewardRatio(EL_el2Time, buyStopLevel + 50*_Point, rrText, buySignalColor);
}
}
}
else {
// Just another PB2
EL_pb2Count++;
MarkBarOnChartEL(time, EL_pb2High, EL_pb2Color, "PB2-" + IntegerToString(EL_pb2Count), 4);
EL_pb2High = high; // Update pb2High to the current candle's high
}
}
else if(high > EL_pb2High && high < EL_bar0High && !EL_realTimeSignalGenerated) {
// Record the signal bar (the bar before this one)
EL_signalBarTime = iTime(_Symbol, PERIOD_CURRENT, 2);
EL_signalBarHigh = iHigh(_Symbol, PERIOD_CURRENT, 2);
EL_signalBarLow = iLow(_Symbol, PERIOD_CURRENT, 2);
EL_signalBarOpen = iOpen(_Symbol, PERIOD_CURRENT, 2); // Added for signal bar confirmation
EL_signalBarClose = iClose(_Symbol, PERIOD_CURRENT, 2); // Added for signal bar confirmation
// Check if signal bar is bullish (close > open)
bool isValidSignalBar = (EL_signalBarClose > EL_signalBarOpen);
// Mark signal bar on chart
MarkSignalBarEL(EL_signalBarTime, EL_signalBarHigh, EL_signalBarLow);
// This is 2EL
EL_el2Time = time;
EL_el2High = high;
EL_el2Low = low;
EL_currentState = STATE_FOUND_2E;
EL_isCompletedSetup = true; // Mark this as a completed setup
// First clear all other objects, keeping only dashboard
ClearAllExceptDashboardEL();
// Mark 2EL on chart
MarkBarOnChartEL(EL_el2Time, EL_el2High, EL_el2Color, "2EL", 1);
// Add BUY STOP order visualization and risk management if signals are enabled
if(EnableTradingSignals) {
double buyStopLevel = EL_pb2High + _Point; // Just above PB2 high
// Create the buy stop visualization
CreateBuyStopSignal(EL_el2Time, buyStopLevel, EL_pb2High, isValidSignalBar);
// Add take profit visualization if risk management is enabled
if(ShowRiskManagement) {
// Take profit at Bar0 high
double takeProfitLevel = EL_bar0High;
CreateTakeProfitLine(EL_el2Time, takeProfitLevel, "TP-2EL", buySignalColor);
// Risk to reward calculation
double rr = (takeProfitLevel - buyStopLevel) / (buyStopLevel - EL_signalBarLow);
// Display risk:reward ratio
string rrText = "R:R = 1:" + DoubleToString(rr, 2);
DisplayRiskRewardRatio(EL_el2Time, buyStopLevel + 50*_Point, rrText, buySignalColor);
}
}
}
else {
// Still a PB2 candle
EL_pb2Count++;
MarkBarOnChartEL(time, EL_pb2High, EL_pb2Color, "PB2-" + IntegerToString(EL_pb2Count), 4);
EL_pb2High = high; // Update pb2High to the current candle's high
}
}
}
else if(high == EL_el1High && low == EL_el1Low) {
// Candle trades within the range of 1EL - still considered a pullback
EL_pb2Count++;
MarkBarOnChartEL(time, EL_pb2High, EL_pb2Color, "PB2-Range", 4);
EL_pb2High = high; // Update pb2High to the current candle's high
}
break;
case STATE_FOUND_2E:
// Check if 2EL failed
if(low < EL_el2Low) {
// This is a failed 2EL (2ELF)
// First clear all other objects, keeping only dashboard and 2EL
ClearAllExceptDashboardAnd2EL();
// Mark 2ELF on chart
MarkBarOnChartEL(time, low, EL_elfColor, "2ELF", 3); // Bright red for failure
// Reset and look for new setup - but don't clear the 2EL setup markers
// Start a new setup ID for the next attempt
EL_currentSetupID++;
EL_isCompletedSetup = false;
// Change to the special state that will make the next candle Bar0
EL_currentState = STATE_AFTER_2EF;
}
else {
// 2EL is valid, reset to look for new setups
// Need to fully reset all variables from previous setup
ResetSystemEL(true);
// Now set up a new Bar0
EL_bar0Time = time;
EL_bar0High = high;
EL_currentState = STATE_LOOKING_FOR_PB1;
EL_pb1Count = 0; // Ensure pullback counter is reset
// Mark new Bar0 on chart
MarkBarOnChartEL(EL_bar0Time, EL_bar0High, EL_bar0Color, "Bar0", 0);
}
break;
}
}
//+------------------------------------------------------------------+
//| Create a BUY STOP signal visualization |
//+------------------------------------------------------------------+
void CreateBuyStopSignal(datetime time, double price, double pb2High, bool isValidSignalBar) {
// Create unique names for the signal objects
string arrowName = EL_objectPrefix + "BUY_STOP_" + IntegerToString(EL_currentSetupID);
string labelName = EL_objectPrefix + "BUY_LABEL_" + IntegerToString(EL_currentSetupID);
string entryLine = EL_objectPrefix + "ENTRY_LINE_" + IntegerToString(EL_currentSetupID);
// Choose color based on signal bar confirmation
color signalColor = isValidSignalBar ? buySignalColor : clrDarkGray;
string signalText = isValidSignalBar ? "BUY STOP @ " + DoubleToString(price, _Digits) :
"BUY STOP @ " + DoubleToString(price, _Digits) + " (WEAK)";
// Create up arrow for buy
ObjectCreate(0, arrowName, OBJ_ARROW, 0, time, price);
ObjectSetInteger(0, arrowName, OBJPROP_ARROWCODE, 241); // Thick up arrow
ObjectSetInteger(0, arrowName, OBJPROP_COLOR, signalColor);
ObjectSetInteger(0, arrowName, OBJPROP_WIDTH, 3);
ObjectSetInteger(0, arrowName, OBJPROP_SELECTABLE, false);
// Create text label for buy signal
ObjectCreate(0, labelName, OBJ_TEXT, 0, time, price + 30 * _Point);
ObjectSetString(0, labelName, OBJPROP_TEXT, signalText);
ObjectSetInteger(0, labelName, OBJPROP_COLOR, signalColor);
ObjectSetInteger(0, labelName, OBJPROP_FONTSIZE, 12);
ObjectSetInteger(0, labelName, OBJPROP_SELECTABLE, false);
// Create horizontal line at entry level
ObjectCreate(0, entryLine, OBJ_HLINE, 0, 0, price);
ObjectSetInteger(0, entryLine, OBJPROP_COLOR, signalColor);
ObjectSetInteger(0, entryLine, OBJPROP_STYLE, STYLE_DASH);
ObjectSetInteger(0, entryLine, OBJPROP_WIDTH, 1);
ObjectSetInteger(0, entryLine, OBJPROP_SELECTABLE, false);
// Show alert if enabled and signal is valid and not a real-time signal
if(isValidSignalBar && !EL_realTimeSignalGenerated) {
Alert("2EL Pattern Detected - BUY STOP @ ", DoubleToString(price, _Digits));
}
}
//+------------------------------------------------------------------+
//| Create Take Profit Line |
//+------------------------------------------------------------------+
void CreateTakeProfitLine(datetime time, double price, string label, color lineColor) {
// Create unique names for the TP objects
string tpLineName = "TP_Line_" + TimeToString(time);
string tpLabelName = "TP_Label_" + TimeToString(time);
// Create horizontal line for the take profit level
ObjectCreate(0, tpLineName, OBJ_HLINE, 0, 0, price);
ObjectSetInteger(0, tpLineName, OBJPROP_COLOR, takeProfitColor);
ObjectSetInteger(0, tpLineName, OBJPROP_STYLE, STYLE_DASH);
ObjectSetInteger(0, tpLineName, OBJPROP_WIDTH, 2);
ObjectSetInteger(0, tpLineName, OBJPROP_SELECTABLE, false);
// Add Take Profit label at the right edge of the chart
datetime rightEdgeTime = TimeCurrent() + PeriodSeconds(PERIOD_CURRENT) * 20;
ObjectCreate(0, tpLabelName, OBJ_TEXT, 0, rightEdgeTime, price);
ObjectSetString(0, tpLabelName, OBJPROP_TEXT, "TAKE PROFIT @ " + DoubleToString(price, _Digits));
ObjectSetInteger(0, tpLabelName, OBJPROP_COLOR, takeProfitColor);
ObjectSetInteger(0, tpLabelName, OBJPROP_FONTSIZE, 10);
ObjectSetInteger(0, tpLabelName, OBJPROP_ANCHOR, ANCHOR_RIGHT);
}
//+------------------------------------------------------------------+
//| Display Risk-Reward Ratio |
//+------------------------------------------------------------------+
void DisplayRiskRewardRatio(datetime time, double price, string text, color textColor) {
// Create unique name for the R:R text
string rrName = "RR_Text_" + TimeToString(time);
// Create text for risk:reward ratio
ObjectCreate(0, rrName, OBJ_TEXT, 0, time, price);
ObjectSetString(0, rrName, OBJPROP_TEXT, text);
ObjectSetInteger(0, rrName, OBJPROP_COLOR, textColor);
ObjectSetInteger(0, rrName, OBJPROP_FONTSIZE, 10);
ObjectSetInteger(0, rrName, OBJPROP_SELECTABLE, false);
}
//+------------------------------------------------------------------+
//| Mark signal bar for EL system |
//+------------------------------------------------------------------+
void MarkSignalBarEL(datetime time, double high, double low) {
// Create unique name based on current setup ID
string objName = EL_objectPrefix + "SigArrow_" + IntegerToString(EL_currentSetupID);
string txtName = EL_objectPrefix + "SigText_" + IntegerToString(EL_currentSetupID);
string hlName = EL_objectPrefix + "SigSL_" + IntegerToString(EL_currentSetupID);
string slLabel = EL_objectPrefix + "SL_Label_" + IntegerToString(EL_currentSetupID);
// Create arrow at the low of the signal bar (this is the stop loss level)
ObjectCreate(0, objName, OBJ_ARROW, 0, time, low);
ObjectSetInteger(0, objName, OBJPROP_ARROWCODE, 158); // Up arrow
ObjectSetInteger(0, objName, OBJPROP_COLOR, stopLossColor); // Changed to stop loss color
ObjectSetInteger(0, objName, OBJPROP_WIDTH, 3);
// Create text label
ObjectCreate(0, txtName, OBJ_TEXT, 0, time, low - 15 * _Point);
ObjectSetString(0, txtName, OBJPROP_TEXT, "2EL-SIG");
ObjectSetInteger(0, txtName, OBJPROP_COLOR, EL_signalColor);
ObjectSetInteger(0, txtName, OBJPROP_FONTSIZE, 11);
ObjectSetInteger(0, txtName, OBJPROP_WIDTH, 2);
// Create horizontal line at the low of signal bar (stop loss level)
ObjectCreate(0, hlName, OBJ_HLINE, 0, 0, low);
ObjectSetInteger(0, hlName, OBJPROP_COLOR, stopLossColor);
ObjectSetInteger(0, hlName, OBJPROP_STYLE, STYLE_DASH);
ObjectSetInteger(0, hlName, OBJPROP_WIDTH, 2);
// Add Stop Loss label at the right edge of the chart
datetime rightEdgeTime = TimeCurrent() + PeriodSeconds(PERIOD_CURRENT) * 20;
ObjectCreate(0, slLabel, OBJ_TEXT, 0, rightEdgeTime, low);
ObjectSetString(0, slLabel, OBJPROP_TEXT, "STOP LOSS @ " + DoubleToString(low, _Digits));
ObjectSetInteger(0, slLabel, OBJPROP_COLOR, stopLossColor);
ObjectSetInteger(0, slLabel, OBJPROP_FONTSIZE, 10);
ObjectSetInteger(0, slLabel, OBJPROP_ANCHOR, ANCHOR_RIGHT);
}
//+------------------------------------------------------------------+
//| Reset ES system variables |
//+------------------------------------------------------------------+
void ResetSystemES(bool clearObjects = true) {
// Reset counters
ES_pb1Count = 0;
ES_pb2Count = 0;
// IMPORTANT: Always ensure we reset to the proper state
ES_currentState = STATE_LOOKING_FOR_PB1;
// Reset real-time signal flags
ES_realTimeSignalGenerated = false;
ES_currentBarProcessed = false;
// If requested, clear objects for this setup
if(clearObjects) {
ClearCurrentSetupObjectsES();
}
// If we're resetting after finding a 2ES or explicitly not clearing
if(clearObjects) {
// Don't clear the completed setup, just increment the setup ID
ES_currentSetupID++;
ES_isCompletedSetup = false;
}
}
//+------------------------------------------------------------------+
//| Reset EL system variables |
//+------------------------------------------------------------------+
void ResetSystemEL(bool clearObjects = true) {
// Reset counters
EL_pb1Count = 0;
EL_pb2Count = 0;
// IMPORTANT: Always ensure we reset to the proper state
EL_currentState = STATE_LOOKING_FOR_PB1;
// Reset real-time signal flags
EL_realTimeSignalGenerated = false;
EL_currentBarProcessed = false;
// If requested, clear objects for this setup
if(clearObjects) {
ClearCurrentSetupObjectsEL();
}
// If we're resetting after finding a 2EL or explicitly not clearing
if(clearObjects) {
// Don't clear the completed setup, just increment the setup ID
EL_currentSetupID++;
EL_isCompletedSetup = false;
}
}
//+------------------------------------------------------------------+
//| Clear ES objects for the current setup |
//+------------------------------------------------------------------+
void ClearCurrentSetupObjectsES() {
// Delete all objects that belong to the current setup ID
for(int i = ObjectsTotal(0) - 1; i >= 0; i--) {
string name = ObjectName(0, i);
// Check if this object belongs to our ES indicator
if(StringSubstr(name, 0, StringLen(ES_objectPrefix)) == ES_objectPrefix) {
// Only delete objects that are not part of the dashboard or EMA
if(StringFind(name, "State") < 0 &&
StringFind(name, "Info") < 0) {
// Delete this object
ObjectDelete(0, name);
}
}
}
}
//+------------------------------------------------------------------+
//| Clear EL objects for the current setup |
//+------------------------------------------------------------------+
void ClearCurrentSetupObjectsEL() {
// Delete all objects that belong to the current setup ID
for(int i = ObjectsTotal(0) - 1; i >= 0; i--) {
string name = ObjectName(0, i);
// Check if this object belongs to our EL indicator
if(StringSubstr(name, 0, StringLen(EL_objectPrefix)) == EL_objectPrefix) {
// Only delete objects that are not part of the dashboard or EMA
if(StringFind(name, "State") < 0 &&
StringFind(name, "Info") < 0) {
// Delete this object
ObjectDelete(0, name);
}
}
}
}
//+------------------------------------------------------------------+
//| Clear all ES objects except dashboard |
//+------------------------------------------------------------------+
void ClearAllExceptDashboardES() {
// Delete all ES objects except dashboard elements and signal bar objects
for(int i = ObjectsTotal(0) - 1; i >= 0; i--) {
string name = ObjectName(0, i);
// Check if this object belongs to our ES indicator
if(StringSubstr(name, 0, StringLen(ES_objectPrefix)) == ES_objectPrefix) {
// Only keep dashboard objects and signal bar objects
if(StringFind(name, "State") < 0 &&
StringFind(name, "Info") < 0 &&
StringFind(name, "SigArrow") < 0 &&
StringFind(name, "SigText") < 0 &&
StringFind(name, "SigSL") < 0) {
// Delete this object
ObjectDelete(0, name);
}
}
}
}
//+------------------------------------------------------------------+
//| Clear all EL objects except dashboard |
//+------------------------------------------------------------------+
void ClearAllExceptDashboardEL() {
// Delete all EL objects except dashboard elements and signal bar objects
for(int i = ObjectsTotal(0) - 1; i >= 0; i--) {
string name = ObjectName(0, i);
// Check if this object belongs to our EL indicator
if(StringSubstr(name, 0, StringLen(EL_objectPrefix)) == EL_objectPrefix) {
// Only keep dashboard objects and signal bar objects
if(StringFind(name, "State") < 0 &&
StringFind(name, "Info") < 0 &&
StringFind(name, "SigArrow") < 0 &&
StringFind(name, "SigText") < 0 &&
StringFind(name, "SigSL") < 0) {
// Delete this object
ObjectDelete(0, name);
}
}
}
}
//+------------------------------------------------------------------+
//| Clear all ES objects except dashboard and 2ES |
//+------------------------------------------------------------------+
void ClearAllExceptDashboardAnd2ES() {
// Delete all ES objects except dashboard elements and 2ES
for(int i = ObjectsTotal(0) - 1; i >= 0; i--) {
string name = ObjectName(0, i);
// Check if this object belongs to our ES indicator
if(StringSubstr(name, 0, StringLen(ES_objectPrefix)) == ES_objectPrefix) {
// Keep dashboard objects and 2ES objects
if(StringFind(name, "State") < 0 &&
StringFind(name, "Info") < 0 &&
StringFind(name, "2ES") < 0 &&
StringFind(name, "SigArrow") < 0 && // Also keep signal bar objects
StringFind(name, "SigText") < 0 &&
StringFind(name, "SigSL") < 0) {
// Delete this object
ObjectDelete(0, name);
}
}
}
}
//+------------------------------------------------------------------+
//| Clear all EL objects except dashboard and 2EL |
//+------------------------------------------------------------------+
void ClearAllExceptDashboardAnd2EL() {
// Delete all EL objects except dashboard elements and 2EL
for(int i = ObjectsTotal(0) - 1; i >= 0; i--) {
string name = ObjectName(0, i);
// Check if this object belongs to our EL indicator
if(StringSubstr(name, 0, StringLen(EL_objectPrefix)) == EL_objectPrefix) {
// Keep dashboard objects and 2EL objects
if(StringFind(name, "State") < 0 &&
StringFind(name, "Info") < 0 &&
StringFind(name, "2EL") < 0 &&
StringFind(name, "SigArrow") < 0 && // Also keep signal bar objects
StringFind(name, "SigText") < 0 &&
StringFind(name, "SigSL") < 0) {
// Delete this object
ObjectDelete(0, name);
}
}
}
}
//+------------------------------------------------------------------+
//| Mark bar on chart for ES system |
//+------------------------------------------------------------------+
void MarkBarOnChartES(datetime time, double price, color arrowColor, string label, int importance) {
// Skip drawing PB1 and PB2 markers on the chart
if(StringFind(label, "PB1") >= 0 || StringFind(label, "PB2") >= 0) {
// Still track them internally but don't display
return;
}
// Include setup ID in the object name for selective deletion
string objName = ES_objectPrefix + "Arrow_" + IntegerToString(ES_currentSetupID) + "_" + TimeToString(time);
string txtName = ES_objectPrefix + "Text_" + IntegerToString(ES_currentSetupID) + "_" + TimeToString(time);
// Create arrow with larger size for more important labels
ObjectCreate(0, objName, OBJ_ARROW, 0, time, price);
ObjectSetInteger(0, objName, OBJPROP_ARROWCODE, 158); // Up arrow (pointing from below)
ObjectSetInteger(0, objName, OBJPROP_COLOR, arrowColor);
// Vertical positioning and font size based on importance
int verticalOffset = 0;
int fontSize = 0;
int arrowWidth = 0;
switch(importance) {
case 0: // Bar0 - most important
verticalOffset = -10; // Negative for below candle
fontSize = 14;
arrowWidth = 4;
break;
case 1: // 2ES - second most important
verticalOffset = -25; // Negative for below candle
fontSize = 12;
arrowWidth = 4;
break;
case 2: // 1ES - third most important
verticalOffset = -20; // Negative for below candle
fontSize = 11;
arrowWidth = 3;
break;
case 3: // 2ESF - important failure
verticalOffset = -35; // Negative for below candle
fontSize = 12;
arrowWidth = 4;
break;
case 4: // PB1/PB2 - least important
verticalOffset = -50; // Negative for below candle
fontSize = 9;
arrowWidth = 2;
// Add transparency for less important markers
arrowColor = (color)ColorToARGB(arrowColor, 180);
break;
}
ObjectSetInteger(0, objName, OBJPROP_WIDTH, arrowWidth);
// Create text label with improved visibility
ObjectCreate(0, txtName, OBJ_TEXT, 0, time, price + verticalOffset * _Point);
ObjectSetString(0, txtName, OBJPROP_TEXT, label);
ObjectSetInteger(0, txtName, OBJPROP_COLOR, arrowColor);
ObjectSetInteger(0, txtName, OBJPROP_FONTSIZE, fontSize);
ObjectSetInteger(0, txtName, OBJPROP_WIDTH, (importance <= 2) ? 2 : 1);
}
//+------------------------------------------------------------------+
//| Mark bar on chart for EL system |
//+------------------------------------------------------------------+
void MarkBarOnChartEL(datetime time, double price, color arrowColor, string label, int importance) {
// Skip drawing PB1 and PB2 markers on the chart
if(StringFind(label, "PB1") >= 0 || StringFind(label, "PB2") >= 0) {
// Still track them internally but don't display
return;
}
// Include setup ID in the object name for selective deletion
string objName = EL_objectPrefix + "Arrow_" + IntegerToString(EL_currentSetupID) + "_" + TimeToString(time);
string txtName = EL_objectPrefix + "Text_" + IntegerToString(EL_currentSetupID) + "_" + TimeToString(time);
// Create arrow with larger size for more important labels
ObjectCreate(0, objName, OBJ_ARROW, 0, time, price);
ObjectSetInteger(0, objName, OBJPROP_ARROWCODE, 159); // Down arrow (pointing from above)
ObjectSetInteger(0, objName, OBJPROP_COLOR, arrowColor);
// Vertical positioning and font size based on importance
int verticalOffset = 0;
int fontSize = 0;
int arrowWidth = 0;
switch(importance) {
case 0: // Bar0 - most important
verticalOffset = 10; // Positive for above candle
fontSize = 14;
arrowWidth = 4;
break;
case 1: // 2EL - second most important
verticalOffset = 25; // Positive for above candle
fontSize = 12;
arrowWidth = 4;
break;
case 2: // 1EL - third most important
verticalOffset = 20; // Positive for above candle
fontSize = 11;
arrowWidth = 3;
break;
case 3: // 2ELF - important failure
verticalOffset = 35; // Positive for above candle
fontSize = 12;
arrowWidth = 4;
break;
case 4: // PB1/PB2 - least important
verticalOffset = 50; // Positive for above candle
fontSize = 9;
arrowWidth = 2;
// Add transparency for less important markers
arrowColor = (color)ColorToARGB(arrowColor, 180);
break;
}
ObjectSetInteger(0, objName, OBJPROP_WIDTH, arrowWidth);
// Create text label with improved visibility
ObjectCreate(0, txtName, OBJ_TEXT, 0, time, price + verticalOffset * _Point);
ObjectSetString(0, txtName, OBJPROP_TEXT, label);
ObjectSetInteger(0, txtName, OBJPROP_COLOR, arrowColor);
ObjectSetInteger(0, txtName, OBJPROP_FONTSIZE, fontSize);
ObjectSetInteger(0, txtName, OBJPROP_WIDTH, (importance <= 2) ? 2 : 1);
}
//+------------------------------------------------------------------+
//| Update 2ES dashboard with current system state |
//+------------------------------------------------------------------+
void UpdateDashboardES() {
string stateText = "";
switch(ES_currentState) {
case STATE_LOOKING_FOR_BAR0:
stateText = "Looking for Bar0";
break;
case STATE_LOOKING_FOR_PB1:
stateText = "Looking for PB1";
break;
case STATE_LOOKING_FOR_1E:
stateText = "Looking for 1ES";
break;
case STATE_LOOKING_FOR_PB2:
stateText = "Looking for PB2";
break;
case STATE_LOOKING_FOR_2E:
stateText = "Looking for 2ES";
break;
case STATE_FOUND_2E:
stateText = "Found 2ES";
break;
case STATE_AFTER_2EF:
stateText = "After 2ESF, Next Bar = Bar0";
break;
}
ObjectSetString(0, ES_objectPrefix + "State", OBJPROP_TEXT, "State: " + stateText);
ObjectSetString(0, ES_objectPrefix + "Bar0Info", OBJPROP_TEXT, "Bar0: " + TimeToString(ES_bar0Time) + " Low: " + DoubleToString(ES_bar0Low, _Digits));
if(ES_pb1Count > 0)
ObjectSetString(0, ES_objectPrefix + "PB1Info", OBJPROP_TEXT, "PB1: " + IntegerToString(ES_pb1Count) + " pullbacks");
else
ObjectSetString(0, ES_objectPrefix + "PB1Info", OBJPROP_TEXT, "PB1: Not found yet");
if(ES_currentState >= STATE_LOOKING_FOR_PB2)
ObjectSetString(0, ES_objectPrefix + "1ESInfo", OBJPROP_TEXT, "1ES: " + TimeToString(ES_es1Time) + " Low: " + DoubleToString(ES_es1Low, _Digits));
else
ObjectSetString(0, ES_objectPrefix + "1ESInfo", OBJPROP_TEXT, "1ES: Not found yet");
if(ES_pb2Count > 0)
ObjectSetString(0, ES_objectPrefix + "PB2Info", OBJPROP_TEXT, "PB2: " + IntegerToString(ES_pb2Count) + " pullbacks");
else
ObjectSetString(0, ES_objectPrefix + "PB2Info", OBJPROP_TEXT, "PB2: Not found yet");
if(ES_currentState == STATE_FOUND_2E)
ObjectSetString(0, ES_objectPrefix + "2ESInfo", OBJPROP_TEXT, "2ES: " + TimeToString(ES_es2Time) + " Low: " + DoubleToString(ES_es2Low, _Digits));
else
ObjectSetString(0, ES_objectPrefix + "2ESInfo", OBJPROP_TEXT, "2ES: Not found yet");
// Add 2ESF info
if(ES_currentState == STATE_AFTER_2EF)
ObjectSetString(0, ES_objectPrefix + "2ESFInfo", OBJPROP_TEXT, "2ESF detected");
else
ObjectSetString(0, ES_objectPrefix + "2ESFInfo", OBJPROP_TEXT, "2ESF: None detected");
}
//+------------------------------------------------------------------+
//| Update 2EL dashboard with current system state |
//+------------------------------------------------------------------+
void UpdateDashboardEL() {
string stateText = "";
switch(EL_currentState) {
case STATE_LOOKING_FOR_BAR0:
stateText = "Looking for Bar0";
break;
case STATE_LOOKING_FOR_PB1:
stateText = "Looking for PB1";
break;
case STATE_LOOKING_FOR_1E:
stateText = "Looking for 1EL";
break;
case STATE_LOOKING_FOR_PB2:
stateText = "Looking for PB2";
break;
case STATE_LOOKING_FOR_2E:
stateText = "Looking for 2EL";
break;
case STATE_FOUND_2E:
stateText = "Found 2EL";
break;
case STATE_AFTER_2EF:
stateText = "After 2ELF, Next Bar = Bar0";
break;
}
ObjectSetString(0, EL_objectPrefix + "State", OBJPROP_TEXT, "State: " + stateText);
ObjectSetString(0, EL_objectPrefix + "Bar0Info", OBJPROP_TEXT, "Bar0: " + TimeToString(EL_bar0Time) + " High: " + DoubleToString(EL_bar0High, _Digits));
if(EL_pb1Count > 0)
ObjectSetString(0, EL_objectPrefix + "PB1Info", OBJPROP_TEXT, "PB1: " + IntegerToString(EL_pb1Count) + " pullbacks");
else
ObjectSetString(0, EL_objectPrefix + "PB1Info", OBJPROP_TEXT, "PB1: Not found yet");
if(EL_currentState >= STATE_LOOKING_FOR_PB2)
ObjectSetString(0, EL_objectPrefix + "1ELInfo", OBJPROP_TEXT, "1EL: " + TimeToString(EL_el1Time) + " High: " + DoubleToString(EL_el1High, _Digits));
else
ObjectSetString(0, EL_objectPrefix + "1ELInfo", OBJPROP_TEXT, "1EL: Not found yet");
if(EL_pb2Count > 0)
ObjectSetString(0, EL_objectPrefix + "PB2Info", OBJPROP_TEXT, "PB2: " + IntegerToString(EL_pb2Count) + " pullbacks");
else
ObjectSetString(0, EL_objectPrefix + "PB2Info", OBJPROP_TEXT, "PB2: Not found yet");
if(EL_currentState == STATE_FOUND_2E)
ObjectSetString(0, EL_objectPrefix + "2ELInfo", OBJPROP_TEXT, "2EL: " + TimeToString(EL_el2Time) + " High: " + DoubleToString(EL_el2High, _Digits));
else
ObjectSetString(0, EL_objectPrefix + "2ELInfo", OBJPROP_TEXT, "2EL: Not found yet");
// Add 2ELF info
if(EL_currentState == STATE_AFTER_2EF)
ObjectSetString(0, EL_objectPrefix + "2ELFInfo", OBJPROP_TEXT, "2ELF detected");
else
ObjectSetString(0, EL_objectPrefix + "2ELFInfo", OBJPROP_TEXT, "2ELF: None detected");
}