What is the problem of this code?

 

Hi there,

I am trying to test simplest EA which only buy's a stock at a specified price. But when I do strategy test, I get no result and seems there is a problem in my code. May you help me to know is my code a complete robot or what is the problem?


Thanks in advanced!


//+------------------------------------------------------------------+
#property copyright "Almas.co"
#property link      "https://www.mql5.com"
#property version   "1.00"

input int      StopLoss=250;      // Stop Loss
input int      TakeProfit=250;   // Take Profit
input int      EA_Magic=12345;   // EA Magic Number
input double   Lot=1;          // Lots to Trade

double p_close; // Variable to store the close value of a bar
int STP, TKP;   // To be used for Stop Loss & Take Profit values

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   STP = StopLoss;
   TKP = TakeProfit;
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

      //--- Define some MQL5 Structures we will use for our trade
   MqlTick latest_price;     // To be used for getting recent/latest price quotes
   MqlTradeRequest mrequest;  // To be used for sending our trade requests
   MqlTradeResult mresult;    // To be used to get our trade results
   MqlRates mrate[];         // To be used to store the prices, volumes and spread of each bar
   ZeroMemory(mrequest);     // Initialization of mrequest structure
   
   
   /*
     Let's make sure our arrays values for the Rates, ADX Values and MA values 
     is store serially similar to the timeseries array
*/
// the rates arrays
   ArraySetAsSeries(mrate,true);

   
   
//--- Get the last price quote using the MQL5 MqlTick Structure
   if(!SymbolInfoTick(_Symbol,latest_price))
     {
      Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
      return;
     }

//--- Get the details of the latest 3 bars
   if(CopyRates(_Symbol,_Period,0,3,mrate)<0)
     {
      Alert("Error copying rates/history data - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }


   //--- we have no errors, so continue
//--- Do we have positions opened already?
   bool Buy_opened=false;  // variable to hold the result of Buy opened position
   bool Sell_opened=false; // variables to hold the result of Sell opened position

      if(PositionSelect(_Symbol)==true) // we have an opened position
     {
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
        {
         Buy_opened=true;  //It is a Buy
        }
      else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
        {
         Sell_opened=true; // It is a Sell
        }
     }
     
     // Copy the bar close price for the previous bar prior to the current bar, that is Bar 1
         p_close=mrate[1].close;  // bar 1 close price

      if(p_close == 2600)
        {
         // any opened Buy position?
         if(Buy_opened)
           {
            Alert("We already have a Buy Position!!!");
            return;    // Don't open a new Buy Position
           }

    ZeroMemory(mrequest);
         mrequest.action = TRADE_ACTION_DEAL;                                  // immediate order execution
   //      mrequest.price = NormalizeDouble(latest_price.ask,_Digits);           // latest ask price
  //       mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss
   //      mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit
         mrequest.symbol = _Symbol;                                            // currency pair
         mrequest.volume = Lot;                                                 // number of lots to trade
         mrequest.magic = EA_Magic;                                             // Order Magic Number
         mrequest.type = ORDER_TYPE_BUY;                                        // Buy Order
         mrequest.type_filling = ORDER_FILLING_IOC; //ORDER_FILLING_FOK;                             // Order execution type
         mrequest.deviation=100;                                                // Deviation from current price
         //--- send order
         OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Buy order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }
           
      }
       
           
   
   
   
  }
//+------------------------------------------------------------------+

            
 

You can use OrderCheck() before sending the actual order.

 
Marco vd Heijden:

You can use OrderCheck() before sending the actual order.

I use a demo account with $1000000 deposit and just like to use previous data to test my EA(Strategy tester).
 
Any error message and/or screenshot would be helpful. Document expected vs observed behavior. The listing is ok, if you want to expose it, but sometimes even this is too much information. I guess no one here has time and nerves to walk through your code.
 
lippmaje:
Any error message and/or screenshot would be helpful. Document expected vs observed behavior. The listing is ok, if you want to expose it, but sometimes even this is too much information. I guess no one here has time and nerves to walk through your code.
I get no error messages in terminals or log files. I also have no any result in terminals to take screen shots. The code compiles and runs without problem or error(Strategy tester works and completes the chart but nothing happens in trade or history tabs)
 
Marco vd Heijden:

You can use OrderCheck() before sending the actual order.

I did both the checks but the problem exists as before:


         if(OrderSend(mrequest,mresult)) 
         Print("Everything is OK");
          else Alert("There is a problem here");

         if(OrderCheck(mrequest,mmresult)) 
         Print("Everything is OK");
          else Alert("There is a problem hereee");

         
 
