How to calculate basket Break-Event price (weight average) by using ATR?
-
Question for Fxdreema CommunityHello everyone,
I’m working on a basket / recovery strategy and I want to use ATR to calculate one Break-Even price to close all open orders (not per order).
Conceptually, I understand that I need to calculate a weighted average basket price, like this:
BasketPrice = Σ (OrderOpenPrice × OrderLots) / Σ OrderLotsExample:
(1.10000 × 0.10) + (1.09800 × 0.20) + (1.09600 × 0.40) ----------------- = BasketPrice 0.10 + 0.20 + 0.40After that, I want to do:
- BUY basket →
BasketBE = BasketPrice + ATR - SELL basket →
BasketBE = BasketPrice − ATR
My problem is how to calculate this weighted basket price inside Fxdreema blocks.
Specifically, I’m not sure:
- Which blocks should be used to loop through all open orders
- How to accumulate (sum) OrderOpenPrice × OrderLots
- How to sum total lots
- And where to store these values so I can calculate
BasketPrice
If anyone has done something similar (basket BE, recovery, or grid systems), I’d really appreciate guidance on the correct block structure or logic flow in Fxdreema.
Thank you very much

- BUY basket →
-
Try this Custom Code Block (mql4)
First Go to Variables tab, define:
basketPrice = 0
basketLots = 0
basketBE = 0
atrValue = 0
isBuyBasket = 1Then use this code in a custom code block
// Reset values
basketPrice = 0;
basketLots = 0;
basketBE = 0;
atrValue = 0;
isBuyBasket = 1; // Assume BUY by defaultint 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;
}
}