Get profit last closed position in point.

Liana
361
Liana  

Hi!

I need to get profit in point of last closed position.  

This is the code, but something wrong. Please help.

Thank you. 

 

double GetProfitLastClosePosInPoint(string sy="", int op=-1, int mn=-1) {
  double p;
  int      i, k=OrdersHistoryTotal(), pr=0;

 
 
  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if (OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)){
         if (mn<0 || OrderMagicNumber()==mn) {
          p=MarketInfo(OrderSymbol(), MODE_POINT);
          if (p==0) if (StringFind(OrderSymbol(),  "JPY")<0) p=0.0001; else p=0.01;
          
          
          if (OrderType()==OP_BUY) {
            pr+=(OrderClosePrice()-OrderOpenPrice())/p;
          }
          
         
          if (OrderType()==OP_SELL) {
            pr+=(OrderOpenPrice()--OrderClosePrice())/p;
          }
       
       
        }
      }
    }
  }
  return(pr);
} 
qjol
3200
qjol  

i didn't checked ur code, but always countdown
& pls specify what's wrong

for (i=k-1; i>=0; i--)
Liana
361
Liana  
qjol:

 pls specify what's wrong

')' - unbalanced right parenthesis

Simon Gniadkowski
17309
Simon Gniadkowski  
liana:

Hi!

I need to get profit in point of last closed position.  

This is the cod, but something wrong. Please help.

Looks like there is a missing   (  brace here . . .

      if ( OrderSymbol() == sy || sy == "" ) && ( op < 0 || OrderType() == op ) )
         {
qjol
3200
qjol  

this is also wrong

pr+=(OrderOpenPrice()--OrderClosePrice())/p; //twice minus
Liana
361
Liana  

Oh thank you!

 

I made the changes - it looks like this now.

int GetProfitLastClosePosInPoint(string sy="", int op=-1, int mn=-1) {
  double p;
  int      i, k=OrdersHistoryTotal(), pr=0;

 
 
  if (sy=="0") sy=Symbol();
  for (i=k-1; i>=0; i--) {
    
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if ((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)){
         if (mn<0 || OrderMagicNumber()==mn) {
          p=MarketInfo(OrderSymbol(), MODE_POINT);
          if (p==0) if (StringFind(OrderSymbol(),  "JPY")<0) p=0.0001; else p=0.01;
          
          
          if (OrderType()==OP_BUY) {
            pr+=(OrderClosePrice()-OrderOpenPrice())/p;
          }
          
         
          if (OrderType()==OP_SELL) {
            pr+=(OrderOpenPrice()-OrderClosePrice())/p;
          }
       
       
        }
      }
    }
  }
  return(pr);
}
qjol
3200
qjol  
is there a problem now ? if so then what is it
Liana
361
Liana  

thank you it works now.

Liana
361
Liana  

Yes it works for one position. I get profit only for one last close position (Symbol, Order Type, Magic).

Can you please help me to change it that I have the sum of profit for all close positions (Symbol, Order Type, Magic).

I am really stuck :( Please help.

Simon Gniadkowski
17309
Simon Gniadkowski  
liana:

Yes it works for one position. I get profit only for one last close position (Symbol, Order Type, Magic).

Can you please help me to change it that I have the sum of profit for all close positions (Symbol, Order Type, Magic).

I am really stuck :( Please help.

Please show what you have tried . . .  or explain what you tried.
Liana
361
Liana  

I tried to use this function to get profit for one last order and than my logic is like this

int Profit =0;

int NewProfit =0 ;

NewProfit =  GetProfitLastClosePosInPoint (NULL, -1, magic);

 if (NewProfit >0){


 Profit = Profit+NewProfit;

 }

 But I understand that each time Profit is again =0.