rezaeee:

I did both the checks but the problem exists as before:


Is it important the market is open or closed when I check my code in strategy test on previous data?

 
rezaeee:

Is it important the market is open or closed when I check my code in strategy test on previous data?

No

 

I checked my code again when the market is open and got these alert messages:


There is a problem here
There is a problem hereee
The Buy order request could not be completed -error:4756
 

I also use a demo account to test my code, is it important and maybe cuased the problem?


I tried to use my EA but I got this error message:

The Buy order request could not be completed -error:4756

What is the meaning of this error? Is that mean the broker is blocked using EA robots?


#property link      "https://www.mql5.com"
#property version   "1.00"


input int      StopLoss=250;      // Stop Loss
input int      TakeProfit=250;   // Take Profit
input int      EA_Magic=12345;   // EA Magic Number
input double   Lot=1000;          // Lots to Trade
input double buying_price = 100;  //I will change it by hand before running the expert respect to market price

double p_close; // Variable to store the close value of a bar
int STP, TKP;   // To be used for Stop Loss & Take Profit values

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   STP = StopLoss;
   TKP = TakeProfit;
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

      //--- Define some MQL5 Structures we will use for our trade
   MqlTick latest_price;     // To be used for getting recent/latest price quotes
   MqlTradeRequest mrequest;  // To be used for sending our trade requests
   MqlTradeResult mresult;    // To be used to get our trade results
   MqlRates mrate[];         // To be used to store the prices, volumes and spread of each bar
   ZeroMemory(mrequest);     // Initialization of mrequest structure
   MqlTradeCheckResult mmresult;
   
   /*
     Let's make sure our arrays values for the Rates, ADX Values and MA values 
     is store serially similar to the timeseries array
*/
// the rates arrays
   ArraySetAsSeries(mrate,true);

   
   
//--- Get the last price quote using the MQL5 MqlTick Structure
   if(!SymbolInfoTick(_Symbol,latest_price))
     {
      Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
      return;
     }

//--- Get the details of the latest 3 bars
   if(CopyRates(_Symbol,_Period,0,3,mrate)<0)
     {
      Alert("Error copying rates/history data - error:",GetLastError(),"!!");
      ResetLastError();
      return;
     }


   //--- we have no errors, so continue
//--- Do we have positions opened already?
   bool Buy_opened=false;  // variable to hold the result of Buy opened position
   bool Sell_opened=false; // variables to hold the result of Sell opened position

      if(PositionSelect(_Symbol)==true) // we have an opened position
     {
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
        {
         Buy_opened=true;  //It is a Buy
        }
      else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
        {
         Sell_opened=true; // It is a Sell
        }
     }
     
     // Copy the bar close price for the previous bar prior to the current bar, that is Bar 1
         p_close=mrate[1].close;  // bar 1 close price
         

      if(latest_price.ask <= buying_price)
        {
        /*
         // any opened Buy position?
         if(Buy_opened)
           {
            Alert("We already have a Buy Position!!!");
            return;    // Don't open a new Buy Position
           }
           */

    ZeroMemory(mrequest);
         mrequest.action = TRADE_ACTION_DEAL;                                  // immediate order execution
         mrequest.price = latest_price.ask; //NormalizeDouble(latest_price.ask,_Digits);           // latest ask price
         mrequest.sl = latest_price.ask - STP;        //NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss
         mrequest.tp = latest_price.ask + TKP; //NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit
         mrequest.symbol = _Symbol;                                            // currency pair
         mrequest.volume = Lot;                                                 // number of lots to trade
         mrequest.magic = EA_Magic;                                             // Order Magic Number
         mrequest.type = ORDER_TYPE_BUY;                                        // Buy Order
         mrequest.type_filling = ORDER_FILLING_IOC; //ORDER_FILLING_FOK;                             // Order execution type
         mrequest.deviation=100;                                                // Deviation from current price
         //--- send order
       //  OrderSend(mrequest,mresult);
         
         if(OrderSend(mrequest,mresult)) 
         Print("Everything is OK");
          else Alert("There is a problem here");

         if(OrderCheck(mrequest,mmresult)) 
         Print("Everything is OK");
          else Alert("There is a problem hereee");

         
         
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Buy order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }
           
      }
  }
 
rezaeee:


Do not double post.

I have deleted your other topic and copy/pasted the code in your last post here

Reason: