Question for the Guru's - page 2

 
I'm a little confused on what you're after... are you trying to get the number of long trades (9), the number of short trades (7), the total of all long trades (-3.69) and the total of all short trades (-8.63)?
 

Yes,with that function i am just trying to get the total amount lost for buy and sell, then add them together.

 
So if you had 5 losers at -10.00 and 3 winners at +3.00 you want to retrieve -10.00 (not -7.00)?
 
honest_knave:
So if you had 5 losers at -10.00 and 3 winners at +3.00 you want to retrieve -10.00 (not -7.00)?
yes. and get the total loss in currency.
 

See if this makes sense:

   int    longcnt     = 0,
          shortcnt    = 0;
   double longlosses  = 0,
          shortlosses = 0;
   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) { continue; }
      if(OrderType() == OP_BUY)
        {
         longcnt++;
         double result = OrderProfit() + OrderCommission() + OrderSwap();
         if(result<0) { longlosses += result; }
        }
      else if(OrderType() == OP_SELL)
        {
         shortcnt++;
         double result = OrderProfit() + OrderCommission() + OrderSwap();
         if(result<0) { shortlosses += result; }
        }
     }
   printf("Long trades = %i",longcnt);
   printf("Short trades = %i",shortcnt);
   printf("Long trade losses = %.2f",longlosses);
   printf("Short trade losses = %.2f",shortlosses);
 
honest_knave:

See if this makes sense:

 

Not the "Right in front of my face" solution I expected.

I see my problem is understanding the count/addition part. Definitely something I need to work on.

Thank you very much!!  

 

Well, there were quite a few issues with your other code. FWIW:

Be careful with your brackets.

         if(OrderType()==OP_BUY)
            if(OrderClosePrice()<OrderOpenPrice())
              {
               Long_Count=OrderOpenPrice()-OrderClosePrice();
              }
               Long_Count++; 

I'm guessing you only want Long_Count++ to run if OrderType()==OP_BUY? That isn't the case with your code. You would need to do this:

if(OrderType()==OP_BUY)
  {
   if(OrderClosePrice()<OrderOpenPrice())
     {
      Long_Count=OrderOpenPrice()-OrderClosePrice();
     }
   Long_Count++; 
  }

More fundamentally, I'm not sure why you are adding 1 to Long_Count, and also setting the value of Long_Count to the price difference between OrderOpenPrice() and OrderClosePrice() each time? It seems to me you are mixing apples (the count) and oranges (the price difference). There is a big difference between: 

 Long_Count=OrderOpenPrice()-OrderClosePrice();

 and

 Long_Count+=OrderOpenPrice()-OrderClosePrice();

 

A count should really be an integer or a long, not a double.

Also, if you subtract the prices from one another you're not taking into account your lot sizes. OrderProfit() seems more logical to me. It looks like you don't have any commission, but it is probably good practice to account for it (and swap).

HTH

 
honest_knave:

Well, there were quite a few issues with your other code. FWIW:

Be careful with your brackets.

I'm guessing you only want Long_Count++ to run if OrderType()==OP_BUY? That isn't the case with your code. You would need to do this:

More fundamentally, I'm not sure why you are adding 1 to Long_Count, and also setting the value of Long_Count to the price difference between OrderOpenPrice() and OrderClosePrice() each time? It seems to me you are mixing apples (the count) and oranges (the price difference). There is a big difference between: 

 and

 

A count should really be an integer or a long, not a double.

Also, if you subtract the prices from one another you're not taking into account your lot sizes. OrderProfit() seems more logical to me. It looks like you don't have any commission, but it is probably good practice to account for it (and swap).

HTH

I see what you are talking about now. 

At least some of the "Right in front of my face" showed up, Brackets. Using brackets to define exactly what I am looking for is something lack in doing sometimes, especially since I code more on paper than the computer, I tend to abbreviate everything down to its simplest form which is not always the correct way of doing things.

I am reading up Operators and Expressions right now, then I will go through your code,figure what I need to learn and how to apply it. I am glad to have the week off work now.

Your help is very much appreciated, Thank You. 

 
void RiskManagement_Pips()
  {
/*-------------------------------------------------------------------------------------------------*/
   int Long_Count=0;
   int Short_Count=0;
   int Total_Count=0;

   double Long_Loss=0;
   double Short_Loss=0;
   double Total_Loss=0;
   double Order_Loss=0;

   double Pips_Available=0;

   datetime Reset=iTime(NULL,PERIOD_D1,0);
   double Risk_Reset=Acct_RiskCapitol*Pips;

   for(int a=OrdersHistoryTotal()-1;a>=0;a--)
      if(OrderSelect(a,SELECT_BY_POS,MODE_HISTORY))
        {
         if(OrderType()==OP_BUY)
           {
            if(OrderOpenTime()>Reset)
               if(OrderClosePrice()<OrderOpenPrice())
                 {
                  Long_Count++;
                  Order_Loss=OrderProfit();
                  if(Order_Loss<0){Long_Loss+=Order_Loss;}
                 }
           }
         if(OrderType()==OP_SELL)
           {
            if(OrderOpenTime()>Reset)
               if(OrderClosePrice()>OrderOpenPrice())
                 {
                  Short_Count++;
                  Order_Loss=OrderProfit();
                  if(Order_Loss<0){Short_Loss+=Order_Loss;}
                 }
           }
        }

   Total_Count=Long_Count+Short_Count;
   double Long_Loss_Conversion=Long_Loss*Pips;
   double Short_Loss_Conversion=Short_Loss*Pips;
   double Total_Loss_Conversion=(Long_Loss+Short_Loss)*Pips;
   Pips_Available=Risk_Reset+Total_Loss_Conversion;

   Print("------------------------------------------");
   Print("Long_Count "+IntegerToString(Long_Count,2));
   Print("Short_Count "+IntegerToString(Short_Count,2));
   Print("Total_Count "+IntegerToString(Total_Count,2));
   Print("....................................");

   Print("Long_Loss "+DoubleToStr(MathAbs(Long_Loss),2));
   Print("Short_Loss "+DoubleToStr(MathAbs(Short_Loss),2));
   Total_Loss=Long_Loss+Short_Loss;
   Print("Total_Loss "+DoubleToStr(MathAbs(Total_Loss),2));
   Print("....................................");

   Print("Long_Loss_Conversion "+DoubleToStr(MathAbs(Long_Loss_Conversion),Digits));
   Print("Short_Loss_Conversion "+DoubleToStr(MathAbs(Short_Loss_Conversion),Digits));
   Print("Total_Loss_Conversion "+DoubleToStr(MathAbs(Total_Loss_Conversion),Digits));
   Print("....................................");

   Print("Risk_Reset "+DoubleToStr(Risk_Reset,Digits));
   Print("Pips_Available "+DoubleToStr(Pips_Available,Digits));
   Print("------------------------------------------");
/*-------------------------------------------------------------------------------------------------*/
  }

 My initial attempt.

Comments? Criticisms? Laughter? 

 

Does it do what you want? That's the most important thing; the rest is academic.

Just some thoughts from quickly skimming through it:

You can save a line of code by checking OrderOpenTime() before testing the OrderType(). 

for(int a=OrdersHistoryTotal()-1;a>=0;a--)
      if(OrderSelect(a,SELECT_BY_POS,MODE_HISTORY))
        {
         if(OrderOpenTime()<Reset) { continue; }
         if(OrderType()==OP_BUY)
           {
            //if(OrderOpenTime()>Reset)  << get rid
               if(OrderClosePrice()<OrderOpenPrice())
                 {
                  Long_Count++;
                  Order_Loss=OrderProfit();
                  if(Order_Loss<0){Long_Loss+=Order_Loss;}
                 }
           }
         if(OrderType()==OP_SELL)
           {
            //if(OrderOpenTime()>Reset)  << get rid
               if(OrderClosePrice()>OrderOpenPrice())
                 {
                  Short_Count++;
                  Order_Loss=OrderProfit();
                  if(Order_Loss<0){Short_Loss+=Order_Loss;}
                 }
           }
        }

 

An order cannot be more than one type. So use an 'if / else if' statement:

 

   for(int a=OrdersHistoryTotal()-1;a>=0;a--)
      if(OrderSelect(a,SELECT_BY_POS,MODE_HISTORY))
        {
         if(OrderType()==OP_BUY)
           {
            if(OrderOpenTime()>Reset)
               if(OrderClosePrice()<OrderOpenPrice())
                 {
                  Long_Count++;
                  Order_Loss=OrderProfit();
                  if(Order_Loss<0){Long_Loss+=Order_Loss;}
                 }
           }
         else if(OrderType()==OP_SELL)
           {
            if(OrderOpenTime()>Reset)
               if(OrderClosePrice()>OrderOpenPrice())
                 {
                  Short_Count++;
                  Order_Loss=OrderProfit();
                  if(Order_Loss<0){Short_Loss+=Order_Loss;}
                 }
           }
        }

 

It depends on how you want to deal with commission and swap (can the first 'if' criteria be met while the second 'if' statement fails?), but this may be simpler:

 

if(OrderClosePrice()<OrderOpenPrice()) // <<< can something pass here
  {
   Long_Count++;
   Order_Loss=OrderProfit();
   if(Order_Loss<0){Long_Loss+=Order_Loss;} // <<< but fail here? Depends on commission / swap
  }

Order_Loss=OrderProfit();
if(Order_Loss<0)
  {
   Long_Count++;
   Long_Loss+=Order_Loss;
  }
Reason: