Download MetaTrader 5

Time Trader EA - CFD issue on FxPro broker

To add comments, please log in or register
Chris1303
78
Chris1303  

Hi All,

I have an EA that opens trade at a specified time.

It is working well on currencies.

However I have a problem with CFDs.

Curious is that if I use Strategy Tester, EA works perfectly.

But when I set EA on my demo account to forward test it, it is not opening trades.

No errors and no warnings in the code. It is not about stop level - tried 100 tp/sl to be sure. Money is also not a problem. 4% margin, min 10 shares, AT&T shares, 900$ demo account.

Could you please advise what should I check and what can be the root cause of such issue?

I tried to forward test it on AT&T shares.

Please find below code:

 

  

//+------------------------------------------------------------------+
//|                                                                  |
//|                         TIME TRADER                              |
//|                                                                  |
//+------------------------------------------------------------------+

//---- input parameters

extern double   Lots=10;
extern int      TakeProfit=100;
extern int      StopLoss=100;
extern int      Slip=5;
extern string   TradeSettings="Mt4 time(min-max): hours 0-23, minutes 0-59";
extern bool     AllowBuy=true;
extern bool     AllowSell=false;
extern int      TradeYear=2016;
extern int      TradeMonth=1;
extern int      TradeDay=12;
extern int      TradeHour=18;
extern int      TradeMinutes=10;
extern int      TradeSeconds=0;
extern string   MagicNumbers="To be changed in case of conflict with other EAs";
extern int      BuyMagicNumber =10001;
extern int      SellMagicNumber =10002;
extern string Aotl="-------------------------------------";//--
extern string artl= "Configuración Trailing"; 
extern string A1tl="-------------------------------------";//--
 string ttrailing=" 0 no 1 cl 2 hl 3 ema 4 atr"; 
 int   tipotrailing  =1; 

 extern bool   activartrailing  =false;  
  
 bool   AllPositions  =false;         
extern   bool   ProfitTrailing=false; 
extern int    TrailingStep  =1; 
extern int TrailingStop=5;
extern int Trailingstar=10;        

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert starts                                  |
//+------------------------------------------------------------------+

int start()
  {
//----
int StopMultd,Sleeper=1;



int digits=MarketInfo(Symbol(),MODE_DIGITS);
StopMultd=1;
double TP=TakeProfit;
double SL=StopLoss*StopMultd;
int Slippage=Slip*StopMultd;

// Calculate stop loss
double slb=Ask-SL*Point;
double sls=Bid+SL*Point;

// Calculate take profit
double tpb=Ask+TP*Point;
double tps=Bid-TP*Point;

//-------------------------------------------------------------------+
//Check open orders
//-------------------------------------------------------------------+
if(OrdersTotal()>0){
  for(int i=1; i<=OrdersTotal(); i++)          // Cycle searching in orders
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
        {
          if(OrderMagicNumber()==BuyMagicNumber&& OrderSymbol()==Symbol()) {int halt1=1;}
          if(OrderMagicNumber()==SellMagicNumber&& OrderSymbol()==Symbol()) {int halt2=1;}
        }
     }
}
//-------------------------------------------------------------------+


if((halt1!=1)&&(AllowBuy==true)){// halt1

// Buy criteria
if ((TradeHour==Hour())&&(TradeMinutes==Minute())&&(TradeSeconds==Seconds())&&(TradeYear==Year())&&(TradeMonth==Month())&&(TradeDay==Day())) //Signal Buy
 {
   int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"time trader buy order ",BuyMagicNumber,0,Blue);
   if(openbuy<1){int buyfail=1;}
 }
 
}// halt1
 
if((halt2!=1)&&(AllowSell==true)){// halt2
RefreshRates();
 // Sell criteria
 if ((TradeHour==Hour())&&(TradeMinutes==Minute())&&(TradeSeconds==Seconds())&&(TradeYear==Year())&&(TradeMonth==Month())&&(TradeDay==Day())) //Signal Sell
 {
   int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"time trader sell order ",SellMagicNumber,0,Green);
   if(opensell<1){int sellfail=1;}
 }
 
}// halt2

//-----------------------------------------------------------------------------------------------------
if(OrdersTotal()>0){
  for(i=1; i<=OrdersTotal(); i++){          // Cycle searching in orders
  
      if (OrderSelect(i-1,SELECT_BY_POS)==true){ // If the next is available
        
          if((OrderMagicNumber()==BuyMagicNumber)&& OrderSymbol()==Symbol()&&(OrderTakeProfit()==0||OrderStopLoss()==0)) { bool m1=OrderModify(OrderTicket(),0,slb,tpb,0,CLR_NONE); }
          if((OrderMagicNumber()==SellMagicNumber)&& OrderSymbol()==Symbol()&&(OrderTakeProfit()==0||OrderStopLoss()==0)) {bool m2= OrderModify(OrderTicket(),0,sls,tps,0,CLR_NONE); }

        }
     }
}

 if (activartrailing){
 
     for( i=0; i<OrdersTotal(); i++) 
     {
        if (OrderSelect( i, SELECT_BY_POS, MODE_TRADES)) 
        {
           if (AllPositions || OrderSymbol()==Symbol()) 
           { //
          if (OrderOpenPrice()+(Trailingstar*Point)<Ask&&OrderType()==OP_BUY) {TrailingPositions();
         }
           if (OrderOpenPrice()-(Trailingstar*Point)>Bid&&OrderType()==OP_SELL) {TrailingPositions();
          }
        
           
         
           }
        }
        
        
     }
 }
//-------------------------------------------------------------------+
// Error processing
//-------------------------------------------------------------------+
if(buyfail==1||sellfail==1){
int Error=GetLastError();
  if(Error==130){Alert("Wrong stops. Retrying."); RefreshRates();}
  if(Error==133){Alert("Trading prohibited.");}
  if(Error==2){Alert("Common error.");}
  if(Error==146){Alert("Trading subsystem is busy. Retrying."); Sleep(500); RefreshRates();}

}


//-------------------------------------------------------------------
   return(0);
  }
//+-----------------------------------


void TrailingPositions() 
  {
   double pBid;
    double pAsk; 
    double pp,seguridadtl;
if (TrailingStop<MarketInfo(Symbol(),MODE_STOPLEVEL)&&OrderSymbol()==Symbol())TrailingStop=MarketInfo(Symbol(),MODE_STOPLEVEL);
   pp=MarketInfo(OrderSymbol(), MODE_POINT);
     if (OrderType()==OP_BUY) 
     {
      pBid=MarketInfo(OrderSymbol(), MODE_BID);
        if (!ProfitTrailing || (pBid-OrderOpenPrice())>TrailingStop*pp) 
        {
           if (OrderStopLoss()<pBid-(TrailingStop+TrailingStep-1)*pp) 
           {seguridadtl=NormalizeDouble(pBid-TrailingStop*pp,Digits);
           if (pBid-TrailingStop*pp >pBid-(MarketInfo(Symbol(),MODE_STOPLEVEL)*pp)) seguridadtl=NormalizeDouble((pBid-MarketInfo(Symbol(),MODE_STOPLEVEL)*pp),Digits);
          if (OrderStopLoss() != seguridadtl)  ModifyStopLoss(seguridadtl);
            return;
           }
        }
     }
     if (OrderType()==OP_SELL) 
     {
      pAsk=MarketInfo(OrderSymbol(), MODE_ASK);
        if (!ProfitTrailing || OrderOpenPrice()-pAsk>TrailingStop*pp) 
        {
           if (OrderStopLoss()>pAsk+(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0) 
           {seguridadtl=NormalizeDouble(pAsk+TrailingStop*pp,Digits);
           if (pAsk+TrailingStop*pp <pAsk+(MarketInfo(Symbol(),MODE_STOPLEVEL)*pp)) seguridadtl=NormalizeDouble((pAsk+MarketInfo(Symbol(),MODE_STOPLEVEL)*pp),Digits);
          if (OrderStopLoss() != seguridadtl)  ModifyStopLoss(seguridadtl);
            return;
           }
        }
     }
  }
  //  StopLoss 
//+------------------------------------------------------------------+
  void ModifyStopLoss(double ldStopLoss) 
  {
   bool fm;
   fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,Black);

  }
        
Keith Watford
Moderator
10614
Keith Watford  
if(OrdersTotal()>0){
  for(int i=1; i<=OrdersTotal(); i++)          // Cycle searching in orders

int i  initial value should be 0

 

if ((TradeHour==Hour())&&(TradeMinutes==Minute())&&(TradeSeconds==Seconds())&&(TradeYear==Year())&&(TradeMonth==Month())&&(TradeDay==Day()))

 If you don't get a tick at that exact second, the code will not be executed

Carl Schreiber
7364
Carl Schreiber  
Do you know the time shift of backtest data and broker data?
Chris1303
78
Chris1303  

GumRai,

 

Thank you for your reply. I will try to change int i initial value to 0.

If it fails, I will try to delete TradeSeconds parameter. Then, if I understand it correctly, there need to be a tick in this exact minute to execute a trade. It is not a problem if we are talking about currencies but when we are talking about shares, there also could be a minute without a tick.

Is it possible to make EA independent from getting a tick to execute a trade?

 

gooly,

 

Thank you also for your reply.

I don`t know if I understand it correctly but I have backtested EA using MT4 dedicated for FxPro (downloaded from broker site). Broker time zone is GMT+2. Historical data is also downloaded from FxPro servers so I think there is no time shift change between backtest data and broker data. Please correct me if I`m wrong. 

Keith Watford
Moderator
10614
Keith Watford  
Is it possible to make EA independent from getting a tick to execute a trade?
It may be possible by using OnTimer(), but I have never had any need to do something at a precise time. I have always found execution at the first incoming tick after the time to be good enough.
Chris1303
78
Chris1303  

I understand. 

I have made changes that you advised previosly and CFD trades are opening correctly now.

I still assume it must be a tick through the whole minute to execute a trade. I think it is a rare situation to have no tick for the whole minute on US stocks however it happens.

If I face with significant percentage of lack of trade execution then I will experiment with OnTimer() function.

Thank you for you help!

I appreciate it. 

To add comments, please log in or register