We'll reuse state machines, visualization, and alert logic for both patterns in a single Expert Advisor.
mql4 Copy code
//+------------------------------------------------------------------+
//| EMA Double Bottom & Double Top (1-2 Pattern) |
//| Adapted from Pine Script Logic |
//+------------------------------------------------------------------+
#property copyright "Your Name"
#property link "Your Website"
#property version "1.00"
#property strict
#property indicator_chart_window // Overlay on price chart
// ———— Configurable Inputs ————
input int emaPeriod = 20; // EMA Period
input int swingLookback = 1; // Swing Low/High Lookback (N Bars)
input double doublePatternTolerance = 0.5; // Double Pattern Tolerance (%)
input double necklineTouchThreshold = 0.1; // Neckline Touch Threshold (%)
input double patternFailureThreshold = 1.0; // Pattern Failure Threshold (%)
input color emaColor = clrBlue; // EMA Color
input color necklineColor = clrRed; // Neckline Color
input color bottomMarkerColor = clrLimeGreen;// Bottom Marker Color
input color topMarkerColor = clrCrimson; // Top Marker Color
input color alertMarkerColor = clrRed; // Alert Marker Color
// ———— Global Variables (State Management) ————
static int stateBottom = 0; // 0: Idle | 1: 1st Bottom | 2: Below EMA | 3: 2nd Bottom | 4: Alert Triggered (Buy)
static int stateTop = 0; // 0: Idle | 1: 1st Top | 2: Above EMA | 3: 2nd Top | 4: Alert Triggered (Sell)
static double firstLowBottom = 0.0; // 1st Low (Double Bottom)
static double firstHighTop = 0.0; // 1st High (Double Top)
static double necklineBottom = 0.0; // Neckline (Double Bottom)
static double necklineTop = 0.0; // Neckline (Double Top)
static double secondLowBottom = 0.0; // 2nd Low (Double Bottom)
static double secondHighTop = 0.0; // 2nd High (Double Top)
static datetime lastBarTime = 0; // Track last bar time
// ———— Helper Functions ————
bool IsNewBar() {
if (Time[0] != lastBarTime) {
lastBarTime = Time[0];
return true;
}
return false;
}
double PercentDiff(double price1, double price2) {
return MathAbs((price1 - price2) / price2) * 100;
}
// ———— Confirmed Swing Low/High Detection ————
bool IsConfirmed(HighLowType hlType, int shift) {
if (shift < swingLookback) return false;
if (hlType == HighLowType_Low) { // Swing Low
bool isLowerThanPast = true;
for (int i = 1; i <= swingLookback; i++) {
if (iLow(_Symbol, _Period, shift) >= iLow(_Symbol, _Period, shift + i)) {
isLowerThanPast = false;
break;
}
}
bool isLowerThanFuture = true;
for (int i = 1; i <= swingLookback; i++) {
if (shift - i < 0) break;
if (iLow(_Symbol, _Period, shift) >= iLow(_Symbol, _Period, shift - i)) {
isLowerThanFuture = false;
break;
}
}
return isLowerThanPast && isLowerThanFuture;
}
else { // Swing High
bool isHigherThanPast = true;
for (int i = 1; i <= swingLookback; i++) {
if (iHigh(_Symbol, _Period, shift) <= iHigh(_Symbol, _Period, shift + i)) {
isHigherThanPast = false;
break;
}
}
bool isHigherThanFuture = true;
for (int i = 1; i <= swingLookback; i++) {
if (shift - i < 0) break;
if (iHigh(_Symbol, _Period, shift) <= iHigh(_Symbol, _Period, shift - i)) {
isHigherThanFuture = false;
break;
}
}
return isHigherThanPast && isHigherThanFuture;
}
}
enum HighLowType {
HighLowType_Low,
HighLowType_High
};
void DrawTextMarker(datetime barTime, double price, string text, color markerColor, bool belowBar = true) {
string objName = StringConcatenate("TextMarker_", barTime);
if (ObjectFind(objName) != -1) ObjectDelete(objName);
double yOffset = belowBar ? -0.0005 * price : 0.0005 * price;
ObjectCreate(objName, OBJ_TEXT, 0, barTime, price + yOffset);
ObjectSetString(0, objName, OBJPROP_TEXT, text);
ObjectSetInteger(0, objName, OBJPROP_COLOR, markerColor);
ObjectSetInteger(0, objName, OBJPROP_FONT_SIZE, 8);
ObjectSetInteger(0, objName, OBJPROP_ALIGN, ALIGN_CENTER);
}
void DrawNeckline(double price, datetime startTime, datetime endTime, bool isBottom, bool isConfirmed = false) {
string objName = isBottom ? (isConfirmed ? "Neckline_Bottom_Confirmed" : "Neckline_Bottom_Temp")
: (isConfirmed ? "Neckline_Top_Confirmed" : "Neckline_Top_Temp");
if (ObjectFind(objName) != -1) ObjectDelete(objName);
ObjectCreate(objName, OBJ_TREND, 0, startTime, price, endTime, price);
ObjectSetInteger(0, objName, OBJPROP_COLOR, isConfirmed ? necklineColor : clrOrange);
ObjectSetInteger(0, objName, OBJPROP_STYLE, isConfirmed ? STYLE_DASH : STYLE_DOT);
ObjectSetInteger(0, objName, OBJPROP_WIDTH, isConfirmed ? 2 : 1);
if (isConfirmed) {
string labelName = isBottom ? "Neckline_Bottom_Label" : "Neckline_Top_Label";
ObjectCreate(labelName, OBJ_TEXT, 0, startTime, price + (isBottom ? 0.0005 * price : -0.0005 * price));
ObjectSetString(0, labelName, OBJPROP_TEXT, StringFormat("Neckline: %.2f", price));
ObjectSetInteger(0, labelName, OBJPROP_COLOR, necklineColor);
ObjectSetInteger(0, labelName, OBJPROP_FONT_SIZE, 8);
}
}
//+------------------------------------------------------------------+
//| Expert Advisor tick function |
//+------------------------------------------------------------------+
void OnTick() {
if (!IsNewBar()) return;
double ema = iMA(_Symbol, _Period, emaPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
static string emaLineName = "EMA_Line";
if (ObjectFind(emaLineName) == -1) {
ObjectCreate(emaLineName, OBJ_TREND, 0, Time[1], iMA(_Symbol, _Period, emaPeriod, 0, MODE_EMA, PRICE_CLOSE, 1),
Time[0], iMA(_Symbol, _Period, emaPeriod, 0, MODE_EMA, PRICE_CLOSE, 0));
} else {
ObjectMove(emaLineName, 0, Time[1], iMA(_Symbol, _Period, emaPeriod, 0, MODE_EMA, PRICE_CLOSE, 1));
ObjectMove(emaLineName, 1, Time[0], iMA(_Symbol, _Period, emaPeriod, 0, MODE_EMA, PRICE_CLOSE, 0));
}
ObjectSetInteger(0, emaLineName, OBJPROP_COLOR, emaColor);
ObjectSetInteger(0, emaLineName, OBJPROP_WIDTH, 2);
// ———————— DOUBLE BOTTOM LOGIC ————————
if (stateBottom == 0) { // Idle → Look for 1st Bottom
bool isTouchEmaFromBelow = (Close[2] < ema) && (Low[1] <= ema) && (Close[1] > ema);
if (isTouchEmaFromBelow && IsConfirmed(HighLowType_Low, 1)) {
stateBottom = 1;
firstLowBottom = Low[1];
DrawTextMarker(Time[1], Low[1], "1", bottomMarkerColor, true);
Print("StateBottom 0 → 1: 1st Bottom Confirmed at ", firstLowBottom);
}
}
else if (stateBottom == 1) { // 1st Bottom → Track Neckline Candidate
if (High[1] > necklineBottom || necklineBottom == 0.0) {
necklineBottom = High[1];
DrawNeckline(necklineBottom, Time[1], Time[1] + (Period() * 60) * 20, true, false);
}
if (Close[1] < ema) {
stateBottom = 2;
Print("StateBottom 1 → 2: Price Closed Below EMA");
}
if (PercentDiff(Low[1], firstLowBottom) > patternFailureThreshold) {
stateBottom = 0;
firstLowBottom = 0.0;
necklineBottom = 0.0;
Print("StateBottom 1 → 0: Pattern Failed (Price Below Failure Threshold)");
}
}
else if (stateBottom == 2) { // Below EMA → Look for 2nd Bottom
bool is2ndBottom = IsConfirmed(HighLowType_Low, 1) && (Close[1] < ema) &&
(PercentDiff(Low[1], firstLowBottom) <= doublePatternTolerance);
if (is2ndBottom) {
stateBottom = 3;
secondLowBottom = Low[1];
necklineBottom = MathMax(necklineBottom, High[1]);
DrawTextMarker(Time[1], Low[1], "2", bottomMarkerColor, true);
DrawNeckline(necklineBottom, Time[1], Time[1] + (Period() * 60) * 20, true, true);
Print("StateBottom 2 → 3: 2nd Bottom Confirmed at ", secondLowBottom, ". Neckline: ", necklineBottom);
}
if (PercentDiff(Low[1], firstLowBottom) > patternFailureThreshold) {
stateBottom = 0;
firstLowBottom = 0.0;
necklineBottom = 0.0;
Print("StateBottom 2 → 0: Pattern Failed (Price Below Failure Threshold)");
}
}
else if (stateBottom == 3) { // 2nd Bottom → Wait for Neckline Touch
double necklineThreshold = necklineBottom * (1 - (necklineTouchThreshold / 100));
bool isNecklineTouched = (High[1] >= necklineThreshold) || (Close[1] > necklineBottom);
if (isNecklineTouched) {
stateBottom = 4;
Alert(StringFormat("Double Bottom: Neckline Touched!\nTicker: %s\nTimeframe: %s\nNeckline: %.2f\nCurrent Price: %.2f",
_Symbol, EnumToString(_Period), necklineBottom, Close[1]));
DrawTextMarker(Time[1], High[1], "⚠ Neckline!", alertMarkerColor, false);
Print("StateBottom 3 → 4: Neckline Touched at ", Close[1]);
}
if (PercentDiff(Low[1], secondLowBottom) > patternFailureThreshold) {
stateBottom = 0;
firstLowBottom = 0.0;
necklineBottom = 0.0;
secondLowBottom = 0.0;
Print("StateBottom 3 → 0: Pattern Failed (Price Below Failure Threshold)");
}
}
else if (stateBottom == 4) {
stateBottom = 0;
firstLowBottom = 0.0;
necklineBottom = 0.0;
secondLowBottom = 0.0;
Print("StateBottom 4 → 0: Alert Triggered → Reset");
}
// ———————— DOUBLE TOP LOGIC ————————
if (stateTop == 0) { // Idle → Look for 1st Top
bool isTouchEmaFromAbove = (Close[2] > ema) && (High[1] >= ema) && (Close[1] < ema);
if (isTouchEmaFromAbove && IsConfirmed(HighLowType_High, 1)) {
stateTop = 1;
firstHighTop = High[1];
DrawTextMarker(Time[1], High[1], "1", topMarkerColor, false);
Print("StateTop 0 → 1: 1st Top Confirmed at ", firstHighTop);
}
}
else if (stateTop == 1) { // 1st Top → Track Neckline Candidate
if (Low[1] < necklineTop || necklineTop == 0.0) {
necklineTop = Low[1];
DrawNeckline(necklineTop, Time[1], Time[1] + (Period() * 60) * 20, false, false);
}
if (Close[1] > ema) {
stateTop = 2;
Print("StateTop 1 → 2: Price Closed Above EMA");
}
if (PercentDiff(High[1], firstHighTop) > patternFailureThreshold) {
stateTop = 0;
firstHighTop = 0.0;
necklineTop = 0.0;
Print("StateTop 1 → 0: Pattern Failed (Price Above Failure Threshold)");
}
}
else if (stateTop == 2) { // Above EMA → Look for 2nd Top
bool is2ndTop = IsConfirmed(HighLowType_High, 1) && (Close[1] > ema) &&
(PercentDiff(High[1], firstHighTop) <= doublePatternTolerance);
if (is2ndTop) {
stateTop = 3;
secondHighTop = High[1];
necklineTop = MathMin(necklineTop, Low[1]);
DrawTextMarker(Time[1], High[1], "2", topMarkerColor, false);
DrawNeckline(necklineTop, Time[1], Time[1] + (Period() * 60) * 20, false, true);
Print("StateTop 2 → 3: 2nd Top Confirmed at ", secondHighTop, ". Neckline: ", necklineTop);
}
if (PercentDiff(High[1], firstHighTop) > patternFailureThreshold) {
stateTop = 0;
firstHighTop = 0.0;
necklineTop = 0.0;
Print("StateTop 2 → 0: Pattern Failed (Price Above Failure Threshold)");
}
}
else if (stateTop == 3) { // 2nd Top → Wait for Neckline Touch
double necklineThreshold = necklineTop * (1 + (necklineTouchThreshold / 100));
bool isNecklineTouched = (Low[1] <= necklineThreshold) || (Close[1] < necklineTop);
if (isNecklineTouched) {
stateTop = 4;
Alert(StringFormat("Double Top: Neckline Touched!\nTicker: %s\nTimeframe: %s\nNeckline: %.2f\nCurrent Price: %.2f",
_Symbol, EnumToString(_Period), necklineTop, Close[1]));
DrawTextMarker(Time[1], Low[1], "⚠ Neckline!", alertMarkerColor, true);
Print("StateTop 3 → 4: Neckline Touched at ", Close[1]);
}
if (PercentDiff(High[1], secondHighTop) > patternFailureThreshold) {
stateTop = 0;
firstHighTop = 0.0;
necklineTop = 0.0;
secondHighTop = 0.0;
Print("StateTop 3 → 0: Pattern Failed (Price Above Failure Threshold)");
}
}
else if (stateTop == 4) {
stateTop = 0;
firstHighTop = 0.0;
necklineTop = 0.0;
secondHighTop = 0.0;
Print("StateTop 4 → 0: Alert Triggered → Reset");
}
}