Here is an example of an .mq4 code for an indicator that can find a Piercing Line candle pattern within a chart.
This code provides a basic framework for detecting Piercing Line candle patterns and showing them on a trading chart. It is important to thoroughly test and verify this custom indicator before using it in live trading.
You are welcome to modify and customized the below example to fit your specific trading strategy.
//+------------------------------------------------------------------+
//| Piercing.mq4|
//| Copyright 2023, Richpips|
//| https://www.richpips.com/ |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 LimeGreen
#property indicator_color2 Red
extern int period = 14; // Moving Average period
extern double threshold = 0.5; // Piercing Line threshold
double ma[];
double bullish[];
double bearish[];
int counted_bars = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
SetIndexBuffer(0, bullish);
SetIndexBuffer(1, bearish);
SetIndexStyle(0, DRAW_ARROW);
SetIndexArrow(0, 159);
SetIndexLabel(0, "Piercing Bullish");
SetIndexStyle(1, DRAW_ARROW);
SetIndexArrow(1, 158);
SetIndexLabel(1, "Piercing Bearish");
IndicatorDigits(4);
IndicatorShortName("Piercing");
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i, counted_bars = IndicatorCounted();
double prev_close, prev_open, curr_close, curr_open;
if (counted_bars < 0)
return(-1);
if (counted_bars > 0)
counted_bars--;
for (i = 0; i < Bars; i++)
{
prev_close = Close[i+1];
prev_open = Open[i+1];
curr_close = Close[i];
curr_open = Open[i];
ma[i] = iMA(NULL, 0, period, 0, MODE_SMA, PRICE_CLOSE, i);
if (prev_close < ma[i+1] && curr_open > ma[i] &&
(prev_close - prev_open) / (0.001 + prev_high - prev_low) > threshold &&
(curr_close - curr_open) / (0.001 + curr_high - curr_low) > threshold)
{
bullish[i] = Low[i] - 10 * Point;
bearish[i] = 0;
}
else if (prev_close > ma[i+1] && curr_open < ma[i] &&
(prev_open - prev_close) / (0.001 + prev_high - prev_low) > threshold &&
(curr_open - curr_close) / (0.001 + curr_high - curr_low) > threshold)
{
bullish[i] = 0;
bearish[i] = High[i] + 10 * Point;
}
else
{
bullish[i] = 0;
bearish[i] = 0;
}
}
return(0);
}
Explanation:
- The
init()
function initializes the indicator by setting up the indicator buffers, colors, and labels. - The
deinit()
function is called when the indicator is removed from the chart. - The
start()
function is called for every new price tick, and it is responsible for identifying the Piercing Line candle pattern and setting the values of