EA does not trade live

 

When I tried to trade on demo account my codes do not place trades.

However, everything seems fine on Strategy Tester.

No errors in Expert Tab or Logs. Yes the code compiles


//+------------------------------------------------------------------+
//|                                               arek_radek_001.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\Trade.mqh>

CTrade         m_trade;
//---
double      MA[];                // array for iMA indicator values
//---- handles for indicators
int         MA_handle;
datetime lastClose;
double open;
double close;
double Ask;
double Bid;
double   high;
double   low;
double TakeProfit;
int num;
static datetime waiting;
// trading object
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
bool IsNewCandle()
  {
   if(iTime(_Symbol,PERIOD_M15,1) == lastClose)
     {
      return (false);
     }
   if(iTime(_Symbol,PERIOD_M15,1)!=lastClose)
     {
      lastClose=iTime(_Symbol,PERIOD_M15,1);
      return(true);
     }
   return(false);
  }

bool IsNewBar() { 
   static   datetime lastBar;
            datetime currBar  =  iTime(Symbol(), Period(), 0);
   
   if(lastBar != currBar) {
      lastBar  =  currBar;
      return (true); 
   } else {
      return(false);
   }
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnInit()
  {
   


   MA_handle=iMA(_Symbol,PERIOD_M5,28,0,MODE_EMA,PRICE_CLOSE);


   


  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {

   CopyBuffer(MA_handle,0,0,1,MA);
   ArraySetAsSeries(MA,true);
   open = iOpen(NULL,0,0);
   close =  iClose(_Symbol,PERIOD_CURRENT,0);
   Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   high  = iHigh(Symbol(),Period(),0);
   low   = iLow(Symbol(),Period(),0);
   double current_high = iHigh(_Symbol, PERIOD_M5, 0);
   double current_low = iLow(_Symbol, PERIOD_M5, 0);
   double previous_high = iHigh(_Symbol, PERIOD_M5, 1);
   double previous_low = iLow(_Symbol, PERIOD_M5, 1);
   double previous_previous_high = iHigh(_Symbol, PERIOD_M5, 2);
   double previous_previous_low = iLow(_Symbol, PERIOD_M5, 2);
   double TakeProfit = 400;
   double StopLoss = 1200;
   double TakeProfitLevelBUY = Ask + TakeProfit*Point();   

   double StopLossLevelBUY = Ask - StopLoss*Point();
   
   double TakeProfitLevelSELL = Bid - TakeProfit*Point();   

   double StopLossLevelSELL = Bid + StopLoss*Point();
   static datetime dtBarCurrent   = WRONG_VALUE;
   datetime dtBarPrevious  = dtBarCurrent;
   dtBarCurrent   = (datetime) SeriesInfoInteger(_Symbol, _Period, SERIES_LASTBAR_DATE);
   bool     boolNewBarFlag = (dtBarCurrent != dtBarPrevious);

   double previous_mean = (previous_high+previous_low)/2;
   double current_mean = (current_low+current_high)/2;      
     

   if(previous_low<MA[0] && current_low>MA[0]  && Ask > MA[0] && TimeToTrade() )
     {
      if(IsNewCandle()){
      m_trade.Buy(1,_Symbol,0,StopLossLevelBUY,TakeProfitLevelBUY,NULL);
      }

     }
   if(previous_high>MA[0] &&current_high <MA[0]  && Bid < MA[0] &&TimeToTrade() )
     {
      if(IsNewCandle()){ 
         m_trade.Sell(1,_Symbol,0,StopLossLevelSELL,TakeProfitLevelSELL,NULL);
      }
     }


  }

bool TimeToTrade()
{
   MqlDateTime dtTimeCurrent;
   TimeCurrent(dtTimeCurrent);
   if(dtTimeCurrent.hour >= 9 && dtTimeCurrent.hour < 17)
      return(true);
   return(false);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Buying(double lot,int magic,int stop,int takeProfit)
  {
//int a = Buy(_Symbol,OP_BUY,FixedLotSize,Ask,50,Low[0],NormalizeDouble(Ask+TakeProfit*5*_Point,_Digits),NULL,magic,0,Green);
   return;


  }
//+------------------------------------------------------------------+
void Selling(double lot,int magic,int stop,int takeProfit)
  {
//int b =  Sell(_Symbol,OP_SELL,FixedLotSize,Bid,50,High[0],NormalizeDouble(Bid-TakeProfit*5*_Point,_Digits),NULL,magic,0,Red);
   return;

  }

//+------------------------------------------------------------------+
 
Polandball:D Me:

When I tried to trade on demo account my codes do not place trades.

However, everything seems fine on Strategy Tester.

No errors in Expert Tab or Logs. Yes the code compiles


I played around with your code, rewriting it as follows for easier debugging:

   timeToTrade = TimeToTrade() ;
   isCandleNew = IsNewCandle();
   if(isCandleNew)
     {
      DebugBreak();
      if(previous_low < MA[0] && current_low > MA[0]  && Ask > MA[0] && timeToTrade)
        {
         m_trade.Buy(1,_Symbol,0,StopLossLevelBUY,TakeProfitLevelBUY,NULL);
         DebugBreak();
        }
      if(previous_high > MA[0] && current_high < MA[0]  && Bid < MA[0] && timeToTrade)
        {
         m_trade.Sell(1,_Symbol,0,StopLossLevelSELL,TakeProfitLevelSELL,NULL);
         DebugBreak();
        }
     }

Your IsNewCandle() is working fine, so I commented it out to make it easier to debug the remaining statements.

I then commented out your other if statements to check the m_trade functions - they seem to work fine and are creating trades.

So the issue is that your next if statements are rarely evaluating to true (if ever - they never did during my tests) - that's why you are not getting trades.

      if(previous_low < MA[0] && current_low > MA[0]  && Ask > MA[0] && timeToTrade)
        {
        }
      if(previous_high > MA[0] && current_high < MA[0]  && Bid < MA[0] && timeToTrade)
        {
        }

As they are complicated if statements with 4 terms (16 possible outcomes of which only 1 can be true), it can be hard to analyse. So I suggest breaking these down into simpler steps and debugging to figure out how to fix the logic to what you want it to be.

I would suggest removing TimeToTrade() for the time being so you can focus on fixing the high/low Ask/Bid comparisons in isolation.

My leaning would be to separate the timing conditions from the financial ones

   if((isCandleNew) && (timeToTrade))
     {
        //financial computations
     }
 
Your IsNewCandle is at the start of a new M15. Your MA[0] condition is on the M5 chart. Unlikely to both be true at the same time.
Reason: