I wanna know how i can close all orders if one order reaches the take profit
The EA expects a position to close when Take Profit is triggered. After that, an attempt is made to close all positions (without taking into account the symbol and Magic number) and delete all pending orders (without taking into account the symbol and Magic number).
As an adviser understands that the position was closed as a result of Take Profit triggering:
- transaction TRADE_TRANSACTION_DEAL_ADD is first caught (adding a transaction to the history)
- check - is there really a deal in history
- we filter out the excess: the deal must be either BUY or SELL, it must be a deal DEAL_ENTRY_OUT (Exit the market) or DEAL_ENTRY_INOUT (U-turn)
- AND MOST IMPORTANT - this must be a DEAL_REASON_TP transaction (The transaction was completed as a result of the Take Profit order being triggered)
//+------------------------------------------------------------------+ //| TradeTransaction function | //+------------------------------------------------------------------+ void OnTradeTransaction(const MqlTradeTransaction &trans, const MqlTradeRequest &request, const MqlTradeResult &result) { //--- get transaction type as enumeration value ENUM_TRADE_TRANSACTION_TYPE type=trans.type; //--- if transaction is result of addition of the transaction in history if(type==TRADE_TRANSACTION_DEAL_ADD) { if(HistoryDealSelect(trans.deal)) m_deal.Ticket(trans.deal); else return; if(m_deal.DealType()==DEAL_TYPE_BUY || m_deal.DealType()==DEAL_TYPE_SELL) { if(m_deal.Entry()==DEAL_ENTRY_OUT || m_deal.Entry()==DEAL_ENTRY_INOUT) { long deal_reason=-1; if(m_deal.InfoInteger(DEAL_REASON,deal_reason)) { if((ENUM_DEAL_REASON)deal_reason==DEAL_REASON_TP) { m_need_close_all=true; // close all positions m_need_delete_all=true; // delete all pending orders } } } } } }
Full code: Close All if Take Profit activation
Is it possible to have it for MT4?
Zazu :
Is it possible to have it for MT4?
Is it possible to have it for MT4?
You are on the MQL5 forum. Therefore, you received an answer in MQL5. There is a special section for the old terminal: MQL4 and MetaTrader 4
Ok Vladimir, thanks for the answer
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
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