bool wasClosedByTP = MathAbs( OrderClosePrice() - OrderTakeProfit() ) < MathAbs( OrderClosePrice() - OrderStopLoss() );Difficult?
Hello!
I would like to close trades on filters of TP and SL, instead of placing SL and TP in OrderSend() function.
I find it difficult to identify when a trade is closed on SL or TP, and as I may need to trigger some actions when TP or SL is hit, I find it more easier to use TP and SL as filters for closing trades.
Any pros and cons using SL/TP as filters instead of using OrderSend() function SL and TP?
I use this approach in MQL5 - not sure if you have an equivalent in MQL4 but posting just in case you have such an option
//+------------------------------------------------------------------+ void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result) //+------------------------------------------------------------------+ { if(request.position > 0) { switch(request.action) { case TRADE_ACTION_SLTP: //SL/TP close { //Execute code for SL or TP break; } //end case case TRADE_ACTION_DEAL: //Manual Close { //Execute code for Manual closure break; } //end case } //end switch } //end - if(request.position > 0) //+------------------------------------------------------------------+ } // OnTradeTransaction() End //+------------------------------------------------------------------+
MQL4 does not have a OnTradeTransaction event. One has to keep track of the tickets or loop through the order history to find things out. There are also no deals or positions, only orders.
William's post is the most relevant so far for the OP's query.
MQL4 does not have a OnTradeTransaction event. One has to keep track of the tickets or loop through the order history to find things out. There are also no deals or positions, only orders.
William's post is the most relevant so far for the OP's query.
@Fernando and @William - Thanks for clarifying
Thank you.
But this is to find if...I would like to know when...and if I add also a partial TP on the way...things become more complicated.
What I would like to know is if there is any difference between SL/TP set as parameters(prices) in OrderSend() function, and SL/TP set as target prices for closing a trade.
Thank you
Your question is uclear.From my understanding of your question, this is what I think you want to know ...
- You can have broker-side stops, set when you place the order with OrderSend, or ...
- ... you can have "virtual stops" that you track and manage within your code, but never place with order when you use OrderSend.
If you are referring to virtual stops (stealth stops), then you must implement all your own code to handle them. There are no functions in MQL to handle "virtual stops". You have to code it yourself.
In other words, you have to monitor the current prices and when the reach the levels/targets that you have defined in your code, you then proceed to close the orders programmatically.
Your question is uclear.From my understanding of your question, this is what I think you want to know ...
- You can have broker-side stops, set when you place the order with OrderSend, or ...
- ... you can have "virtual stops" that you track and manage within your code, but never place with order when you use OrderSend.
If you are referring to virtual stops (stealth stops), then you must implement all your own code to handle them. There are no functions in MQL to handle "virtual stops". You have to code it yourself.
In other words, you have to monitor the current prices and when the reach the levels/targets that you have defined in your code, you then proceed to close the orders programmatically.
Thank you!
This is what I am asking, about "virtual stops", if you guys encountered any problems using virtual stops instead of broker-side stops.
What do you mean by if we encountered any problems with "virtual stops"?
"Virtual stops" are handled by your code. They are not on the broker side. They are on the client side.
Broker stops will be triggered even if you are disconnected from the trading account.
Virtual stops will only be triggered if your are connected and your EA is functional.
The best is to use both types. Virtual stops at the intended targets and broker side stops further out to act as a fail-safe in case the EA stops or the connection is lost.
int InpStopLoss =50; //in pips int InpTakeProfit =50; //in pips int ticket = Trade.OpenBuyOrder(_Symbol,Volum.LotSize(InpStopLoss,2,2,0),magic); //StopLoss function where I add 1.3 to SL and TP Trade.ModifyOpenOrder(ticket,InpStopLoss*1.3,InpTakeProfit*1.3); // Stop function void StoplOss() { double pask = SymbolInfoDouble(_Symbol,SYMBOL_ASK); double pbid = SymbolInfoDouble(_Symbol,SYMBOL_BID); for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS)) { if(OrderMagicNumber()==magic && OrderCloseTime()==0 && OrderSymbol()==_Symbol) { if(OrderType()==OP_BUY) { if(pbid < OrderOpenPrice()-InpStopLoss*point()) { if(Trade.CloseBuyOrder(magic,_Symbol)) { Print("Order ",OrderTicket()," closed on SL"); } /// /// /// and the same for takeProfit ...
I couldn't think on something more easier...what do you guys think?
Thanks!
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello!
I would like to close trades on filters of TP and SL, instead of placing SL and TP in OrderSend() function.
I find it difficult to identify when a trade is closed on SL or TP, and as I may need to trigger some actions when TP or SL is hit, I find it more easier to use TP and SL as filters for closing trades.
Any pros and cons using SL/TP as filters instead of using OrderSend() function SL and TP?