135 lines
5.2 KiB
MQL5
135 lines
5.2 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| Article-22469-Candlestick-Alphabetical-Encoding-System.mq5 |
|
|
//| Copyright 2023, MetaQuotes Ltd. |
|
|
//| https://www.mql5.com |
|
|
//+------------------------------------------------------------------+
|
|
#property script_show_inputs
|
|
#property strict
|
|
|
|
input int lookback = 500;
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Custom indicator initialization function |
|
|
//+------------------------------------------------------------------+
|
|
int OnInit()
|
|
{
|
|
//--- indicator buffers mapping
|
|
return(INIT_SUCCEEDED);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Custom indicator iteration function |
|
|
//+------------------------------------------------------------------+
|
|
int OnCalculate(const int32_t rates_total,
|
|
const int32_t 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 int32_t &spread[])
|
|
{
|
|
//--- set arrays as series
|
|
ArraySetAsSeries(time, true);
|
|
static datetime lastTime;
|
|
int Lmt = (prev_calculated == 0) ?
|
|
MathMin(rates_total - 3, lookback - 3) : 3;
|
|
if(lastTime == time[0])
|
|
return 0;
|
|
lastTime = time[0];
|
|
for(int i = Lmt; i >= 0; i--)
|
|
{
|
|
CheckBullishPattern(i);
|
|
CheckBearishPattern(i);
|
|
}
|
|
//--- return value of prev_calculated for next call
|
|
return(rates_total);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Candle Type labeling function |
|
|
//+------------------------------------------------------------------+
|
|
string CandleType(int shift)
|
|
{
|
|
//--- Get price data for the specified candle
|
|
double open = iOpen(NULL, 0, shift); // Price Open
|
|
double close = iClose(NULL, 0, shift); // Price Close
|
|
double high = iHigh(NULL, 0, shift); // Price High
|
|
double low = iLow(NULL, 0, shift); // Price Low
|
|
|
|
//--- Calculate candle components
|
|
double body = MathAbs(close - open);
|
|
double upperWick = high - MathMax(open, close);
|
|
double lowerWick = MathMin(open, close) - low;
|
|
|
|
//--- Handle bullish candles
|
|
if(close > open)
|
|
{
|
|
if(body > 1.5 * upperWick && body > 1.5 * lowerWick) // long, short, marubozu
|
|
return "A";
|
|
if(2 * body < upperWick && 2 * body < lowerWick) // spinning top
|
|
return "G";
|
|
if(lowerWick > 2.5 * body && lowerWick > 2 * upperWick) // pinbar
|
|
return "H";
|
|
if(upperWick > 2.5 * body && upperWick > 2 * lowerWick) // inverted pinbar
|
|
return "E";
|
|
}
|
|
//--- Handle bearish candles
|
|
else
|
|
if(close < open)
|
|
{
|
|
if(body > 1.5 * upperWick && body > 1.5 * lowerWick)
|
|
return "a";
|
|
if(2 * body < upperWick && 2 * body < lowerWick)
|
|
return "g";
|
|
if(lowerWick > 2.5 * body && lowerWick > 2 * upperWick)
|
|
return "h";
|
|
if(upperWick > 2.5 * body && upperWick > 2 * lowerWick)
|
|
return "e";
|
|
}
|
|
else
|
|
if(close == open)
|
|
return "D"; // doji
|
|
//--- Return empty string if no conditions met
|
|
return "";
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Check for bullish two-candle patterns and print if found |
|
|
//+------------------------------------------------------------------+
|
|
void CheckBullishPattern(int shift)
|
|
{
|
|
string candle2 = CandleType(shift + 1); // older candle
|
|
string candle1 = CandleType(shift); // newer candle
|
|
string pattern = candle2 + candle1;
|
|
//--- Bullish patterns (uppercase pairs)
|
|
if(pattern == "AH" || pattern == "AE" || pattern == "AG" ||
|
|
pattern == "HA" || pattern == "HE" || pattern == "HG" ||
|
|
pattern == "EA" || pattern == "EH" || pattern == "EG" ||
|
|
pattern == "GA" || pattern == "GH" || pattern == "GE")
|
|
{
|
|
Print("Bullish pattern detected: ", pattern);
|
|
Alert("Bullish pattern detected: ", pattern);
|
|
}
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Check for bearish two-candle patterns and print if found |
|
|
//+------------------------------------------------------------------+
|
|
void CheckBearishPattern(int shift)
|
|
{
|
|
string candle2 = CandleType(shift + 1);
|
|
string candle1 = CandleType(shift);
|
|
string pattern = candle2 + candle1;
|
|
//--- Bearish patterns (lowercase pairs)
|
|
if(pattern == "ah" || pattern == "ae" || pattern == "ag" ||
|
|
pattern == "ha" || pattern == "he" || pattern == "hg" ||
|
|
pattern == "ea" || pattern == "eh" || pattern == "eg" ||
|
|
pattern == "ga" || pattern == "gh" || pattern == "ge")
|
|
{
|
|
Print("Bearish pattern detected: ", pattern);
|
|
Alert("Bearish pattern detected: ", pattern);
|
|
}
|
|
}
|
|
//+------------------------------------------------------------------+
|