MQL4 Two-Bar Auction Pattern Indicator
Here's a complete MQL4 custom indicator for testing the two-bar auction pattern theory:
//+------------------------------------------------------------------+
//| TwoBarAuctionPattern.mq4 |
//| Multi-Timeframe Auction |
//| Oscillator (MTAO) |
//+------------------------------------------------------------------+
#property copyright "Two-Bar Auction Pattern Indicator"
#property link ""
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 6
// Buffer properties
#property indicator_color1 clrLime // Buy arrow
#property indicator_color2 clrRed // Sell arrow
#property indicator_color3 clrDodgerBlue // Low phase marker
#property indicator_color4 clrOrange // High phase marker
#property indicator_color5 clrYellow // Strong signal
#property indicator_color6 clrMagenta // Phase shift warning
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 1
#property indicator_width4 1
#property indicator_width5 3
#property indicator_width6 2
// Input parameters
input int CalibrationBars = 100; // Bars to calibrate pattern
input int PhaseShift = 0; // Manual phase adjustment (0 or 1)
input double MinAuctionScore = 60; // Minimum auction score (0-100)
input bool ShowPhaseMarkers = true; // Show LOW/HIGH phase dots
input bool UseMultiTimeframe = true; // Include H1 analysis
input int SignalSensitivity = 70; // Signal sensitivity (0-100)
input bool ShowAlerts = true; // Enable alerts
// Buffers
double BuyBuffer[];
double SellBuffer[];
double LowPhaseBuffer[];
double HighPhaseBuffer[];
double StrongSignalBuffer[];
double PhaseShiftBuffer[];
// Global variables
int g_referenceBar = 0;
double g_auctionScore = 0;
bool g_patternCalibrated = false;
int g_lastAlertBar = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Set buffer indices
SetIndexBuffer(0, BuyBuffer);
SetIndexBuffer(1, SellBuffer);
SetIndexBuffer(2, LowPhaseBuffer);
SetIndexBuffer(3, HighPhaseBuffer);
SetIndexBuffer(4, StrongSignalBuffer);
SetIndexBuffer(5, PhaseShiftBuffer);
// Set arrow codes for signals
SetIndexStyle(0, DRAW_ARROW);
SetIndexArrow(0, 233); // Up arrow
SetIndexStyle(1, DRAW_ARROW);
SetIndexArrow(1, 234); // Down arrow
// Set dot style for phase markers
SetIndexStyle(2, DRAW_ARROW);
SetIndexArrow(2, 159); // Dot for LOW phase
SetIndexStyle(3, DRAW_ARROW);
SetIndexArrow(3, 159); // Dot for HIGH phase
// Strong signal marker
SetIndexStyle(4, DRAW_ARROW);
SetIndexArrow(4, 178); // Strong signal marker
// Phase shift warning
SetIndexStyle(5, DRAW_ARROW);
SetIndexArrow(5, 251); // Warning symbol
// Set empty values
SetIndexEmptyValue(0, 0.0);
SetIndexEmptyValue(1, 0.0);
SetIndexEmptyValue(2, 0.0);
SetIndexEmptyValue(3, 0.0);
SetIndexEmptyValue(4, 0.0);
SetIndexEmptyValue(5, 0.0);
// Set indicator labels
SetIndexLabel(0, "Buy Signal");
SetIndexLabel(1, "Sell Signal");
SetIndexLabel(2, "LOW Phase");
SetIndexLabel(3, "HIGH Phase");
SetIndexLabel(4, "Strong Signal");
SetIndexLabel(5, "Phase Shift");
// Initialize
g_referenceBar = CalibratePatter();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Calibrate the pattern to find reference point |
//+------------------------------------------------------------------+
int CalibratePatter()
{
if(Bars < CalibrationBars) return 0;
double bestScore = 0;
int bestReference = 0;
// Test both phase possibilities
for(int phase = 0; phase <= 1; phase++)
{
double score = 0;
for(int i = CalibrationBars; i >= 20; i--)
{
bool isLowPhase = IsLowPhase(i, phase);
double barScore = CalculateAuctionScore(i, isLowPhase);
score += barScore;
}
if(score > bestScore)
{
bestScore = score;
bestReference = phase;
}
}
g_patternCalibrated = true;
return bestReference + PhaseShift;
}
//+------------------------------------------------------------------+
//| Determine if bar is in LOW or HIGH phase |
//+------------------------------------------------------------------+
bool IsLowPhase(int barIndex, int reference)
{
return ((barIndex + reference) % 2 == 0);
}
//+------------------------------------------------------------------+
//| Calculate auction completion score for a bar |
//+------------------------------------------------------------------+
double CalculateAuctionScore(int barIndex, bool isLowPhase)
{
if(barIndex >= Bars - 1) return 0;
double high = High[barIndex];
double low = Low[barIndex];
double open = Open[barIndex];
double close = Close[barIndex];
double range = high - low;
if(range == 0) return 50; // Doji bar
double score = 0;
double bodyRange = MathAbs(close - open);
double upperWick = high - MathMax(open, close);
double lowerWick = MathMin(open, close) - low;
if(isLowPhase) // Should create/test lows
{
// Type i: Immediate downtrend
if(close < open && bodyRange > range * 0.6)
{
score = 80 + (20 * (open - close) / range);
}
// Type ii: High rejection
else if(upperWick > range * 0.5 && close < open + range * 0.3)
{
score = 60 + (20 * upperWick / range);
}
// Type iii: Close near highs (setting up for next bar)
else if(close > high - range * 0.25)
{
score = 40 + (20 * (close - low) / range);
}
else
{
score = 30; // Weak auction
}
}
else // HIGH phase - should create/test highs
{
// Type i: Immediate uptrend
if(close > open && bodyRange > range * 0.6)
{
score = 80 + (20 * (close - open) / range);
}
// Type ii: Low rejection
else if(lowerWick > range * 0.5 && close > open - range * 0.3)
{
score = 60 + (20 * lowerWick / range);
}
// Type iii: Close near lows (setting up for next bar)
else if(close < low + range * 0.25)
{
score = 40 + (20 * (high - close) / range);
}
else
{
score = 30; // Weak auction
}
}
return MathMin(100, MathMax(0, score));
}
//+------------------------------------------------------------------+
//| Check for multi-timeframe confluence |
//+------------------------------------------------------------------+
double GetMultiTimeframeScore(int barIndex)
{
if(!UseMultiTimeframe) return 0;
double mtfScore = 0;
// Get H1 bar information
int h1BarIndex = iBarShift(NULL, PERIOD_H1, Time[barIndex]);
if(h1BarIndex >= 0)
{
bool h1LowPhase = IsLowPhase(h1BarIndex, g_referenceBar);
double h1Score = CalculateH1Score(h1BarIndex, h1LowPhase);
// Current M15 phase
bool m15LowPhase = IsLowPhase(barIndex, g_referenceBar);
// Confluence bonus
if((m15LowPhase && h1LowPhase) || (!m15LowPhase && !h1LowPhase))
{
mtfScore += h1Score * 0.5;
}
else
{
mtfScore -= h1Score * 0.25; // Divergence penalty
}
}
return mtfScore;
}
//+------------------------------------------------------------------+
//| Calculate H1 auction score |
//+------------------------------------------------------------------+
double CalculateH1Score(int h1BarIndex, bool isLowPhase)
{
double h1High = iHigh(NULL, PERIOD_H1, h1BarIndex);
double h1Low = iLow(NULL, PERIOD_H1, h1BarIndex);
double h1Open = iOpen(NULL, PERIOD_H1, h1BarIndex);
double h1Close = iClose(NULL, PERIOD_H1, h1BarIndex);
double h1Range = h1High - h1Low;
if(h1Range == 0) return 50;
double score = 0;
double bodyRange = MathAbs(h1Close - h1Open);
if(isLowPhase && h1Close < h1Open)
{
score = 60 + (40 * bodyRange / h1Range);
}
else if(!isLowPhase && h1Close > h1Open)
{
score = 60 + (40 * bodyRange / h1Range);
}
else
{
score = 40;
}
return score;
}
//+------------------------------------------------------------------+
//| Generate trading signals |
//+------------------------------------------------------------------+
void GenerateSignals(int barIndex)
{
if(barIndex >= Bars - 2 || !g_patternCalibrated) return;
bool isLowPhase = IsLowPhase(barIndex, g_referenceBar);
double auctionScore = CalculateAuctionScore(barIndex, isLowPhase);
double mtfScore = GetMultiTimeframeScore(barIndex);
double totalScore = auctionScore + mtfScore;
// Clear buffers for this bar
BuyBuffer[barIndex] = 0;
SellBuffer[barIndex] = 0;
StrongSignalBuffer[barIndex] = 0;
// Check for BUY signal (completing LOW phase, expecting HIGH phase)
if(isLowPhase && auctionScore >= MinAuctionScore)
{
// Check for swing low
bool isSwingLow = (Low[barIndex] < Low[barIndex+1] &&
Low[barIndex] < Low[barIndex+2]);
if(isSwingLow || auctionScore >= 80)
{
BuyBuffer[barIndex] = Low[barIndex] - GetSignalDistance();
// Strong signal if multi-timeframe confirms
if(totalScore >= SignalSensitivity + 20)
{
StrongSignalBuffer[barIndex] = Low[barIndex] - GetSignalDistance() * 2;
}
// Alert
if(ShowAlerts && barIndex == 0 && g_lastAlertBar != Bars)
{
Alert("Two-Bar Auction BUY Signal: ", Symbol(), " ", Period(),
" Score: ", DoubleToStr(totalScore, 1));
g_lastAlertBar = Bars;
}
}
}
// Check for SELL signal (completing HIGH phase, expecting LOW phase)
else if(!isLowPhase && auctionScore >= MinAuctionScore)
{
// Check for swing high
bool isSwingHigh = (High[barIndex] > High[barIndex+1] &&
High[barIndex] > High[barIndex+2]);
if(isSwingHigh || auctionScore >= 80)
{
SellBuffer[barIndex] = High[barIndex] + GetSignalDistance();
// Strong signal if multi-timeframe confirms
if(totalScore >= SignalSensitivity + 20)
{
StrongSignalBuffer[barIndex] = High[barIndex] + GetSignalDistance() * 2;
}
// Alert
if(ShowAlerts && barIndex == 0 && g_lastAlertBar != Bars)
{
Alert("Two-Bar Auction SELL Signal: ", Symbol(), " ", Period(),
" Score: ", DoubleToStr(totalScore, 1));
g_lastAlertBar = Bars;
}
}
}
}
//+------------------------------------------------------------------+
//| Get appropriate distance for signal arrows |
//+------------------------------------------------------------------+
double GetSignalDistance()
{
double atr = iATR(NULL, 0, 14, 0);
return atr * 0.5;
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
int limit = rates_total - prev_calculated;
if(prev_calculated > 0) limit++;
// Limit calculation to recent bars for performance
if(limit > 500) limit = 500;
// Process bars
for(int i = limit - 1; i >= 0; i--)
{
// Mark phase if enabled
if(ShowPhaseMarkers)
{
bool isLowPhase = IsLowPhase(i, g_referenceBar);
if(isLowPhase)
{
LowPhaseBuffer[i] = Low[i] - GetSignalDistance() * 0.3;
HighPhaseBuffer[i] = 0;
}
else
{
HighPhaseBuffer[i] = High[i] + GetSignalDistance() * 0.3;
LowPhaseBuffer[i] = 0;
}
}
// Generate trading signals
GenerateSignals(i);
// Check for phase shift (pattern break)
if(i > 0 && i < limit - 3)
{
CheckPhaseShift(i);
}
}
// Display info
if(rates_total > CalibrationBars)
{
DisplayInfo();
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| Check for pattern phase shift |
//+------------------------------------------------------------------+
void CheckPhaseShift(int barIndex)
{
double score1 = CalculateAuctionScore(barIndex, IsLowPhase(barIndex, g_referenceBar));
double score2 = CalculateAuctionScore(barIndex+1, IsLowPhase(barIndex+1, g_referenceBar));
double score3 = CalculateAuctionScore(barIndex+2, IsLowPhase(barIndex+2, g_referenceBar));
double avgScore = (score1 + score2 + score3) / 3;
// If average score is very low, pattern might be shifting
if(avgScore < 40)
{
PhaseShiftBuffer[barIndex] = High[barIndex] + GetSignalDistance() * 3;
}
else
{
PhaseShiftBuffer[barIndex] = 0;
}
}
//+------------------------------------------------------------------+
//| Display information on chart |
//+------------------------------------------------------------------+
void DisplayInfo()
{
string info = "Two-Bar Auction Pattern\n";
info += "Current Phase: " + (IsLowPhase(0, g_referenceBar) ? "LOW" : "HIGH") + "\n";
info += "Auction Score: " + DoubleToStr(CalculateAuctionScore(0, IsLowPhase(0, g_referenceBar)), 1) + "\n";
info += "Pattern Calibrated: " + (g_patternCalibrated ? "Yes" : "No");
Comment(info);
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
// Recalibrate pattern periodically
if(Bars % 200 == 0)
{
g_referenceBar = CalibratePatter();
}
}
//+------------------------------------------------------------------+
//| Deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
ObjectsDeleteAll();
}
//+------------------------------------------------------------------+
Installation and Usage Instructions:
-
Installation:
- Save the code as
TwoBarAuctionPattern.mq4 in your MT4/MQL4/Indicators folder
- Compile in MetaEditor (F7)
- Restart MT4 or refresh the Navigator
-
Setup for Testing:
- Open XAU/USD M15 chart
- Drag the indicator onto the chart
- Use default settings or adjust:
CalibrationBars: 100 (for pattern detection)
MinAuctionScore: 60 (signal quality threshold)
ShowPhaseMarkers: true (to see LOW/HIGH phases)
-
Visual Signals:
- Green arrows (↑): Buy signals at LOW phase completion
- Red arrows (↓): Sell signals at HIGH phase completion
- Blue dots: LOW phase bars
- Orange dots: HIGH phase bars
- Yellow squares: Strong signals with high confluence
- Magenta symbols: Phase shift warnings
-
Testing Strategy:
- The indicator will automatically calibrate to find the pattern
- Look for arrows appearing at phase completions
- Strong signals (yellow) have higher probability
- Check the Comment area for current phase and score
-
Optimization Tips:
- Adjust
MinAuctionScore for more/fewer signals
- Toggle
UseMultiTimeframe to test with/without H1 confluence
- Use
PhaseShift (0 or 1) if pattern seems offset
The indicator will show you exactly when the two-bar auction pattern completes and generates tradeable signals based on the theory you described.