how to create a function to execute only on closing any position. whether ontrade or ontradetransaction . ?
void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result) { if(trans.type != 6) return; // only interested in DEAL_ADD events. if(trans.symbol!=_Symbol) return; // only interested in this symbol. if(HistoryDealGetInteger(trans.deal,DEAL_ENTRY) != DEAL_ENTRY_OUT) return; // only interested in position closures // do what you do here... }
Here's my code that will run when there's a closed position,
void CStrategy::OnTradeTransactionEvent(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result) { // when closing buy position if(trans.type == TRADE_TRANSACTION_DEAL_ADD && trans.deal_type == DEAL_TYPE_SELL && HistoryDealSelect(trans.deal) && HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_OUT) { // do your stuff here } // when closing sell position if(trans.type == TRADE_TRANSACTION_DEAL_ADD && trans.deal_type == DEAL_TYPE_BUY && HistoryDealSelect(trans.deal) && HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_OUT) { // do your stuff here } }
struct CStrategy { void OnTradeTransactionEvent(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result) { // when closing buy position if(trans.type == TRADE_TRANSACTION_DEAL_ADD && trans.deal_type == DEAL_TYPE_SELL && trans.deal_type == DEAL_TYPE_BUY && HistoryDealSelect(trans.deal) && HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_OUT) { Print("when closing both any one") // do your stuff here } // when closing sell position if(trans.type == TRADE_TRANSACTION_DEAL_ADD && trans.deal_type == DEAL_TYPE_BUY && HistoryDealSelect(trans.deal) && HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_OUT) { Print("when closing only buy ") // do your stuff here } } }
used this code but no respone on closing any of position
void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result) { if(trans.type != 6) return; // only interested in DEAL_ADD events. if(trans.symbol!=_Symbol) return; // only interested in this symbol. if(HistoryDealGetInteger(trans.deal,DEAL_ENTRY) != DEAL_ENTRY_OUT) return; // only interested in position closures //string content = dealInfo(); // if(content != NULL) // { Print("sent data on trade"); //sendRequest(content); // } }
used your code but not working I am not getting any call to this function on closing any position.
I have created some functions to run ONLY when I CLOSE any POSITION and to achieve this i am using this function.
But this function is running on all events whether i am placing a trade or modifying deleting .
please tell me how to create a logic that executes only when any position is closed.
this might help.
https://www.mql5.com/en/docs/event_handlers/ontradetransaction
in the sample cod/example is reference to case 10036: return("TRADE_RETCODE_POSITION_CLOSED"); break;
- www.mql5.com
https://www.mql5.com/ru/code/35561
I have modified this by @Vladimir Karputov
i merely changed line #84 from TRADE_TRANSACTION_DEAL_ADD
I am unable to try it until next week. If you prove it working, please give report.
found this article too.
https://www.mql5.com/en/articles/1111
It deals with all types of deals, so should be able to remove pending orders and modify types from the code.
- www.mql5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
But this function is running on all events whether i am placing a trade or modifying deleting .
please tell me how to create a logic that executes only when any position is closed.