DEV Community

HloniTheCoder
HloniTheCoder

Posted on

Trend Based Signal System Using MQL5

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

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;
Enter fullscreen mode Exit fullscreen mode

#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);
}
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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:

Simple Moving Average Based Strategies.

Within this repository we define the Golden Cross Strategy and Trend Based Signal System.

The Golden Cross Strategy is a double moving average based strategy in which bullish momentum is defined by the fast-period moving average crossing above the slow-period moving average, and bearish momentum is defined by the fast-period moving average crossing below the slow-period moving average.

The Trend Based Signal System is based on a fast and slow period moving average, bullish momentum is based on the fast-period moving average moving above the slow-period moving average and bearish momentum is based on it moving below the slow-period moving average.

When we receive a bullish candlestick pattern while price has bullish momentum, a bullish signal is output When we receicve a bearish candlestick pattern while price has bearish momentum, a bearish signal is output.

The Golden Cross Strategy is based on procedural coding in…

Top comments (0)