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(..)
}
- put code in start() not in init()
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.
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?
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).

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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,