EA Not opening when Period function is used

 

Hi all

I would like my ea to check if daily chart (Engulfing&Harami), if it's buy/sell then i would like to do the same on the M5 Chart.

Below is my code, please help if you know the problem.

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int CountTrades()
 {
  int Count=0;
  for(int a=OrdersTotal()-1;a>=0;a--)
   {
    if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES))
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
    if(OrderType()==OP_BUY||OrderType()==OP_SELL)
    Count++;
   }
  return(Count);
 } 

void Buy()
 {
  double CloseBar=iClose(NULL,0,1);
  double OpenBar=iOpen(NULL,0,1);
  double LowBar=iLow(NULL,0,1);
  double HighBar=iHigh(NULL,0,1);
  double CloseBarNext=iClose(NULL,0,2);
  double OpenBarNext=iOpen(NULL,0,2);
  double LowBarNext=iLow(NULL,0,2);
  double HighBarNext=iHigh(NULL,0,2);
  
  double EngulfingBuy=LowBar<LowBarNext&&HighBar>HighBarNext&&CloseBar>OpenBarNext&&OpenBar<CloseBar&&OpenBarNext>CloseBarNext;
  double HaramiBuy=OpenBarNext>CloseBarNext&&CloseBar>OpenBar&&HighBarNext>HighBar&&OpenBarNext>CloseBar&&LowBarNext<LowBar;
  
  int TimeFrameM5=Period()==PERIOD_M5;
  int TimeFrameD1=Period()==PERIOD_D1;
  
  if(TimeFrameM5==true&&EngulfingBuy&&HaramiBuy&&TimeFrameD1==false&&EngulfingBuy&&HaramiBuy)
  {
   if(HaramiBuy)
   {
    int BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"JackBuda",MagicNumber,0,clrBlue);
   }

   else if(EngulfingBuy)
   {
    int BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"JackBuda",MagicNumber,0,clrBlue);
   }
  
  }
 }
 
void Sell()
 {
  double CloseBar=iClose(NULL,0,1);
  double OpenBar=iOpen(NULL,0,1);
  double LowBar=iLow(NULL,0,1);
  double HighBar=iHigh(NULL,0,1);
  double CloseBarNext=iClose(NULL,0,2);
  double OpenBarNext=iOpen(NULL,0,2);
  double LowBarNext=iLow(NULL,0,2);
  double HighBarNext=iHigh(NULL,0,2);
  
  double EngulfingSell=LowBar<LowBarNext&&HighBar>HighBarNext&&CloseBar<OpenBarNext&&OpenBar>CloseBar&&OpenBarNext<CloseBarNext;
  double HaramiSell=OpenBarNext<CloseBarNext&&CloseBar<OpenBar&&HighBarNext<HighBar&&OpenBarNext<CloseBar&&LowBarNext>LowBar;
  
  int TimeFrameM5=Period()==PERIOD_M5;
  int TimeFrameD1=Period()==PERIOD_D1;
  
  if(TimeFrameM5==true&&EngulfingSell&&HaramiSell&&TimeFrameD1==false&&EngulfingSell&&HaramiSell)
  {
   if(HaramiSell)
   {
    int SellTrade=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"JackBuda",MagicNumber,0,clrRed);
   }
   
   else if(EngulfingSell)
   {
    int SellTrade=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"JackBuda",MagicNumber,0,clrRed);
   }
  }
  
 }  

void OnTick()
 {
  double Spread=MarketInfo(Symbol(),MODE_SPREAD);
  double MaxLot=MarketInfo(Symbol(),MODE_MAXLOT);
  int Leverage=AccountLeverage();
 
  Comment("Spread is "+DoubleToStr(Spread/10,1)+
  "\nMax Lot For Broker is "+DoubleToStr(MaxLot/1,1)+
  "\nLeverage For Broker is "+DoubleToStr(Leverage/1,1));
  
  if(AccountBalance()>10&&CountTrades()<2){Buy();}

  if(AccountBalance()>10&&CountTrades()<2){Sell();}

  
  for(int Loop2=OrdersTotal()-1;Loop2>=0;Loop2--)
  {
   if(OrderSelect(Loop2,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   { 
    bool CloseTrade=false;
    int Type=OrderType();
    double Profit=OrderLots()*200;
    switch(Type)
    {
     case OP_BUY:if(OrderProfit()>=Profit)CloseTrade=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrGreen);break;
     case OP_SELL:if(OrderProfit()>=Profit)CloseTrade=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrGreen);break;
     //Print("ErrorCode:",GetLastError());                         
    }
   }
  }
  
 }
 
Jack Buda:

EA Not opening when Period function is used

  1. How To Ask Questions The Smart Way. 2004
              Don't rush to claim that you have found a bug.
    Questions Not To Ask
              My program doesn't work. I think system facility X is broken.

    It is almost always your code.

  2. Jack Buda: I would like my ea to check if daily chart (Engulfing&Harami), if it's buy/sell then i would like to do the same on the M5 Chart.

    You are not checking the M5 and D1 charts. You are reading the current chart only.

  3. When will EngulfingSell and HaramiSell both be true at the same time?

 
William Roeder:
  1. How To Ask Questions The Smart Way. 2004
              Don't rush to claim that you have found a bug.
    Questions Not To Ask
              My program doesn't work. I think system facility X is broken.

    It is almost always your code.

  2. You are not checking the M5 and D1 charts. You are reading the current chart only.

  3. When will EngulfingSell and HaramiSell both be true at the same time?

1. I will try my best to ask questions in a smarter way next time.

2. So if I'm reading the M5 & D1, how can i check the M5 & D1 chart? Buy using ChartPeriod() function? If there's another function please let me know on how it is done.

3. I was trying this for the first time (Engulfing & Harami) with checking timeframes, hence i need help with the code.

Reason: