opening a positon on the exact moment the fast MA crosses the slow MA

 
Hello guys,

I am looking for a solution to my EA on how to open a position (BUY or SELL) on the exact moment the fast MA crosses the slow MA.
All i have seen is rather on the closing of the last bar or on the opening of the next one.
I want the EA to start a position (BUY or SELL) right after the two MAs crosses each other without the need of the actual bar closing.

Can anyone helo with this code? Please.

Thank you guys in advance
 
Maiabots:
Hello guys,

I am looking for a solution to my EA on how to open a position (BUY or SELL) on the exact moment the fast MA crosses the slow MA.
All i have seen is rather on the closing of the last bar or on the opening of the next one.
I want the EA to start a position (BUY or SELL) right after the two MAs crosses each other without the need of the actual bar closing.

Can anyone helo with this code? Please.

Thank you guys in advance

From my experience is not good idea to do the way you are thinking:

  • The two MAs could cross and uncross several times within the same bar[0]
  • The two MAs could cross between two bars

The idea of using bar[1] and bar[2] is to prevent false-positive crosses

 

EA's run on every tick so it is quiet simple to have orders sent as soon as a cross happens rather than at the end/start of a bar.


If you look at Moving Average.MQ4 than comes with MT4 you will see this in the code:


ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
//--- sell conditions
   if(Open[1]>ma && Close[1]<ma)
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
      return;
     }


This code is executed every tick, what it is doing is look at the previous bar Open and Close (by using shift [1]), if the bar opened above the ma and closed below it, the bar has crossed the ma to the down side and it will open a short position.

Effectively this order would be sent on the first tick of the new bar, as on that first tick it would check the previous bar open/close and trade or not.

If the code was changed to:


if(Open[0]>ma && Close[0]<ma)


We have a shift of [0] which means the current (forming) bars price, so on every tick the current bid would be Close[0] and would be checked and if it crosses below the ma.

That way the trade could be placed as soon as it crosses, rather than wait until the bar is closed.


You could use the same process to trade as soon as 2 EMA's crossed etc..


Though as Fernando says, MA's can cross and uncross multiple times while a bar is forming so caution is needed.

 
Fernando Morales:

From my experience is not good idea to do the way you are thinking:

  • The two MAs could cross and uncross several times within the same bar[0]
  • The two MAs could cross between two bars

The idea of using bar[1] and bar[2] is to prevent false-positive crosses

Thanks Fenando for your comment.

I am totaly new to programing EAs... I understand your first point and it makes total sense...

Well... the idea to try to solve that is (and it is a big what if)

As soon as the Fast MA crosses the Slow MA and goes forward 1 tick, the EA places a pending order to inverting the position ending up the same size on the exact point of the current position was open. it has to be freaking fast cuz it as you said can go on and on within the same bar and inverting positions lots of times until it goes its way up or down.


I know I know it might pay some good spreads but I have to at least test this idea and see if the big moves can pay off all the spreads and stoplosses it could generate.


But your second point I couldn't picture how it could be a downside to my EA. The MAs almost always cross each other between two bars. Can you elucidate it so this noob (me) can undderstand

Can you tell me your thoughts about this new information?

thank you again... 




 
Lachlan Meakin:

EA's run on every tick so it is quiet simple to have orders sent as soon as a cross happens rather than at the end/start of a bar.


If you look at Moving Average.MQ4 than comes with MT4 you will see this in the code:



This code is executed every tick, what it is doing is look at the previous bar Open and Close (by using shift [1]), if the bar opened above the ma and closed below it, the bar has crossed the ma to the down side and it will open a short position.

Effectively this order would be sent on the first tick of the new bar, as on that first tick it would check the previous bar open/close and trade or not.

If the code was changed to:



We have a shift of [0] which means the current (forming) bars price, so on every tick the current bid would be Close[0] and would be checked and if it crosses below the ma.

That way the trade could be placed as soon as it crosses, rather than wait until the bar is closed.


You could use the same process to trade as soon as 2 EMA's crossed etc..


Though as Fernando says, MA's can cross and uncross multiple times while a bar is forming so caution is needed.

Thank you so much Lachlan

I saw that the code you shared is from mql4... and If it can be replicated to mql5 it will tremendously help me. Is it? Is it the same code for mql5? 
By any chance, would you have it in mql5?

Another thing I answered Fernando and shared some ideas... I would love to know your thoughts about that as well.

once again... thank you very much. 

Reason: