# EA trading in the opposite direction

1202

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:
Moderator
35118

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 ?
1202

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:
Moderator
35118

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.

1202

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.

Moderator
35118

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;
}```
1202

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 ?

Moderator
35118

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)```
1202

angevoyageur:

Yes. You just wrote  :

and now :

buf1 is fast (3 for example)

buf2 is slow (5 for example)

Moderator
35118

buf1 is fast (3 for example)

buf2 is slow (5 for example)

Ok, so what is your problem ?
1202

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.