Re: How can get average profit value for buy and sell orders

#2
kyawswarlin wrote:
Fri Nov 13, 2020 8:45 pm
when multiple open buy and sell orders..need to close buy order by average takeprofit value and sell orders also.
How can get average profit value for buy and sell orders.
Not average value for all.
Buy average profit and sell average profit.
Thanks you coders.
I am using this one.

Code: Select all


    for (int i = OrdersTotal() - 1; i >= 0; i--) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == _Symbol && OrderMagicNumber() == MagicNumber) {
         _profit = OrderProfit() + OrderSwap() + OrderCommission();
         if (OrderType() == OP_BUY) {
            sum_lot_b += OrderLots();
            sum_price_b += OrderOpenPrice() * OrderLots();
            sum_profit_b += _profit;
         }
         if (OrderType() == OP_SELL) {
            sum_lot_s += OrderLots();
            sum_price_s += OrderOpenPrice() * OrderLots();
            sum_profit_s += _profit;
         }
      }
   }
   if (sum_price_b > DBL_EPSILON) {
      sum_price_b /= sum_lot_b;
      ave_price_b = NormalizeDouble( (sum_profit_b/(MathAbs(sum_lot_b*MarketInfo(Symbol(),MODE_TICKVALUE)))*MarketInfo(Symbol(),MODE_TICKSIZE)), _Digits);
   }
   if (sum_price_s > DBL_EPSILON) {
      sum_price_s /= sum_lot_s;
      ave_price_s = NormalizeDouble( (sum_profit_s/(MathAbs(sum_lot_s*MarketInfo(Symbol(),MODE_TICKVALUE)))*MarketInfo(Symbol(),MODE_TICKSIZE)), _Digits);
   }  


  1. Similar Topics

    1. How to calculate average price for open orders

      0 Replies 446 Views

      by jack19, Mon May 31, 2021 8:15 am in cTrader Forum

      0 Replies
      446 Views
    2. 5 Replies
      966 Views
    3. Take Profit with defined lot size - script - not EA

      4 Replies 512 Views

      by Black Art, Sat Mar 06, 2021 1:14 am in MT4 Indicators

      4 Replies
      512 Views

Return to “Coding Help”