Try this Custom Code Block (mql4)
First Go to Variables tab, define:
basketPrice = 0
basketLots = 0
basketBE = 0
atrValue = 0
isBuyBasket = 1
Then use this code in a custom code block
// Reset values
basketPrice = 0;
basketLots = 0;
basketBE = 0;
atrValue = 0;
isBuyBasket = 1; // Assume BUY by default
int buyCount = 0;
int sellCount = 0;
// Loop through open trades (current symbol only)
for (int i = OrdersTotal() - 1; i >= 0; i--) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol() != Symbol()) continue;
int type = OrderType();
if (type == OP_BUY || type == OP_SELL) {
basketPrice += OrderOpenPrice() * OrderLots();
basketLots += OrderLots();
if (type == OP_BUY) buyCount++;
if (type == OP_SELL) sellCount++;
}
}
}
// Avoid division by zero
if (basketLots > 0) {
basketPrice = basketPrice / basketLots;
atrValue = iATR(Symbol(), 0, 14, 0); // ATR(14)
// Determine direction
isBuyBasket = (buyCount >= sellCount) ? 1 : 0;
// Calculate Basket BE
if (isBuyBasket == 1) {
basketBE = basketPrice + atrValue;
} else {
basketBE = basketPrice - atrValue;
}
}