case code

Post Reply
censura10
Posts: 5
Joined: Wed Apr 08, 2015 11:36 am

case code

Post by censura10 »

Hi everyone, hope you are all well can someone quickly check this and see if i have it set correctly.


I am counting losing trades which uses variable LossCountCount each loss increases the LossCountCount by 1

Then with this i am using the case function to return a different value see below. I wanted for example if LossCountCount was 5 that it returns a value 4 and this is then passed to LossCount variable. My concern is that having now returned 4 via case function LossCountCount is now 4 not 5 which is the correct count of the lossing trades.

If this the case can i for example return the case value directly to the LossCount variable and not affect the LossCountCount value. Hope it makes sense and you can help. Kind regards

Code: Select all

switch (LossCountCount)          
     {                                  
      case 1 : return (1);          
      case 2 : return (1);     
      case 3 : return (2);    
      case 4 : return (3);           
      case 5 : return (4);           
      case 6 : return (6);          
      case 7 : return (9);         
      case 8 : return (14); 
         
      default: return (1);
     }                                 
      
      LossCount == LossCountCount;
      
     GlobalVariableSet("Loss"+Symbol(),LossCount);
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

case code

Post by renexxxx »

Just create a function that maps the integer value of LossCountCount to a new integer value, eg:

Code: Select all

int fnLossCountCount(int param) {

   int result;
   switch(param) {

      case 1 : result = 1; break;
      case 2 : result = 1; break;
      case 3 : result = 2; break;
      case 4 : result = 3; break;
      case 5 : result = 4; break;
      case 6 : result = 6; break;
      case 7 : result = 9; break;
      case 8 : result = 14; break;
         
      default: result = 1;
   }                                
   return(result);
}
and than you can you can assign LossCount to that mapped value like so:

Code: Select all

  int LossCount = fnLossCountCount( LossCountCount );
  
  GlobalVariableSet("Loss"+Symbol(),LossCount);
The function does not alter the value of LossCountCount. (By the way, you may want to rethink the names of your variables, but I leave that up to you).

Note that I have opted to do away with the multiple return() statements inside the switch, as it is bad coding practice to have multiple returns from a function (i.e. a function should be exited at only one place). But technically, you can use the multiple returns.

Cheers ...
censura10
Posts: 5
Joined: Wed Apr 08, 2015 11:36 am

case code

Post by censura10 »

Hi and thank you for your help, i applied the code and got this error

'fnLossCountCount' - function can be declared only in the global scope

thank you
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

case code

Post by renexxxx »

censura10 » Thu May 28, 2015 8:35 pm wrote:Hi and thank you for your help, i applied the code and got this error

'fnLossCountCount' - function can be declared only in the global scope

thank you
Yes, that's right. Don't declare the fnLossCountCount function inside another function, but in the global scope.
censura10
Posts: 5
Joined: Wed Apr 08, 2015 11:36 am

case code

Post by censura10 »

Hi sorry for being a bit of a nob but only started back trying to code an ea and even when i did it a couple of years ago it was a bit of trial and error

I tried to move the whole block out and call it but it would not allow switch statement in global, i then tried to just move the function but said has to have body?

so i know you must wish you had not replied but if you can great if not I thank you for your time

Kind regards

Code: Select all

void CalculateDailyResult()
{
   //Calculate the no of winners and losers from today's trading. These are held in the history tab.

   LossTrades = 0;
   LossCount=1;
   int LossCountCount=1;
   WinTrades = 0;
   OverallProfit = 0;
   
   
   
   for (int cc = 0; cc <= OrdersHistoryTotal(); cc++)
   {
      if (!OrderSelect(cc, SELECT_BY_POS, MODE_HISTORY) ) continue;
      if (OrderSymbol() != Symbol() ) continue;
      if (OrderMagicNumber() != MagicNumber) continue;
      
      OverallProfit+= (OrderProfit() + OrderSwap() + OrderCommission() );
      if (OrderProfit() > 0) WinTrades++;
      if (OrderProfit() < 0) LossTrades++; 
      
                           // End of the 'switch' body   
      
      
      
      
      
      if (OrderProfit() < 0) LossCountCount=LossCountCount+1;
     if (OrderProfit() > 0) LossCountCount=1;
     GlobalVariableSet("longtrade"+Symbol(),0);
     GlobalVariableSet("shorttrade"+Symbol(),0);
     // if (OrderProfit() < 0) {step=step+1;}
     
     
                
      
     
    
     int fnLossCountCount(int param)
    
    {
 
   int result;
   switch(param) {
 
      case 1 : result = 1; break;
      case 2 : result = 1; break;
      case 3 : result = 2; break;
      case 4 : result = 3; break;
      case 5 : result = 4; break;
      case 6 : result = 6; break;
      case 7 : result = 9; break;
      case 8 : result = 14; break;
         
      default: result = 1;
   }                                
   return(result);
}
 
  LossCount = fnLossCountCount( LossCountCount );
 
  GlobalVariableSet("Loss"+Symbol(),LossCount);
  
      
     // GlobalVariableSet("step"+Symbol(),step);
      
    }//for (int cc = 0; cc <= tot -1; cc++)
   
   

}//End void CalculateDailyResult()
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

case code

Post by renexxxx »

You have declared the fnLossCountCount function inside the CalculateDailyResult function. In MQL4 functions must be declared at the global scope (you can define class methods inside a class, but that is another story).

In order for your code to work, you must have the global variables int LossTrades; int LossCount; int WinTrades; double OverallProfit; and int MagicNumber; somewhere else declared.

The following code is compilable:

Code: Select all

int fnLossCountCount(int param) {

   int result;
   switch(param) {

      case 1 : result = 1; break;
      case 2 : result = 1; break;
      case 3 : result = 2; break;
      case 4 : result = 3; break;
      case 5 : result = 4; break;
      case 6 : result = 6; break;
      case 7 : result = 9; break;
      case 8 : result = 14; break;
         
      default: result = 1;
   }                                
   return(result);
}


int LossTrades;
int LossCount;
int WinTrades;
double OverallProfit;
int MagicNumber;

void CalculateDailyResult() {

   //Calculate the no of winners and losers from today's trading. These are held in the history tab.
   LossTrades = 0;
   LossCount  = 1;
   int LossCountCount = 1;
   WinTrades  = 0;
   OverallProfit = 0;

   for (int cc = 0; cc <= OrdersHistoryTotal(); cc++) {

      if (!OrderSelect(cc, SELECT_BY_POS, MODE_HISTORY) ) continue;
      if (OrderSymbol() != Symbol() ) continue;
      if (OrderMagicNumber() != MagicNumber) continue;

      OverallProfit+= (OrderProfit() + OrderSwap() + OrderCommission() );
      if (OrderProfit() > 0) WinTrades++;
      if (OrderProfit() < 0) LossTrades++; 

      if (OrderProfit() < 0) LossCountCount=LossCountCount+1;
      if (OrderProfit() > 0) LossCountCount=1;
   
      GlobalVariableSet("longtrade"+Symbol(),0);
      GlobalVariableSet("shorttrade"+Symbol(),0);
      
      // if (OrderProfit() < 0) {step=step+1;}

      LossCount = fnLossCountCount( LossCountCount );

      GlobalVariableSet("Loss"+Symbol(),LossCount);

      // GlobalVariableSet("step"+Symbol(),step);

   }//for (int cc = 0; cc <= tot -1; cc++)
}
Cheers ...
censura10
Posts: 5
Joined: Wed Apr 08, 2015 11:36 am

case code

Post by censura10 »

Hi thank you I am really grateful for your help, corrected my code with yours and testing.

Thank you again
Post Reply

Return to “Coders Hangout”