Loop to get Lowest NPL values for Arrays (MT4)

 

Hi, I tried to get the BUY and SELL's Lowest Net Profit or Loss (LLNPL) for each symbol in my EA, but I cannot get the correct answers ... My codes are as follows :-

   string arTrSym[8]={"AUDCAD","AUDCHF","AUDJPY","AUDNZD","AUDUSD","CADCHF","CADJPY","CHFJPY"};

   double bLLNPL[], sLLNPL[];
   ArrayResize(bLLNPL,ArraySize(arTrSym));
   ArrayResize(sLLNPL,ArraySize(arTrSym));

   for(int i=0; i<ArraySize(arTrSym); i++) {
   double bSum=0, sSum=0;
      for(int j=OrdersTotal()-1; j>=0; j--) {
      if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==arTrSym[i] && (OrderMagicNumber()==mnBUY || OrderMagicNumber()==mnSEL)) {
      if(OrderType()==OP_BUY)    bSum+=OrderProfit()+OrderCommission()+OrderSwap();
      if(OrderType()==OP_SELL)   sSum+=OrderProfit()+OrderCommission()+OrderSwap();}}
   bLLNPL[i]=bSum; sLLNPL[i]=sSum;}

   ArraySort(bLLNPL,WHOLE_ARRAY,0,MODE_ASCEND); // -50.00,-23.02 ... 30.50
   ArraySort(sLLNPL,WHOLE_ARRAY,0,MODE_ASCEND); // -50.00,-23.02 ... 30.50

   Print("bLLNPL "+DoubleToStr(bLLNPL[0],2)+" / "+DoubleToStr(bLLNPL[1],2)+" / "+DoubleToStr(bLLNPL[2],2)+" / "+DoubleToStr(bLLNPL[3],2)+" ++ sLLNPL "+
   DoubleToStr(sLLNPL[0],2)+" / "+DoubleToStr(sLLNPL[1],2)+" / "+DoubleToStr(sLLNPL[2],2)+" / "+DoubleToStr(sLLNPL[3],2));




Could you please advise where I coded wrongly (FYI, there are open trades for the arTrSym when I ran the Print) ?

I notice that if the NPL values of ArraySorted symbols are positive values (in profit position), the output will be "0.00", my question : the OrderProfit()+OrderCommission()+OrderSwap() should give both positive and negative values, and should not convert the positive values to "0.00", am I right ?

Thanks
 

This should be posted under MT4 section. It is working on my side...

Are you sure you do not need to separate mnBuy and mnSell ?(using OR is weird as it might be true for a buy order while the order is a sell)

 
Yashar Seyyedin #:
This should be posted under MT4 section. It is working perfectly on my side...
Yes, it works, but my the other question is : I notice that if the NPL values of ArraySorted symbols are positive values (in profit position), the output will be "0.00", my question : the OrderProfit()+OrderCommission()+OrderSwap() should give both positive and negative values, and should not convert the positive values to "0.00", am I right ?
 

I have a sample code that shows ArraySort is not turning any negative or positive number into zero:

int OnInit()
  {
   double array1[]={3, 1, 2, 4};
   double array2[]={-3, -1, -2, -4};
   ArraySort(array1,WHOLE_ARRAY,0,MODE_ASCEND); // -50.00,-23.02 ... 30.50
   ArraySort(array2,WHOLE_ARRAY,0,MODE_ASCEND); // -50.00,-23.02 ... 30.50   
   
   string s1="";
   for(int i=0;i<4;i++)
   {
      s1=StringConcatenate(s1, array1[i], ",");
   }
   Print(s1);

   string s2="";
   for(int i=0;i<4;i++)
   {
      s2=StringConcatenate(s2, array2[i], ",");
   }
   Print(s2);
   
   return(INIT_SUCCEEDED);
  }
 
Yashar Seyyedin #:
StringConcatenate
If I test the ArraySort in a script, it will not turn the positive (for MODE_ASCEND) and negative (for MODE_DESCEND) into zero, but why it turns into zero in EA ? Why the ArraySort works differently ?
 
Chin Min Wan #:
If I test the ArraySort in a script, it will not turn the positive (for MODE_ASCEND) and negative (for MODE_DESCEND) into zero, but why it turns into zero in EA ? Why the ArraySort works differently ?

Print the original array first... You will probably see those zero values in the original array also.

 
Yashar Seyyedin #:

This should be posted under MT4 section. It is working on my side...

Are you sure you do not need to separate mnBuy and mnSell ?(using OR is weird as it might be true for a buy order while the order is a sell)

OK, I accept your advice, just changed to as follows :-

   for(int i=0; i<ArraySize(arTrSym); i++) {
   double bSum=0, sSum=0;
      for(int j=OrdersTotal()-1; j>=0; j--) {
      if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==arTrSym[i]) {
      if(OrderMagicNumber()==mnBUY && OrderType()==OP_BUY)  bSum+=OrderProfit()+OrderCommission()+OrderSwap();
      if(OrderMagicNumber()==mnSEL && OrderType()==OP_SELL) sSum+=OrderProfit()+OrderCommission()+OrderSwap();}}
   bLLNPL[i]=bSum; sLLNPL[i]=sSum;}
 
Yashar Seyyedin #:

Print the original array first... You will probably see those zero values in the original array also.

Assalamualaikum Yashar, you're so kind to serve the community, therefore most likely I'll directly appoint you to be my freelance developer for my new job (create a multi-symbol EA + add-on some additional features) in next 1 or 2 weeks time. Kindly PM me whenever you're in "free" status, thanks
Reason: