Flag Counter help.

 

fellow programmers,

n‌eed help on reset the flag for LotSequence counter as below.

  1. ‌The LotSequence() function will run a the beginning of new bar, H1
  2. Function DailyProfit() will check Current Daily profit.
  3. if Daily profit <0, counter should run 1 time and lot should be StandardLot*Multiply_1.
  4. On the next new bar (H1) & if Daily profit <0, counter should run 2 time and lot should be StandardLot*Multiply_2.
  5. On the next next new bar (H1) & if Daily profit <0, counter should reset to 0, and run 1 time and lot should be StandardLot*Multiply_1 again.

T‌est result: Lot open StandardLot without multiplying even in Negative Daily profit.

‌Any idea where did I miss? 

double LotSequence()
{
   double   uselot = 0;
   int      counter = 0; // Initial flag
   double   StandardLot = 0.10;
   int      Multiply_1 = 2,
            Multiply_2 = 5;
   
   if(DailyProfit()>=0) // Positive Daily Profit
   {
      counter=0;
      uselot = StandardLot;
   }
   
   else if (DailyProfit()<0) // Negative Daily Profit
   {
      counter++;
      if(counter==1) uselot = StandardLot*Multiply_1;
      else if(counter==2) 
      {
         uselot = StandardLot*Multiply_2;
         counter=0; // Reset the counter to 0
      }
   }
   
   return(uselot);
}

double DailyProfit()
{
   double ProfitLoss = 0, Commission = 0, Swap = 0, Deposit = 0, Profit = 0;
   for(int order = 0; order <= OrdersHistoryTotal() - 1; order++)
   {
      bool select = OrderSelect(order,SELECT_BY_POS,MODE_HISTORY);
      if(TimeDay(OrderCloseTime())==Day()) 
      {
         ProfitLoss+=OrderProfit();
         Commission+=OrderCommission();
         Swap+=OrderSwap();
      }
   }
   double ActualProfitLoss = NormalizeDouble((ProfitLoss+Commission+Swap),2);
   return(ActualProfitLoss);
}


Documentation on MQL5: Standard Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Standard Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
Standard Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions - Reference on algorithmic/automated trading language for MetaTrader 5
 
I suggest you to go through your program with the debugger to find in what situation it doesn't what you want it to do..
 
Carl Schreiber:
I suggest you to go through your program with the debugger to find in what situation it doesn't what you want it to do..


Hi Carl,

I‌ believe it is the flagging problem.
The rest of the code is just simple Open order function.  no complex logic.
tried to use while function.

T‌est Result:

  1. if Daily profit >=0, uselot = StandardLot;
  2. else if Daily profit <0, uselot = StandardLot*Multiply_2; <---- it skip StandardLot*Multiply_1. It should run StandardLot*Multiply_1 first.
  3. any idea where did I do wrong?
double LotSequence()
{
   double   uselot = 0;
   int      counter = 0; // Initial flag
   double   StandardLot = 0.10;
   int      Multiply_1 = 2,
            Multiply_2 = 5;
   
   if(DailyProfit()>=0) // Positive Daily Profit
   {
      counter=0;
      uselot = StandardLot;
   }
   
  else
  {
      while (counter<=2)
      {
         if(counter==1) uselot = StandardLot*Multiply_1;
         if(counter==2) uselot = StandardLot*Multiply_2;
         counter++;
      }
  }
   return(uselot);
}