Take Trade on each cross over Simple_Moving_Average........

 

Hello All,

I am asking for MT4, currently working on SMA strategy. I have coded these codes, now I want that EA should execute buy/sell on each crossover of SMA. But I can't understand how to that, can any one help so that EA take buy/sell position on every single cross over.

input int SlowMA = 40;
input int FastMA = 10;


void OnTick()
  {
   string signal ="";
   
   double CurrentFastMovingAverage = iMA(_Symbol,_Period,FastMA,0,MODE_SMA,PRICE_CLOSE,0);
   double PreviousFastMovingAverage = iMA(_Symbol,_Period,FastMA,0,MODE_SMA,PRICE_CLOSE,1);
   double CurrentSlowMovingAverage = iMA(_Symbol,_Period,SlowMA,0,MODE_SMA,PRICE_CLOSE,0);
   double PreviousSlowMovingAverage = iMA(_Symbol,_Period,SlowMA,0,MODE_SMA,PRICE_CLOSE,1);
   
   if ((PreviousFastMovingAverage>PreviousSlowMovingAverage) && (CurrentFastMovingAverage<CurrentSlowMovingAverage))
      signal = "sell";
      
   if ((PreviousFastMovingAverage<PreviousSlowMovingAverage) && (CurrentFastMovingAverage>CurrentSlowMovingAverage))
      signal = "buy";
      
   if ( signal=="buy" && OrdersTotal()==0)
   OrderSend(_Symbol,OP_BUY,0.10,Ask,3,0,Ask+100*_Point,NULL,0,0,Green);
   
   if (signal=="sell" && OrdersTotal()==0)
   OrderSend(_Symbol,OP_SELL,0.10,Bid,3,0,Bid-100*_Point,NULL,0,0,Red)
   
   Comment("THE CURRENT SIGNAL IS : ", signal);
  }
//+------------------------------------------------------------------+

Please help.

 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button


 
Can anyone help me here !
 
KANISHKA007: I want that EA should execute buy/sell on each crossover of SMA. But I can't understand how to that, can any one help s
Look for a cross.
double aPrev = …, aCurr = …,
       bPrev = …, bCurr = …;
bool   wasUp = aPrev > bPrev,
        isUp = aCurr > bCurr,
       isCross = isUp != wasUp;
Reason: