Martigale function causing - error 134 (not enough money)

 
Martingale function causing - error 134(not enough money)
However, there is enough money.

I tried increasing the expert properties deposit amount which had no effect
I thought perhaps the loop was taking too long to find a LotSize

So I removed the breaks to allow the loop to count back to 0 from OrdersHistoryTotal()-1.
This corrected the errors to confirm that taking too long was not the issue, but I want to break to get the correct return value.

Anyhow here are some of the errors I'll just post the early stages to try an shorten the SRC

I see that it seems to increase the lot size almost instantly and does not print out the ongoing increase with ticket number as it should too.
I thought I should additional printouts of Ticket # 17, Ticket #16 too.

If I changed the lot size in expert properties to 1.25 the error goes away but also does not work correctly. I will keep reviewing it but I am having trouble hunting this down.


2014.11.11 10:13:34.483	2012.06.07 02:15  Tester: not enough money for sell 5.12 EURUSD at 1.25561 sl: 1.25661 tp: 1.25361 [2012.06.07 02:15]
2014.11.11 10:13:34.483	2012.06.07 02:15  functiontest EURUSD,M15: Order LotSize was 2.56 Increasing to 5.12 Ticket#18
2014.11.11 10:13:34.483	2012.06.07 01:45  functiontest EURUSD,M15: Alert: OrderSend Failed: 0
2014.11.11 10:13:33.483	2012.02.28 20:30  Tester: not enough money for sell 5.12 EURUSD at 1.34686 sl: 1.34786 tp: 1.34486 [2012.02.28 20:30]
2014.11.11 10:13:33.483	2012.02.28 20:30  functiontest EURUSD,M15: Order LotSize was 2.56 Increasing to 5.12 Ticket#18
2014.11.11 10:13:33.483	2012.02.28 19:00  functiontest EURUSD,M15: Alert: OrderSend Failed: 0
2014.11.11 10:13:33.483	2012.02.28 19:00  functiontest EURUSD,M15: Alert: OrderSend Failed: 134
2014.11.11 10:13:33.483	2012.02.28 19:00  functiontest EURUSD,M15: OrderSend error 134
2014.11.11 10:13:33.483	2012.02.28 19:00  Tester: PrevBalance: 9517.88, PrevPL: 0.00, PrevEquity 9517.88, PrevMargin: 0.00, NewMargin: 13803, FreeMargin: -4284.82
2014.11.11 10:13:33.483	2012.02.28 19:00  Tester: not enough money for buy 5.12 EURUSD at 1.34798 sl: 1.34698 tp: 1.34998 [2012.02.28 19:00]
2014.11.11 10:13:33.483	2012.02.28 19:00  functiontest EURUSD,M15: Order LotSize was 2.56 Increasing to 5.12 Ticket#18
2014.11.11 10:13:24.783	TestGenerator: current spread 13 used


Here is the code including the martigale() function

//+------------------------------------------------------------------+
//|                                                 functiontest.mq4 |
//|                                                 function testing |
//|                      Function Testing                            |
//+------------------------------------------------------------------+
#property copyright "Open Function Testing"


//---- input parameters
extern double LotSize=0.01;
extern int    TakeProfit=20;
extern double lotmultiplier=2.0;
extern int    StopLoss=10;
extern int    AB_distance=10; //not used
extern int    MagicNumber=123486;
extern double fibo_retrace=0.500;  //not used
extern double fibo_extension=0.618;  //not used
  
//for reference: http://www.forexfibonacci.com/calculate_fibonacci_levels/04/



//++++ These are adjusted for 5 digit brokers.

int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   if (Digits == 5 || Digits == 3)
   {    // Adjust for five (5) digit brokers.
      pips2dbl    = Point*10; pips2points = 10;
   } 
   else 
    {    
      pips2dbl    = Point;    pips2points =  1;
    }
    // OrderSend(... Slippage * pips2points, Bid - StopLossPips * pips2dbl
     
   
    
//---- 

//----
   return(0);
  }
  
  
  
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
   
    
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {

   static datetime PreviousTime=0;
   
   if(PreviousTime==Time[0])
      return(0);
   else PreviousTime=Time[0]; // PreviousTime==Time[0] returns until PremiousTime!=Time[0] else makes it ==[0] again and should run the start() once for each change
   
   macdsignaltrade();                  

//+------------------------------------------------------------------+
//| end start function                                               |
//+------------------------------------------------------------------+                
   return(0);
   }    


//+------------------------------------------------------------------+
//|     My Functions (testing)                                       |
//+------------------------------------------------------------------+

void macdsignaltrade()
{
   double faster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);//MODE_MAIN
   double slower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL
   double prevfaster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,2);//MODE_MAIN previous faster
   double prevslower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2); //MODE_SIGNAL previous slower
   //make one for shift 3 and 4 to evaluate the fractals
   
   if(prevfaster < prevslower && faster > slower)OrderEntry(0);
      //Print(" MACD UP");
   if(prevfaster > prevslower && faster < slower)OrderEntry(1);
      //Print(" MACD DOWN");
 
}


