Download MetaTrader 5
To add comments, please log in or register
Do you know C++? MQL5 has much in common with it. Just try it yourself!
Marcelo Coutinho
1104
Marcelo Coutinho 2014.05.07 18:20 

Good Afternoon. Any clue of what is wrong?

This introductory EA, buys and sells based on the movement of two moving averages as described below. This is a learning exploratory exercise.

When I run the ST requesting to make combinations of the MM periods, the most profitable usually have the first MA bigger than the second one, i.e, when the fast MA cross the slow MA, the system sells instead of buying and vice e versa.

I have made several tests, and it is always like this and it makes no sense to me. Any clue from where we can start?

   if((iMA_buf1[1]-iMA_buf2[1])<=0 && (iMA_buf1[0]-iMA_buf2[0])>0)
     {
      Print("[BUY] - ticks: ",ticks," ContratosAtual: ",ContratosAtual," contratos: ",contratos," order: ",order," orderAtual: ",orderAtual," iMA_buf1[1]-iMA_buf2[1]: ",DoubleToString(iMA_buf1[1]-iMA_buf2[1],6)," iMA_buf1[0]-iMA_buf2[0]: ",DoubleToString(iMA_buf1[0]-iMA_buf2[0],6));
      order=1;
     }
   if((iMA_buf1[1]-iMA_buf2[1])>=0 && (iMA_buf1[0]-iMA_buf2[0])<0)
     {
      Print("[SELL] - ticks: ",ticks," ContratosAtual: ",ContratosAtual," contratos: ",contratos," order: ",order," orderAtual: ",orderAtual," iMA_buf1[1]-iMA_buf2[1]: ",DoubleToString(iMA_buf1[1]-iMA_buf2[1],6)," iMA_buf1[0]-iMA_buf2[0]: ",DoubleToString(iMA_buf1[0]-iMA_buf2[0],6));
      order=-1;
     }

Picture attached shows MA1 with 6 periods and MA2 4 periods so here, system is buying instead of selling. Columns ordered by Profit.

Regards 

Files:
Alain Verleyen
Moderator
28561
Alain Verleyen 2014.05.07 18:50  
YouTrade:

Good Afternoon. Any clue of what is wrong?

This introductory EA, buys and sells based on the movement of two moving averages as described below. This is a learning exploratory exercise.

When I run the ST requesting to make combinations of the MM periods, the most profitable usually have the first MA bigger than the second one, i.e, when the fast MA cross the slow MA, the system sells instead of buying and vice e versa.

I have made several tests, and it is always like this and it makes no sense to me. Any clue from where we can start?

Picture attached shows MA1 with 6 periods and MA2 4 periods so here, system is buying instead of selling. Columns ordered by Profit.

Regards 

How do you get the value of iMA_buf1 and iMA_buf2 ?
Marcelo Coutinho
1104
Marcelo Coutinho 2014.05.07 19:26  
angevoyageur:
How do you get the value of iMA_buf1 and iMA_buf2 ?

Here is ...

I have placed a Picture with another example. 

int         iMA_handle1,iMA_handle2,iStdDev_handle;
double      iMA_buf1[],iMA_buf2[],iStdDev_buf[];

(...)

