No Trades Can Be Opened on Demo and Real Accounts

Yasar Sari  

Hello friends, I mentioned earlier that I am working on a strategy that opens a buy trade on every drop. I supported Thank you to everyone who helped. The system works smoothly in strategy mode, but does not open trading on demo and real account. Can experts examine the codes and tell where the error is? Thank you from now. In all codes section;

//include the trade Library
#include <Trade\Trade.mqh>


input int                              starting_TP                   =  500;     //50 point tp for first position
input int                              next_TP                       =  750;     //75 point tp for subsequent positions
input int                              minimum_Drop_inPrice          =  500;     //50 point drop for second pos
input double                           starting_lot                  =  0.1;     //starting lot size
input double                           lot_multiplier                =  1.1;     //multiplier for lot size for new positions

input double                           min_Equity_to_maintain        =  1;       // if equity falls below this, ea will terminate


double starting_Equity, current_Equity;


//--- Service Variables (Only accessible from the MetaEditor)
CTrade myTradingControlPanel;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---
   starting_Equity = AccountInfoDouble(ACCOUNT_EQUITY);
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
   
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//---
   
   
   //Close EA if equity falls Below user input levels
   current_Equity = AccountInfoDouble(ACCOUNT_EQUITY);
   if(current_Equity < min_Equity_to_maintain)
   {
      Alert("Equity has fallen below critical, closing EA");
      //CloseByAllOpenOrders();
      ExpertRemove();
   }
//END OF IMPORTANT CHECKS EACH TICK
//-------------------------------------------------------------------------------
//MAIN EA CODE BEGINS HERE
//---------------------------------------------------------------------------   
   double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   
   //if we have no open position or order      
   int total_OpenPositions = GetTotalOpenPositions();
   if(total_OpenPositions == 0)
   {
      OpenABuyPosition(starting_lot,0,Ask + (starting_TP * _Point));
   }//end of zero open positions
   else // we have open positions
   {
      //find latest position
      ulong latest_ticket = GetLatestTicketNumber();
      
      if(PositionSelectByTicket(latest_ticket) == true)
      {
         double position_open_price = PositionGetDouble(POSITION_PRICE_OPEN);
         double position_vol = PositionGetDouble(POSITION_VOLUME);
         
         if(Ask < (position_open_price - (minimum_Drop_inPrice*_Point))) //price drops by required amount
         {
            double lot = NormalizeDouble((position_vol*lot_multiplier),_Digits);
            OpenABuyPosition(lot,0,Ask + (next_TP * _Point));
         }
         
      }
   }
   
}
//+------------------------------------------------------------------+



//+------------------------------------------------------------------+
//| Get Open Positions total function                                  |
//+------------------------------------------------------------------+
int GetTotalOpenPositions()
{
   int totalActiveOrders = 0;
   
   for(int i=PositionsTotal()-1; i>=0; i--) // Loop through all open positions (including those not opened by EA)
   {
      ulong ticketNumber = 0;
      
      if(PositionGetSymbol(i) == Symbol())//matches the symbol and selects the position for further processing
      {
         ticketNumber = PositionGetTicket(i); // This selects open position by the order they appear in our list of positions
         if(ticketNumber == 0)
         {
            Print("Position exists but not selected. Error: ", GetLastError());  
            ResetLastError();
            continue;
         }//end of if ticket is 0 
         else
         //if(PositionGetInteger(POSITION_MAGIC) == magicNumber)
         {
            totalActiveOrders++;
         }//end of if magic number matches         
      }//end of symbol matching      
   }//end of for loop
   
   return totalActiveOrders;
}//end of function
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Get Current Profit / Loss function                               |
//+------------------------------------------------------------------+
double GetCurrentProfitLoss()
{
   double totalprofitLoss = 0;
   
   for(int i=PositionsTotal()-1; i>=0; i--) // Loop through all open positions (including those not opened by EA)
   {
      ulong ticketNumber = 0;
      
      if(PositionGetSymbol(i) == Symbol())//matches the symbol and selects the position for further processing
      {
         ticketNumber = PositionGetTicket(i); // This selects open position by the order they appear in our list of positions
         if(ticketNumber == 0)
         {
            Print("Position exists but not selected. Error: ", GetLastError());  
            ResetLastError();
            continue;
         }//end of if ticket is 0 
         else
         //if(PositionGetInteger(POSITION_MAGIC) == magicNumber)
         {
            totalprofitLoss = totalprofitLoss + PositionGetDouble(POSITION_PROFIT);
         }//end of if magic number matches         
      }//end of symbol matching      
   }//end of for loop
   
   return totalprofitLoss;
}//end of function
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Get Latest Position ticket number function                       |
//+------------------------------------------------------------------+
ulong GetLatestTicketNumber()
{
   ulong LatestTicket = 0;
   long latestPositionTime = 0;
   
   for(int i=PositionsTotal()-1; i>=0; i--) // Loop through all open positions (including those not opened by EA)
   {
      ulong ticketNumber = 0;
      
      
      if(PositionGetSymbol(i) == Symbol())//matches the symbol and selects the position for further processing
      {
         ticketNumber = PositionGetTicket(i); // This selects open position by the order they appear in our list of positions
         if(ticketNumber == 0)
         {
            Print("Position exists but not selected. Error: ", GetLastError());  
            ResetLastError();
            continue;
         }//end of if ticket is 0 
         else
         //if(PositionGetInteger(POSITION_MAGIC) == magicNumber)
         {
            if(PositionGetInteger(POSITION_TIME) > latestPositionTime)
            {
               LatestTicket = ticketNumber;
               latestPositionTime = PositionGetInteger(POSITION_TIME);               
            }
             
         }//end of if magic number matches         
      }//end of symbol matching      
   }//end of for loop
   
   return LatestTicket;
}//end of function
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Open a buy position function                                     | 
//+------------------------------------------------------------------+

void OpenABuyPosition(double LotSize, double StopLoss, double TakeProfit)
{
   double posLot = LotSize;
   double posSL = StopLoss;
   double posTP = TakeProfit;
   
   int tries = 5;
   while(tries > 0) // Loop to reattempt opening positions if the initial attempt failed
   {
      myTradingControlPanel.PositionOpen(Symbol(),ORDER_TYPE_BUY,posLot,SymbolInfoDouble(Symbol(),SYMBOL_ASK),posSL,posTP,NULL); // Open a Buy (i.e. long) position
      
      if(myTradingControlPanel.ResultRetcode()==TRADE_RETCODE_PLACED || myTradingControlPanel.ResultRetcode()==TRADE_RETCODE_DONE) // Request is completed or order placed
      {
         tries = 0;
         //position_Taken_on_Signal = true;
         Print(" Buy order has been successfully placed with Ticket#: ",myTradingControlPanel.ResultOrder());                  
      }
      else
      {
         if(tries > 0) // If we are here, it means the position was not open.
         {
            tries--;
            Alert("Retrying to open buy position as requested",GetLastError());
            Sleep(250); // Wait for 250 milliseconds before reattempting to open the position
         }
         else
         {
            Alert(" Buy order request could not be completed. Error: ",GetLastError());
            ResetLastError();
         }
      }    
   }
   
}//end of open buy function
//+------------------------------------------------------------------+

The error is as follows;


Error code 4756 "Failed to send the trade request."

I would be glad if you help.

Alain Verleyen  
Do you know that the market are now closed ?
Yasar Sari  
Alain Verleyen #:
Piyasanın artık kapalı olduğunu biliyor musunuz?
Yes I know. It gives this error on weekdays as well. I am not stupid.
Fernando Carreiro  
Yasar Sari: Hello friends, I mentioned earlier that I am working on a strategy that opens a buy trade on every drop. I supported Thank you to everyone who helped. The system works smoothly in strategy mode, but does not open trading on demo and real account. Can experts examine the codes and tell where the error is? Thank you from now. In all codes section; The error is as follows; I would be glad if you help.

Your alert is reporting the runtime error, but you are not reporting the trade server error which will explain the reason, which will also be reported in the logs which you don't seem to be analysing.

So, in the case of your code, also report the "myTradingControlPanel.ResultRetcode()".

EDIT: Also note that you are not checking the return status of the "PositionOpen" method, which returns a boolean.

Alain Verleyen  
Yasar Sari #:
Yes I know. It gives this error on weekdays as well. I am not stupid.

You posted just after the markets closure, and you are using a loop with sleep(), so with the information you posted, it could perfectly be a problem of "market close".

Anyway, error 4756 is useless, as Fernando said you need to print the ResultRetcode to know the real reason.

Yasar Sari  
Fernando Carreiro #:

Your alert is reporting the runtime error, but you are not reporting the trade server error which will explain the reason, which will also be reported in the logs which you don't seem to be analysing.

So, in the case of your code, also report the "myTradingControlPanel.ResultRetcode()".

EDIT: Also note that you are not checking the return status of the "PositionOpen" method, which returns a boolean.


yes, I added the code and got error code 10026. "Automatic trading has been disabled by the server"

I don't know which loop is the problem. can you show me the part i need to fix?

Carl Schreiber  
Yasar Sari #:


yes, I added the code and got error code 10026. "Automatic trading has been disabled by the server"

I don't know which loop is the problem. can you show me the part i need to fix?

For this MQ has installed in the editor the debugger:

https://www.metatrader5.com/en/metaeditor/help/development/debug
https://www.mql5.com/en/articles/654
https://www.mql5.com/en/articles/35
https://www.mql5.com/en/articles/2041
https://www.mql5.com/en/articles/272
https://www.mql5.com/en/articles/150

Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
Fernando Carreiro  
Yasar Sari #: yes, I added the code and got error code 10026. "Automatic trading has been disabled by the server" I don't know which loop is the problem. can you show me the part i need to fix?

10026

TRADE_RETCODE_SERVER_DISABLES_AT

Autotrading disabled by server


This could mean that your broker (or that type of account) does not allow trading with automation, such as with a Expert Advisor (EA) and that It only allows manual trading.

Speak to your broker about it or change to a different broker.
Reason: