Can anybody please tell me what is wrong with this short Martingale code?

 

I have a piece of Martingale code in my EA. When I run a back test on my EA with the MartingaleFunc == false there is no problem with the back test. It runs. But as soon as I set the MartingaleFunc == true the back test refuses to start.

This tells me there must be something wrong with the code for this function, but i have gone through it a hundred times and cannot see what can be wrong.

Can somebody PLEASE help me?

Here is the piece of code:

                // Martingale Function
                int WinCount;
                int LossCount;
                double BaseLotSize = dLotSize;
                
                if (MartingaleFunc == true)
                    
                       for(int Count = OrdersHistoryTotal()-1; ; Count--)
               {
                  OrderSelect(Count,SELECT_BY_POS,MODE_HISTORY);
                     if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
                        {
                           if(OrderProfit() > 0 && LossCount == 0) WinCount++;
                           else if(OrderProfit() < 0 && WinCount == 0) LossCount++;
                           else break;
                        }
              }
           
        
         if(MartingaleType == 0) int ConsecutiveCount = LossCount;
         else if(MartingaleType == 1) ConsecutiveCount = WinCount;
         if(ConsecutiveCount > MaxMartingale) ConsecutiveCount = MaxMartingale;
         dLotSize = BaseLotSize * MathPow(LotMultiplier,ConsecutiveCount);
         
 
ernest02:
MartingaleFunc == false there is no problem with the back test. It runs. But as soon as I set the MartingaleFunc == true the back test refuses to start.
Problem is not a function of code, problem is else where. Look in the journal tab.
 
WHRoeder:
Problem is not a function of code, problem is else where. Look in the journal tab.

I have run the back test and find that after more than one loss the LossCount is still == 0. (It should be 2 in this particular case)

When I check the OrdersHistoryTotal() it actually shows there are two transactions in the history. Both these transactions were losses.

Why if there are two historical loss transactions will the LossCount counter not pick it up?

 
I'm new to MQ4 programming, but I'm wondering if you need to make those variables an extern global at the top of your EA instead of declaring the int inside your function. I'm wondering if the variable is recreated with a value of 0 each time you enter the function there, like would be the case in C, since when you leave the function the variables would all disappear if they aren't globals.
 
roy7:
I'm new to MQ4 programming, but I'm wondering if you need to make those variables an extern global at the top of your EA instead of declaring the int inside your function. I'm wondering if the variable is recreated with a value of 0 each time you enter the function there, like would be the case in C, since when you leave the function the variables would all disappear if they aren't globals.

Roy,

Thanks for trying to help!

I will try your advice.

 
ernest02:

Roy,

Thanks for trying to help!

I will try your advice.


Does not make a difference Roy!

I also test for the value of LossCount IMMEDIATELY after the "break" operator - before it has an opportunity to reset.

This is a real mystery to me!

 

Or try:


static int WinCount;

static int LossCount;


Just change your existing lines to have "static". If I remember right, this means the values will persist over time but only be available to this local part of the code.

 
roy7:

Or try:


static int WinCount;

static int LossCount;


Just change your existing lines to have "static". If I remember right, this means the values will persist over time but only be available to this local part of the code.


Roy,

This also makes no difference!

So the two problems I have persist;

  1. If I set the MartingaleFunct == true in the back test environment the EA does not want to run, except if I set the external variable == true and compile the EA.
  2. Even with several transactions closed the LossCount and WinCount variables do not increment at all.
Can anybody help please?
 
Ah, sorry I wasn't more help.
 
ernest02:
I have run the back test and find that after more than one loss the LossCount is still == 0. (It should be 2 in this particular case)
You said " back test refuses to start." MAKE UP YOUR MIND.
 if (MartingaleFunc == true)
                    
                       for(int Count = OrdersHistoryTotal()-1; ; Count--)
               {
                  OrderSelect(Count,SELECT_BY_POS,MODE_HISTORY);
                     if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
                        {
What happens when count goes negative - infinite loop
for(int pos = OrdersHistoryTotal()-1; pos >= 0; pos--) if(
    OrderSelect(pos,SELECT_BY_POS,MODE_HISTORY)
&&  OrderSymbol() == Symbol() 
&& OrderMagicNumber() == MagicNumber){
         if(OrderProfit() > 0){ if (Losspos != 0) break; Winpos++; }
    else if(OrderProfit() < 0){ if (Winpos  != 0) break; Losspos++;}
}
 
            // Martingale Function
                int WinCount = 0;   // declare AND initialize your variables
                int LossCount = 0;
                double BaseLotSize = dLotSize;
                
                if (MartingaleFunc == true)
                    
                       for(int Count = OrdersHistoryTotal()-1; Count >= 0 ; Count--) // infinite loop you had here.
               {
                  OrderSelect(Count,SELECT_BY_POS,MODE_HISTORY);
                     if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
                        {
                           if(OrderProfit() > 0 && LossCount == 0) WinCount++;
                           else if(OrderProfit() < 0 && WinCount == 0) LossCount++;
                           else break;
                        }
              }
           
        
         if(MartingaleType == 0) int ConsecutiveCount = LossCount;
         else if(MartingaleType == 1) ConsecutiveCount = WinCount;
         if(ConsecutiveCount > MaxMartingale) ConsecutiveCount = MaxMartingale;
         dLotSize = BaseLotSize * MathPow(LotMultiplier,ConsecutiveCount);
Try it and give feedback. Post more code next time.