How to close the orders of same currency pair

To add comments, please log in or register
Cromo
259
Cromo  

I want to know how to close the orders of same currency pair.

For example, There are three open orders.

1. EU buy order with TP/SL

2.GU sell order with TP/SL

3. EU sell order with TP/SL

4. UJ sell order with TP/SL


Once the #1 reach to TP,  how can I close #3 also at the same time?

#3 does not still reach to TP/SL but I want to close together.

How can I code it?

void CloseOrder(int ClosePosition)
{
         for(int i=OrdersTotal()-1;i>=0;i--)
         {
                 int res;
                 if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
                 {
                         if(OrderMagicNumber()==MAGIC && OrderSymbol()==Symbol())
                         {
                                 if(OrderType()==OP_SELL && ClosePosition!=1)
                                 {
                                         res=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10,Silver);
                                 }
                                 else if(OrderType()==OP_BUY && ClosePosition!=-1) 
                                 {
                                         res=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10,Silver);
                                 }
                         }
                 }
         }
}
amando
2250
amando  

There are diffrent ways to do this but it depends, if you always have 4 open orders or if this is different


one way, you can check if the last cloed order reach the TP and then you can close all other one

an other way, if you always have 4 open orders, you can check how many open order you have and is this below 4 you can close the rest


in genreal, on OrderClose() you have to use bid and ask, bcs the OrderClosePrice is for the OrderHistory()

William Roeder
20214
William Roeder  
amando: in genreal, on OrderClose() you have to use bid and ask, bcs the OrderClosePrice is for the OrderHistory()

Wrong.

In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
  1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
              Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
    For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
  2. and check OrderSelect in case earlier positions were deleted.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
After Sleep and between server calls the market will have changed. You must update your variables. To use any pre-defined variables and series arrays you must call RefreshRates
          RefreshRates - Timeseries and Indicators Access - MQL4 Reference RefreshRates updates:
Predefined variables: Ask, Bars, Bid, Close[], High[], Low[], Open[], Point, Time[], Volume[]
          RefreshRates - Timeseries and Indicators Access - MQL4 Reference
          Predefined Variables - MQL4 Reference
Also updates: Hour, Minute, and Seconds
          Minute() returns wrong values - or am I wrong? - MQL4 and MetaTrader 4 - MQL4 programming forum
And updates: OrderClosePrice() on the next order select call.
sdlg
61
sdlg  
kajironpu:

I want to know how to close the orders of same currency pair.

For example, There are three open orders.

1. EU buy order with TP/SL

2.GU sell order with TP/SL

3. EU sell order with TP/SL

4. UJ sell order with TP/SL


Once the #1 reach to TP,  how can I close #3 also at the same time?

#3 does not still reach to TP/SL but I want to close together.

How can I code it?

you must use OrdersHistory() to look for the closed order with "tp" comment.
Nartan Braga
13
Nartan Braga  
hello there haw i knew transfer me profits from the site sistema to my account...! 
eggse i need a broke..!? anyone available let me know thanks have a wonderful day...!
William Roeder
20214
William Roeder  
sdlg: you must use OrdersHistory() to look for the closed order with "tp" comment.

Not all brokers modify comments with TP. brokers can change comments, including complete replacement.

nicholi shen
2392
nicholi shen  
You would need to create a list of open positions for a given symbol and then check if a position has moved from position to deal status. When that happens you'd close the remaining positions in the list. 
Cromo
259
Cromo  

Thank you for your comments.

I want to close the same current pair at the same time. If one order is closed with TP or SL, at the same time, same currency pair should be closed even it is loss.

I may check with using "for int " to find the same currency which is already ordered and close together.  I will try..

 for(int i=OrdersTotal()-1;i>=0;i--)
To add comments, please log in or register