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.
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 012:44:39.040 EA_ADX (XAUUSD,H1) 2020.01.0319:00:00Alert: The Buy order request could not be completed -error:4756
ED 212:44:39.043 Trades 2020.01.0320:00:00 failed market buy 0.1 XAUUSD sl: 1549.09 tp: 1549.16 [Invalid stops]
JP 012:44:39.043 EA_ADX (XAUUSD,H1) 2020.01.0320:00:00Alert: The Buy order request could not be completed -error:4756
ER 212:44:39.049 Trades 2020.01.0321:00:00 failed market buy 0.1 XAUUSD sl: 1549.90 tp: 1549.97 [Invalid stops]
MR 012:44:39.049 EA_ADX (XAUUSD,H1) 2020.01.0321:00:00Alert: The Buy order request could not be completed -error:4756
PP 212:44:39.053 Trades 2020.01.0322:00:00 failed market buy 0.1 XAUUSD sl: 1548.16 tp: 1548.23 [Invalid stops]
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 Structureif(!SymbolInfoTick(_Symbol,latest_price))
{
Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
return;
}
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 positionbool Sell_opened= false ; // variable to hold the result of Sell opened positionif ( PositionSelect ( _Symbol ) == true ) // we have an opened position
{
if ( PositionGetInteger ( POSITION_TYPE ) == POSITION_TYPE_BUY )
{
Buy_opened = true ; //It is a Buy
}
elseif ( 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 positionbool Sell_opened = false ; // variables to hold the result of Sell opened positionif ( 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
} elseif ( PositionGetInteger ( POSITION_TYPE ) == POSITION_TYPE_BUY )
{
Buy_opened = true ; //It is a Buy
}
}
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 Purchasebool Sell_opened=false; // variable storing the result of the open position Saleif (PositionSelect(_Symbol) ==true) // we have an open position
{
if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
Buy_opened = true; // It's a Purchase
}
elseif(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 positionbool 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
}
elseif(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.
Does anyone know if I can use this EA for mini-indexes or mini-dollars?
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.
The file is at the end of the Guide.
How can I solve this problem?
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:
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:
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
to:
Some papers have minimum stop loss rules, read more here: https://www.mql5.com/pt/forum/319474
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:
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 ...
For this one...
Note: Just after comes the line...
This way it worked fine for me. I hope you find it useful.
Best regards.
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