Discussion of article "Step-by-Step Guide to Writing an Expert Advisor in MQL5 for Beginners" - page 22

 
Good morning, If someone could help me please. To my understanding, I did everything that is indicated in the article. But I can not manage to have two simultaneous open operations, beyond what I understand that the author wanted to do not to place buy if there was already buy or sell if there was already sell. My strategy, for example, sends a sell signal when I have an open buy order, and instead of opening a sale, the code closes the buy operation without it being able to touch the tp/sl. And so I am left with no buy or sell. The sell closes buy and buy closes sell. I am using only one pair, only one time frame and only one lot size in my strategy. Can you tell me how I can have buy and sell open at the same time if it triggers the strategy? That is to say, that the orders are ONLY closed by tp/sl. Thank you very much in advance.
 
Great article!
 
Sella170:
Does anyone know if I can use this EA for mini-indexes or mini-dollars?
Yes, you can use the EA by dragging the mouse to the chart or menu > insert > Expert advisor > {nomedoseurobo}.

I hope your question has been answered.

 
Many thanks for the excellent article. Where and how would one implement the Intraday Time Filter module into your code?
 

Hi guys, good morning.

I'm in the"Strategy Tester" with this file from the end of the Guide and it shows the following error:

*the error has already been discussed here in this article, but no written solution has worked for me and I need your help.


OF      0       12:44:39.040    EA_ADX (XAUUSD,H1)      2020.01.03 19:00:00   Alert: The Buy order request could not be completed -error:4756
ED      2       12:44:39.043    Trades  2020.01.03 20:00:00   failed market buy 0.1 XAUUSD sl: 1549.09 tp: 1549.16 [Invalid stops]
JP      0       12:44:39.043    EA_ADX (XAUUSD,H1)      2020.01.03 20:00:00   Alert: The Buy order request could not be completed -error:4756
ER      2       12:44:39.049    Trades  2020.01.03 21:00:00   failed market buy 0.1 XAUUSD sl: 1549.90 tp: 1549.97 [Invalid stops]
MR      0       12:44:39.049    EA_ADX (XAUUSD,H1)      2020.01.03 21:00:00   Alert: The Buy order request could not be completed -error:4756
PP      2       12:44:39.053    Trades  2020.01.03 22:00:00   failed market buy 0.1 XAUUSD sl: 1548.16 tp: 1548.23 [Invalid stops]


The file is at the end of the Guide.

How can I solve this problem?

 
surfdoode2:


I also still have this error having made both suggested corrections:

ZeroMemory(mrequest);

and also

if(Buy_opened || Sell_opened)

Please can someone help with the error 4756? It just seems unable to place a Buy Order.

Check if you used the SymbolInfoTick function and wrote this part of the code:

//--- 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;
     }
Documentation on MQL5: Market Info / SymbolInfoTick
Documentation on MQL5: Market Info / SymbolInfoTick
  • www.mql5.com
SymbolInfoTick - Market Info - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
There is an error in the logic of the algorithm here:
 //--- Do we have positions opened already?
     bool Buy_opened= false ;   // variable to hold the result of Buy opened position
     bool Sell_opened= false ; // variable 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
         }
    }

Since PositionSelect selects the first position for _Symbol, if the EA opens a buy position, the Sell_opened variable will always be false while the buy position is open, causing multiple sell positions to be realised whenever there is a sell signal.

