Ecco un esempio di codice completo per un Expert Advisor in MQL4 che segue le regole di trading descritte per l'apertura e la chiusura di posizioni basate su EMA e RSI:
// Input parameters
input int EMA_fast_period = 8;
input int EMA_slow_period = 20;
input int RSI_period = 14;
input double StopLossPips = 50; // in pips
// Enums for order direction
enum OrderDirection {
ORDER_NONE, // No order
ORDER_BUY, // Buy order
ORDER_SELL // Sell order
};
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Initialization code here, if necessary
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Get current EMA and EMA from previous candle
double ema_fast_current = iMA(NULL, 0, EMA_fast_period, 0, MODE_EMA, PRICE_CLOSE, 0);
double ema_fast_previous = iMA(NULL, 0, EMA_fast_period, 0, MODE_EMA, PRICE_CLOSE, 1);
double ema_slow_current = iMA(NULL, 0, EMA_slow_period, 0, MODE_EMA, PRICE_CLOSE, 0);
double ema_slow_previous = iMA(NULL, 0, EMA_slow_period, 0, MODE_EMA, PRICE_CLOSE, 1);
// Get current RSI
double rsi_current = iRSI(NULL, 0, RSI_period, PRICE_CLOSE, 0);
// Check if there are no open trades
if (PositionsTotal() == 0)
{
// Check for BUY condition
if (ema_fast_previous < ema_slow_previous && ema_fast_current > ema_slow_current && rsi_current > 50)
{
OpenTrade(ORDER_BUY);
}
// Check for SELL condition
else if (ema_fast_previous > ema_slow_previous && ema_fast_current < ema_slow_current && rsi_current < 50)
{
OpenTrade(ORDER_SELL);
}
}
else
{
// Check for exit conditions if there is an open position
CloseTradeOnExitConditions(ema_fast_current, ema_fast_previous);
}
}
//+------------------------------------------------------------------+
//| Function to open a trade |
//+------------------------------------------------------------------+
void OpenTrade(OrderDirection direction)
{
double price;
double sl;
if (direction == ORDER_BUY)
{
price = Ask;
sl = NormalizeDouble(price - StopLossPips * Point, _Digits);
// Open Buy trade logic here
OrderSend(Symbol(), OP_BUY, 0.01, price, 3, sl, 0, "EMA RSI Buy", 0, clrGreen);
}
else if (direction == ORDER_SELL)
{
price = Bid;
sl = NormalizeDouble(price + StopLossPips * Point, _Digits);
// Open Sell trade logic here
OrderSend(Symbol(), OP_SELL, 0.01, price, 3, sl, 0, "EMA RSI Sell", 0, clrRed);
}
}
//+------------------------------------------------------------------+
//| Function to close a trade based on EMA cross |
//+------------------------------------------------------------------+
void CloseTradeOnExitConditions(double ema_fast_current, double ema_fast_previous)
{
double price;
if (OrderSelect(0, SELECT_BY_POS) && OrderSymbol() == Symbol())
{
if (OrderType() == OP_BUY &&
ema_fast_previous > ema_slow_current && ema_fast_current < ema_slow_current)
{
// Close Buy position
price = Bid; // For closing a buy order, you need the bid price
OrderClose(OrderTicket(), OrderLots(), price, 3, clrNONE);
}
else if (OrderType() == OP_SELL &&
ema_fast_previous < ema_slow_current && ema_fast_current > ema_slow_current)
{
// Close Sell position
price = Ask; // For closing a sell order, you need the ask price
OrderClose(OrderTicket(), OrderLots(), price, 3, clrNONE);
}
// Maybe include a routine to close any other unexpected orders
}
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Cleanup code here, if necessary
}
Questo script è un esempio di base di un EA su MT4 che apre e chiude ordini a seconda delle condizioni specificate, compreso l'uso di uno stop loss in pips. Assicurati di eseguire backtesting e test in modalità demo prima di utilizzare quest'EA in un ambiente di trading live, poiché il codice sopra è solo un esempio ed è privo di molti controlli necessari in un ambiente di trading reale.
Per favorire la scalabilità e la manutenibilità, il codice potrebbe essere suddiviso in funzioni più piccole con specifici compiti, e potrebbero essere aggiunti ulteriori controlli per errori, gestione del rischio e logging.