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.
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.
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.
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().
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" ?
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:
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.
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!
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:
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().
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.
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?
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().

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
This piece of code is executing... or not... a bit random... not sure exactly why...
Kindly advise... Appreciated !
Later note: I am closing only Sell and Buy orders !
Please do not send me to the manual ... i read it.