Download MetaTrader 5

Quick moving average question

To add comments, please log in or register
C0mput3r
162
C0mput3r  

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?

Thanks in advance!

Anton 

Alain Verleyen
Moderator
33784
Alain Verleyen  
Print your value and check why.
C0mput3r
162
C0mput3r  
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.
Alain Verleyen
Moderator
33784
Alain Verleyen  
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.

C0mput3r
162
C0mput3r  
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

Mladen Rakic
65704
Mladen Rakic  
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
C0mput3r
162
C0mput3r  
Mladen Rakic:

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! 

To add comments, please log in or register