Firstly, you don't check for the last closed order, so any order in history that closed with profit will call the function close()
int close()
as the function is a bool, you should get into the habit of declaring it such
bool close()
if(OrderProfit()>0) { while(true) { result=close(); if(result!=TRUE) { error=GetLastError(); Alert("Last1 = ",error); } else break; } }
The fuction close() can only return false, so result will always be false and you will be stuck in an endless loop.
static int oldOrdersHistoryTotal; if(oldOrdersHistoryTotal==0) oldOrdersHistoryTotal=OrdersHistoryTotal(); if(oldOrdersHistoryTotal!=OrdersHistoryTotal()) { datetime time_close=0; int last_ticket=0; for(int i=(OrdersHistoryTotal()-1);i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) { if(OrderCloseTime()>time_close) { time_close=OrderCloseTime(); last_ticket=OrderTicket(); } } } if(OrderSelect(last_ticket,SELECT_BY_TICKET) && OrderProfit()>0) { //Code to close orders } oldOrdersHistoryTotal=OrdersHistoryTotal(); }
This should get you started
thanks for that code but it dose not work
using that code I am able to close only 2 trades but not all of them
thanks for that code but it dose not work
using that code I am able to close only 2 trades but not all of them
hello mate
in my opinion, if I use your code, every time new order pops up in history it detects it and terminates closing existing orders.
I need code which would close ALL opened orders (no matter the profit is) except one when you close any of currently opened orders which is profitable.
I have been trying to do that for over a week but not succeeded.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
my code should close ALL opened positions except one when last closed order was profitable but it returns error
i will be very grateful if you can help me
in my opinion error is in that part of the code