need help for calculated total pips for order closed today (Mql5) - page 3

 
nicholi shen:

Thanks, but that's how it's done. 

-----------------------
-----------------------
-----------------------
-----------------------
-----------------------
position_id: 383147618
in tickets (1) = [363190125]
out tickets (1) = [375954098]
zero divide in 'Test8.mq5' (176,7)
 
nicholi shen:

Thanks, but that's how it's done. 

 :) well sometimes a little creativity can buck the standard procedures :).  Although this is not as efficient as what nicholi provided... it does the job!

You have at your disposal essemtial variables startDateTime,endDateTime, and _magic

In order to associate the deal_position_id's to a corresponding Magic Number... we take a little detour and gather a string list (comma separated) of all the deal_positions_IDs that are associated with the magic number.hec you can even associates part of the order_comments as well and produce the position_ids that YOU need that way to.

So what is it then?   Since we are interested in the "in" and "out" deals and as you loop thru the DealsHistory you can see that they pop up as the index is increments plus you know you can identify the "in"/"out" deals with the deal_entry_type.

Well then lets create a very simple array (in[]) of type struct.  The struct array will contain 3 props:   deal_position_id, deal_type,deal_price.  So since the "in" deals are returned as we step thru the index first tell the script that when deal_entry_type=DEAL_ENTRY_IN to populate the in[] Array structure with the 3 items we need.    Now later on the "out" deals start to appear and since we know that those positions_IDs need to match the "in" deals and we already have that in an array in[], well then we just program the script for when  deal_entry_type=DEAL_ENTRY_IN to loop thru the already populated in[] array and whenever we see that the in.position_id=out_position_id ... we do the subtraction and get the pips etc etc etc.

Oh and since I kinda still like that old MT4 MarketInfo() function I am including the include code below as well.  That allows ya to use the function in MT5 just as it you were in MT4 LOL

Anyhow... see the code... figure it out... and enjoy!

Enjoy!!!!!

include <mt4Functions.mqh>

struct in_struct { ulong pos_id; long order_type; double price; };
in_struct in[];
string magic_posIDs;

double ClosedPips(int _magic, string _text,datetime endDateTime, datetime beginDateTime){
  long deal_ticket=0,deal_type,deal_position_id,deal_entry;
  double pip_profit=0,deal_price;
  int historyDealsTotal,cnt=0;
  string deal_symbol;
  
  HistorySelect(beginDateTime,endDateTime);
  historyDealsTotal=HistoryDealsTotal();
  
  //the following generates a list of position Ids related to the magic or text_comments (magic_posIDs)
  if(StringLen(magic_posIDs)==0) getPositionIDsFromMagic(_magic,_text,historyDealsTotal,magic_posIDs);//Only need to do this once. right?  LOL

  //Current Trades open/closed this week
  for(int i=0; i<historyDealsTotal; i++) {
      if((deal_ticket=(int)HistoryDealGetTicket(i))==0) continue;
      deal_type = HistoryDealGetInteger(deal_ticket,DEAL_TYPE);//Buy/Sell or initial deposited etc
      if(deal_type == DEAL_TYPE_BALANCE) continue; //Skip Balance operations

      deal_position_id = HistoryDealGetInteger(deal_ticket,DEAL_POSITION_ID);
      deal_symbol = HistoryDealGetString(deal_ticket,DEAL_SYMBOL);
      deal_entry = (ENUM_DEAL_ENTRY)HistoryDealGetInteger(deal_ticket,DEAL_ENTRY); //either an DEAL_ENTRY_OUT(out) or DEAL_ENTRY_IN(in)
      deal_price = HistoryDealGetDouble(deal_ticket,DEAL_PRICE);
                                 
      if(_magic>-1) //only look at trades with this magic number/text comment
         if(StringFind(magic_posIDs,(string)deal_position_id)==-1) continue;
                     
       //get all 'in' deals into in[] array and store the position_id, deal_type, and deal_price
       if(deal_entry==DEAL_ENTRY_IN) { 
              ArrayResize(in,cnt+1);
              in[cnt].pos_id=deal_position_id;
              in[cnt].order_type=deal_type;
              in[cnt].price=deal_price;
              cnt++;
       }

      //If we get into this portion that means that the in[] array is ready with populated deal prices (open prices)
      if(deal_entry==DEAL_ENTRY_OUT) {  
            double deal_pointz=MarketInfo(deal_symbol,MODE_POINT);   
            if(MarketInfo(deal_symbol,MODE_DIGITS)==5 || MarketInfo(deal_symbol,MODE_DIGITS)==3) deal_pointz=10*deal_pointz;         
            for(int x=0; x<ArraySize(in); x++) {
               if(deal_position_id==in[x].pos_id) {
                  if(deal_type==OP_SELL) pip_profit+=(deal_price-in[x].price)/deal_pointz;
                  if(deal_type==OP_BUY) pip_profit+=(in[x].price-deal_price)/deal_pointz;
               }
            }                   
      }
   } 
   
   return(pip_profit);
}

void getPositionIDsFromMagic(int _magic,string _text,int _historyDealsTotal, string& posIDs) { 
   
   long dealTicket,dealPosition_id,dealType,dealMagicNumber;
   string dealComment;
 
   posIDs=""; 
   for(int i=_historyDealsTotal-1; i>=0; i--) {
      if((dealTicket=(int)HistoryDealGetTicket(i))==0) continue;
      dealType = HistoryDealGetInteger(dealTicket,DEAL_TYPE); //buy/sell/limit/stop etc
      if(dealType == DEAL_TYPE_BALANCE) continue;  //skip the balance stuff
      
      dealPosition_id = HistoryDealGetInteger(dealTicket,DEAL_POSITION_ID);
      dealMagicNumber = HistoryDealGetInteger(dealTicket,DEAL_MAGIC);
      dealComment = HistoryDealGetString(dealTicket,DEAL_COMMENT);             

      if(_magic>-1)
         if(_magic!=dealMagicNumber) continue; 
      
      if(StringLen(_text)>0)
         if(StringFind(dealComment,"_"+_text+"_")==-1) continue;  
                   
      posIDs+=(string)dealPosition_id+",";
   }
   
   if(StringLen(posIDs)>0)
      posIDs=StringSubstr(posIDs,0,StringLen(posIDs)-1); //returns byRef a string of posIDs for given MagicNumber and/or text comment
}


zee mt4Functions.mqh:

MqlTick last_tick;

//--- Declaration of constants
#define OP_BUY 0           //Buy 
#define OP_SELL 1          //Sell 
#define OP_BUYLIMIT 2      //Pending order of BUY LIMIT type 
#define OP_SELLLIMIT 3     //Pending order of SELL LIMIT type 
#define OP_BUYSTOP 4       //Pending order of BUY STOP type 
#define OP_SELLSTOP 5      //Pending order of SELL STOP type 
//---
#define MODE_OPEN 0
#define MODE_CLOSE 3
#define MODE_VOLUME 4 
#define MODE_REAL_VOLUME 5
#define MODE_TRADES 0
#define MODE_HISTORY 1
#define SELECT_BY_POS 0
#define SELECT_BY_TICKET 1
//---
#define DOUBLE_VALUE 0
#define FLOAT_VALUE 1
#define LONG_VALUE INT_VALUE
//---
#define CHART_BAR 0
#define CHART_CANDLE 1
//---
#define MODE_ASCEND 0
#define MODE_DESCEND 1
//---
#define MODE_LOW 1
#define MODE_HIGH 2
#define MODE_TIME 5
#define MODE_BID 9
#define MODE_ASK 10
#define MODE_POINT 11
#define MODE_DIGITS 12
#define MODE_SPREAD 13
#define MODE_STOPLEVEL 14
#define MODE_LOTSIZE 15
#define MODE_TICKVALUE 16
#define MODE_TICKSIZE 17
#define MODE_SWAPLONG 18
#define MODE_SWAPSHORT 19
#define MODE_STARTING 20
#define MODE_EXPIRATION 21
#define MODE_TRADEALLOWED 22
#define MODE_MINLOT 23
#define MODE_LOTSTEP 24
#define MODE_MAXLOT 25
#define MODE_SWAPTYPE 26
#define MODE_PROFITCALCMODE 27
#define MODE_MARGINCALCMODE 28
#define MODE_MARGININIT 29
#define MODE_MARGINMAINTENANCE 30
#define MODE_MARGINHEDGED 31
#define MODE_MARGINREQUIRED 32
#define MODE_FREEZELEVEL 33
//---
#define EMPTY -1

