automatic order closing

 
Hello dear Friends, 

This piece of code is executing... or not... a bit random... not sure exactly why... 

if ((AccountProfit()/AccountEquity())*100>SlingShotPerC) { 

SlingShot = 1; 
while(OrdersTotal()!=0) { 

for(int Sx=OrdersTotal()-1;Sx>=0;Sx--) 
{RefreshRates(); 
if(!OrderSelect(x,SELECT_BY_POS,MODE_TRADES)) continue; 
if(OrderType()==OP_BUY || OrderType()==OP_SELL) 
{ 
if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrRed)){Alert(GetLastError());} 
else Alert("All Closed"); Sleep (200); 
} 

Alert ("SLINGSHOT ACTIVE - YOU MAY WANT TO ANALYZE THE TRADING CONTEXT: SLINGHSOT ACTIVE FOR HOURS::",SlingShotMsec,"::::::"); 

}} 

Sleep(SlingShotMsec); 

} 


Kindly advise...  Appreciated ! 


Later note: I am closing only Sell and Buy orders !

Please do not send me to the manual ... i read it.

 
CB:

This piece of code is executing... or not... a bit random... not sure exactly why... 
Please do not send me to the manual ... i read it.

(1) IF you've read the manual, you'd have known NOT to supply OrderClosePrice() here:

if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrRed)){Alert(GetLastError());} 

(2) Not sure if your "piece of code" includes this line? If not, then the problem is likely elsewhere - need to see more of your code.

if ((AccountProfit()/AccountEquity())*100>SlingShotPerC) { 

(3) This message's location is too premature:

else Alert("All Closed"); Sleep (200); 

(4) Be aware that when you have orders other than Buy and Sell, and if you run multiple EA, this code will get you into infinite loop.


 
Seng Joo Thio: (1) IF you've read the manual, you'd have known NOT to supply OrderClosePrice() here:

Where in the manual? Using OCP (instead of Bid/Ask) is fine (no need to check order type for close price.) But if you potentially close multiple orders, you must call RefreshRates after the server call and before the next OrderSelect.

 
William Roeder:

Where in the manual? Using OCP (instead of Bid/Ask) is fine (no need to check order type for close price.) But if you potentially close multiple orders, you must call RefreshRates after the server call and before the next OrderSelect.


great... so what seems to be the issue...?


to answer Seng in the same message: i am closing just Buy and Sell orders.

 
William Roeder:

Where in the manual? Using OCP (instead of Bid/Ask) is fine (no need to check order type for close price.) But if you potentially close multiple orders, you must call RefreshRates after the server call and before the next OrderSelect.

I do agree with you about using OrderClosePrice().

However I don't see why you "must call RefreshRates() before OrderSelect" ? You don't. OrderClosePrice()returns the current close price according to the type AFTER OrderSelect() is called, independently of any usage of RefreshRates().

 
CB:


great... so what seems to be the issue...?


to answer Seng in the same message: i am closing just Buy and Sell orders.

         for(int Sx=OrdersTotal()-1;Sx>=0;Sx--)
           {
            RefreshRates();
            if(!OrderSelect(x,SELECT_BY_POS,MODE_TRADES)) continue;
           ...

What is "x" ?

 
Alain Verleyen: However I don't see why you "must call RefreshRates() before OrderSelect" ? You don't. OrderClosePrice()returns the current close price

I never said before the OrderSelect, I said between server calls. It can take minutes for server calls to complete during news. OCP becomes Bid or Ask when selected. They don't update unless RefreshRates is called. After Sleep and between server calls the market will have changed. You must update your variables before using any Predefined Variables or series arrays — you must call RefreshRates. 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 programming forum
And updates: OrderClosePrice() on the next OrderSelect call.
 
William Roeder:

Where in the manual? Using OCP (instead of Bid/Ask) is fine (no need to check order type for close price.) But if you potentially close multiple orders, you must call RefreshRates after the server call and before the next OrderSelect.

Whoa! that is very convenient! 

 
William Roeder:

I never said before the OrderSelect, I said between server calls. It can take minutes for server calls to complete during news. OCP becomes Bid or Ask when selected. They don't update unless RefreshRates is called. After Sleep and between server calls the market will have changed. You must update your variables before using any Predefined Variables or series arrays — you must call RefreshRates. RefreshRates updates:

Alain Verleyen:

However I don't see why you "must call RefreshRates() before OrderSelect" ? You don't. OrderClosePrice()returns the current close price according to the type AFTER OrderSelect() is called, independently of any usage of RefreshRates(). 

Curiously, I wrote these few lines in the hope of finding out whether OrderClosePrice() "refreshes" the price it returns... 

void OnTick()
  {
   while (true)
   {
      for (int i=OrdersTotal(); i>=0; i--)
      {
         if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
            if (OrderClose(OrderTicket(),OrderLots(),OrderClosePrice()*2,0))
               break;
            else
               Print (GetLastError(), ", ", OrderClosePrice()*2);
      }
      Sleep(1000);
   }
  }

I run this EA, then randomly open any buy or sell order... and I realized that they get closed regardless... no error at all... which seemed to imply that OrderClose() function ignores the close price supplied... you guys know why?


Update: (1) Right, different behavior for different broker, and (2) Found a broker that does not ignore the close price, and it is apparent that OrderClosePrice() DOES return the latest price - no need to call RefreshRates().

 
William Roeder:

I never said before the OrderSelect, I said between server calls. It can take minutes for server calls to complete during news. OCP becomes Bid or Ask when selected. They don't update unless RefreshRates is called. After Sleepand between server calls the market will have changed. You must update your variables before using any Predefined Variables or series arrays — you must call RefreshRates. RefreshRates updates:

So it seems I have to repeat again as you don't listen. (and by the way you explicitly wrote "before the next OrderSelect" and it's plain wrong concerning OrderClosePrice()).

RefreshRates() has nothing to do with OrderClosePrice(). RefreshRates() will NOT update OrderClosePrice(). OCP is NOT a Predefined Variables or series arrays.

OrderSelect() will refresh OrderClosePrice() and nothing else.

Please check it before arguing more.

 
Seng Joo Thio:

Curiously, I wrote these few lines in the hope of finding out whether OrderClosePrice() "refreshes" the price it returns... 

I run this EA, then randomly open any buy or sell order... and I realized that they get closed regardless... no error at all... which seemed to imply that OrderClose() function ignores the close price supplied... you guys know why?

Because it depends on the execution type (if it's an ECN broker or not). An "instant" execution needs a price, a "market" execution will use the market price whatever the price provided.

Update: (1) Right, different behavior for different broker, and (2) Found a broker that does not ignore the close price, and it is apparent that OrderClosePrice() DOES return the latest price - no need to call RefreshRates().

Right. And if for some reasons, you need to refresh OrderClosePrice() (with lastest ask or bid), the only way to do it is with OrderSelect(), for example in a loop with error management.
Reason: