I also tried the last order close code by closing the last buy ticket , but it closes all buy orders,
Can you help me please,
int last_opened_ticket_buy() { datetime lastTime_b = 0; int lastTicket_b; for( int a=OrdersTotal()-1; a>= 0 ; a--) if ( OrderSelect(a,SELECT_BY_POS,MODE_TRADES) && OrderMagicNumber()== magic && OrderSymbol()==Symbol() && OrderType()==OP_BUY ) { if ( OrderOpenTime()>=lastTime_b ) { lastTime_b=OrderOpenTime(); lastTicket_b=OrderTicket(); } } return(lastTicket_b); }
It would be a safer practice to initialize lastTicket_b to something like -1.
You started your loop at OrdersTotal() - 1.
OrdersTotal() - 1 is the Oldest Order, 0 would be the most recent order.
Make sure to account for unexpected changed in OrdersTotal() while your loop is running, some orders can be closed or opened while your program is running if Ticks are coming in really fast.
If a change in OrdersTotal() happens during the loop, make sure your program reacts as you expect it to. (Especially if you plan to close multiple positions in a single loop)
Failing to do so may result in orders being skipped in the loop, so they stay open without the EA noticing it..
Also, always check the returned value of OrderSelect, it is a bool.
Prepare an error path if it fails.
You started your loop at OrdersTotal() - 1.
OrdersTotal() - 1 is the Oldest Order, 0 would be the most recent order.
Make sure to account for unexpected changed in OrdersTotal() while your loop is running, some orders can be closed or opened while your program is running if Ticks are coming in really fast.
If a change in OrdersTotal() happens during the loop, make sure your program reacts as you expect it to. (Especially if you plan to close multiple positions in a single loop)
Failing to do so may result in orders being skipped in the loop, so they stay open without the EA noticing it..
Also, always check the returned value of OrderSelect, it is a bool.
Prepare an error path if it fails.
ARRESSS:
Hello, I am trying to close the last order only, for example I have 3 buy orders and 1 sell orders, and I want to close the last buy order only, not all 3, only the last one, I tried the current order close code, but instead of closing the last one, it closes all buy orders, Could you help me :
void close_last_buy() { bool last_Buy_Closed_Orders=true; for ( int m=OrdersTotal()-1; m>=0; m--) { if ( OrderSelect(m,SELECT_BY_POS,MODE_TRADES) ) { if (OrderSymbol()==Symbol() && OrderMagicNumber()==magic ) { if ( OrderType()== OP_BUY && last_Buy_Closed_Orders ) //&& m==1) { close_it=OrderClose(OrderTicket(),OrderLots(),Bid,slippage,Lime); //break; ok this code truu last_Buy_Closed_Orders=false; if ( close_it==0 ) { error_nbr=GetLastError(); error_strg=ErrorDescription(error_nbr); Print("Alert_Error_in_close_buy!!!!"+IntegerToString(error_nbr)+error_strg); } } } } } }
Jeremie Courchesne #:
It would be a safer practice to initialize lastTicket_b to something like -1.
You started your loop at OrdersTotal() - 1.
OrdersTotal() - 1 is the Oldest Order, 0 would be the most recent order.
Make sure to account for unexpected changed in OrdersTotal() while your loop is running, some orders can be closed or opened while your program is running if Ticks are coming in really fast.
If a change in OrdersTotal() happens during the loop, make sure your program reacts as you expect it to. (Especially if you plan to close multiple positions in a single loop)
Failing to do so may result in orders being skipped in the loop, so they stay open without the EA noticing it..
Also, always check the returned value of OrderSelect, it is a bool.
Prepare an error path if it fails.
It would be a safer practice to initialize lastTicket_b to something like -1.
You started your loop at OrdersTotal() - 1.
OrdersTotal() - 1 is the Oldest Order, 0 would be the most recent order.
Make sure to account for unexpected changed in OrdersTotal() while your loop is running, some orders can be closed or opened while your program is running if Ticks are coming in really fast.
If a change in OrdersTotal() happens during the loop, make sure your program reacts as you expect it to. (Especially if you plan to close multiple positions in a single loop)
Failing to do so may result in orders being skipped in the loop, so they stay open without the EA noticing it..
Also, always check the returned value of OrderSelect, it is a bool.
Prepare an error path if it fails.
Thank you very much Jeremie, you are right, I do not foresee the other scenario in case of failure. I will take your advice into consideration. thank you very much.
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
Hello, I am trying to close the last order only, for example I have 3 buy orders and 1 sell orders, and I want to close the last buy order only, not all 3, only the last one, I tried the current order close code, but instead of closing the last one, it closes all buy orders, Could you help me :