Pattern_Hammer_Shooting Star
Download scriptThis script is designed as a technical analysis tool for the MetaTrader platform. Its primary purpose is to automatically scan price charts to identify specific candlestick patterns that may signal a potential reversal in market direction. By automating this visual search, the tool helps traders spot key momentum shifts more efficiently.
Core Functionality
The logic operates by analyzing historical price data to detect two main types of candlestick formations: Bearish Shooting Stars and Bullish Hammers. These are "reversal" patterns that suggest a trend might be exhausting itself and heading in the opposite direction.
1. Pattern Identification Logic
To determine if a candle qualifies as a specific pattern, the script evaluates several geometric criteria simultaneously:
- Bearish Shooting Stars: The script looks for candles where the upper "wick" (the line showing price movement above the body) is significantly longer than the lower wick and the actual body of the candle. It also checks that these candles occur at relative highs compared to the preceding bars.
- Bullish Hammers: Conversely, the script looks for candles where the lower wick is much longer than the upper wick and the body. It specifically searches for these at relative lows in the price action.
2. Mathematical Precision
Rather than just looking at the "look" of a candle, the logic uses specific mathematical ratios to ensure accuracy:
- Body-to-Wick Ratios: It compares the size of the wicks against the size of the candle body based on customizable percentage settings.
- Relative Volatility: It calculates the average range of recent candles to ensure the patterns identified are significant enough to be noteworthy within the current market context.
- Minimum Size: It includes a filter to ensure the candles have a minimum "length" (size), preventing the tool from flagging tiny, insignificant price fluctuations.
Visual and Audible Feedback
When the script identifies one of these patterns, it performs three actions:
- Visual Markers: It places arrows on the chart (Green for Bullish/Hammer, Yellow/Down for Bearish/Shooting Star) to highlight exactly where the pattern occurred.
- On-Screen Labels: It prints text labels (e.g., "HMR" for Hammer or "SS" for Shooting Star) directly on the chart so you can see the specific variation detected.
- Real-Time Alerts: If enabled, the script will trigger a pop-up alert and notification the moment a pattern is identified on the current live candle, providing immediate notice to the trader.
User Customization
The logic is built with several "switches" that allow a user to toggle specific features on or off, such as choosing whether to display the labels, which specific types of patterns to alert on, and how far away from the candle the visual markers should be placed.
//+------------------------------------------------------------------+
//| Pattern Recognition v1.0 |
//| (complete rewrite and name change of pattern alert) |
//| |
//| Copyright © 2005, Jason Robinson |
//| (jasonrobinsonuk, jnrtrading) |
//| http://www.jnrtrading.co.uk |
//| |
//| This is still work in progress and needs LOTS of testing |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Jason Robinson (jnrtrading)."
#property link "http://www.jnrtrading.co.uk"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Yellow
#property indicator_color2 Yellow
extern bool Show_Alert = true;
extern int Pointer_Offset = 9;
extern int High_Offset = 10;
extern bool Display_ShootStar = true;
extern bool Show_ShootStar_Alert = true;
extern int Offset_ShootStar = 12;
extern color Color_ShootStar = Green;
extern int Text_ShootStar = 8;
extern bool Display_Hammer = true;
extern bool Show_Hammer_Alert = true;
extern int Offset_Hammer = 6;
extern color Color_Hammer = Green;
extern int Text_Hammer = 8;
extern double Candle_WickBody_Percent=0.90;
extern int CandleLength=12;
//---- buffers
double upArrow[];
double downArrow[];
string PatternText[10000];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init() {
//---- indicators
SetIndexStyle(0,DRAW_ARROW, EMPTY);
SetIndexArrow(0,72);
SetIndexBuffer(0, downArrow);
SetIndexStyle(1,DRAW_ARROW, EMPTY);
SetIndexArrow(1,71);
SetIndexBuffer(1, upArrow);
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit() {
ObjectsDeleteAll(0, OBJ_TEXT);
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start(){
double Range, AvgRange;
int counter, setalert;
static datetime prevtime = 0;
int shift;
int shift1;
int shift2;
int shift3;
int shift4;
string pattern, period;
int setPattern = 0;
int alert = 0;
int arrowShift;
int textShift;
double O, O1, O2, C, C1, C2, C3, L, L1, L2, L3, H, H1, H2, H3;
double CL, CLmin, CL1, CL2, BL, BLa, BL90, BL1, BL2, UW, UWa, UW1, UW2, LW, LWa, LW1, LW2, BodyHigh, BodyLow;
BodyHigh = 0;
BodyLow = 0;
if(prevtime == Time[0]) {
return(0);
}
prevtime = Time[0];
switch (Period()) {
case 1:
period = "M1";
break;
case 5:
period = "M5";
break;
case 15:
period = "M15";
break;
case 30:
period = "M30";
break;
case 60:
period = "H1";
break;
case 240:
period = "H4";
break;
case 1440:
period = "D1";
break;
case 10080:
period = "W1";
break;
case 43200:
period = "MN";
break;
}
for (int j = 0; j < Bars; j++) {
PatternText[j] = "pattern-" + j;
}
for (shift = 0; shift < Bars; shift++) {
setalert = 0;
counter=shift;
Range=0;
AvgRange=0;
for (counter=shift ;counter<=shift+9;counter++) {
AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
}
Range=AvgRange/10;
shift1 = shift + 1;
shift2 = shift + 2;
shift3 = shift + 3;
shift4 = shift + 4;
O = Open[shift1];
O1 = Open[shift2];
O2 = Open[shift3];
H = High[shift1];
H1 = High[shift2];
H2 = High[shift3];
H3 = High[shift4];
L = Low[shift1];
L1 = Low[shift2];
L2 = Low[shift3];
L3 = Low[shift4];
C = Close[shift1];
C1 = Close[shift2];
C2 = Close[shift3];
C3 = Close[shift4];
if (O>C) {
BodyHigh = O;
BodyLow = C; }
else {
BodyHigh = C;
BodyLow = O; }
CL = High[shift1]-Low[shift1];
CL1 = High[shift2]-Low[shift2];
CL2 = High[shift3]-Low[shift3];
BL = Open[shift1]-Close[shift1];
UW = High[shift1]-BodyHigh;
LW = BodyLow-Low[shift1];
BLa = MathAbs(BL);
BL90 = BLa*Candle_WickBody_Percent;
// Bearish Patterns
// Check for Bearish Shooting Star
if ((H>=H1)&&(H>H2)&&(H>H3)) {
if (((UW/2)>LW)&&(UW>(2*BL90))&&(CL>=(CandleLength*Point))&&(O!=C)/*&&(L>L1)&&(L>L2)*/) {
if (Display_ShootStar == true) {
ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], High[shift1] + (Pointer_Offset+Offset_ShootStar+High_Offset)*Point);
ObjectSetText(PatternText[shift], "SS 2", Text_ShootStar, "Times New Roman", Color_ShootStar);
downArrow[shift1] = High[shift1] + (Pointer_Offset*Point);
}
}
if (Show_ShootStar_Alert) {
if (setalert == 0 && Show_Alert == true) {
pattern = "Shooting Star 2";
setalert = 1;
}
}
}
// Check for Bearish Shooting Star
if ((H>=H1)&&(H>H2)&&(H>H3)) {
if (((UW/3)>LW)&&(UW>(2*BL90))&&(CL>=(CandleLength*Point))&&(O!=C)/*&&(L>L1)&&(L>L2)*/) {
if (Display_ShootStar == true) {
ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], High[shift1] + (Pointer_Offset+Offset_ShootStar+High_Offset)*Point);
ObjectSetText(PatternText[shift], "SS 3", Text_ShootStar, "Times New Roman", Color_ShootStar);
downArrow[shift1] = High[shift1] + (Pointer_Offset*Point);
}
}
if (Show_ShootStar_Alert) {
if (setalert == 0 && Show_Alert == true) {
pattern = "Shooting Star 3";
setalert = 1;
}
}
}
// Check for Bearish Shooting Star
if ((H>=H1)&&(H>H2)&&(H>H3)) {
if (((UW/4)>LW)&&(UW>(2*BL90))&&(CL>=(CandleLength*Point))&&(O!=C)/*&&(L>L1)&&(L>L2)*/) {
if (Display_ShootStar == true) {
ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], High[shift1] + (Pointer_Offset+Offset_ShootStar+High_Offset)*Point);
ObjectSetText(PatternText[shift], "SS 4", Text_ShootStar, "Times New Roman", Color_ShootStar);
downArrow[shift1] = High[shift1] + (Pointer_Offset*Point);
}
}
if (Show_ShootStar_Alert) {
if (setalert == 0 && Show_Alert == true) {
pattern = "Shooting Star 4";
setalert = 1;
}
}
}
// End of Bearish Patterns
// Bullish Patterns
// Check for Bullish Hammer
if ((L<=L1)&&(L<L2)&&(L<L3)) {
if (((LW/2)>UW)&&(LW>BL90)&&(CL>=(CandleLength*Point))&&(O!=C)/*&&(H<H1)&&(H<H2)*/) {
if (Display_Hammer == true) {
ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], Low[shift1] - (Pointer_Offset+Offset_Hammer)*Point);
ObjectSetText(PatternText[shift], "HMR 2", Text_Hammer, "Times New Roman", Color_Hammer);
upArrow[shift1] = Low[shift1] - (Pointer_Offset*Point);
}
}
if (Show_Hammer_Alert) {
if (setalert == 0 && Show_Alert == true) {
pattern = "Bullish Hammer 2";
setalert = 1;
}
}
}
// Check for Bullish Hammer
if ((L<=L1)&&(L<L2)&&(L<L3)) {
if (((LW/3)>UW)&&(LW>BL90)&&(CL>=(CandleLength*Point))&&(O!=C)/*&&(H<H1)&&(H<H2)*/) {
if (Display_Hammer == true) {
ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], Low[shift1] - (Pointer_Offset+Offset_Hammer)*Point);
ObjectSetText(PatternText[shift], "HMR 3", Text_Hammer, "Times New Roman", Color_Hammer);
upArrow[shift1] = Low[shift1] - (Pointer_Offset*Point);
}
}
if (Show_Hammer_Alert) {
if (setalert == 0 && Show_Alert == true) {
pattern = "Bullish Hammer 3";
setalert = 1;
}
}
}
// Check for Bullish Hammer
if ((L<=L1)&&(L<L2)&&(L<L3)) {
if (((LW/4)>UW)&&(LW>BL90)&&(CL>=(CandleLength*Point))&&(O!=C)/*&&(H<H1)&&(H<H2)*/) {
if (Display_Hammer == true) {
ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], Low[shift1] - (Pointer_Offset+Offset_Hammer)*Point);
ObjectSetText(PatternText[shift], "HMR 4", Text_Hammer, "Times New Roman", Color_Hammer);
upArrow[shift1] = Low[shift1] - (Pointer_Offset*Point);
}
}
if (Show_Hammer_Alert) {
if (setalert == 0 && Show_Alert == true) {
pattern = "Bullish Hammer 4";
setalert = 1;
}
}
}
// End of Bullish Patterns
if (setalert == 1 && shift == 0) {
Alert(Symbol(), " ", period, " ", pattern);
setalert = 0;
}
} // End of for loop
return(0);
}
//+------------------------------------------------------------------+
Comments