71616211
71616211
71616211
71616211
My bot opens a sell position to close a buy trade immediately after a buy position as been opened.
Who knows why should please help me out this is my open trade function; void OpenTrade(int tradeType) {

// Calculate lot size

double lotSize = 0.2; // Fixed lot size

// Get the current Bid and Ask prices

double Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);

double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);



// Calculate the Average True Range (ATR)

int atrPeriod = 14; // ATR period

double atr = iATR(Symbol(), 0, atrPeriod);



// Set the Stop Loss (SL) and Take Profit (TP) levels based on ATR

double sl, tp;

if (tradeType == ORDER_BUY) {

sl = Bid - (20 * atr); // 10 x ATR below the entry price

tp = Ask + (12 * atr); // 12 x ATR above the entry price

} else if (tradeType == ORDER_SELL) {

sl = Bid + (20 * atr); // 10 x ATR above the entry price

tp = Bid - (12 * atr); // 12 x ATR below the entry price

} else {

Print("Invalid trade type: ", tradeType);

return; // Exit function if trade type is invalid

}



// Validate stop levels and adjust if necessary

double stopLevel = SymbolInfoInteger(Symbol(), SYMBOL_TRADE_STOPS_LEVEL) * _Point; // Minimum stop level in points

if (stopLevel > 0) {

if (MathAbs(sl - ((tradeType == ORDER_BUY) ? Bid : Ask)) < stopLevel) {

Print("SL too close to market price. Adjusting SL.");

sl = (tradeType == ORDER_BUY) ? Ask - (stopLevel + 2 * _Point) : Ask + (stopLevel + 2 * _Point);

}

if (MathAbs(tp - ((tradeType == ORDER_BUY) ? Bid : Ask)) < stopLevel) {

Print("TP too close to market price. Adjusting TP.");

tp = (tradeType == ORDER_BUY) ? Ask + (stopLevel + 2 * _Point) : Ask - (stopLevel + 2 * _Point);

}

}



// Prepare the trade request

MqlTradeRequest request;

MqlTradeResult result;

ZeroMemory(request);

ZeroMemory(result);

request.action = TRADE_ACTION_DEAL;

request.symbol = Symbol();

request.volume = lotSize;

request.type = (tradeType == ORDER_BUY) ? ORDER_TYPE_BUY : ORDER_TYPE_SELL;

request.price = (tradeType == ORDER_BUY) ? SymbolInfoDouble(Symbol(), SYMBOL_ASK) : SymbolInfoDouble(Symbol(), SYMBOL_BID);

request.sl = NormalizeDouble(sl, (int) _Digits);

request.tp = NormalizeDouble(tp, (int) _Digits);

request.deviation = 10; // Maximum allowed slippage in points

request.magic = 123456; // Unique identifier for the EA

request.comment = "Trade opened by RIRI'S BOT"; // Custom comment for the trade



// Send the trade request

if (!OrderSend(request, result)) {

Print("Error opening trade. Retcode: ", result.retcode);

} else {

if (result.retcode == 10009) // TRADE_RETCODE_DONE

{

Print("Trade opened successfully. Ticket: ", result.order, ", SL: ", request.sl, ", TP: ", request.tp);

TradesToday++; // Increment trades today counter

isTradeOpen = true; // Set flag to indicate a trade is open

}

else if (result.retcode == 10008) // TRADE_RETCODE_REQUOTE

{

Print("Requote error. Trying again..."); // You can retry the trade here or handle the error differently

}

else

{

Print("Trade failed to open. Retcode: ", result.retcode);

}

}

}
71616211
Registered at MQL5.community