Pattern Alert_v3

Download script
Author: Copyright � 2005, MetaQuotes Software Corp.
Pattern Alert_v3

This script is an automated technical analysis tool designed for the MetaTrader trading platform. Its primary purpose is to scan historical and real-time market price data to identify specific candlestick patterns that traders use to predict potential trend reversals or continuations.

Core Functionality

The logic operates by scanning each price "candle" (the movement of price over a specific timeframe) and comparing the relationship between consecutive candles. When the script recognizes a specific geometric arrangement of price action that matches a known trading pattern, it performs three actions:

  1. Visual Marking: It places an arrow on the chart to highlight exactly where the pattern occurred.
  2. Text Labeling: It writes the name of the identified pattern (e.g., "Bullish Engulfing") directly onto the chart for easy identification.
  3. Real-Time Alerts: If a pattern is detected on the most recent completed candle, the script triggers a pop-up alert and prints a notification in the platform's log.

Identified Patterns

The script is programmed to recognize several specific types of price action:

  • Engulfing Patterns: It looks for instances where a new candle completely "covers" or "engulfs" the body of the previous candle, signaling a strong shift in momentum (available for both Bullish and Bearish directions).
  • Star Patterns: These are more complex three-candle formations. The script identifies:
    • Morning Stars & Evening Stars: Three-candle reversal patterns involving a large candle, a small "star" or "doji" candle, and a confirming reversal candle.
    • Morning & Evening Doji Stars: Similar to above, but specifically identifying the presence of a "Doji" (a candle where the opening and closing prices are virtually equal) in the center of the sequence.
  • Tweezer Patterns: The script looks for instances where two consecutive candles have nearly identical highs or lows, suggesting that the market is hitting a significant ceiling or floor (Tweezer Tops and Tweezer Bottoms).
  • Dark Cloud Cover: A specific bearish reversal pattern where a bullish candle is followed by a bearish candle that opens above the previous high but closes well into the lower half of the previous body.

Smart Adjustments

To ensure the tool remains readable and useful across different trading styles, the logic includes a "dynamic positioning" system. Because a chart viewed on a 1-minute timeframe looks very different from a 4-hour timeframe, the script automatically adjusts the distance of its labels and arrows based on the time interval you are currently viewing. This prevents the visual markers from overlapping or appearing cluttered regardless of your zoom level.

User Experience

