OrderClosePrice() equals order OrderTakeProfit()
Doubles rarely compare equal.
A TP is a market order. The close price will usually be above the TP and in the case of a price gap be 100's pips away. NOT EQUAL.
double deltaTP = MathAbs(OrderClosePrice()-OrderTakeProfit()), deltaSL = MathAbs(OrderClosePrice()-OrderStopLoss()); bool wasClosedByTP = deltaTP < deltaSL;
i don't think so those solution can work for me?!
because a day maybe have a lot batch of trading, maybe 1 batch have 4 trading...either one of 4 are T/P then close all trade. and then will buy 4 trade again...
i don't think so those solution can work for me?!
Save all ticket numbers, with OrderSelect() (https://docs.mql4.com/trading/OrderSelect), check if all positions are still open or not. If it is closed by TP then close all other positions.
int ticket1, ticket2, ticket3, ticket4; //--- check for ticket2 if (OrderSelect (ticket1, SELECT_BY_TICKET, MODE_TRADES) == false) //--- ticket1 is closed { if (OrderSelect (ticket1, SELECT_BY_TICKET, MODE_HISTORY) == true && OrderClosePrice() >= OrderTakeProfit() ) // ticket1 is closed by TP { // close and delete other positions return (0); } } else //--- ticket1 is still open { //--- check for ticket2 = same like ticket1 if (OrderSelect (ticket2, SELECT_BY_TICKET, MODE_TRADES) == false) //--- ticket2 is closed { if (OrderSelect (ticket2, SELECT_BY_TICKET, MODE_HISTORY) == true && OrderClosePrice() >= OrderTakeProfit() ) // ticket2 is closed by TP { // close and delete other positions return(0); } } else //--- ticket2 is still open { //--- check for ticket3 //--- and so on and so on } }
Save all ticket numbers, with OrderSelect() (https://docs.mql4.com/trading/OrderSelect), check if all positions are still open or not. If it is closed by TP then close all other positions.
i don't think so is a good a idea,
because when a trade are T/P and suddenly computer failure and restarted. this will cause can't track the batch of trade are T/P or not.
(sometimes 1 batch of trading is 4 trade, some are 6 trades, the number of trade are depend on indicator chart,number of trades are not fixed)
1. set variable for counting the total orders in the start of the Start() function. e.g. TotalOrd = IsExecuted (OP_SELL) or (OP_BUY) or what ever
1. Do not use orderstotal() instead use the function below for counting using magic number.
int IsExecuted(int orderType) { int count = 0; for (int i = 0; i < OrdersTotal(); i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == CurrentSymbol && OrderMagicNumber()==MagicNumber) { if (OrderType() == orderType) count++; } }
Then at the end of the Start() function again check whether TotalOrd is decrease by 1. it means 1 of your trade hit TP or SL or what ever. But decrease for sure!
So then close all the trade or do your required action
1. set variable for counting the total orders in the start of the Start() function. e.g. TotalOrd = IsExecuted (OP_SELL) or (OP_BUY) or what ever
1. Do not use orderstotal() instead use the function below for counting using magic number.
i don't think so is a good a idea,
because when a trade are T/P and suddenly computer failure and restarted. this will cause can't track the batch of trade are T/P or not.
(sometimes 1 batch of trading is 4 trade, some are 6 trades, the number of trade are depend on indicator chart,number of trades are not fixed)
I see, then we can use ubzen early answer and bachapk codes - btw there several syntax error with bachapk codes.
1. Every time you send order successfully, count it up. Save the OrderOpenTime() of the very first order, it will be useful when checking the historical order.
#include <stdlib.mqh> int ticket = 0; static datetime First_Order_Time = 0; static int Total_Send_Order; ticket = 0; ticket = OrderSend(...); if (ticket > 0) if (Total_Send_Order == 0) { if (ticket, SELECT_BY_TICKET, MODE_TRADES) == true) First_Order_Time = OrderOpenTime(); } Total_Send_Order ++; else { Print ("Fail to send order with error ", ErrorDescription(GetLastError())); }
2. Check the number of current order - like bachapk codes, only better :)
int pos; static int Total_Current_Orders; for (pos = OrdersTotal() - 1; pos >= 0; pos --) { if (OrderSelect (pos, SELECT_BY_POS, MODE_TRADES) == true && OrderSymbol () == Symbol() && OrderMagicNumber() == Magic_Number) Total_Current_Orders ++; }
3. Check if total number of sent order (1) is match with (or more than) total number of current order (2). If it's not, check the historical orders, if one of historical data was closed by TP, then close/delete all orders.
if (Total_Current_Order < Total_Send_Order) { for (pos = OrdersHistoryTotal() - 1; pos >= 0; pos --) { if (OrderSelect (pos, SELECT_BY_POS, MODE_HISTORY) == true && OrderSymbol () == Symbol() && OrderMagicNumber() == Magic_Number && OrderOpenTime () >= First_Order_Time) { if (OrderClosePrice () >= OrderTakeProfit()) { //--- Close and delete all OrderClose //--- When all is closed and deleted, nulling some parameters First_Order_Time = 0; Total_Current_Order = 0; Total_Send_Order = 0; } } } }
4. If you worry that the value of static / global declared variable may be lost when MT4 is restarted, then you can save them in a file or in MT4 Global Variable (https://docs.mql4.com/globals)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
example:
i have 4 trading. second trade are T/P, then close or delete all trade...have any idea??use magic number?