Table of Contents
ToggleMT4 and MT5 Code for SMA Crossover Strategy
Thanks to the MetaTrader 4 or 5 platforms, it is also possible to code the SMA crossover strategy by creating custom indicators or expert advisors; for those who trade using these platforms, SMA crossovers are very effective. The following is the code cross over for both MT4 and MT5 for those looking to automate the SMA crossover strategy.
What is a Moving Average (MA) in forex trading
Moving Average is one of the commonly used technical indicators in Forex. A Moving Average is a type of indicator that enables traders to manage price data more easily because it combines a series of data into a single dynamic indicator. Moving Average is used to help traders recognize the direction of the market, without disturbance coming from the day-to-day pricing alternatives and assist in foreseeing the general movement of the market.
There are different types of moving averages, but the reason for their implementation is the same: to maximize the view of the market trend. In this article, we will examine closely how moving averages are implemented in forex trading.
How Moving Averages Operate
A moving average determines the average exchange rate of a currency pair over a specific number of intervals (for example days or hours) and represents it in the form of a line graph on a price chart. The intervals can differ in their length, and this is attributed to the short term (for example 10, 20, or 50 periods) or even long-term strategies that a trader could be using. For instance, 100 and 200 periods could be considered as long-term.
For instance, during the 10-period moving average, the MA will take 10 closing prices and plot that as a point on the graph. When the next 10-period comes, data in the graph is kept the same except the latest price is added for the new time period, thereby creating an average that keeps changing.
Types of Moving Averages
Simple Moving Average (SMA):
The SMA moving average is the simplest form of moving averages calculated when the closing prices of certain currency pairs for the specified period are simply added and divided. A fifty-period SMA would mean that the last fifty closing prices were used to calculate the average price among other features. Activities are based on the averages using the moving averages and other techniques.
Exponential Moving Average (EMA):
Recent prices are weighted more heavily for the EMA, as they are more responsive to any new prices that are coming to the table. Because of rapid changes in the EMA, it is applicable in trades where the market requires quicker shifts.
Weighted Moving Average (WMA):
The WMA provides a larger weighting to recent times according to the date, much like the EMA but is linear again. It also incorporates older data but puts more importance on recent time periods as well.
How Moving Averages are Used in Forex Trading
Trends:
Moving averages are primarily used to identify the general trends in the market. If the average is moving upwards, it indicates an uptrend, while a downward-moving average suggests a downtrend. If the market is sideways or flat, the moving average will also reflect that condition.
Support and Resistance Levels:
Moving averages can act as dynamic support or resistance levels. In an uptrend, prices may “bounce” off the moving average, while in a downtrend, the moving average can act as resistance.
Crossing Averages:
Whenever moving averages cross, that is taken either as a buy or sell signal. For example, in a Simple Moving Average (SMA) Trading Strategy, when the short-term average crosses above the long-term average, it generates a buy signal, and when the short-term average crosses below the long-term average, it generates a sell signal.
Analysis of Price Data:
In periods of high price fluctuations, moving averages help smooth out the volatility, offering a less erratic line that aids traders in decision-making.
Let’s assume you are trading the EUR/USD pair. You then add a 50-day and a 200-day simple moving average to the chart.
- If the 50-day SMA crosses above the 200-day SMA, the market is likely to trend upwards, and this is known as a Golden Cross.
- If the 50-day SMA crosses below the 200-day SMA, the market is likely trending downwards, a phenomenon known as a Death Cross.
Code for MT4:
//+———————+
//| SMA_Cross.mq4 |
//| Plutusinvestor.com |
//+———————+
#property strict
extern int ShortPeriod = 50; // Short-term SMA period
extern int LongPeriod = 200; // Long-term SMA period
extern double Lots = 0.1; // Lot size for trade
int start()
{
double shortSMA = iMA(NULL, 0, ShortPeriod, 0, MODE_SMA, PRICE_CLOSE, 0);
double longSMA = iMA(NULL, 0, LongPeriod, 0, MODE_SMA, PRICE_CLOSE, 0);
double prevShortSMA = iMA(NULL, 0, ShortPeriod, 0, MODE_SMA, PRICE_CLOSE, 1);
double prevLongSMA = iMA(NULL, 0, LongPeriod, 0, MODE_SMA, PRICE_CLOSE, 1);
// Check for buy signal (Golden Cross)
if (prevShortSMA < prevLongSMA && shortSMA > longSMA)
{
// Check if there are no open buy trades
if (OrdersTotal() == 0)
{
OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, 0, 0, “Buy SMA Crossover”, 0, 0, Green);
}
}
// Check for sell signal (Death Cross)
else if (prevShortSMA > prevLongSMA && shortSMA < longSMA)
{
// Check if there are no open sell trades
if (OrdersTotal() == 0)
{
OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, 0, 0, “Sell SMA Crossover”, 0, 0, Red);
}
}
return 0;
}
Code for MT5:
//+———————+
//| SMA_Cross.mq5 |
//| Plutusinvestor.com |
//+———————+
#property strict
input int ShortPeriod = 50; // Short-term SMA period
input int LongPeriod = 200; // Long-term SMA period
input double Lots = 0.1; // Lot size for trade
void OnTick()
{
double shortSMA = iMA(NULL, 0, ShortPeriod, 0, MODE_SMA, PRICE_CLOSE, 0);
double longSMA = iMA(NULL, 0, LongPeriod, 0, MODE_SMA, PRICE_CLOSE, 0);
double prevShortSMA = iMA(NULL, 0, ShortPeriod, 0, MODE_SMA, PRICE_CLOSE, 1);
double prevLongSMA = iMA(NULL, 0, LongPeriod, 0, MODE_SMA, PRICE_CLOSE, 1);
// Check for buy signal (Golden Cross)
if (prevShortSMA < prevLongSMA && shortSMA > longSMA)
{
if (PositionsTotal() == 0)
{
trade.Buy(Lots, Symbol());
}
}
// Check for sell signal (Death Cross)
else if (prevShortSMA > prevLongSMA && shortSMA < longSMA)
{
if (PositionsTotal() == 0)
{
trade.Sell(Lots, Symbol());
}}
}
How to Use the MT4 and MT5 Code:
For MetaTrader 4 (MT4):
- Copy the MT4 code provided earlier and paste it into the MetaEditor (available in MT4) as a new Expert Advisor.
- After compiling the code, load the indicator onto the chart of the currency pair you want to trade.
- The Expert Advisor will automatically detect SMA crossovers and place buy or sell orders accordingly.
For MetaTrader 5 (MT5):
- Copy the MT5 code and paste it into MetaEditor within MT5.
- Compile the code and attach it to the desired chart.
- The EA will wait for SMA crossovers to occur and then open trades automatically.
Conclusion
The Simple Moving Average crossover strategy is one of the simplest and most effective strategies for identifying trend reversals in the forex market. It works across any currency pair and time frame, making it a versatile tool for traders. With the MT4 and MT5 codes provided, you can easily automate the SMA crossover strategy, allowing you to capitalize on market opportunities without constantly monitoring the market.
Utilizing the SMA crossover strategy can help traders enhance their trading performance by providing reliable buy and sell signals, helping to foresee potential market changes. We wish you profitable trading!