Unable to Execute buy orders at broker terminal

To add comments, please log in or register
EugeneCT
44
EugeneCT  

Dear All, would need some advice for my codes as im trying to write an EA for Executing a Buy order whenever the previous Lower High is broken and vice-versa for Sell Order.

The code works and orders are executed as expected in default MT5 terminal.

However the issue is when i started to backtest at broker terminal it seems to not executing the Buy or Sell order although the Sell Function/Buy Function is called.
Somehow weird as similar code(CExpert.Buy) with different strategy had been tested with same broker and it works.

Would appreciate some enlightenment. My code is as attached.

#include <Trade\Trade.mqh>                 //// Import Trade.mqh library
#include <Expert\Expert.mqh>
CExpert expert;
CTrade trade;
                

//////////////////////////////////////////////////////////////////////////////////////////
/////////// EA General Variable  /////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
#property copyright "Copyright 2020, Eugene"
#property link      ""
#property version   "1.00"





    
   
////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////// PRICE ACTION //////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
  
/////   Input Controls for EA Optimization    
input ENUM_TIMEFRAMES PACandle_Period=PERIOD_H4;
input int Candle_Nos=12;            /// Number of History Candles Check
input double Pips_Difference=10;    /// Number of pips to differenciate Higher Low and Double Btm
input double Stoplosspips=10;       /// Stop Loss by number of Pips from Highest (Reverse from High)
input double Takeprofitpips=10;     /// Take profits by number of Pips from Lowest (Positive= away from extreme)
input double RiskPercentage=1;      /// Risk per trade

static int NumberOfBuyPositions=0,NumberOfSellPositions=0;

/// Higher Low Variable
double Higher_Low=0;     /// Most Recent Higher Low Value(Close of Bearish Candle)
double Higher_Low2=0;    /// Low2 = Low, Low = New Low (2nd most recent)

static double Lower_High=0;  /// Most Recent Lower High Value(Close of Bullish Candle)
static double Lower_High2=0; /// High2=High, High=New High (2nd most recent)


//////////////////////////////////////////////////////////////////////////////////////////
/////////// On Initialization  /////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
void OnInit()
 {


 }
 
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////


void OnTick()

{     

   ///ZigZag_Indicator(H_Series,L_Series);
   Candle_PA();

  

 }   
 

  
  
  
        
 /////////////////////////////////////////////////////////////////////////
 ////////////////////  Candle PA Function    ////////////////////////////
 /////////////////////////////////////////////////////////////////////////
   
  void Candle_PA()
  {
      ///const int Candle_Nos = 12;   /// Candle Nos to record
      
///////////  OHLC Array Variable  ////////////////////////////////////////////////////////////
      double Open[];
      double Close[];
      double High[];
      double Low[];
      datetime Time[];
      double Candle[];  ///Bullish=1,Engulfing Bullish=2,
                        ///Bearish=-1,Engulfing Bearish=-2
                        ///Doji=0;
                 
     
      static double Previous_Candle_Open=0;
      ///static double Previous_Candle_Close=0;
      double Current_Candle_Open=iOpen(_Symbol,_Period,0);
     /// double Current_Candle_Close=iClose(_Symbol,_Period,0);
     
      ArrayResize(Open,Candle_Nos);
      ArrayResize(High,Candle_Nos);
      ArrayResize(Close,Candle_Nos);
      ArrayResize(Low,Candle_Nos);
      ArrayResize(Time,Candle_Nos);
      ArrayResize(Candle,Candle_Nos);
      
 
 
     
///////////  SwingPoint Strategy Variable  ///////////////////////////////////////////////////////

/// Obtaining Highest and Lowest close of Past 10 candles
int High_10_Index=iHighest(_Symbol,_Period,MODE_CLOSE,10,1); /// shift =0 for live update
int Low_10_Index=iLowest(_Symbol,_Period,MODE_CLOSE,10,1);   /// shift = 0 for live update
double High_10=Close[High_10_Index-1];                         /// Obtain Hisghest Close of Last 10 Close
double Low_10=Close[Low_10_Index-1];                           /// Obtain Lowest Close of Last 10 Close
///Print ("High 10 = ",High_10,"Low 10= ",Low_10);


int shift = Candle_Nos-1;                        /// shift for obtaining 
Higher_Low=0;
Higher_Low2=0;
Lower_High=0;
Lower_High2=0;

string SwingPointTrend=NULL;           /// Record the current SwingPointtrend as up or down, probaly useless, KIV
static double SwingPoint_1UP=0;        /// Uptrend Most Recent Swing Point
static double SwingPoint_2UP=0;        /// Uptrend 2nd Most Recent Swing Point
static double SwingPoint_1DN=0;        /// Downtrend Most Recent Swing Point 
static double SwingPoint_2DN=0;        /// Downtrend 2nd Most Recent Swing Point
static bool Higher_Low_Pattern=0;      /// If SP1>SP2,Close[1]>Close[j] && >=iHighest, return 1. SignalTime recorded. Requisite for UPSwingPoint Break.
static bool Lower_High_Pattern=0;      /// When 1, SignalTime recorded. Requisite for DNSwingPoint Break.
static datetime SignalTimeExpire=0;                /// Recorded Datetime when Pattern triggered. Set Time limit for signal life,cause pattern wont change in sideways.

static bool UPSwingPointBreak=NULL;    /// Check if Break of Uptrend SwingPoint happened. /\
static bool DNSwingPointBreak=NULL;    /// Check if Break of Downtrend SwingPoint happened. \/

////  Create Vertical line for 10 candle

datetime Line10Bar = iTime(_Symbol,_Period,10);
static datetime PCandle=0;



if (PCandle!=iTime(_Symbol,_Period,0))
   {
      ObjectDelete(_Symbol,"10 bar Line");
      ObjectCreate(_Symbol,"10 bar Line",OBJ_VLINE,0,Line10Bar,0);
      PCandle=iTime(_Symbol,_Period,0);
   }

          
          
         

 
///////////  Assigning OHLC Array  /////////////////////////////////////////////////////////////////////
      
      for (int i=Candle_Nos-1;i>=0;i--)
         ///if ((Current_Candle_Open!=Previous_Candle_Open) && (Current_Candle_Close!=Previous_Candle_Close))
         {
         
         
         High[i]=iHigh(_Symbol,_Period,i);
         Low[i]=iLow(_Symbol,_Period,i);
         Close[i]=iClose(_Symbol,_Period,i);
         Open[i]=iOpen(_Symbol,_Period,i);
         Time[i]=iTime(_Symbol,_Period,i);
 
         ///Print("Open=",Open[i],",Close= ",Close[i]," , High= ",High[i],", Low = ",Low[i]);
         }
         
         
  ///////////  Identify Candle  /////////////////////////////////////////////////////////////////////
      for (int i=Candle_Nos-1;i>=0;i--)
         {
           if (Open[i]<Close[i])
            Candle[i]=1;
           else if (Open[i]>Close[i])
            Candle[i]=-1;
           else if (Open[i]==Close[i])
            Candle[i]=0;
            
         }
         
       

  ///////////  Identify Swing Point  /////////////////////////////////////////////////////////////////////
BearCloseSearch(shift,Close,Candle);   /// Find most recent Close of Bearish Candle
///Print ("Highest Low of 10 bar = ",Higher_Low);
BullCloseSearch(shift,Close,Candle);   /// Find most recent Close of Bullish Candle
//Print ("Lowest High of 10 bar = ",Lower_High); 
double PD=Pips_Difference/10000;  
int j = Candle_Nos-1;
static datetime IsNewCandle=0;
static double SignalHighest=0;
static double SignalLowest=0;
 
if (Time[0]!=IsNewCandle)
   {
      
         if (Close[1]>(Close[j]+PD) && Close[1]>=High_10  && Close[j]<=Low_10 && Higher_Low>(Higher_Low2+PD) && Higher_Low2>0 )
            {
               ObjectDelete(_Symbol,"SwingPointDN");
               ObjectDelete(_Symbol,"SwingPointUP");   
               Print("Low10= ",Low_10,", High 10 = ",High_10);
               SwingPoint_1UP=Higher_Low;
               SwingPoint_2UP=Higher_Low2;
               SwingPointTrend="UP";
                ObjectCreate(_Symbol,"SwingPointUP",OBJ_HLINE,0,0,SwingPoint_1UP);
               Comment("UpTrend, Wait Retrace");
               Higher_Low_Pattern=true;
               Lower_High_Pattern=false;
               SignalTimeExpire=Time[0];
               SignalHighest=High_10;
               SignalLowest=Low_10;
                     Print("Signal Lowsest = ", SignalLowest);
               Print("Signal Highsest = ", SignalHighest);
               Print("Time = ", SignalTimeExpire);
          
               Print ("Higher Low Exist? -- ",Higher_Low_Pattern);
               Print ("Lower High Exist? -- ",Lower_High_Pattern);
    
             }
          
            
       
       
      
         if (Close[1]<(Close[j]-PD) && Close[1]<=Low_10 && Close[j]>=High_10 && Lower_High<(Lower_High2-PD) && Lower_High2>0)
            {
               ObjectDelete(_Symbol,"SwingPointDN");
               ObjectDelete(_Symbol,"SwingPointUP");
               Print("Low10= ",Low_10,", High 10 = ",High_10);
               SwingPoint_1DN=Lower_High;
               SwingPoint_2DN=Lower_High2;
               SwingPointTrend="DN";
               ObjectCreate(_Symbol,"SwingPointDN",OBJ_HLINE,0,0,SwingPoint_1DN);
               Comment("DownTrend, Wait Retrace");
               Lower_High_Pattern=true;
               Higher_Low_Pattern=false;
               SignalTimeExpire=Time[0];
               SignalHighest=High_10;
               SignalLowest=Low_10;
               Print("Signal Lowsest = ", SignalLowest);
               Print("Signal Highsest = ", SignalHighest);
               Print("Time = ", SignalTimeExpire);
               Print ("Higher Low Exist? -- ",Higher_Low_Pattern);
               Print ("Lower High Exist? -- ",Lower_High_Pattern);
        
            
           }
             

    
Comment ("Higher Low Exist?  ",Higher_Low_Pattern, "/n Lower High Exist?  ",Lower_High_Pattern);
///Print ("Higher Low2 (SP) = ",SwingPoint_2UP,"Higher Low(SP) = ",SwingPoint_1UP);
///Print ("Higher Low2 = ",Higher_Low2,"Higher Low = ",Higher_Low);

///Print ("Lower High2 (SP) = ",SwingPoint_2DN,"  Lower High (SP)= ",SwingPoint_1DN);
///Print ("Lower High 2 = ",Lower_High2,  " Lower High = " ,Lower_High);

  ///////////  Identify Buy and Sell Signal  /////////////////////////////////////////////////////////////////////
  
  int BarIndex=iBarShift(_Symbol,_Period,SignalTimeExpire,true);

  
  if (BarIndex<=10 && Lower_High_Pattern==true && Close[1]> SwingPoint_1DN)
     {
      DNSwingPointBreak=true;
      Print("Break SwingPoint!" , DNSwingPointBreak);
      BuyAction(SignalHighest,SignalLowest);
     }
  else if (BarIndex<=10 && Higher_Low_Pattern==true && Close[1]< SwingPoint_1UP)
     {
      UPSwingPointBreak=true;
      Print("Break SwingPoint!" , UPSwingPointBreak);
      SellAction(SignalHighest,SignalLowest);
     }
   
   
    IsNewCandle=Time[0];
   
   }


    


 }
 /////////////////////////////////////////////////////////////////////////
 ////////////////////  Finding Higher Low   ////////////////////////////
 /////////////////////////////////////////////////////////////////////////
   void BearCloseSearch(int shift,double &Close[],double &Candle[])
{
   
   for (int i=shift-1;i>0;i--)
      if (Candle[i]==-1)
      {
         if (Candle[i+1]==-1)
         {  
          
            Higher_Low=Close[i];
         }
         else if (Candle[i+1]!=-1)
         {
            Higher_Low2=Higher_Low;
            Higher_Low=Close[i];
         }
      }
      
      
} 
  
    
    void BullCloseSearch(int shift,double &Close[],double &Candle[])
   {

   for (int i=shift-1;i>0;i--)
      if (Candle[i]==1)
      {
         if (Candle[i+1]==1) 
          {  
             
             Lower_High=Close[i];
           
           }
        else if (Candle[i+1]!=1) 
           {  
            Lower_High2=Lower_High;
            Lower_High=Close[i];
           }
          } 
            
            
            
      
 
      
    }
      

   void BuyAction(double SignalHighest, double SignalLowest)
   {
    double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);    /// Get Ask Price
    double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
    double Balance=AccountInfoDouble(ACCOUNT_BALANCE);                           /// Get Account Balance
    double Equity=AccountInfoDouble(ACCOUNT_EQUITY);                             /// Get Account Equity
    
    int CurrentBuyOrders = PositionsTotal();        
                            

    
    double SL=0;
    double TP=0;
    
    SL=SignalLowest-(Stoplosspips/10000);
    TP=SignalHighest+(Takeprofitpips/10000);
        
     Print ("Sell Action!");
        /// Print ("CurrentBuyOrders!", CurrentBuyOrders);   
       double lot=NormalizeDouble(RiskPercentage*Balance/((Ask-SL)*10000*1000),2);     
  
   if (CurrentBuyOrders<10)         
      {
         trade.Buy(lot,NULL,Ask,(Ask-0.002),Ask+0.004);
      }
   
   
   }
   
   
   void SellAction(double SignalHighest, double SignalLowest)
   {
    double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);    /// Get Ask Price
    double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
    double Balance=AccountInfoDouble(ACCOUNT_BALANCE);                           /// Get Account Balance
    double Equity=AccountInfoDouble(ACCOUNT_EQUITY);                             /// Get Account Equity
    
    static int OpenBuyOrders =0,OpenSellOrders=0;                                      
    static int NumberOfBuyPositions=0,NumberOfSellPositions=0;
    
    double SL=0;
    double TP=0;
    
    SL=SignalHighest+(Stoplosspips/10000);
    TP=SignalLowest-(Takeprofitpips/10000);
    int CurrentSellOrders = PositionsTotal();   
     Print ("Sell Action!");      
     /// Print ("CurrentSellOrders!", CurrentSellOrders);   
       double lot=NormalizeDouble(RiskPercentage*Balance/((SL-Bid)*10000*1000),2);     
   if (CurrentSellOrders<1)          
      {
         trade.Sell(lot,NULL,Bid,SL,TP);
      }
        
        }


      


 

William Roeder
22447
William Roeder  
EugeneCT: Would appreciate some enlightenment. 
  1. static int NumberOfBuyPositions=0,NumberOfSellPositions=0;
    ⋮
    static double Lower_High=0;  /// Most Recent Lower High Value(Close of Bullish Candle)
    static double Lower_High2=0; /// High2=High, High=New High (2nd most recent)
    
    
    Do you realize that all global variables are static?

  2.       double Close[];
    ⋮
          ArrayResize(Close,Candle_Nos);
    ⋮
    double High_10=Close[High_10_Index-1]; /// Obtain Hisghest Close of Last 10 Close
    What values to you think your array has?
EugeneCT
44
EugeneCT  
Ok, apparently the Array[0] and Array[1] value is not updated.

Problem Solved after declaring all the OHLC Arrays in static. Its odd that the codes works in default MT5 Terminal at the first place.

Many thanks William and have a good day!
To add comments, please log in or register