Identify Candlestick Patterns Using OOP in MQL5
Published: 2023-08-18 22:33:56

## Identify Candlestick Patterns Using Object Oriented Programming

Identify Marubozu, Hammer, Inverted Hammer, Hanging Man, Shooting Star Candlestick Patterns.

Access To The Full Code At The Bottom Of The File.

## Program Description


![Vertical Line Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vi9j72nv4pyo3h2z2gdy.png)

A vertical line will be displayed when a candle pattern occurs.

Each Candlestick Pattern has a colour to identify it:

- Bullish Marubozu are Red.

- Bullish Hammers are Blue.

- Bullish Inverted Hammers are Green.

- Bearish Marubozu are Yellow.

- Bearish Hanging Man are Orange.

- Bearish Shooting Stars are Purple.

## Code Description

```
//Get Class Data From The PriceDataClass

#include <IncludeTutorial/PriceDataClass.mqh>


//Access Candlestick Methods Using The HPriceData Class

HPriceData MyPriceData;


//Date Time Index Of The Candle Stick Types

datetime bull_maru_index;

datetime bull_hammer_index;

datetime bull_inv_hammer_index;

datetime bear_maru_index;

datetime bear_hang_man_index;

datetime bear_shoot_star_idex;
```

At the top of our file we add the code above.

We include the `IncludeTutorial/PriceDataClass.mqh` file.
This file contains the Price Data Classes we will be using.

`Include Tutorial` folder should be stored within your MQL5 Include folder that is provided when you install MetaTrader 5.

`HPriceData` is the class we will be using. It has the candlestick methods to identify candle types.

`HPriceData MyPriceData;` is an instance of the class.
`MyPriceData` will be used to access the necessary methods.

The following code block should be written within the `OnTick()` function.

```
MyPriceData.Add_Price_Data();

int candle_index = 1;

int avr_range = 7;
```

`MyPriceData.Add_Price_Data();` this method adds price data to the price data array that is used within the class.
Without this method you cannot access recent data.

`int candle_index = 1;` the candle index variable is used to index price data.

`int avr_range = 7;` the average number of candles that will be used to determine long or short candles.

```
bool is_bull_maru = MyPriceData.Get_Long_Bullish_Marubozu_Type(candle_index, avr_range);

bool is_hammer = MyPriceData.Get_Long_Hammer_Type(candle_index, avr_range);

bool is_inv_hammer = MyPriceData.Get_Long_Inverted_Hammer_Type(candle_index, avr_range);

bool is_bear_maru = MyPriceData.Get_Long_Bearish_Marubozu_Type(candle_index, avr_range);

bool is_hang = MyPriceData.Get_Long_Hanging_Man_Type(candle_index, avr_range);

bool is_star = MyPriceData.Get_Long_Shooting_Star_Type(candle_index, avr_range);
```

We initialize the Boolean variables that hold candlestick pattern data.

The values of the variables can either be True or False.
True indicates the indexed candle is a candlestick pattern.

```
datetime curr_Time = MyPriceData.Get_DateTime_Value(candle_index);

if(is_bull_maru)
  {
   
   bull_maru_index = curr_Time;
   
  }

if(is_hammer)
  {
   
   bull_hammer_index = curr_Time;
   
  }

if(is_inv_hammer)
  {
   
   bull_inv_hammer_index = curr_Time;
   
  }

if(is_bear_maru)
  {
   
   bear_maru_index = curr_Time;
   
  }

if(is_hang)
  {
   
   bear_hang_man_index = curr_Time;
   
  }

if(is_star)
  {
   
   bear_shoot_star_idex = curr_Time;
   
  }
```
If the candle is a candlestick pattern, we index the time.

This is necessary to draw a vertical line.

```
ObjectCreate(0, "Bullish Marubozu Index", OBJ_VLINE, 0, bull_maru_index, 0,0);
ObjectSetInteger(0, "Bullish Marubozu Index", OBJPROP_COLOR, clrRed);

ObjectCreate(0, "Bearish Marubozu Index", OBJ_VLINE, 0, bear_maru_index, 0,0);
ObjectSetInteger(0, "Bearish Marubozu Index", OBJPROP_COLOR, clrYellow);

ObjectCreate(0, "Hammer Index", OBJ_VLINE, 0, bull_hammer_index, 0,0);
ObjectSetInteger(0, "Hammer Index", OBJPROP_COLOR, clrBlue);

ObjectCreate(0, "Inverted Hammer Index", OBJ_VLINE, 0, bull_inv_hammer_index, 0,0);
ObjectSetInteger(0, "Inverted Hammer Index", OBJPROP_COLOR, clrGreen);

ObjectCreate(0, "Hanging Man Index", OBJ_VLINE, 0, bear_hang_man_index, 0,0);
ObjectSetInteger(0, "Hanging Man Index", OBJPROP_COLOR, clrOrange);

ObjectCreate(0, "Shooting Star Index", OBJ_VLINE, 0, bear_shoot_star_idex, 0,0);
ObjectSetInteger(0, "Shooting Star Index", OBJPROP_COLOR, clrPurple);
```
We create a vertical line when we receive a candlestick pattern.
Each candle pattern type has a colour associated with it.

Full Code, Comments Included:{% embed https://github.com/HloniTheCoder/Candle-Stick-Data %}