void engulfingsignal() //Bullish Engulfing Candle
{
   if(Open[2] > Close[2]        //bear candle identified
      && Open[1] < Close[1]  //bull candle identified
      && Open[1]<= Close[2]  //bull engulfing condition
      && Open[2] < Close[1]) Print(Close[1]," Bullish Engulfing Candle "," and engulfingsize = ",DoubleToStr(Close[1]-Open[1],Digits));  
      //DoubleToStr( Close[1]-Open[1],Digits) forums help
     
   if(Open[2]<Close[2]//bull candle identified
      && Open[1]>Close[1]//bear candle identified
      && Open[1]>= Close[2]  //bear engulfing condition
      && Open[2] > Close[1]) Print(Close[1]," Bearish Engulfing Candle "," and engulfingsize = ",DoubleToStr(Close[1]-Open[1],Digits));  
      //DoubleToStr( Close[1]-Open[1],Digits) forums help);
}




double martingale() /////---->>>causing Alert OrderSend error 134(not enough money) and not enough money messages 
{
   double mlots = LotSize;

for(int i = OrdersHistoryTotal()-1; i>=0; i--) //count backwards closed orders, history is orders from account tab loaded into the terminal
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
       {
        Print("Access to history failed with error (",GetLastError(),")");
        mlots=LotSize;
        break;
       }
             
      if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY)==true) //MODE_HISTORY selects closed and cancelled 
         { 
         if(OrderMagicNumber() == MagicNumber)
            if(OrderSymbol()== Symbol())
               {             
                  if(OrderProfit()<0)  //if trade was a loser each trade will increase LotSize * lotmultiplier 
                     {
                     mlots=OrderLots()* lotmultiplier ;                                   
                     Print("Order LotSize was ",OrderLots()," Increasing to ", mlots," Ticket#", OrderTicket());
                     break;
                     }
                  if(OrderProfit()>0) //if last trade was a winner, adjust lotsize back to default LotSize
                     {
                     mlots=LotSize;                                       
                     Print("No change in LotSize");
                     break;  
                     }
                }  
         }
                 
      }

return(mlots);
} //end martingale()




void macdclosetrade()
{
   
   double faster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);//MODE_MAIN
   double slower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL
   double prevfaster=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,2);//MODE_MAIN previous faster
   double prevslower=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2); //MODE_SIGNAL previous slower
   
   bool result;
   for(int i = OrdersTotal()-1; i >= 0 ; i--)
      { 
      if(OrderSelect(i, SELECT_BY_POS)) 
         if(OrderMagicNumber() == MagicNumber)        
            if(OrderSymbol()== Symbol())
               {
                  if(OrderType() == OP_BUY)          
                     if(prevfaster > prevslower && faster < slower) //if direction goes down on a buy order then close the order
                        {
                        result = OrderClose(OrderTicket(), OrderLots(), Bid, 3*pips2points, Blue);
                           if(result == false)
                              Print("Order",OrderTicket(),"failed to close Error",GetLastError());   
                              Print(" MACD DOWN CLOSE");
                        }
                     
                     
                     if(OrderType() == OP_SELL)           
                        if(prevfaster < prevslower && faster > slower) //if direction goes up on a sell order then close the order     
                           {
                           result = OrderClose(OrderTicket(), OrderLots(), Ask, 3*pips2points, Blue);
                              if(result == false)
                                 Print("Order",OrderTicket(),"failed to close Error",GetLastError());   
                                 Print(" MACD UP CLOSE");
                           }   
               }          
      }


} //end macdclosetrade() function



int OrderEntry(int direction)
{     
   int ticket;
   if(OrdersTotal() > 0) macdclosetrade();
   
   if(direction==0)
      if(OrdersTotal()==0)
         ticket = OrderSend(Symbol(),OP_BUY,martingale(),Ask,3*pips2points,Ask-(StopLoss*pips2dbl),Ask+(TakeProfit*pips2dbl),NULL,MagicNumber,0,Green);
         if(ticket<0) Alert("OrderSend Failed: ", GetLastError());
         
   if(direction==1)
      if(OrdersTotal()==0)
         ticket = OrderSend(Symbol(),OP_SELL,martingale(),Bid,3*pips2points,Bid+(StopLoss*pips2dbl),Bid-(TakeProfit*pips2dbl),NULL,MagicNumber,0,Red);
         if(ticket<0) Alert("OrderSend Failed: ", GetLastError());

         

return(0);
}      

//+------------------------------------------------------------------+
 
Actually not enough money happens because your leverage is not that big. When using martigale, the number can only be multiplied a few times and then it will hit the leverage limits.
 
deysmacro: When using martigale, the number can only be multiplied a few times and then it will hit the leverage limits.
This is why a Martingale is guaranteed to destroy your account.
 
HMMMMMMMM interesting.

For some reason I thought there was still plenty of leverage there. I started with 10k deposit and the trading ended with what seems to be lot size of 5.12 and still showing 9500 in the account balance
However I do see the consecutive losses were 22 so that makes sense that it would overleverage

Thanks.
Reason: