Guys i try to code my EA after one order has been closed by TP to close all the rest orders and start over again . Because this EA is dominated on martingale but it opens in both directions orders . If someone can help it will be much appreciated.
Any EA that shows when market has closed 20 days above/below ema 0 replies
How to check if an order has been closed by SL/TP? 5 replies
Order Comment blank after order closed 4 replies
Dislikeddon't know whether it's valid for all brokers, but u will find [tp] in the comment / as part of the commentIgnored
Dislikedyou have to look for the TPed position in the OrderHistory But to be on the save side, your set of positions should have something in common You could always use a new unique magicnumber to identify trades that are belonging together and look for a trade with the same magic in History where there's [tp] in the comment Another way would be to use the Comment Field itself The first trade doesn't have a comment and all following positions are holding the ticket number of position #1 in the comment field So now you can search for either a closed position...Ignored
// twisted by rockit // copyleft #property strict int tickets[]; int OnInit() { OnTick(); return INIT_SUCCEEDED; } void OnTick() { // check orders status for(int i = 0; i < ArraySize(tickets); i++) { if(OrderSelect(tickets[i], SELECT_BY_TICKET, MODE_HISTORY)) { // check tp hit if(OrderType() == OP_BUY ? OrderClosePrice() >= OrderTakeProfit() : OrderClosePrice() <= OrderTakeProfit()) { // close or delete all same symbol same direction string symbol = OrderSymbol(); int type = OrderType(); for(int j = OrdersTotal() - 1; j >= 0; j--) { OrderSelect(j, SELECT_BY_POS); if(OrderSymbol() == symbol) { if(OrderType() > type + 2) OrderDelete(OrderTicket()); else if(OrderType() == type) OrderClose(OrderTicket(), OrderLots(), type == OP_BUY ? Bid : Ask, 1); } } break; // done } } } // collect orders ArrayResize(tickets, OrdersTotal()); for(int i = 0; i < OrdersTotal(); i++) { OrderSelect(i, SELECT_BY_POS); tickets[i] = OrderTicket(); } }
DislikedI would try like code below (note code not tested). What the code does: It first collects all order tickets from the order queue. On a tick it checks the collected tickets for if any has gone to history; if so it checks for tp proximity; if so it deletes / closes all orders in same symbol and same direction. Then it again collects all tickets and so on.. // twisted by rockit // copyleft #property strict int tickets[]; int OnInit() { OnTick(); return INIT_SUCCEEDED; } void OnTick() { // check orders status for(int i = 0; i < ArraySize(tickets); i++)...Ignored
// TP hit? if(OrderType() == OP_BUY() ? OrderClosePrice() > OrderOpenPrice() : OrderClosePrice() < OrderOpenPrice()) { // yes }
Dislikedtell me what to change in the code at end in order to delete all the open trades no matter the directionIgnored
// close all pending & working if(OrderType() > 1) OrderDelete(OrderTicket()); else OrderClose(OrderTicket(), OrderLots(), type == OP_BUY ? Bid : Ask, 1);
DislikedI have 'implanted' the checks into your code. Hope it works. It considers same magic number and same symbol only. Also: You should really use the new style MQL4; it makes things easier and clearer, because right now you have a lot local variables that overlay global variables, although they are meant to be the same I guess. {file}Ignored
DislikedI have looked at some orders that have been closed out by SL/TP and it appears that the brokers indicate such a close in the comment of the order. So far I have seen the text "[sl]" and "[tp]". In such a case the check for TP hit is accurate and trivial with OrderComment() == "[tp]". Therefore it is recommended to do it that way. Here's a new code taking into account the forementioned and also using another approach. {file}Ignored
QuoteDislikedBtw. did you notice that the EA will never be able to open a sell trade when a new batch starts?
Disliked{quote} Are you testing your code before posting it? Because OrderComment() == "[tp]" doesn't work in this case Have a look what happens in OrderSend and you will see why it can't work Btw. did you notice that the EA will never be able to open a sell trade when a new batch starts?Ignored
Disliked{quote} I do if the test case is trivial (or rather: static). However, this one is not and I did not, but I have told so. I see what you mean, OP already put in a comment - I do not know what happens with the comment in this case if sl/tp is hit - ideally the server would amend the text or fill it if comment is an empty string (i.e. ""). {quote} No, I have not analysed what OP's code in fact does. No time for such (note originally he asked for how to recog. if tp hit). He will have to work his code around a 'find if tp hit'-mechanism. But he can...Ignored
Disliked{quote} Thank you for your post . If you can assist coding this EA correctly your help will be appreciated . Mentioning what is wrong is half job but correcting the mistake is the one that brings results.Ignored
Disliked{quote} The problem is the following: This kind of EAs have been coded over and over again and all got the same result: they do not work in the long run You will make some nice profits, but the day will come where it all blows up One would have to rewrite your EA from scratch. For example I would code it with SL and TP and don't care about closed trades If you have a buy position and a sell position the buy should be closed by SL automatically if the sell hits TPIgnored