# Quick moving average question

Sign up to publish an article and receive 200 USD
129
2016.12.02 11:09

Hi Guys,

I'm having trouble with one of my sell signals.

The buy signal works perfect but when I turn it around for a sell signal it gives me a False statement the whole time.

double MA1L15 = iMA(NULL,15,7,0,MODE_SMA,PRICE_OPEN,0);
double MA2L15 = iMA(NULL,15,21,0,MODE_SMA,PRICE_CLOSE,0);

bool buy_condition_2 = (MA1L15 > MA2L15);
bool sell_condition_2 = (MA1L15 < MA2L15);

Has anyone encountered this before?

Anton

Moderator
29606
2016.12.02 12:10
Print your value and check why.
129
2016.12.02 12:24
Alain Verleyen:
Print your value and check why.

I tried that already and both moving averages have a value.

This is a example of what it printed:

EURUSD,H1: Sell Conditions

EURUSD,H1: Sell 1true

EURUSD,H1: Sell 2false

EURUSD,H1: MA2L151.088302380952381
EURUSD,H1: MA1L151.088302857142855

So my first selling condition, which I didn't post in my original post is true. It is just the second one that for some reason won't accept it.
Moderator
29606
2016.12.02 12:34
C0mput3r:

I tried that already and both moving averages have a value.

This is a example of what it printed:

EURUSD,H1: Sell Conditions

EURUSD,H1: Sell 1true

EURUSD,H1: Sell 2false

EURUSD,H1: MA2L151.088302380952381
EURUSD,H1: MA1L151.088302857142855

So my first selling condition, which I didn't post in my original post is true. It is just the second one that for some reason won't accept it.
bool sell_condition_2 = (MA1L15 < MA2L15);

sell_condition_2 = (1.088302857142855 < 1.088302380952381) : false

It's correct.

Though you have only difference on 7th decimal.

129
2016.12.02 12:58
Alain Verleyen:
bool sell_condition_2 = (MA1L15 < MA2L15);

sell_condition_2 = (1.088302857142855 < 1.088302380952381) : false

It's correct.

Though you have only difference on 7th decimal.

I don't understand then where I could be wrong. It shows that 7 period MA is always higher than the 21 period MA. How can this be? It is averaged out so it should definitly be possible for the 21 to be higher than the 7 at some point.

I just want it to sell when 7 MA < 21 MA and buy when 7 MA > 21 MA

11380
2016.12.02 13:49
C0mput3r:

I don't understand then where I could be wrong. It shows that 7 period MA is always higher than the 21 period MA. How can this be? It is averaged out so it should definitly be possible for the 21 to be higher than the 7 at some point.

I just want it to sell when 7 MA < 21 MA and buy when 7 MA > 21 MA

If you want to sell when fast is less than slow and buy when fast is greater than slow, with your conditions you are always going to be either selling or buying on each and every bar regardless of the previous state.  You should consider using only crosses instead.

PS: it is not showing that 7 period MA is always higher than 21 period MA (at least at my terminal it does not). It works as expected. Check the rest of your code for some accidental assignment
129
2016.12.02 14:00

If you want to sell when fast is less than slow and buy when fast is greater than slow, with your conditions you are always going to be either selling or buying on each and every bar regardless of the previous state.  You should consider using only crosses instead.

PS: it is not showing that 7 period MA is always higher than 21 period MA (at least at my terminal it does not). It works as expected. Check the rest of your code for some accidental assignment

Thank you for your response Mladen! I managed to figure out the problem.. really simple actually, I had the IMA declarations before my OnTick() loop so the two MA values were stuck as the same numbers for the whole loop everytime.

I actually have another buy and sell condition that is the main focus of my EA. I just added the MA condition so that I protect against losses incase of a heavy upward or downward trend.

Thanks for the help and advice!

/