price near 200MA

 
I am trying to design an EA to place a buy when price has been above the 200MA for at least 5 bars and price comes down to the moving average.
it should do the opposite for a sell, ie price must have been below the 200moving average for at least 5 bars then touch the 200ma and initiate a sell.

Will this work?


if (iClose(NULL, PERIOD_H1, 1) > iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 0) &&
iClose(NULL, PERIOD_H1, 2) > iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 0) &&
iClose(NULL, PERIOD_H1, 3) > iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 0) &&
iClose(NULL, PERIOD_H1, 4) > iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 0) &&
iClose(NULL, PERIOD_H1, 5) > iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 0) &&
Bid <= iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 0))


I also want to make sure that once one order has been placed, the EA will not place another order for at least 5 periods.
In fact, I also need to avoid the times price just goes up and down through the moving average for a long period, any ideas?
 
I'm new at this so my advice comes with a health warning...

I would look at using iTime() and iBarshift() to solve your issue..

for example, take a time every time the close is below the MA... when close crosses above MA, the time variable will stop updating

Then use ibarshift to count back how many bars since the last updated time and you can then enter after 5 bars.

Same with the order. You can use OrderOpenTime() and then ibarshift to count back 5 bars before another entry.

Hope that helps
V

SanMiguel:
In fact, I also need to avoid the times price just goes up and down through the moving average for a long period, any ideas?

If you crack that one, do let me know :)

 
Just wanted to add that your current logic compares the 5 closing prices with the same current MA. Concievably, if the MA is falling, your price 5 bars ago could be below the MA 5 bars ago but above the current MA so would pass this test. And similarly the price could have crossed the MA 5 bars ago but it is below the current MA and would fail.
 
Then use ibarshift to count back how many bars since the last updated time and you can then enter after 5 bars.


It's more that price must have been below the MA for 5 bars and then as soon as it touches the MA, it triggers an order.

So,

if (iClose(NULL, PERIOD_H1, 1) > iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 1) &&
iClose(NULL, PERIOD_H1, 2) > iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 2) &&
iClose(NULL, PERIOD_H1, 3) > iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 3) &&
iClose(NULL, PERIOD_H1, 4) > iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 4) &&
iClose(NULL, PERIOD_H1, 5) > iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 5) &&
Bid <= iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 0))
 
"Touches" is an issue I think because the distance between ticks is variable. Maybe an envelope around the MA might help so the MA line is "thicker". As is, the MA is exactly one point thick and the tick may not fall precisely on that point but move under. Even if it's but 1 point under, a miss is as good as a mile. Maybe count your bars as described an then enter when price is inside your MA envelope and ibarshift > 5.

Not tested but I'm thinking along the lines of

 datetime below;
 
 if (Close[1]<ima200[1])
    {
    below=iTime(NULL,0,0);
    }
 
 int shift= iBarShift(NULL,0,below);
 
 
 double upper_envelope=ima200+(5*Point);
 double lower_envelope= ima200-(5*Point);
 
 if (shift>5 && Bid<upper_envelope && Bid>lower_envelope && (OrdersTotal()==0 || iBarShift(NULL,0,OrderOpenTime()>5)); 

BUY

# EDIT: Sorry didn't see the BID <=. The above should still be OK though because the status of time references the prev bar. and perhaps leaving it open to anything below the MA is a bit risky if the market gaps significantly imho.
 
SanMiguel:
I am trying to design an EA to place a buy when price has been above the 200MA for at least 5 bars and price comes down to the moving average.
it should do the opposite for a sell, ie price must have been below the 200moving average for at least 5 bars then touch the 200ma and initiate a sell.

Will this work?


if (iClose(NULL, PERIOD_H1, 1) > iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 0) &&
iClose(NULL, PERIOD_H1, 2) > iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 0) &&
iClose(NULL, PERIOD_H1, 3) > iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 0) &&
iClose(NULL, PERIOD_H1, 4) > iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 0) &&
iClose(NULL, PERIOD_H1, 5) > iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 0) &&
Bid <= iMA(NULL, PERIOD_H1, 200, 0, MODE_SMA, PRICE_CLOSE, 0))


I also want to make sure that once one order has been placed, the EA will not place another order for at least 5 periods.
In fact, I also need to avoid the times price just goes up and down through the moving average for a long period, any ideas?


Thank you for the post.
_________________
http://www.moviesonlinefree.biz

Reason: