fxDreema

    • Register
    • Login
    • Search
    • Back to the main page
    • Categories
    • Recent
    • Tags
    • Popular
    • Search

    How to calculate basket Break-Event price (weight average) by using ATR?

    Questions & Answers
    2
    2
    77
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • W
      windsor-2003 last edited by

      📌 Question for Fxdreema Community

      Hello 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) / Σ OrderLots
      

      Example:

      (1.10000 × 0.10) +
      (1.09800 × 0.20) +
      (1.09600 × 0.40)
      -----------------  = BasketPrice
      0.10 + 0.20 + 0.40
      

      After 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 🙏

      1 Reply Last reply Reply Quote 0
      • vish
        vish last edited by vish

        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;
        }
        }

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post

        Online Users

        T
        M
        P
        S
        A
        E

        22
        Online

        146.6k
        Users

        22.4k
        Topics

        122.6k
        Posts

        Powered by NodeBB Forums | Contributors