The script includes several "Show" toggles, allowing users to turn specific patterns on or off. This enables a customized experience where a trader can focus only on the specific signals they find most reliable for their strategy.

Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
11 Views
0 Downloads
0 Favorites
Pattern Alert_v3
//+------------------------------------------------------------------+
//|                                  Pattern Alert v1.1 (Official)   |
//|                                                                  |
//| You may improve and add to this code but please send it back to  |
//| me for an official release. Thanks.                              |
//|                                                                  |
//|              Copyright © 2005, Jason Robinson                    |
//|               (jasonrobinsonuk,  jnrtrading)                     |
//|                http://www.jnrtrading.co.uk                       |
//|                                                                  |
//|       **THE ONLY OFFICIAL VERSION IS FROM MY WEBSITE**           |
//|         (unless it has been posted by me on a forum)             |
//|                                                                  |
//|Obviously this is still work in progress and needs LOTS of testing|
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//----
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Red
//----
extern bool Show_Text_Labels=true;
extern bool Show_Bearish_Engulfing_Patterns=true;
extern bool Show_Bullish_Engulfing_Patterns=true;
extern bool Show_Star_Patterns=true;
extern bool Show_Tweezer_Bottoms=true;
extern bool Show_Tweezer_Tops=true;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
string PatternText[2000];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
//---- indicators
     for(int i=0; i < Bars; i++) { // Clear buffer
      //ExtMapBuffer1[i] = 0;
      //ExtMapBuffer2[i] = 0;
     }
     for(int j=0; j < Bars; j++) {
      PatternText[j]=CharToStr(j);
     }
   SetIndexStyle(0,DRAW_ARROW, EMPTY);
   SetIndexArrow(0,242);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1,DRAW_ARROW, EMPTY);
   SetIndexArrow(1,241);
   SetIndexBuffer(1, ExtMapBuffer2);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
  int deinit()
  {
   ObjectsDeleteAll(0, OBJ_TEXT);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
  int start()
  {
   Comment("Pattern Alert v1.1 Official");
//----
   static datetime prevtime=0;
   int shift;
   int shift1;
   int shift2;
   int shift3;
   string pattern;
   int setPattern=0;
   int alert=0;
   int arrowShift;
   int textShift;
//----
   if(prevtime==Time[0])
     {
      return(0);
     }
   prevtime=Time[0];
   // Code to determine a good place to put the text and arrow
   switch(Period())
     {
      case 1:
         arrowShift=1;
         textShift=3;
         break;
      case 5:
         arrowShift=3;
         textShift=8;
         break;
      case 30:
         arrowShift=7;
         textShift=13;
         break;
      case 15:
         arrowShift=7;
         textShift=15;
         break;
      case 60:
         arrowShift=9;
         textShift=25;
         break;
      case 1440:
         arrowShift=3;
         textShift=7;
         break;
      case 10080:
         arrowShift=95;
         textShift=235;
         break;
      case 43200:
         arrowShift=150;
         textShift=260;
         break;
     }
   for(int j=0; j < Bars; j++)
     {
      PatternText[j]=j;
     }
   for(shift=0; shift < Bars; shift++)
     {
      shift1=shift + 1;
      shift2=shift + 2;
      shift3=shift + 3;
      // Check for a Bearish Engulfing pattern
      if (Show_Bearish_Engulfing_Patterns==true)
        {
         if ((Close[shift2] > Open[shift2]) && (Open[shift1] > Close[shift2]) && (Close[shift1] < Open[shift2]))
           {
            if (Show_Text_Labels)
              {
               ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], High[shift1] + (Point * textShift));
               ObjectSetText(PatternText[shift], "Bearish Engulfing", 9, "Times New Roman", Red);
              }
            ExtMapBuffer1[shift1]=High[shift1] + (Point * arrowShift);
            if (setPattern==0)
              {
               pattern="Bearish Engulfing Pattern";
               setPattern=1;
               alert=1;
              }
            continue;
           }
        }
      // Check for a Bullish Engulfing pattern
      if (Show_Bullish_Engulfing_Patterns==true)
        {
         if ((Close[shift2] < Open[shift2]) && (Open[shift1] < Close[shift2]) && (Close[shift1] > Open[shift2]))
           {
            if (Show_Text_Labels==true)
              {
               ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], High[shift1] + (Point * textShift));
               ObjectSetText(PatternText[shift], "Bullish Engulfing", 9, "Times New Roman", Red);
              }
            ExtMapBuffer1[shift1]=High[shift1] + (Point * arrowShift);
            if (setPattern==0)
              {
               pattern="Bullish Engulfing Pattern";
               setPattern=1;
               alert=1;
              }
            continue;
           }
        }
      // Check for a Dark Cloud Cover pattern
      if ((Close[2] > Open[2]) && (Open[1] > High[2]) && (Close[1] < ((Open[2] + Close[2])/2)) && (Close[1] > Open[2]))
        {
         if (Show_Text_Labels==true)
           {
            ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], High[shift1] + (Point * textShift));
            ObjectSetText(PatternText[shift], "Dark Cloud Cover", 9, "Times New Roman", Red);
           }
         ExtMapBuffer1[shift1]=High[shift1] + (Point * arrowShift);
         if (setPattern==0)
           {
            pattern="Dark Cloud Cover";
            setPattern=1;
            alert=1;
           }
         continue;
        }
      if (Show_Star_Patterns==true)
        {
         // Check for Evening Doji Star pattern
         if (
            (Close[3] > Open[3]) //Bullish candle
            && (Open[2]==Close[2]) && (Low[2] < Open[2]) && (High[2] > Open[2]) // Doji
            && (Low[2]>=Close[3]) // Doji close higher or equal to Bullish close
            && (High[1]<=Close[2]) && (Open[1]<=Low[2]) && (Open[1]>=Close[3]) &&
            (Close[1] < ((Open[3] + Close[3])/2)) && (Close[1] > Open[3]) // Bearish candle in pattern
                     ){
            if (Show_Text_Labels==true)
              {
               ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], High[shift1] + (Point * textShift));
               ObjectSetText(PatternText[shift], "Evening Doji Star", 9, "Times New Roman", Red);
              }
            ExtMapBuffer1[shift1]=High[shift1] + (Point * arrowShift);
            if (setPattern==0)
              {
               pattern="Evening Doji Star";
               setPattern=1;
               alert=1;
              }
            continue;
           }
         // Check for Morning Doji Star pattern
         if (
            (Close[3] < Open[3]) //Bearish candle
            && (Open[2]==Close[2]) && (Low[2] < Open[2]) && (High[2] > Open[2]) // Doji
            && (High[2]<=Close[3]) // Doji close lower or equal to bearish close
            && (Low[1]>=Close[2]) && (Open[1]>=High[2]) && (Open[1]<=Close[3]) &&
            (Close[1] > ((Open[3] + Close[3])/2)) && (Close[1] < Open[3]) // Bullish candle in pattern
                 )
           {
            if (Show_Text_Labels==true)
              {
               ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], High[shift1] + (Point * textShift));
               ObjectSetText(PatternText[shift], "Morning Doji Star", 9, "Times New Roman", Red);
              }
            ExtMapBuffer1[shift1]=High[shift1] + (Point * arrowShift);
            if (setPattern==0)
              {
               pattern="Morning Doji Star";
               setPattern=1;
               alert=1;
              }
            continue;
           }
         // Check for Evening Star pattern
         if(
            (Close[3] > Open[3]) //Bullish candle
            && (Open[2] > Close[2]) && (Low[2] < Open[2]) && (High[2] > Open[2]) // Second, small candle
            && (Low[2] > Close[3]) // small candle gaps up.
            && (High[1] < Close[2]) && (Open[1]<=Low[2]) && (Open[1] > Close[3]) &&
            (Close[1] < ((Open[3] + Close[3])/2)) && (Close[1] > Open[3]) // Bearish candle in pattern
              )
           {
            if (Show_Text_Labels==true)
              {
               ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], High[shift1] + (Point * textShift));
               ObjectSetText(PatternText[shift], "Evening Star", 9, "Times New Roman", Red);
              }
            ExtMapBuffer1[shift1]=High[shift1] + (Point * arrowShift);
            if (setPattern==0)
              {
               pattern="Evening Star";
               setPattern=1;
               alert=1;
              }
            continue;
           }
         // Check for Morning Star pattern
         if (
            (Close[3] < Open[3]) //Bearish candle
            && (Open[2] < Close[2]) && (Low[2] < Open[2]) && (High[2] > Open[2]) // Second, small candle
            && (High[2] < Close[3]) // Small candle gaps down
            && (Low[1] > Close[2]) && (Open[1]>=High[2]) && (Open[1] < Close[3]) &&
            (Close[1] > ((Open[3] + Close[3])/2)) && (Close[1] < Open[3]) // Bullish candle in pattern
                 )
           {
            if (Show_Text_Labels==true)
              {
               ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], High[shift1] + (Point * textShift));
               ObjectSetText(PatternText[shift], "Morning Star", 9, "Times New Roman", Red);
              }
            ExtMapBuffer1[shift1]=High[shift1] + (Point * arrowShift);
            if (setPattern==0)
              {
               pattern="Morning Star";
               setPattern=1;
               alert=1;
              }
            continue;
           }
        } // End of Show_Star_Patterns condition
      // Check for Tweezer Top
      if (Show_Tweezer_Tops==true)
        {
         if ((High[shift1]==High[shift2]))
           {
            if (Show_Text_Labels==true)
              {
               ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], High[shift1] + (Point * textShift));
               ObjectSetText(PatternText[shift], "Tweezer Top", 9, "Times New Roman", Red);
              }
            ExtMapBuffer1[shift1]=High[shift1] + (Point * arrowShift);
            if (setPattern==0)
              {
               pattern="Tweezer Top";
               setPattern=1;
               alert=1;
              }
            continue;
           }
        }
      // Check for Tweezer Bottom
      if (Show_Tweezer_Bottoms==true)
        {
         if ((Low[shift1]==Low[shift2]))
           {
            if (Show_Text_Labels==true)
              {
               ObjectCreate(PatternText[shift], OBJ_TEXT, 0, Time[shift1], High[shift1] - (Point * textShift));
               ObjectSetText(PatternText[shift], "Tweezer Bottom", 9, "Times New Roman", Red);
              }
            ExtMapBuffer2[shift1]=High[shift1] - (Point * (arrowShift + 7));
            if (setPattern==0)
              {
               pattern="Tweezer Bottom";
               setPattern=1;
               alert=1;
              }
            continue;
           }
        }
      if (shift==1 && alert==1)
        {
         Print(Symbol(),"  M", Period(),"  ", pattern);
         Alert(Symbol(),"  M", Period(),"  ", pattern);
        }
     } // End of for loop
   return(0);
  }
//+------------------------------------------------------------------+

Comments