EA that sells when price crosses an MA

 

I am struggling to write an EA that sends an order after the price crosses 2 MA's in one bar. I have come up with:

int init()
{MA_fast=iMA(NULL,0,MovingPeriod1,1,MODE_SMA,PRICE_CLOSE,1); // gets moving average
MA_slow=iMA(NULL,0,MovingPeriod2,1,MODE_SMA,PRICE_CLOSE,1);// gets fast moving average 4
if (OrdersTotal () <=0 )
{
if (Open [] > MA_fast && Open[] > MA_slow)Comment ("above");
{ if (Bid < MA_fast && Bid < MA_slow)
OrderSend(Symbol(),OP_SELL,0.1,Bid,3,Bid-0.001,Ask+0.001,"My order #2",0,0,Green);
}
if (Open [0] < MA_fast && Open [0] < MA_slow) Comment ("below");
{ if (Ask > MA_fast && Ask > MA_slow)
OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Ask-0.001,Ask+0.001,"My order #2",0,0,Green);
}

}


However this just does not open any orders at all. Any suggestions would be greatly appreciated.


Thanks in advance,

 

You probably want to compare the MA of already fully formed bars so the shift of those is 1 and 2.

You therefore need MA_fast1, MA_fast2, MA_slow1 and MA_slow2 and then compare those:

if(Ma_f1> MA_s2 && MA_f2 < MA_s1){ // break upwards

OrderSend(...)

} else if (MA_f1 < MA_s2 && MA_f2 > MA_s1 ){ // break downwards

OrderSend(..)

}

 
  1. put code in start() not in init()

 
forexCoder:

You probably want to compare the MA of already fully formed bars so the shift of those is 1 and 2.

You therefore need MA_fast1, MA_fast2, MA_slow1 and MA_slow2 and then compare those:

if(Ma_f1> MA_s2 && MA_f2 < MA_s1){ // break upwards

OrderSend(...)

} else if (MA_f1 < MA_s2 && MA_f2 > MA_s1 ){ // break downwards

OrderSend(..)

}

I am trying to find code that will allow me to open a position as soon as the price crosses 2 MA's in one bar. For example, price is above both fast and slow MA's and then, within the space of one bar falls below the slower MA. Ideally an order would open as it crosses the slower MA.

 

ahhhhhhh ok, different then.

I'd use if(open[1] > MA1 && open[1] > MA2 && bid < MA1 && bid < MA2 || open[1] < MA1 && open[1] < MA2 && ask > MA1 && ask > MA2){}. Depends on MA properties, but you should not be having open positions comparable to spread size, unless scalping... but if you're scalping, don't use MAs... what do I know anyway.

 
Thats pretty much what I already have. I have broken up the statements a bit, and use bid & ask prices, but basically the same logic with slightly different syntax .The problem is that when I backtest it, it opens one position on the first bar, closes that position when it hits a stoploss or a takeprofit, and then does nothing more for the rest of the backtest. I have manually checked the charts so I know that it should have opened dozens of positions. I just cant figure it out...
 

No you don't. Recheck my code and correct yours.

EDIT: more to the point correct this:if (Open [] > MA_fast && Open[] > MA_slow)Comment ("above");

 

Thanks, your right.

However now I have the following:

int init()
{double MA_1=iMA(NULL,0,10,1,MODE_SMA,PRICE_CLOSE,1);
double MA_2=iMA(NULL,0,12,1,MODE_SMA,PRICE_CLOSE,1);

if (Open [1] > MA_1 && Open[1] > MA_2 && Bid < MA_1 && Bid < MA_2)
{OrderSend(Symbol(),OP_SELL,0.1,Bid,3,Bid-0.001,Ask+0.001,"",0,0,Green);
}
if (Open [1] < MA_1 && Open [1] < MA_2 && Ask > MA_1&& Ask > MA_2)
{OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Ask-0.001,Ask+0.001,"",0,0,Green);
}
return;
}


Unfortunately, now it won't trade at all. I have put the two MAs (10 & 12) quite closely together, but nothing.

 

Open[1] means the open price if the first fully formed bar (from right to left). You probably want the Open[0] (open price of this (last) bar).

Not sure what parameters you use for MA lines, but there are those that make no trades in such examples :P Are you certain that using the current chart and timeframe(!) you fall into the 2 categories, defined by the above statement?

 
I have tried it with Open [0]. No luck. I have manually reviewed the charts, and using a fast MA of 10 and a slow MA of 12, it should be triggered at least once a day on the 4hour chart. However, not triggered even once with tests of 100 days or more. It is driving me crazy -- I have been stuck on this problem for several days and have literally tried dozens of different combinations. I just cant figure out why. Actually, at this point I would be happy to get it to trade as the price crosses just one MA.
 

Lol, I'll take a look, what symbol?

EDIT: just for fun insert Print("bla2"); and Print("bla2"); next to OrderSend functions, respectively. Let's see if the code even gets into executing OrderSend(), If it does and fails, then your expert log will show you failed attempts. Please check those logs as well (and paste them here).

Reason: