If candle[1] > candle[2] mql5

 

I am trying to create an EA that will buy if MA is up && Candle[1]> candle[2]

I get ">" Illegal operation use.


#include <Trade\Trade.mqh>
CTrade trade;

input group "Trading Inputs"
input double Lots = 0.1;
input double SlDist = 0.0005;

input group "Indicator Inputs"
input ENUM_TIMEFRAMES MaTimeframe = PERIOD_CURRENT;
input int MaPeriod = 20;
input ENUM_MA_METHOD MaSlope = MODE_SMA;

int handleMaSlope;
double MA[];
MqlRates Price[];
ulong trade_ticket =0;

int OnInit(){
   ArraySetAsSeries(Price,true);
   handleMaSlope = iMA(_Symbol,MaTimeframe,MaPeriod,0,MaSlope,PRICE_CLOSE);

   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason){   
} 

void OnTick(){ 
   
   
   CopyRates(_Symbol,PERIOD_CURRENT,0,3,Price);
       
    
   CopyBuffer(handleMaSlope,0,0,2,MA);                    
      
      if(PositionSelectByTicket(trade_ticket) == false){
         trade_ticket = false;
         } 
         
//Buy              
      if(trade_ticket <=0 && MA[0] > MA[1] && Price[1] > Price[2]){
      double ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
                    
            trade.Buy(Lots,_Symbol,ask,ask-SlDist);
            trade_ticket = trade.ResultOrder();             
            }
           
//CLOSE BUY

        if(MA[0] < MA[1]){        
          trade.PositionClose(_Symbol);
          trade_ticket = trade.ResultOrder();
          }
//Sell                         
      if(trade_ticket <=0 && MA[0] < MA[1] && Price[1] < Price[2]){
      double bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
                     
            trade.Sell(Lots,_Symbol,bid,bid+SlDist);
            trade_ticket = trade.ResultOrder();                  
            }     
//CLOSE SELL

        if(MA[0] > MA[1]){        
          trade.PositionClose(_Symbol);
          trade_ticket = trade.ResultOrder();      
          }
}  
 
When you do 
Price[1] > Price[2]

you're actually comparing two MqlRates structures against each other, not the prices themselves. This structure doesn't overload the operator >, thus the error. Just do it like this 

Price[1].close > Price[2].close
or whatever the price series you want and you should be good (the same should be done on other code sections where comparisons are made)