int OnInit()
  {
   iMA_handle1=iMA(my_symbol,my_timeframe,period1,0,tipo_media1,PRICE_CLOSE);

   if(iMA_handle1==INVALID_HANDLE)
     {
      Print("Failed to get the indicator handle1");
      return(-1);
     }

   iMA_handle2=iMA(my_symbol,my_timeframe,period2,0,tipo_media2,PRICE_CLOSE);

   if(iMA_handle2==INVALID_HANDLE)
     {
      Print("Failed to get the indicator handle2");
      return(-1);
     }

   ArraySetAsSeries(iMA_buf1,true);
   ArraySetAsSeries(iMA_buf2,true);

   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
(...)

void OnDeinit(const int reason)
  {
   IndicatorRelease(iMA_handle1);
   IndicatorRelease(iMA_handle2);
   ArrayFree(iMA_buf1);
   ArrayFree(iMA_buf2);
  }
(...)

void OnTick()
  {

   int err1=0;
   int err2=0;


   err1=CopyBuffer(iMA_handle1,0,0,5,iMA_buf1);
   err2=CopyBuffer(iMA_handle2,0,0,5,iMA_buf2);
   (...)
Files:
Alain Verleyen
Moderator
28561
Alain Verleyen 2014.05.07 20:56  
YouTrade:

Here is ...

I have placed a Picture with another example. 

I am not sure what your problem is. I can't see from your code which buffer is for the slow ma and which one is the slow. From the code on your first post, iMA_buf1 has to be the Fast ma and iMA_buf2 the slow MA, right ?

Also you are comparing MA values of candle 1 to candle 0. Candle 0 is the current open candle, its value is always changing, and doesn't correspond to what you can see on a chart with historical data.

Marcelo Coutinho
1104
Marcelo Coutinho 2014.05.07 21:10  
angevoyageur:

I am not sure what your problem is. I can't see from your code which buffer is for the slow ma and which one is the slow. From the code on your first post, iMA_buf1 has to be the Fast ma and iMA_buf2 the slow MA, right ?

Also you are comparing MA values of candle 1 to candle 0. Candle 0 is the current open candle, its value is always changing, and doesn't correspond to what you can see on a chart with historical data.

The opposite ... iMA_buf1 is slow. iMA_buf2 is fast and y see iMA_buf1 value is greater than iMA_buf2.

Regarding [0][1], I am aware of that, just experiencing.

Alain Verleyen
Moderator
28561
Alain Verleyen 2014.05.07 21:16  
YouTrade:

The opposite ... iMA_buf1 is slow. iMA_buf2 is fast and y see iMA_buf1 value is greater than iMA_buf2.

Regarding [0][1], I am aware of that, just experiencing.

If iMA_buf1 is slow then your conditions are reversed. A buy is when fast crosses slow from below to above. A slow is when fast crosses slow from above to below. 

It's always better to give more intuitive name to your variables :

   if(iMA_fast[1]<=iMA_slow[1] && iMA_fast[0]>iMA_slow[0])
     {
      Print("[BUY] - ticks: ",ticks," ContratosAtual: ",ContratosAtual," contratos: ",contratos," order: ",order," orderAtual: ",orderAtual," iMA_buf1[1]-iMA_buf2[1]: ",DoubleToString(iMA_buf1[1]-iMA_buf2[1],6)," iMA_buf1[0]-iMA_buf2[0]: ",DoubleToString(iMA_buf1[0]-iMA_buf2[0],6));
      order=1;
     }
   if(iMA_fast[1]>=iMA_slow[1] && iMA_fast[0]<iMA_slow[0])
     {
      Print("[SELL] - ticks: ",ticks," ContratosAtual: ",ContratosAtual," contratos: ",contratos," order: ",order," orderAtual: ",orderAtual," iMA_buf1[1]-iMA_buf2[1]: ",DoubleToString(iMA_buf1[1]-iMA_buf2[1],6)," iMA_buf1[0]-iMA_buf2[0]: ",DoubleToString(iMA_buf1[0]-iMA_buf2[0],6));
      order=-1;
     }
Marcelo Coutinho
1104
Marcelo Coutinho 2014.05.07 21:21  
angevoyageur:

If iMA_buf1 is slow then your condition are reversed. A buy is when fast crosses slow from below to above. A slow is when fast crosses slow from above to below. 

It's always better to give more intuitive name to your variables :

This is what is happening NO ? 

iMA_buf1[1]-iMA_buf2[1])<=0 ... means buf1<buf2 so buf1(fast) is below buf2(slow)

iMA_buf1[0]-iMA_buf2[0]>0 ... means buf1>buf2 so buf1(fast) is above buf2(slow)

From [1] to [0] ... crossed from below to above (BUY)

Am I wrong ? 

Alain Verleyen
Moderator
28561
Alain Verleyen 2014.05.07 21:23  
YouTrade:

This is what is happening NO ? 

From [1] to [0] ... crossed from below to above (BUY)

Am I wrong ? 

Yes. You just wrote  :

iMA_buf1 is slow

and now :

iMA_buf1[1]-iMA_buf2[1])<=0 ... means buf1<buf2 so buf1(fast) is below buf2(slow)

iMA_buf1[0]-iMA_buf2[0]>0 ... means buf1>buf2 so buf1(fast) is above buf2(slow)
Marcelo Coutinho
1104
Marcelo Coutinho 2014.05.07 21:32  
angevoyageur:

Yes. You just wrote  :

and now :

Please accept my apologies. I have made a mistake.

buf1 is fast (3 for example)

buf2 is slow (5 for example) 

Alain Verleyen
Moderator
28561
Alain Verleyen 2014.05.07 21:38  
YouTrade:

Please accept my apologies. I have made a mistake.

buf1 is fast (3 for example)

buf2 is slow (5 for example) 

Ok, so what is your problem ?
Marcelo Coutinho
1104
Marcelo Coutinho 2014.05.07 21:52  
angevoyageur:
Ok, so what is your problem ?

When I run the ST, the most profitable scenarios are when buf1(fast) is bigger than buf2(slow).

Example buf1=15 periods and buf2 = 10 periods. 

/ /123
To add comments, please log in or register