Global Variables Not Increamenting Properly

 

Tracking wins, and losses 

//Global Variables
         int losses = 0;
         int wins   = 0;

// Scan History 
double ScanClosedTrades()
  {
  HistorySelect(0,TimeCurrent());
  int    deals      = HistoryDealsTotal();
  ulong  ticket     = HistoryDealGetTicket(deals-1);
  double profit     = 0;
      if(ticket>0) 
           {
            long order_magic=HistoryDealGetInteger(ticket,DEAL_MAGIC);
            profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
            
                  if (profit < 0)
                    {
                        losses++;
                        Print("Losses Value :",losses);//losses)
                        Print("Loss Added, Profit:",profit);//profit);
                        
                    } 
                  else 
                    {
                        wins++;
                        Print("Wins Value :",wins);//wins)
                        Print("Win Added, Profit:",profit);//profit);
                        
                    }
                    
           }
      else
           { 
          PrintFormat("In total, in the history %d of deals, we couldn't select a deal"+
          " with the index %d. Error %d",deals,deals-1,GetLastError());
           }
               
   return(profit);                  
  }

After every order only one is shown.

2014.05.31 04:41:49    Core 1    2014.05.28 00:00:00   Wins Value :1.0

2014.05.31 04:41:50    Core 1    2014.05.29 00:00:00   Wins Value :1.0

The new value should be 2.

 
wehsnim:

Tracking wins, and losses 

After every order only one is shown.

2014.05.31 04:41:49    Core 1    2014.05.28 00:00:00   Wins Value :1.0

2014.05.31 04:41:50    Core 1    2014.05.29 00:00:00   Wins Value :1.0

The new value should be 2.

Why ?
 
angevoyageur:
Why ?
I am under the assumption that wins++ will increment the global variable to 1. When the function is called again the global variable being 1 will increment to 2. The logic being used is flawed otherwise it would increment.
 
angevoyageur:
Why ?

Is to possible to update a global variable from within the current function?

I've tried multiple variation what am I missing or is there a post, article anything that can lead me into the right direction?

My understanding at this point is that local variables will take preference

 and always return 1 and reset to 0 everything the function is called

i would like to increment the global variable so that it displays 2

 so that i can use that information in another function.

//--- Scan Closed Trades
double ScanClosedTrades()
  {
  HistorySelect(0,TimeCurrent());
  int returns=0;
  int  a = 0;
  int  b = 0;

  int    deals      = HistoryDealsTotal();
  ulong  ticket     = HistoryDealGetTicket(deals-1);
  double profit     = 0;
      if(ticket>0) 
           {
                long order_magic=HistoryDealGetInteger(ticket,DEAL_MAGIC);
  
                     profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
                   a++;
                  if (profit < 0)
                      
                        Print("Losses Value :",a);//losses)
                        Print("Loss Added, Profit:",profit);//profit);
                    b++;     
                  if (profit > 0)
                       
                        Print("Wins Value :",b);//wins)
                        Print("Win Added, Profit:",profit);//profit);
                       
                        losses++;
                        if (a == 1)
                        wins++;
                        if (b == 1)                   
                   
             Print("Number of trades :",returns);//returns)       
           }
 
      else
           { 
          PrintFormat("In total, in the history %d of deals, we couldn't select a deal"+
          " with the index %d. Error %d",deals,deals-1,GetLastError());
           }

Global variable will increment to 1 but the variable will not store incrementing to 2.

 
wehsnim:

Is to possible to update a global variable from within the current function?

I've tried multiple variation what am I missing or is there a post, article anything that can lead me into the right direction?

My understanding at this point is that local variables will take preference

 and always return 1 and reset to 0 everything the function is called

i would like to increment the global variable so that it displays 2

 so that i can use that information in another function.

Global variable will increment to 1 but the variable will not store incrementing to 2.

The last code posted doesn't make sense, where are the "{ }" ?

What is the value of wins variable at the start of ScanClosedTrades() function ?

We don't know what you have in your history, so it's impossible to us to say how this code will be executed.

 
angevoyageur:

The last code posted doesn't make sense, where are the "{ }" ?

What is the value of wins variable at the start of ScanClosedTrades() function ?

We don't know what you have in your history, so it's impossible to us to say how this code will be executed.

I started reading lots of c++ tutorials in using the ++ arithmetic operation.

The function created was trying to manipulate the global int's Losses = 0 and Wins = 0  by incrementation.

Big No No. From the tutorials I read.

I created a class instead as recommended and all seem to be functioning as desired.

Here is the code and library. In case someone else needs help.

//--- Scan Closed Trades
double ScanClosedTrades()
  {
  HistorySelect(0,TimeCurrent());
  int  a = 0;
  int  b = 0;
  int  c;
  int  d;
  int    deals      = HistoryDealsTotal();
  ulong  ticket     = HistoryDealGetTicket(deals-1);
  double profit     = 0;
      if(ticket>0) 
           {
                profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
                   
                  if (profit < 0)
                    {
                       a++;
                      {
                        Print("Losses Value :",a);//losses)
                        Print("Loss Added, Profit:",profit);//profit);
                      }
                     }   
                  else
                     {
                       b++;
                       {
                        Print("Wins Value :",b);//wins)
                        Print("Win Added, Profit:",profit);//profit);
                       }
                      } 
                  if (a == 1)
                     {
                     Tally.AddLoss();   
                     c = Tally.GetLoss();
                     Print("Current Loss Tally :",c);//Loss Tally)
                     }
                  if (b == 1) 
                     {
                     Tally.AddWin();
                      d = Tally.GetWin();
                     Print("Current Win Tally :",d);//Win Tally)
                     }                  
                   
                  
           }
 
      else
           { 
          PrintFormat("In total, in the history %d of deals, we couldn't select a deal"+
          " with the index %d. Error %d",deals,ticket,GetLastError());
           }
        
   return(profit);                  
  }

What a great learning experience.

Thank you again angevoyageur  adding the braces was needed you are 100 percent correct.








Files:
TallyUp.mqh  2 kb