Does the Moving Averages for One Hr Period crosses each other more than what we see?

 

Hi Friends,

I am a novice to MQL4 programming. I have just started to program my first Expert advisor based on Simple moving average. More than a strategy, this particular exercise is to make me more familiar with the nuances of the automated system as to understand first the constraints and benefits. Anyway let me pose my problem here.

I am constructing MA expert advisor based on one hour chart (JPN-USD). There are two MA's . One is having a period of 9 and another one is having 30. The logic is simple, if the MA9 goes above MA30, the system will go for a BUY, otherwise if it goes below the system will go for a SELL. When the crossing happens, the system will exit the current trade and then go for the next trade choosing BUY or SELL as explained before.

Now, my question is when the Crossing of the MA happens, sometimes from logs, I can see that many more transactions do happen than what we can see. i.e. after the closing of the existing trade, the system goes for a BUY or a SELL and suddenly after some time it will again go for a crossing closing the current trade and continue to open another trade and so on.

Now, from the MA chart, we can see only one crossing which basically makes me suspect that many more crossing of the MA's are actually happening in the One Hr chart, but it retraces depending on the trading (therefore the crossing dissapears ) and after that particular one hour candle is over, we can see only the definite crossings. But i suspect that in reality there are many more crossing which is actually triggering the CLOSE. Now if i switch to 5 minutes chart, i can see that there are many more crossings that what is being showed in the One hr chart. My questions are

1) is my assumption Moving Averages crossing many more times in a One Hour Chart than what we see later is true?

2) When we trade on the One hr. chart, does the the number of crossing in the 5 minutes chart the same as the one hour chart?

or is the One hour chart crossing lesser and the expert advisor running on the one hour chart gets triggered only once the MA crosses in the one hour chart i.e. it has nothing to do with the 5 minute chart crossing?

Please do help me since this will help me to clear many doubts before embarking on clearing the strategy.

 
rajesh_puu:

Hi Friends,

...

Look at this: 'Timing of trade orders not coinciding with CI timings'

 

To my knowledge what you see on the chart is what you get. It maybe that you have coded the MAs with the incorrect parameters. Please post your MA code.

It should look something like this:

double 9ema = iMA( NULL, 60, 9, 0, MODE_SMA, MODE_CLOSE, 0) ;

double 30ema = iMA( NULL, 60, 30, 0, MODE_SMA, MODE_CLOSE, 0) ;

I have assumed you are applying the close price. If you are, its best to wait for the bar to close as the MAs will cross and uncross before the bar closes.

 
fxcourt wrote >>

To my knowledge what you see on the chart is what you get. It maybe that you have coded the MAs with the incorrect parameters. Please post your MA code.

It should look something like this:

double 9ema = iMA( NULL, 60, 9, 0, MODE_SMA, MODE_CLOSE, 0) ;

double 30ema = iMA( NULL, 60, 30, 0, MODE_SMA, MODE_CLOSE, 0) ;

I have assumed you are applying the close price. If you are, its best to wait for the bar to close as the MAs will cross and uncross before the bar closes.

Thanks for the replies, as fxcourt said, i am applying on the closing price and the parameters are exactly as you have given as well. Now, you said that it is best to wait till the one hour chart bar closes. How do i do it? i mean the MA's can cross any time during the trading. can i ask one more thing about Moving average? When we give the period as 9 or 30, does it mean 9 days and 30 days average of all the one hour closing prices?

 

Im guessing here but its sounds like you got the open trade trigger set to the current bar, meaning Bar zero.

Using fxcourts example:

double 9ema = iMA( NULL, 60, 9, 0, MODE_SMA, MODE_CLOSE, 0) ;

double 30ema = iMA( NULL, 60, 30, 0, MODE_SMA, MODE_CLOSE, 0) ;

The last zero in the iMA should be 1. So it sounds like you need to shift your iMA's back one bar. If you are trading on the current live bar this will happen alot as the price is developing.

 
jboddie wrote >>

Im guessing here but its sounds like you got the open trade trigger set to the current bar, meaning Bar zero.

Using fxcourts example:

double 9ema = iMA( NULL, 60, 9, 0, MODE_SMA, MODE_CLOSE, 0) ;

double 30ema = iMA( NULL, 60, 30, 0, MODE_SMA, MODE_CLOSE, 0) ;

The last zero in the iMA should be 1. So it sounds like you need to shift your iMA's back one bar. If you are trading on the current live bar this will happen alot as the price is developing.

Hi Jboddie,

Thanks for the advice and help. yeah..you are right i am always working on the current candle. In your opinion, which is better? Working on the CLOSE_PRICE of the previous candle or the OPEN_PRICE of the current candle? Since i am working on the one hour chart, i believe working on the CLOSE_PRICE of current bar is more accurate as it reflects the latest pricing movement, but then if there is a limitation on the crossing, i guess i have to follow either the OPEN_PRICE of the current candle or the CLOSE_PRICE of the previous candle.

Thanks for all the help.

Rajesh

 
rajesh_puu wrote >>

Hi Jboddie,

Thanks for the advice and help. yeah..you are right i am always working on the current candle. In your opinion, which is better? Working on the CLOSE_PRICE of the previous candle or the OPEN_PRICE of the current candle? Since i am working on the one hour chart, i believe working on the CLOSE_PRICE of current bar is more accurate as it reflects the latest pricing movement, but then if there is a limitation on the crossing, i guess i have to follow either the OPEN_PRICE of the current candle or the CLOSE_PRICE of the previous candle.

Thanks for all the help.

Rajesh

Well, you can take my advice as you will, but I am a newbie like yourself. I have been studying forex for 6 months and MQL4 for about half as much time. I have no programming experience of any kind except the past few months. Anyways here is my thoughts on what you ask. Whether to buy on current bar or close of the previous depends your method of trading. For this particular situation that you are trading off of I would say trade on the previous do to using the iMA's because it CAN present you with the problem you are currently having. You CAN trade on current bar with iMA's but will have to throw a bar time filter in with a price filter so it will only trigger LATE into the bar and price is following thru in aggreeance with your trigger. This would collect a few more pips rather than opening on previous bar, but much more complicated to code. I would like to make a point to you about you saying " working on current bars close price". Well, the close price of previous bar is not very old data. If your EMA's have crossed then the close price of the previous bar will only be 1 second old at most when trade is opened. Hard to explain but hope you understand.

Reason: