I added the following to capture any current market positions:
ulong buyTicket = 0, sellTicket = 0; for(int i = 0; i < PositionsTotal(); i++) { ulong ticket = PositionGetTicket(i); PositionSelectByTicket(ticket); if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) { buyTicket = ticket; Buy_opened = true; } else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) { sellTicket = ticket; Sell_opened = true; } }
I added this to make sure the sell is closed before opening the buy.
if(Buy_Condition_1 && Buy_Condition_2 && Buy_Condition_3 && Buy_opened==false) { // Close sell order if(sellTicket > 0) { PositionSelectByTicket(sellTicket); request.action = TRADE_ACTION_DEAL; request.type = ORDER_TYPE_BUY; request.symbol = _Symbol; request.position = sellTicket; request.volume = PositionGetDouble(POSITION_VOLUME); request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK); request.deviation = 5; bool sent = OrderSend(request, result); Sell_opened = false; }
I split the opening order and SL_TP modifcation into two stages. 1st stage
request.action = TRADE_ACTION_DEAL; request.price = NormalizeDouble(latest_price.ask,_Digits); request.sl = 0; request.tp = 0; request.position = 0; //Clear out redundant ticket request.symbol = Symbol(); request.volume = LotToTrade; request.type = ORDER_TYPE_BUY; request.type_filling = ORDER_FILLING_FOK; request.deviation=5; // Deviation from current price request.magic = Expert_Magic; //--- send order bool sent = OrderSend(request,result);
2nd stage stage
// Modify SL/TP if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE) { request.action = TRADE_ACTION_SLTP; request.position = result.order; PositionSelectByTicket(result.order); double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN); if(SLprice > 0) request.sl = SLprice; if(TPprice > 0) request.tp = TPprice; if(request.sl > 0 && request.tp > 0) sent = OrderSend(request,result); } else { Alert("The Buy order request could not be completed -error:",GetLastError()); ResetLastError(); return; }
Max Brown #:
I added the following to capture any current market positions:
I added this to make sure the sell is closed before opening the buy.
I split the opening order and SL_TP modifcation into two stages. 1st stage
2nd stage stage
Thank you for your response ! May I ask you why it is better to split opening orders into two stages ?
Max Brown #:
Noted, I will continue to do this from now on because it allows to check the SL and TP prices easily, and it is an useful check ! Thanks again sir.
For me it makes the process easier, old habits. However, it is not necessary because brokers no longer require a two stage order submission.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello everybody,
I'm a beginner in MQL5 and programmation in general. I started developing my EA last month following examples online and in the documentation and I have been able to implement custom indicators, buy and sells following conditions but I am now facing a problem that I don't know how to solve. Let me explain :
I would like my EA to close open trades (buys and sells) when the signal line of one of my indicators crosses with another line (no problem here I think). I found the code to do just that in the documentation, so I don't think the problem comes from the instructions. The problem is that there is a request that can't get executed properly, and I don't know why. I suspect an error in the memory.
Note that this problem only occurs when trying to close trades and not when opening them.
Here is the message that is shown in the terminal, for example when trying to close the trade #48 :
"failed market buy 0.67 EURUSD, close #48 buy 0.67 EURUSD 1.18298 [Invalid request]
OrderSend error 4756
retcode=10013 deal=0 order=0"
I of course searched for this problem on the forum but I couldn't find an answer (there is maybe one but I didn't find it...) and read the doc to see if I couldn't fix it myself, so I decided to ask to the community.
Thank you very much in advance,
Here is the code of my EA :