// Loop through all recently closed positions
for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
// Check if it's a sell order and its stoploss was hit
if (OrderType() == OP_SELL && OrderStopLoss() > 0 && OrderStopLoss() >= OrderClosePrice())
{
// Place buy trade
ticket = OrderSend(Symbol(), OP_BUY, lotSize, entryPrice, 3, 0, 0, "Buy order placed by MyScript", 0, 0, clrNONE);
// Check for errors
if (ticket < 0)
{
Print("Error placing buy order: ", GetLastError());
}
else
{
Print("Buy order placed successfully at price: ", entryPrice);
}
}
}
}