Author: Copyright � 2009, Paul Stringer.
Harami

This script is a specialized technical analysis tool designed to identify specific candlestick patterns known as "Harami" formations. These patterns are often interpreted by traders as potential signals that the current price trend might be about to reverse.

How the Logic Works

The script scans through historical price data on your chart, comparing two consecutive candlesticks to see if they meet a strict set of visual and mathematical criteria:

1. Size Filtering To filter out "market noise" (tiny, insignificant movements) or extreme outliers that might not be reliable, the script checks the physical size of both the "Master" candle (the previous large candle) and the "Harami" candle (the current smaller candle). It ensures both candles fall within a user-defined range of minimum and maximum sizes.

2. Proportional Analysis The script doesn't just look for any small candle; it calculates the ratio between the two. For a signal to be generated, the Harami candle must be a specific proportion of the Master candle's size. This ensures that the second candle is noticeably smaller than the first, which is a key characteristic of this specific trading pattern.

3. Trend Reversal Detection Once the sizes are confirmed, the script looks for "enveloping" behavior to determine the direction of the potential reversal:

  • Bearish Reversal Signals: If a large downward candle (bearish) is followed by a small upward candle (bullish) that is contained within the body of the previous candle, the script identifies this as a potential shift in momentum.
  • Bullish Reversal Signals: Conversely, if a large upward candle (bullish) is followed by a small downward candle (bearish) contained within the body of the previous candle, it identifies a potential shift toward a downward move.

Visual Output

When the script successfully identifies these conditions, it places visual markers (arrows) directly on your chart:

  • Cyan Arrows: These appear to highlight potential bullish reversals (where price might start moving up).
  • Red Arrows: These appear to highlight potential bearish reversals (where price might start moving down).

The arrows are automatically positioned slightly above or below the candles so they do not clutter the price action, making it easier for you to see the signals at a glance.

Miscellaneous
Implements a curve of type %1
6 Views
0 Downloads
0 Favorites
Harami
//+------------------------------------------------------------------+
//|                                                       Harami.mq4 |
//|                                 Copyright © 2009, Paul Stringer. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2009, Paul Stringer."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Cyan

//---- input parameters
extern int       MinMasterSize = 40;
extern int       MaxMasterSize = 500;
extern int       MinHaramiSize = 20;
extern int       MaxHaramiSize = 300;
extern double	 MaxRatioHaramiToMaster = 0.75;
extern double	 MinRatioHaramiToMaster = 0.5;
extern int       ArrowOffSet = 35;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,218);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,217);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(1,0.0);
//----

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
	int	_Counted = IndicatorCounted();
	int	_ProcessBarIndex = 0;
	int _SubIndex = 0;
	double _Max = 0;
	double _Min = 0;
	double _SL = 0;
	double _TP = 0;
	bool _WeAreInPlay = false;
	int _EncapsBarIndex = 0;
	string _Name;
	
	double _MasterBarSize = 0;
	double _HaramiBarSize = 0;
	
	// Process any bars not processed
	for ( _ProcessBarIndex = 1; _ProcessBarIndex <= ( Bars - _Counted ); _ProcessBarIndex++ ) 
	{
		// Get the bar sizes
		_MasterBarSize = MathAbs ( Open [ _ProcessBarIndex + 1 ] - Close [ _ProcessBarIndex + 1 ] );
		_HaramiBarSize = MathAbs ( Open [ _ProcessBarIndex ] - Close [ _ProcessBarIndex ] );
		
		// Ensure no "zero-divide" errors
		if ( _MasterBarSize > 0) 
		{
			// Ensure the Master & Harami bars satisfy the ranges
			if (
				( _MasterBarSize < ( MaxMasterSize * Point ) ) && 
				( _MasterBarSize > ( MinMasterSize * Point ) ) &&
				( _HaramiBarSize < ( MaxHaramiSize * Point ) ) && 
				( _HaramiBarSize > ( MinHaramiSize * Point ) ) &&
				( ( _HaramiBarSize / _MasterBarSize ) <= MaxRatioHaramiToMaster ) && 
				( ( _HaramiBarSize / _MasterBarSize ) >= MinRatioHaramiToMaster )
				)
			{

				// Is it reversal in favour of a BEAR reversal...
				if ( 
					( Open [ _ProcessBarIndex + 1 ] > Close [ _ProcessBarIndex + 1 ] ) &&
					( Open [ _ProcessBarIndex ] < Close [ _ProcessBarIndex ] ) &&
					( Close [ _ProcessBarIndex + 1 ] < Open [ _ProcessBarIndex ] ) && 
					( Open [ _ProcessBarIndex + 1 ] > Close [ _ProcessBarIndex ] ) 
					)
				{
					// Reversal favouring a bull coming...
					ExtMapBuffer2 [ _ProcessBarIndex - 1 ] = Open [ _ProcessBarIndex -1 ] - ( ArrowOffSet * Point );
				} 

				// Is it reversal in favour of a BULL reversal...
				if ( 
					( Open [ _ProcessBarIndex + 1 ] < Close [ _ProcessBarIndex + 1 ] ) &&
					( Open [ _ProcessBarIndex ] > Close [ _ProcessBarIndex ] ) &&
					( Close [ _ProcessBarIndex + 1 ] > Open [ _ProcessBarIndex ] ) && 
					( Open [ _ProcessBarIndex + 1 ] < Close [ _ProcessBarIndex ] ) 
					)
				{
					// Reversal favouring a bull coming...
					ExtMapBuffer1 [ _ProcessBarIndex -1 ] = High [ _ProcessBarIndex - 1 ] + ( ArrowOffSet * Point );
				}
			}
		}
	}
	
	// Finished...
	return(0);
}
//+------------------------------------------------------------------+

Comments