Per includere lo Stop Loss, Take Profit e Trailing Stop nel tuo EA, dovrai aggiungere alcune variabili di input per permettere la configurazione di questi valori e poi implementare la logica necessaria per gestire queste funzionalità.
Ecco il codice aggiornato con queste funzioni:
//+------------------------------------------------------------------+
//| SimpleEMA.mq4 |
//| Copyright 2021, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property strict
// Input parameters
input int FastEMA_Period = 8; // Period for the fast EMA
input int SlowEMA_Period = 20; // Period for the slow EMA
input int RSI_Period = 14; // Period for RSI
input double Lots = 0.1; // Lot size for the trades
input double StopLoss = 50; // Stop loss in pips
input double TakeProfit = 100; // Take profit in pips
input int TrailStart = 30; // Trailing stop start in pips
input int TrailDistance = 15; // Trailing stop distance in pips
int ticket = -1; // Order ticket
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Initialization code here
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Calculate indicators
double fastEMA = iMA(NULL, 0, FastEMA_Period, 0, MODE_EMA, PRICE_CLOSE, 0);
double slowEMA = iMA(NULL, 0, SlowEMA_Period, 0, MODE_EMA, PRICE_CLOSE, 0);
double rsi = iRSI(NULL, 0, RSI_Period, PRICE_CLOSE, 0);
// Define the Stop Loss and Take Profit prices
double sl_buy = NormalizeDouble(Ask - StopLoss * Point, Digits);
double tp_buy = NormalizeDouble(Ask + TakeProfit * Point, Digits);
double sl_sell = NormalizeDouble(Bid + StopLoss * Point, Digits);
double tp_sell = NormalizeDouble(Bid - TakeProfit * Point, Digits);
// Check for Buy condition
if(fastEMA > slowEMA && rsi > 50)
{
if(ticket <= 0 || OrderType() != OP_BUY)
{
if(ticket > 0)
OrderClose(ticket, OrderLots(), OrderClosePrice(), 3, CLR_NONE);
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, sl_buy, tp_buy, "EMA Buy Order", 0, 0, clrGreen);
}
// Add trailing stop here
if(ticket > 0 && OrderType() == OP_BUY)
ManageTrailingStop(TrailStart, TrailDistance, ticket);
}
// Check for Sell condition
else if(fastEMA < slowEMA && rsi < 50)
{
if(ticket <= 0 || OrderType() != OP_SELL)
{
if(ticket > 0)
OrderClose(ticket, OrderLots(), OrderClosePrice(), 3, CLR_NONE);
ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, sl_sell, tp_sell, "EMA Sell Order", 0, 0, clrRed);
}
// Add trailing stop here
if(ticket > 0 && OrderType() == OP_SELL)
ManageTrailingStop(TrailStart, TrailDistance, ticket);
}
}
//+------------------------------------------------------------------+
//| Function to manage trailing stop |
//+------------------------------------------------------------------+
void ManageTrailingStop(int TrailStartPips, int TrailDistancePips, int ticket)
{
double TrailStartPrice;
double TrailDistancePrice;
if(OrderSelect(ticket, SELECT_BY_TICKET) && OrderType() <= OP_SELL)
{
// Calculate trailing stop for BUY order
if(OrderType() == OP_BUY)
{
TrailStartPrice = OrderOpenPrice() + TrailStartPips * Point;
TrailDistancePrice = Bid - TrailDistancePips * Point;
if(Bid > TrailStartPrice && OrderStopLoss() < TrailDistancePrice)
{
OrderModify(ticket, OrderOpenPrice(), TrailDistancePrice, OrderTakeProfit(), 0, clrNONE);
}
}
// Calculate trailing stop for SELL order
else if(OrderType() == OP_SELL)
{
TrailStartPrice = OrderOpenPrice() - TrailStartPips * Point;
TrailDistancePrice = Ask + TrailDistancePips * Point;
if(Ask < TrailStartPrice && OrderStopLoss() > TrailDistancePrice)
{
OrderModify(ticket, OrderOpenPrice(), TrailDistancePrice, OrderTakeProfit(), 0, clrNONE);
}
}
}
}
//+------------------------------------------------------------------+
Questo codice ti dà un punto di partenza, ma dovrai testarlo e modificarlo ulteriormente per adattarlo alle tue esigenze specifiche. Ricordati sempre di verificare e ottimizzare gli EA in un ambiente sicuro come un conto demo o su un tester di strategia MT4 prima di usarli in un contesto di trading reale. Il trading automatico implica un rischio significativo e richiede un monitoraggio e un aggiornamento costante delle strategie.