Trend Based Signal System Using MQL5
Published: 2023-08-25 13:12:04

## Identify Bullish and Bearish Signals Using Momentum

The Trend Based Signal System is used to identify levels of significance based on bullish and bearish momentum.
A level of significance is a level in price that can be used to enter or exit positions, or set position stops.

We identify bullish or bearish momentum using the fast and slow moving average indicator.

During moments of bullish momentum, we identify bullish signals.
During moments of bearish momentum, we identify bearish signals.


![Signal Performance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajpltfspzkgux9z7394z.png)

## Project Description

When the fast moving average indicator is above the slow moving average indicator it represents bullish momentum.
When there is bullish momentum, we identify bullish signals.
Bullish Signals are identified using bullish candlestick patterns.

When the fast moving average indicator is below the slow moving average indicator it represents bearish momentum.
When there is bearish momentum, we identify bearish signals.
Bearish Signals are identified using bearish candlestick patterns.


## Program Description

Bullish Signals are identified using a blue horizontal line.
Bearish Signals are identified using a red horizontal line.

We will be using Bullish and Bearish Harami and Engulfing Candlestick Patterns.

## Code Description

At the top our file we will include the following code.

```
#include <IncludeTutorial/IndicatorDataClass>

#include <IncludeTutorial/PriceDataClass>

HSMA FastMAData;

HSMA SlowMAData;

HPriceData MyPriceData;
```
`#include` is used to get the necessary data classes for indicator types and price data.

`HSMA` is the Simple Moving Average class used to access our moving average data.

`HPriceData` is the Price Data Class used to access our price data.


```
int OnInit()
{
   FastMAData.Create_SMA_Definition(_Symbol, _Period, 50);
   
   SlowMAData.Create_SMA_Definition(_Symbol, _Period, 200);
}
```

Within the `OnInit` function we will create the Moving Average Indicator Definitions.
The `Create_SMA_Definition` method creates the indicator definition and initializes the indicator averaging period to 50, for the fast ma, and 200, for the slow ma.


Within the `OnTick` function we will include the following code:

```
FastMAData.Add_Array_Data();

SlowMAData.Add_Array_Data();

MyPriceData.Add_Price_Data();
```

The `Add_Array_Data` method is used to add the indicator data to the indicator array.
This method is based on the `CopyBuffer` function.

```
double fast_ma_value = FastMAData.Get_Indicator_Value(1);

double slow_ma_value = SlowMAData.Get_Indicator_Value(1);
```

Get the most recent fast and slow moving average value.

```
bool fast_above_slow = fast_ma_value > slow_ma_value;

bool fast_below_slow = fast_ma_value < slow_ma_value;
```
The conditions required for determining bullish and bearish momentum.



```
bool bullish_engulfing = MyPriceData.Get_Is_Bullish_Engulfing(1,5);

bool bearish_engulfing = MyPriceData.Get_Is_Bearish_Engulfing(1,5);

bool bullish_harami = MyPriceData.Get_Is_Bullish_Harami(1,5);

bool bearish_harami = MyPriceData.Get_Is_Bearish_Harami(1,5);
```
Using the `MyPriceData` object, we will determine the engulfing and harami candlestick type.
We will find whether or not the most recently completed candlestick, indexed at 1, is an engulfing pattern or harami pattern.


```
bool bullish_signal = fast_above_slow && (bullish_engulfing || bullish_harami);

bool bearish_signal = fast_below_slow && (bearish_engulfing || bearish_harami);
```
The above code defines the Boolean variables that handle bullish and bearish signals.

Bullish Signals are defined by a bullish candlestick patterns and bullish momentum, defined by the fast and slow moving average.

Bearish Signals are defined by bearish candlestick patterns and bearish momentum, defined by the fast and slow moving average.


```
close_value = MyPriceData.Get_Close_Value(1);

if(bullish_signal){
   ObjectCreate(0, "Bullish Signal Level", OBJ_HLINE, 0, 0, close_value);
   ObjectSetInteger(0, "Bullish Signal Level", OBJPROP_COLOR, clrBlue);
}


if(bearish_signal){
   ObjectCreate(0, "Bearish Signal Level", OBJ_HLINE, 0, 0, close_value);
   ObjectSetInteger(0, "Bearish Signal Level", OBJPROP_COLOR, clrRed);
}
```

When we receive a bullish or bearish signal, we will draw a horizontal line to identify the price in which the signal was identified.

Bullish Signals are identified using a blue horizontal line.
Bearish Signals are identified using a red horizontal line.

Access to the full code:
{% embed https://github.com/HloniTheCoder/Simple-Moving-Average-Data %}