Help me to correct counting bars number since price cross over ema

 

i'm new bee im MQL4 , in amibroker , we can easy get bars since price cross ema with built in funtion barsince ().  I have been trying to do count bars in mql4 but the result's not correct. Please , to correct my code , thank you so much ><


void OnTick()

  {
//---
//--- get minimum stop level 
   double minstoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL); 
   Print("Minimum Stop Level=",minstoplevel," points"); 
   double price=Ask; 
//--- calculated SL and TP prices must be normalized 
   double stoploss=NormalizeDouble(Bid-minstoplevel*Point,Digits); 
   double takeprofit=NormalizeDouble(Bid+minstoplevel*Point,Digits); 

   double movingAverage = iMA(NULL,0,5,0,MODE_EMA,PRICE_HIGH,0);
   double movingAveragel = iMA(NULL,0,5,0,MODE_EMA,PRICE_HIGH,1);
   double movingAveragemid = iMA(NULL,0,5,0,MODE_EMA,PRICE_OPEN,0);
   double movingAveragemid1ow = iMA(NULL,0,5,0,MODE_EMA,PRICE_LOW,0);
   
   double movingAveragemid1 = iMA(NULL,0,5,0,MODE_EMA,PRICE_OPEN,1);// ema open 5 -1
   double movingAveragemid1ow1 = iMA(NULL,0,5,0,MODE_EMA,PRICE_LOW,1);// ema low 5 -1
   double movingAveragemid1ow12 = iMA(NULL,0,5,0,MODE_EMA,PRICE_LOW,2);// ema low 5 -1
   
   double movingAveragemid13 = iMA(NULL,0,13,0,MODE_EMA,PRICE_OPEN,0);
   double mvLast13L = iMA(NULL,0,13,0,MODE_EMA,PRICE_OPEN,1);
   double movingAveragemid30 = iMA(NULL,0,30,0,MODE_EMA,PRICE_OPEN,0);
   double mvlast30 = iMA(NULL,0,30,0,MODE_EMA,PRICE_LOW,1);
   
   double currentHigh = High[0];
   double currentClose = Close[0];
   double currentOpen = Open[0];
   double currentLow = Low[0];
   double currentLow15 = iLow(NULL,PERIOD_M15,0);// lay gia low cua khung thoi gian 15 phut
   double lastPrice = Close[1];
   double lastHigh = High[1];
   double lastLow = Low[1];
   double Ask,Bid; 

   double Spread=Ask-Bid; 


   double a = (currentClose>movingAveragemid) && (mvlast30>movingAveragemid1)&& (movingAveragemid>movingAveragemid30);
   int sobar=iBarShift(NULL, 0, a);

      Comment(StringFormat("Show prices\nSpread = %G\nBar = %d",Spread,sobar));


   if (OrdersTotal()<3)
      
   if ((sobar>=1)&& (sobar<3)){
   // buy price > mid 5 open
   order=OrderSend(NULL,OP_BUY,0.04,Ask,1,movingAveragemid1ow,0,NULL,0,0,clrGreen);
   } 
  // else if ((movingAveragemid13>mvLast13L) &&(movingAveragemid1ow>movingAveragemid13)&& (movingAveragemid13>movingAveragemid30)&& (currentLow>movingAveragemid)&& (movingAveragemid+4*_Point>currentClose)&& (movingAveragemid>movingAveragemid1)) {
   //buy
   //order=OrderSend(NULL,OP_BUY,0.04,Ask,3,movingAveragemid-4*_Point,0,NULL,0,0,clrGreen);
    //void Alert("Last price: " +lastPrice + "Current Price: "+ currentPrice);
   //}
   if ((Ask > movingAveragemid1ow)){
      PlaySound("ok.wav");
   // sell
   //order= OrderSend(NULL,OP_SELLLIMIT,0.04,currentHigh,3,currentClose+10*_Point,currentClose-20*_Point,NULL,0,0,clrRed);
   //Alert("Last price: " +lastPrice + "Current Price: "+ currentPrice);
   }   
   
  }
 

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