double MarketInfo(string symbol, int type)
  {
   SymbolInfoTick(symbol,last_tick);
   double Bid=last_tick.bid;
   double Ask=last_tick.ask;             

   switch(type)
     {
      case MODE_LOW:
         return(SymbolInfoDouble(symbol,SYMBOL_LASTLOW));
      case MODE_HIGH:
         return(SymbolInfoDouble(symbol,SYMBOL_LASTHIGH));
      case MODE_TIME:
         return((double)SymbolInfoInteger(symbol,SYMBOL_TIME));
      case MODE_BID:     
         return(Bid);
      case MODE_ASK:     
         return(Ask);
      case MODE_POINT:
         return(SymbolInfoDouble(symbol,SYMBOL_POINT));
      case MODE_DIGITS:
         return((double)SymbolInfoInteger(symbol,SYMBOL_DIGITS));
      case MODE_SPREAD:
         return((double)SymbolInfoInteger(symbol,SYMBOL_SPREAD));
      case MODE_STOPLEVEL:
         return((double)SymbolInfoInteger(symbol,SYMBOL_TRADE_STOPS_LEVEL));
      case MODE_LOTSIZE:
         return(SymbolInfoDouble(symbol,SYMBOL_TRADE_CONTRACT_SIZE));
      case MODE_TICKVALUE:
         return(SymbolInfoDouble(symbol,SYMBOL_TRADE_TICK_VALUE));
      case MODE_TICKSIZE:
         return(SymbolInfoDouble(symbol,SYMBOL_TRADE_TICK_SIZE));
      case MODE_SWAPLONG:
         return(SymbolInfoDouble(symbol,SYMBOL_SWAP_LONG));
      case MODE_SWAPSHORT:
         return(SymbolInfoDouble(symbol,SYMBOL_SWAP_SHORT));
      case MODE_STARTING:
         return(0);
      case MODE_EXPIRATION:
         return(0);
      case MODE_TRADEALLOWED:
         return(0);
      case MODE_MINLOT:
         return(SymbolInfoDouble(symbol,SYMBOL_VOLUME_MIN));
      case MODE_LOTSTEP:
         return(SymbolInfoDouble(symbol,SYMBOL_VOLUME_STEP));
      case MODE_MAXLOT:
         return(SymbolInfoDouble(symbol,SYMBOL_VOLUME_MAX));
      case MODE_SWAPTYPE:
         return((double)SymbolInfoInteger(symbol,SYMBOL_SWAP_MODE));
      case MODE_PROFITCALCMODE:
         return((double)SymbolInfoInteger(symbol,SYMBOL_TRADE_CALC_MODE));
      case MODE_MARGINCALCMODE:
         return(0);
      case MODE_MARGININIT:
         return(0);
      case MODE_MARGINMAINTENANCE:
         return(0);
      case MODE_MARGINHEDGED:
         return(0);
      case MODE_MARGINREQUIRED:
         return(0);
      case MODE_FREEZELEVEL:
         return((double)SymbolInfoInteger(symbol,SYMBOL_TRADE_FREEZE_LEVEL));

      default: return(0);
     }
   return(0);
  }