Something Strange About MT5 And My EA

 

Hello. I'm noticing something a bit strange about my trades on MT5. My EA has been working perfectly until now. I noticed that a certain functionality no longer work. I contacted my broker and was told that they've thoroughly checked my account and confirmed that there are no limitations for me to apply for EA, and that I should check with the MetaQuotes team. But unfortunately, I can only make non-financial complaints only on this forum.

I made a functionality on my EA to close or exit trade when a candle closes below the 50 Moving Average for buy trade, and also exit trade when it closes above the 50 Moving Average for sell trade.

This was working very well when I traded live before. Now it doesn't work anymore. I switched over to Strategy Tester and made it run. It worked. I did same on Demo, It worked also. But it never did on my Live account.

Now when my EA enters a trade, I have to close it manually when that condition is met. This is really frustrating, and not what I intended. Is this now the norm or an error? Below is the code. Any help will be much appreciated.


//+.------------------------------------------------------------------+
//|                                                   ExpertMAMA.mq5 |
//|                   Copyright 2009-2017, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009-2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

#include <Trade/Trade.mqh>

CTrade trade;

int OnInit()
  {
//---
   Print("Starting IchiMA Expert Advisor...");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
    Print("Exiting IchiMA Expert Advisor...");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(){
   OnSell();

   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

   string signal ="";
   
   //Ichimoku and MA50 Definition
   double IchimokuAArray[], IchimokuBArray[], MA50Array[];
   
   int Ichimoku_handle = iIchimoku(_Symbol,_Period,9,26,52);
   int MA50 = iMA(_Symbol,_Period,50,0,MODE_EMA,PRICE_CLOSE);

   
   CopyBuffer(Ichimoku_handle,2,0,2,IchimokuAArray);
   CopyBuffer(Ichimoku_handle,3,0,2,IchimokuBArray);
   CopyBuffer(MA50,0,0,2,MA50Array);
   
   //Price Information       
   MqlRates PriceInformation[];
   ArraySetAsSeries(PriceInformation,true);
   int Data = CopyRates(Symbol(),Period(),1,Bars(Symbol(),Period()),PriceInformation);
   
   //Candle Information
   double high_wick = PriceInformation[0].high;
   double low_wick = PriceInformation[0].low;
   double close_Price = PriceInformation[0].close;
   
   // Below is when EA enters Buy trade when the high and low wick of the candle is above 
   // the Ichimoku cloud and 50MA
   if((((high_wick > IchimokuAArray[0] && low_wick > IchimokuAArray[0]) && (high_wick > IchimokuBArray[0] && low_wick > IchimokuBArray[0])) || 
   ((high_wick > IchimokuBArray[0] && low_wick > IchimokuBArray[0]) && (high_wick > IchimokuAArray[0] && low_wick > IchimokuAArray[0]))) && 
   (high_wick > MA50Array[0] && low_wick > MA50Array[0]))
   {
      signal = "buy";
      
   }
   
   // Below is when EA exit trade when price closes below the 50MA
   if((((MA50Array[0] > IchimokuAArray[0] || MA50Array[0] < IchimokuBArray[0]) || (MA50Array[0] > IchimokuBArray[0] || MA50Array[0] < IchimokuAArray[0]))) && 
   (close_Price < MA50Array[0]))
   {
      signal = "exit_trade";
   }
      
   if(signal == "exit_trade" && PositionsTotal() > 0)
   CloseAllBuyPositionsThisPair();
  
   if(signal == "buy" && PositionsTotal() == 0)
   trade.Buy(0.01,NULL,Ask);

}

void CloseAllBuyPositionsThisPair(){
   for(int i = PositionsTotal()-1; i >= 0; i--){
      int ticket = (int)PositionGetTicket(i);
      string CurrencyPair = PositionGetSymbol(i);
      
      int PositionDirection = (int)PositionGetInteger(POSITION_TYPE);
      
      if(PositionDirection == POSITION_TYPE_BUY)
      
      if(_Symbol == CurrencyPair){
         trade.PositionClose(ticket);
      }
   }
}

//***********************************************************************************************

void OnSell(){
   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

   string signal ="";
   
   //Ichimoku and MA50 Definition
   double IchimokuAArray[], IchimokuBArray[], MA50Array[];
   
   int Ichimoku_handle = iIchimoku(_Symbol,_Period,9,26,52);
   int MA50 = iMA(_Symbol,_Period,50,0,MODE_EMA,PRICE_CLOSE);

   
   CopyBuffer(Ichimoku_handle,2,0,2,IchimokuAArray);
   CopyBuffer(Ichimoku_handle,3,0,2,IchimokuBArray);
   CopyBuffer(MA50,0,0,2,MA50Array);

   //Price Information      
   MqlRates PriceInformation[];
   ArraySetAsSeries(PriceInformation,true);
   int Data = CopyRates(Symbol(),Period(),1,Bars(Symbol(),Period()),PriceInformation);
   
   //Candle Information
   double high_wick = PriceInformation[0].high;
   double low_wick = PriceInformation[0].low;
   double close_Price = PriceInformation[0].close;
   
   // Below is when EA enters Sell trade when the high and low wick of the candle is below 
   // the Ichimoku cloud and 50MA
   if((((high_wick < IchimokuAArray[0] && low_wick < IchimokuAArray[0]) && (high_wick < IchimokuBArray[0] && low_wick < IchimokuBArray[0])) || 
   ((high_wick < IchimokuBArray[0] && low_wick < IchimokuBArray[0]) && (high_wick < IchimokuAArray[0] && low_wick < IchimokuAArray[0]))) && 
   (high_wick < MA50Array[0] && low_wick < MA50Array[0]))
   {
      signal = "sell";
      
   }
   
   // Below is when EA exit trade when price closes above the 50MA
   if((((MA50Array[0] < IchimokuAArray[0] || MA50Array[0] > IchimokuBArray[0]) || (MA50Array[0] < IchimokuBArray[0] || MA50Array[0] > IchimokuAArray[0]))) && 
   (close_Price > MA50Array[0]))
   {
      signal = "exit_trade";
   }
      
   if(signal == "exit_trade" && PositionsTotal() > 0)
   CloseAllSellPositionsThisPair();
   
   if(signal == "sell" && PositionsTotal() == 0)
   trade.Sell(0.01,NULL,Bid);

   
   
}      

void CloseAllSellPositionsThisPair(){
   for(int i = PositionsTotal()-1; i >= 0; i--){
      int ticket = (int)PositionGetTicket(i);
      string CurrencyPair = PositionGetSymbol(i);
      
      int PositionDirection = (int)PositionGetInteger(POSITION_TYPE);
      
      if(PositionDirection == POSITION_TYPE_SELL)
      
      if(_Symbol == CurrencyPair){
         trade.PositionClose(ticket);
      }
   }
}
 

Your code is really not good, inappropriate use of indicator handle (should be initialized in OnInit), no error checking, bad usage of PositionXXX functions.

You were lucky it did something useful in the past.

 
Alain Verleyen #:

Your code is really not good, inappropriate use of indicator handle (should be initialized in OnInit), no error checking, bad usage of PositionXXX functions.

You were lucky it did something useful in the past.

I'm still learning and developing it though. Thanks a lot for the input. Much appreciated. I will improve on it as I advance further. But what about the trade not exiting below or above the moving average. Does it still have to do with my code? Because it worked before.

 
Hi! I don’t really understand that… if anybody can enlighten me… how come OnSell() function is called in Ontick() function… but Onsell() contains everything it is in Ontick()… why?