I am working on this little RSI Expert Advisor in MQL4. It is trading fine at taking buys and sels but I just can´t get my trailing stop to work. I am going mad..:-)
I have added the code below. It is an simple EA based on the RSI. I would just like to add an trailing stop whenever a buy or sell trade is taken.
Can anyone see the problem?
// Define parameters for your EA outside of functions
input int takeProfit = 20; // Take Profit value in pips
input int stopLoss = 50; // Stop Loss value in pips
input int trailingStop = 50; // Trailing stop distance in pips
// Flag to track if a trade has been placed in the current tick
bool tradePlaced = false;
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Reset the tradePlaced flag at the beginning of each tick
tradePlaced = false;
// We create a string variable for the signal
string signal = "";
// Calculate the RSI value
double RSIValue = iRSI(_Symbol, _Period, 14, PRICE_CLOSE, 0);
// Declare take profit and stop loss variables
double takeProfitLevel, stopLossLevel;
// If it is above 70
if (RSIValue > 70)
{
signal = "sell";
takeProfitLevel = Bid - takeProfit * _Point;
stopLossLevel = Bid + stopLoss * _Point;
}
// If it is below 30
if (RSIValue < 30)
{
signal = "buy";
takeProfitLevel = Ask + takeProfit * _Point;
stopLossLevel = Ask - stopLoss * _Point;
}
// Buy or sell 10 Microlot based on the signal
if (OrdersTotal() == 0 && !tradePlaced)
{
if (signal == "buy")
{
int buyTicket = OrderSend(_Symbol, OP_BUY, 0.50, Ask, 3, 0, takeProfitLevel, "Buy Order", 0, stopLossLevel, Green);
if (buyTicket > 0)
{
Print("Buy Order placed successfully. Ticket: ", buyTicket);
tradePlaced = true; // Set the flag to true
}
else
{
Print("Error placing Buy Order. Error code: ", GetLastError());
}
}
else if (signal == "sell")
{
int sellTicket = OrderSend(_Symbol, OP_SELL, 0.50, Bid, 3, 0, takeProfitLevel, "Sell Order", 0, stopLossLevel, Red);
if (sellTicket > 0)
{
Print("Sell Order placed successfully. Ticket: ", sellTicket);
tradePlaced = true; // Set the flag to true
}
else
{
Print("Error placing Sell Order. Error code: ", GetLastError());
}
}
}
// Trailing stop logic
if (OrdersTotal() > 0)
{
for (int i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
// Check if the order is a Buy or Sell order
if (OrderSymbol() == _Symbol)
{
double currentProfit = OrderProfit();
double openPrice = OrderOpenPrice();
double trailingStopLevel;
// Calculate the trailing stop level
if (OrderType() == OP_BUY)
{
trailingStopLevel = openPrice + trailingStop * _Point;
}
else if (OrderType() == OP_SELL)
{
trailingStopLevel = openPrice - trailingStop * _Point;
}
// Update the stop loss to the trailing stop level
double newStopLoss = 0;
if (OrderType() == OP_BUY)
{
newStopLoss = MathMax(openPrice + currentProfit - trailingStop * _Point, trailingStopLevel);
}
else if (OrderType() == OP_SELL)
{
newStopLoss = MathMin(openPrice + currentProfit + trailingStop * _Point, trailingStopLevel);
}
if (newStopLoss != OrderStopLoss())
{
if (OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, Green))
{
Print("Trailing Stop updated successfully. Ticket: ", OrderTicket());
}
else
{
Print("Error updating Trailing Stop. Error code: ", GetLastError());
}
}
}
}
}
}
// Chart output for the signal
Comment("The current signal is: ", signal);
}
I am hoping someone can see the issue and guide me in the right direction
Can anyone see the issue?
I would try to
when setting it.