To correct this and know if there are buy and sell positions open simultaneously, the following logic can be used:

 //--- 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
   {
       for ( int i= 0 ;i< PositionsTotal ();i++){
	 
         PositionSelectByTicket ( PositionGetTicket (i));

         if ( PositionGetInteger ( POSITION_TYPE ) == POSITION_TYPE_SELL )
         {
            Sell_opened = true ; // It is a Sell
         } else if ( PositionGetInteger ( POSITION_TYPE ) == POSITION_TYPE_BUY )
         {
            Buy_opened = true ; //It is a Buy
         }
      }
   
Documentação sobre MQL5: Funções de Negociação / PositionSelect
Documentação sobre MQL5: Funções de Negociação / PositionSelect
  • www.mql5.com
PositionSelect - Funções de Negociação - Referência MQL5 - Referência sobre algorítimo/automatização de negociação na linguagem para MetaTrader 5
 
Vitor Gama Barreto:

Hello everyone, good morning.

I'm in the"Strategy Tester" with this file from the end of the Guide and it shows the following error:

*the error has already been discussed here in this article, but no written solution has worked for me and I need your help.



The file is at the end of the Guide.

How can I solve this problem?

Check the sale price you are requesting, to do this use breakpoint to debug the mrequest.price, mrequest.sl and mrequest.tp variables.

Try changing

mrequest.type_filling = ORDER_FILLING_FOK;                               // Order execution type

to:

mrequest.type_filling = ORDER_FILLING_IOC;                               // Order execution type


Some papers have minimum stop loss rules, read more here: https://www.mql5.com/pt/forum/319474

ERRO 4756 - INVALID PRICE
ERRO 4756 - INVALID PRICE
  • 2019.08.06
  • www.mql5.com
Ja tentei de tudo, por favor, preciso muito da ajuda de voces!! alguem pode me dar uma luz??? E retorna o : 2019.08.05 13:42:51.236 Core 1 2019.08...
 

Hello,

Thank you very much for the article Samuel, you are a crack!

It was a bit difficult to get it to work with my broker because the article is in "net" mode and I had to look for information to put it in "hedging" mode. The first problem I encountered was with the mrequest.type_filling .

For the XM broker, the one that works is:

mrequest.type_filling = ORDER_FILLING_IOC;                                          // Type of execution of the order

If you don't know which one your MT5 allows, my recommendation is to use the TypeFilling() functionin a Print(TypeFilling()) or a Comment( TypeFilling()) and it returns the type; if it tells you it is 1 (ORDER_FILLING_FOK) or 2 (ORDER_FILLING_IOC) you know which one you have to put(https://www.mql5.com/docs/constants/tradingconstants/orderproperties#enum_order_type_filling) becauseORDER_FILLING_FOK (1) doesn't work for me with this broker.


Once that is fixed, the problem is that PositionSelect opens a lot of positions. Although Samuel doesn't say it in his strategy, I understand that the objective is to open a single buy or sell position (whichever comes first) with the established criteria (the 4 boolean steps) and that until this position is closed, we can't open another one.

What I have done is to change this code ...

//--- we have no errors, so we continue.
//--- Do we have open positions yet?
    bool Buy_opened=false;  // variable storing the result of the open position Purchase
    bool Sell_opened=false; // variable storing the result of the open position Sale
    
    if (PositionSelect(_Symbol) ==true)  // we have an open position
    {
         if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
         {
            Buy_opened = true;  // It's a Purchase
         }
         else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
         {
            Sell_opened = true; // It's a Sale
         }
    }

For this one...

//--- We have no errors so we continue.
//--- Do we have open positions yet?
   bool Buy_opened = false;               // Variable storing the result of the open Buy position
   bool Sell_opened = false;              // Variable storing the result of the open Sell position
   
   // Let's go through all the orders to see if there are any open ones.
   for(int i = 0; i < PositionsTotal(); i++)   
        {
         ulong ticket = PositionGetTicket(i);
         PositionSelectByTicket(ticket);
    
    if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
         {
          Buy_opened = true;       // is a purchase
         }
         else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
         {
          Sell_opened = true;      // is a sale
         }
   }

// if there is an open position we wait until the active operation is closed (touching the SL or theTP).
   if (Buy_opened || Sell_opened) return; 

Note: Just after comes the line...

//--- Copy the closing price from the previous bar to the current bar, which is bar1.
   p_close=mrate[1].close;

This way it worked fine for me. I hope you find it useful.

Best regards.

 
surfdoode2:


I also still have this error having made both suggested corrections:

ZeroMemory(mrequest);

and also

if(Buy_opened || Sell_opened)

Please can someone help with the error 4756? It just seems unable to place a Buy Order.


Maybe the Filling Type of your Broker  is ORDER_FILLING_IOC , you can try to edit the code from:

mrequest.type_filling = ORDER_FILLING_FOK;

To:

mrequest.type_filling = ORDER_FILLING_IOC;

I had the same problem and now I solved it successfully