very strange problem

 

Hi All, I seem to be having a very strange problem with metatrader, i don't know if it's my broker or what, but i can't begin to imagine what it is. I have an EA that works sometimes, but not others. I wrote it myself so i know what's in it. I put it on and it might open the 2 pending orders it's meant too, but then maybe the next time, it either doesn't open any orders, or sometimes it continually opens orders and ignores the code limiting the orders opened. Sometimes when I download another instance of Metatrader, it starts working again, then stops again. Sometimes opening another instance of Metatrader doesn't help at all. The strange thing is that with the last broker, this same thing happened. The same code works every time on a demo account, it's only when i try it on a live account do these things start happening. Another thing is, to try to diagnose the problem i wrote a simple script to try to open an order and now it says can't open order because trading is disabled. But manual trading works. Also, again, the same open order script works on a demo account.

Does anyone have any idea what might be going on? What could cause code to work perfectly fine on a demo account, but then start, basically glitching, on a live account? and for the same thing to happen with two brokers?


I really appreciate any suggestions, or something i can try.


Thank you

 

By the way, i condensed the code to check it. this should limit the orders opened. Again it works on the demo account, but on the live account it just keeps opening orders.


//+------------------------------------------------------------------+
//|                                                   timer test.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
   int magic_number = 101;
   int TotalOrderCount(){
     int count=0;
     for(int pos = OrdersTotal(); pos >= 0 ; pos--) if (
         OrderSelect(pos, SELECT_BY_POS)                       
     &&  OrderSymbol()       == Symbol() && OrderMagicNumber() == magic_number){            
         count++;
      }
      return(count);
   }

int OnInit()
  {
//--- create timer

   EventSetTimer(1);



     
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
     
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   datetime time1 = D'2018.09.04 04:56:00';
  
   bool gbpusd1 = true;
  
   int from_price = 90;
  
   int SL = 30;
  
   int TP = 2000;
  
   int before_that_time = 3;
  
   double finish_time = 5;
  
   double lots =  0.01;
  
   datetime start_time = time1 - before_that_time;
  
   bool open_orders;
  
   if (TotalOrderCount() < 1) open_orders = true;

   double sell_ticket;
   double buy_ticket;
  
   if ((TimeCurrent() >= start_time && TimeCurrent() <= time1 + finish_time && gbpusd1) && open_orders)
   {
    double buy_ticket = OrderSend("GBPUSDp",OP_BUYSTOP,lots,Ask+Point*from_price,900,Ask-Point*SL,Ask+Point*TP,NULL,101,clrNONE);
    double sell_ticket = OrderSend("GBPUSDp",OP_SELLSTOP,lots,Bid-Point*from_price,900,Bid+Point*SL,Bid-Point*TP,NULL,101,clrNONE);
   }
  
    if (TimeCurrent() >= time1 + 30 && TimeCurrent() <= time1 + 47)
    {
    for(int cnt=OrdersTotal();cnt>=0;cnt--)
    {
    if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false )
    continue;
    if(OrderType() == OP_BUY)
    OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),300,clrNONE);
    else if(OrderType() == OP_SELL)
    OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),300,clrNONE);
    else if(OrderType() == OP_SELLSTOP)
    OrderDelete(OrderTicket(),clrNONE);
    else if(OrderType() == OP_BUYSTOP)
    OrderDelete(OrderTicket(),clrNONE);
    }   
          
    }
  
  }
//+------------------------------------------------------------------+

 
Check in the terminal whether here: Ctrl-O => tab Expert => Allow autom. Tarding is enabled!
 
yes, trading is enabled
 
Drew Clayman:

By the way, i condensed the code to check it. this should limit the orders opened. Again it works on the demo account, but on the live account it just keeps opening orders.


Even if it is a condesned version of your code, you have NO Error Handling procedures. How would you know what went wrong?


Demo environment is the Perfect World. Real Account is Chaos... You need to handle EVERY exception, otherwise you will loose money.
 
I rented the <> and and <> I have tried in vain but this just do not download although all the tipu free products download no problem and work no problem. I bought about two weeks ago. what can I do. anyone can help me ??
 
ismail cole:
I rented the <> and and <> I have tried in vain but this just do not download although all the tipu free products download no problem and work no problem. I bought about two weeks ago. what can I do. anyone can

Try another machine

 

you should read the manuel

https://docs.mql4.com/eventfunctions/eventsettimer

EventSetTimer - Working with Events - MQL4 Reference
EventSetTimer - Working with Events - MQL4 Reference
  • docs.mql4.com
The function indicates to the client terminal, that for this indicator or Expert Advisor, events from the timer must be generated with the specified periodicity. Normally, this function must be called from the OnInit() function or from a class constructor. In order to handle events coming from the timer, the Expert Advisor must have the...
 

Your ea just send too frequent orders so you should put a pause in your code .If you continue to use this ea ,the server will no longer accept any orders from your ea .

There are 2 solutions :

1 .Use the fonction Sleep

int start()

{

Sleep(2*60*1000); // The number in the brackets is the period of pause in milliseconds .Here it's 2 min I recommand at least

RefreshRates();


return(0);

}


2.Start your EA every given period (1 min,5min,15min,30min ...and so on) depending on the timeframe


datetime LastStartTime;

int start()

{

if(LastStartTime!=Time[0])

{

LastStartTime=Time[0];

// Put your entire code from here


}

return(0);

}

Reason: