Does MQL4 support recursion?

 

When I use a recursion, the system returns "stack overflow". Is there a method to sovle it?

I am using the Heiken Ashi indicator, there is a recursoin in it so I want to imbed the indicator code to my EA.

Of course I can use iCustom() to call this indicator, but it is very slow when back testing - more than 20 minutes to do a test.

 

Increase stack volume by compiler directive:

#property stacksize 150000000
 
Rosh:

Increase stack volume by compiler directive:


I get the same error when complying. When I change the stacksize like you suggested MT4 crashes...

 
Nauticus:

I get the same error when complying. When I change the stacksize like you suggested MT4 crashes...

That means you have to revise your code. Try to rewrite this one more optimal.

 
Rosh:

That means you have to revise your code. Try to rewrite this one more optimal.

Well I tried optimizing it, but I still the the same error. Actually the code doesn't seem that complicated. I'll post it here for you to check where am I making a mistake.


double Force_0, Force_1, Force_2, Force_3, Stoch_m, 
Stoch_s, Bar_High, Bar_Low, Balance;
bool New_Bar;
extern double Lots=0.01;  
static datetime New_Time=0;
// ----------------------------- START EA -------------------------------
int start()
{
 if (New_Time!=Time[0])                                            // Used to calculate a new bar.
 {
  New_Time=Time[0];                         
  New_Bar=true;
  return(New_Bar);
 }
  Bar_High=iHigh(NULL,0,1);
  Bar_Low=iLow(NULL,0,1);
  Force_0=iForce(NULL,0,21,MODE_EMA, PRICE_MEDIAN,0);               // Force of the 0 bar
  Force_1=iForce(NULL,0,21,MODE_EMA, PRICE_MEDIAN,1);               // Force of the 1 bar
  Force_2=iForce(NULL,0,21,MODE_EMA, PRICE_MEDIAN,4);               // Force of the 4 bar
  Force_3=iForce(NULL,0,21,MODE_EMA, PRICE_MEDIAN,7);               // Force of the 7 bar
  Stoch_m=iStochastic(NULL,0,6,4,5,MODE_EMA,0,MODE_MAIN,1);         // SO main line
  Stoch_s=iStochastic(NULL,0,6,4,5,MODE_EMA,0,MODE_SIGNAL,1);       // SO signal line

// ---------------------- Long Positions -----------------------------------------------
if (New_Bar==true && Stoch_m > Stoch_s && (Stoch_m + Stoch_s) < 130)
{
 if (Buy_1()==true && OrdersTotal()==0)                             // Buy_1() and Buy_2() are... 
 {                                                                  // UD functions. See below.
  if (Ask >= Bar_High+10*Point)
  {
   RefreshRates();                                                
   OrderSend("EURUSD",0,Lots,Ask,3,0,Ask+100*Point);
  }
 } 
 if (Buy_2()==true && OrdersTotal()==0)
 {
  RefreshRates();                                                
  OrderSend("EURUSD",0,Lots,Ask,3,0,Ask+100*Point);
 }
}

// --------------------------------- Short Positions --------------------------------------  
if (New_Bar==true && Stoch_m < Stoch_s && (Stoch_m + Stoch_s) > 70)  
{
 if (Sell_1()==true && OrdersTotal()==0)                             // Sell_1() and Sell_2() are...
 {                                                                   // UD functions. See below.
  if (Bid <= Bar_Low-10*Point)
  {
   RefreshRates();                                                
   OrderSend("EURUSD",1,Lots,Bid,3,0,Bid-100*Point);
  }
 }
 if (Sell_2()==true && OrdersTotal()==0)
 {
  RefreshRates();                                                
  OrderSend("EURUSD",1,Lots,Bid,3,0,Bid-100*Point);
 }
} 
return;
}

// ----------------------- BUY CRITERIA 1 ------------------------------------------
bool Buy_1()
{ 
 if (Force_1 >= 0.07 && Force_1 < 0.20 && (Force_2 < 0.07 || Force_2 < 0) && (Force_3 < 0.07 || Force_3 < 0)) 
 {
  return(Buy_1()==true);
 }
} 

// ------------------------- BUY CRITERIA 2 --------------------------------------
bool Buy_2()
{ 
 if (Force_0 >= 0.20 && (Force_1 < 0.07 || Force_1 < 0))
 {
  return(Buy_2()==true);
 }
}


// ----------------------- SELL CRITERIA 1 ---------------------------------------------
bool Sell_1()
{
 if (Force_1 <= -0.07 && Force_1 > -0.20 && (Force_2 > -0.07 || Force_2 > 0) && (Force_3 < 0.07 || Force_3 < 0))
 {
  return(Sell_1()==true);
 }
} 


// ------------------------------ SELL CRITERIA 2 ----------------------------------------
bool Sell_2()
{
 if (Force_0 <= -0.20 && (Force_1 > -0.07 || Force_1 > 0))
 {
  return(Sell_2()==true);
 }
} 

 
Nauticus:

Well I tried optimizing it, but I still the the same error. Actually the code doesn't seem that complicated. I'll post it here for you to check where am I making a mistake.



See my article HTML Walkthrough Using MQL4. There is sample of optimization.

 
Rosh:

See my article HTML Walkthrough Using MQL4. There is sample of optimization.

Thanks. I'll try it with this. I posted the code above, so is rewriting it all with arrays really necessary?

 
May be you have not enough memory?
 

let's interpret this code fragment. I might be wrong... but just after Buy_1 is invoked and whether "if condition" is true, it calls itself infinite. Perhaps you should change logic to second approach

bool Buy_1()
{
if (Force_1 >= 0.07 && Force_1 < 0.20 && (Force_2 < 0.07 || Force_2 < 0) && (Force_3 < 0.07 || Force_3 < 0))
{
return(Buy_1()==true);
}
}

// second approach

bool Buy_1()
{

// call function to update force_1, force_2 and force_3 HERE
if (Force_1 >= 0.07 && Force_1 < 0.20 && (Force_2 < 0.07 || Force_2 < 0) && (Force_3 < 0.07 || Force_3 < 0))
{
return(Buy_1()==true);
}

return(false);
}

I don't understand why you need recursivity here. You know that the "start" function is automatically invoked for every tick received by the terminal. Using this knowledge, buy_1 only needs to be that way:

// non-recursive approach

bool Buy_1()
{

if (Force_1 >= 0.07 && Force_1 < 0.20 && (Force_2 < 0.07 || Force_2 < 0) && (Force_3 < 0.07 || Force_3 < 0))
{
return(true);
}

else

return(false);
}

This also applies to sell_1(), ...

 

Perfect! This works. It's interesting how I just changed the return(Buy_1()=true) with just return(true) and it got through.


Thanks, Abstract Mind. Now it opens positions with no problem. I still have a lot of work on it though.

Reason: