Counting profit for all open BUY / SELL orders

 

Hello guys.

Iam trying to count current profit for all buy/ sell open orders. I got so far:

double CheckBuyProfit(){
double profit=OrderProfit() + OrderSwap() + OrderCommission();
        if (OrdersTotal()>0){
            for( int i = 0 ; i < OrdersTotal() ; i++ ) {
                if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES )){
                        if( (OrderSymbol()==Symbol()) && OrderType()==OP_BUY) {
                                profit+=OrderProfit() + OrderSwap() + OrderCommission();
                        }
                }
           }
        }
return profit;
}

It worked quite well till I got an idea to verify this counter - I compare this profit value to value from AccountProfit() and run test with buy orders only. Results are different, not much, but I dont know why... Is there something what am I doing wrong?  Please feel free to correct my code.

Thank you!

 
  1. double profit=OrderProfit() + OrderSwap() + OrderCommission();
    You start your sum with zero.
  2. You can not use any Trade Functions until you first select an order.

  3. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              PositionClose is not working - MQL5 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles

  4. The if is unnecessary; the for would exit immediately.
 
ramp:

Hello guys.

Iam trying to count current profit for all buy/ sell open orders. I got so far:

It worked quite well till I got an idea to verify this counter - I compare this profit value to value from AccountProfit() and run test with buy orders only. Results are different, not much, but I dont know why... Is there something what am I doing wrong?  Please feel free to correct my code.

Thank you!

try this, not tested!

int MagicNumber=12345;

double CheckBuyProfit()
  {
   double profit = 0;
   for(int x = OrdersTotal() - 1; x >= 0; x--)
     {
      if(!OrderSelect(x, SELECT_BY_POS, MODE_TRADES))
         break;
      if(OrderSymbol() != Symbol() && OrderMagicNumber() != MagicNumber)
         continue;
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         profit += OrderProfit() + OrderSwap() + OrderCommission();
     }
   return(profit);
  }
 
Kenneth Parling:

try this, not tested!

Perfect, works great! Thank you man :)


And William Roeder also thank you. Appreciate your advice :)

 
Kenneth Parling #:

try this, not tested!

Hi, what does its mean of MagicNumber?
 
nguyenduc999 #: what does its mean of MagicNumber?

Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
          Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
          PositionClose is not working - MQL5 programming forum (2020.02.21)
          MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
          Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (1 February 2011)

You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

 
Kenneth Parling #:

try this, not tested!

Hi,


I have this to calculate the current days profit.  

 double GetBalanceDaily() // Daily Start Balance

{
   double _resD=AccountBalance();
   datetime lastClose=iTime(NULL,PERIOD_D1,0);
      for(int pos=0; pos<OrdersHistoryTotal(); pos++) if(
         OrderSelect(pos,SELECT_BY_POS,MODE_HISTORY)&&lastClose<OrderCloseTime())
           { 
            if (OrderType()<2)_resD+=-1*(OrderProfit()+OrderSwap()+OrderCommission());
           }
   return _resD;
   
   
    static datetime _dt_w = iTime(NULL, PERIOD_D1, 0);
    
      if(_dt_w != iTime(NULL, PERIOD_D1, 0))
      {
      
         _dt_w = iTime(NULL, PERIOD_D1, 0);
   
         daily_balance=AccountBalance();
        
         
    
      }
  }

I'm having issues trying to work out the previous days profit..

i tried this, I know it's not correct, but just trying to get a response.  When I just tested TimeHour, it returned, but then would continue for the whole hour, so i tried to specify the Hour and Minutes, No luck.

I would prefer to do a calc, but not sure how to call previous days profit.

BankedDaily=NormalizeDouble(AccountBalance()-GetBalanceDaily(),2);
        if ((TimeHour(Time[0]) == 4) &&(TimeMinute(iTime(NULL, PERIOD_MN1, 0)) == 01))
       
               {send_alert("Daily Banked $"+BankedDaily+" ");YdayBanked=BankedDaily;}