Optimizing a trading strategy to its max potential is a challenging and time-consuming task since it requires extensive testing, refining, and trial and error. No algorithm can guarantee maximum profitability, and improvements can always be made through additional research and fine-tuning.
The best results can be achieved by refining the strategy over time and via rigorous backtesting and parameter optimization using MetaTrader's native strategy tester. However, I'll provide an updated version of the code with additional improvements to help you get started.
File Name: Advanced_MA_EA.mq5
//+------------------------------------------------------------------+
//| Advanced_MA_EA.mq5 |
//| Copyright 2021, Your_Name_Here. |
//| https://www.example-website.com |
//+------------------------------------------------------------------+
#property copyright "2021, Your_Name_Here"
#property link "https://www.example-website.com"
#property version "1.0"
#property strict
#property description "An advanced Moving Average EA for MT5 with trend confirmation, dynamic stop loss, and additional filters and parameters (use at your own risk and optimize for better performance)."
input int FastMAPeriod = 10;
input int SlowMAPeriod = 30;
input int TrendMAPeriod = 200;
input int ADXPeriod = 14;
input int CCICommodityChannelIndexPeriod = 14;
input ENUM_APPLIED_PRICE CCIApplyTo = PRICE_TYPICAL;
input double RiskPercentage = 1;
input double ADXFilterLevel = 25;
input double CCIUpperLevel = 100;
input double CCILowerLevel = -100;
input ENUM_TIMEFRAMES TimeFrame = PERIOD_H1;
int FastMAHandle;
int SlowMAHandle;
int TrendMAHandle;
int ADXHandle;
int CCIHandle;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
FastMAHandle = iMA(_Symbol, TimeFrame, FastMAPeriod, 0, MODE_SMA, PRICE_CLOSE);
SlowMAHandle = iMA(_Symbol, TimeFrame, SlowMAPeriod, 0, MODE_SMA, PRICE_CLOSE);
TrendMAHandle = iMA(_Symbol, TimeFrame, TrendMAPeriod, 0, MODE_SMA, PRICE_CLOSE);
ADXHandle = iADX(_Symbol, TimeFrame, ADXPeriod);
CCIHandle = iCCI(_Symbol, TimeFrame, CCICommodityChannelIndexPeriod, CCIApplyTo);
if (FastMAHandle == INVALID_HANDLE || SlowMAHandle == INVALID_HANDLE || TrendMAHandle == INVALID_HANDLE || ADXHandle == INVALID_HANDLE || CCIHandle == INVALID_HANDLE)
{
return (INIT_FAILED);
}
return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ExpertRemove();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double fastMA[];
double slowMA[];
double trendMA[];
double adxValue[];
double cciValue[];
// Copy data from indicators
int copiedFast = CopyBuffer(FastMAHandle, 0, 0, 2, fastMA);
int copiedSlow = CopyBuffer(SlowMAHandle, 0, 0, 2, slowMA);
int copiedTrend = CopyBuffer(TrendMAHandle, 0, 0, 1, trendMA);
int copiedADX = CopyBuffer(ADXHandle, 0, 0, 1, adxValue);
int copiedCCI = CopyBuffer(CCIHandle, 0, 0, 1, cciValue);
if (copiedFast == -1 || copiedSlow == -1 || copiedTrend == -1 || copiedADX == -1 || copiedCCI == -1) return;
if (adxValue[0] >= ADXFilterLevel)
{
// Check for crossovers, trend confirmation, and additional filter conditions
if (fastMA[0] > slowMA[0] && fastMA[1] < slowMA[1] && SymbolInfoDouble(_Symbol, SYMBOL_ASK) > trendMA[0] && cciValue[0] > CCILowerLevel)
{
trade("BUY");
}
else if (fastMA[0] < slowMA[0] && fastMA[1] > slowMA[1] && SymbolInfoDouble(_Symbol, SYMBOL_ASK) < trendMA[0] && cciValue[0] < CCIUpperLevel)
{
trade("SELL");
}
}
}
void trade(string cmd)
{
MqlTradeRequest request = {};
MqlTradeResult result = {};
request.symbol = _Symbol;
request.volume = calculate_lot_size(RiskPercentage);
request.deviation = 10;
request.magic = 1234;
request.type_filling = ORDER_FILLING_FOK;
if (cmd == "BUY")
{
request.action = TRADE_ACTION_DEAL;
request.type = ORDER_TYPE_BUY;
}
else if (cmd == "SELL")
{
request.action = TRADE_ACTION_DEAL;
request.type = ORDER_TYPE_SELL;
}
request.price = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
double ATR = iATR(_Symbol, TimeFrame, 14, 0);
request.sl = NormalizeDouble(request.price - (2.5 * ATR), _Digits);
request.tp = NormalizeDouble(request.price + (3.5 * ATR), _Digits);
OrderSend(request, result);
}
double calculate_lot_size(double riskPercentage)
{
double accountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
double riskAmount = (riskPercentage / 100.0) * accountBalance;
double ATR = iATR(_Symbol, TimeFrame, 14, 0);
int stopLossPips = int(2.5 * (_Digits == Digits() ? ATR : Point));
double lotSize = riskAmount / (stopLossPips * SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE));
return NormalizeDouble(lotSize, 2);
}
//+------------------------------------------------------------------+
Changes in this Advanced_MA_EA version:
- Added Average Directional Index (ADX) to filter out trades during low-volatility periods. Use the
ADXFilterLevel parameter to set the minimum ADX value to allow a trade.
- Added the Commodity Channel Index (CCI) to further confirm trades. Use
CCIUpperLevel and CCILowerLevel parameters to define overbought and oversold conditions, respectively.
- Removed the TakeProfit variable as it's dynamically calculated using ATR.
This updated version of the code incorporates more advanced techniques to enhance the profitability of the strategy. However, again, it's important to test, refine, and validate this EA on historical data before deploying it on a live account. Please, keep in mind that trading carries inherent risks, and there are no guarantees, even with the best trading algorithms in the world.