Golden Cross Strategy Using MQL5
Published: 2023-08-25 10:50:53

## Identify Bullish And Bearish Momentum Using The Golden Cross Strategy
The Golden Cross Strategy is a technical analysis based trading strategy that involves two moving average indicators.

## Project Description
This system involves a fast and slow period moving average.
A fast-period moving average is the ma that follows the movement of price closely.
A slow-period moving average is the ma that defines the momentum of price.

When the fast-period ma crosses above the slow-period ma, it defines bullish momentum.
When the fast-period ma crosses below the slow-period ma, it defines bearish momentum.

When a signal for bullish momentum occurs, a vertical line will be drawn to index the date time value.
When we get a signal for bearish momentum we will draw a vertical line to index its occurrence.

## Program Description

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

A blue vertical line will be drawn to define an up cross.
A red vertical line will be drawn to define a down cross.

## Code Description

The following strategy code should be written within the `OnTick()` function.
The `OnTick` function is a function that executes on every price change.

Within the `OnTick` function, we will initialize the arrays that will store the indicator data.

```
double fast_ma_array[];

double slow_ma_array[];

ArraySetAsSeries(fast_ma_array, true);

ArraySetAsSeries(slow_ma_array, true);

```
The `ArraySetAsSeries` function sets the array index 0 to the latest MA value.
This is a important function for the performance of the strategy.

```
int fast_ma_indicator = iMA(_Symbol, _Period, 50, 0, MODE_SMA, PRICE_CLOSE);

int slow_ma_indicator = iMA(_Symbol, _Period, 200, 0, MODE_SMA, PRICE_CLOSE);
```

The above code initializes the variables that store the indicator data.
We initialize the fast and slow moving averages.
The averaging period of the fast ma is 50.
The averaging period of the slow ma is 200.

```
CopyBuffer(fast_ma_indicator, 0, 0, 10, fast_ma_array);

CopyBuffer(slow_ma_indicator, 0, 0, 10, slow_ma_array);
```
The `CopyBuffer` function copies the indicator data to the indicator data array.
We copy data from the index 0 to index 10.
The index 0 represents the candle index that is currently forming.
The index 10 represents the 10th index away from the current candle.


```
int curr_fast_value = fast_ma_array[1];

int prev_fast_value = slow_ma_array[2];

int curr_slow_value = slow_ma_array[1];

int prev_slow_value = slow_ma_array[2];

```
We get the most recent moving average values.
Index 1 represents the most recently completed candle.
Index 2 represents the candle before the most recently completed candle.

Index 1 is the candle before our currently forming candle.

```
bool curr_fast_above_slow = curr_fast_value > curr_slow_value;

bool prev_fast_above_slow = prev_fast_value > prev_slow_value;
```

Conditions to determine whether the fast moving average is above the slow moving average.
A Boolean variable can only be true or false.

```
bool up_cross = curr_fast_above_slow && !(prev_fast_above_slow);

bool down_cross = !(curr_fast_above_slow) && prev_fast_above_slow;
```

Conditions to determine an up or down cross state.

An up cross means the current fast moving average value is greater than the current slow moving average value, and the previous fast moving average value is less than the previous slow moving average value.

An down cross means the current fast moving average value is less than the current slow moving average value, and the previous fast moving average value is greater than the previous slow moving average value.

The `!` operator is the not Boolean operator, if a variable is true the not operator changes the value to false, and does the opposite for false. 


```
MqlRates price_data_array[];

ArraySetAsSeries(price_data_array, true);

CopyRates(_Symbol, _Period, 1, 2, price_data_array);
```
The `MqlRates` type allows us to access price data within an array.
The `CopyRates` function copies price data to the price data array.


```
if(up_cross){
   ObjectCreate(0, "Up Cross Line", OBJ_VLINE, 0, price_data_array[1].time, 0, 0);
   ObjectSetInteger(0, "Up Cross Line", OBJPROP_COLOR, clrBlue);
}

if(down_cross){
   ObjectCreate(0, "Down Cross Line", OBJ_VLINE, 0, price_data_array[1].time, 0, 0);
   ObjectSetInteger(0, "Down Cross Line", OBJPROP_COLOR, clrRed);
}
```

If the up cross condition is true, we will draw a vertical line at the index in which the cross occured.
If the down cross condition is true, we will draw a vertical line at the index in which it occured.

The line representing an up cross will be colored blue, whereas the down cross line will be colored red.

For access to the full code:

{% embed https://github.com/HloniTheCoder/Simple-Moving-Average-Data %}