Hi everyone
I try to close some orders (buy/sell) with OrderClose()
My code is this:
I call functions like this:
As you can see firstly i try to close buy orders (that it work clearly)
but in next when i try to close sell orders i got invalid price error
Do you know whats wrong?
Pass the normalized price
NormalizeDouble(Bid, Digits()) NormalizeDouble(Ask, Digits())
Call RefreshRates() before every OrderClose()
// #1 Update prices (Bid/Ask) RefreshRates(); // #2 Normalize the price double price = NormalizeDouble(Bid, Digits()); // #3 Attempt to close OrderClose(...)
while(!OrderClose(OrderTicket(), OrderLots(), Ask, slippage, clrYellow))
Don't use an infinite loop. You risk getting looped. Use the for() operator to limit the maximum number of iterations
Simple example
bool orderClose(int ticket, int type, double volume, int attempts = 3) { for(int i = 1; i <= attempts; i++) { RefreshRates(); double price = type == 0 ? NormalizeDouble(Bid, Digits()) : NormalizeDouble(Ask, Digits()); ResetLastError(); if(OrderClose(ticket, volume, price, 999)) return(true); PrintFormat("OrderClose error %i. Ticket %i", GetLastError(), ticket); } return(false); }
- Each server call takes time. When it finishes, your predefined variables will be wrong. RefreshRates after each call.
- Or, stop using Bid/Ask and use OrderClosePrice() to be direction independent.
- Prices you get from the terminal (Bid/Ask) are already normalized.
Is the "before" option much worse? Not only calling the server can take time. I prefer to update prices immediately before use (in trading functions).
- Or, stop using Bid/Ask and use OrderClosePrice() to be direction independent.
I collect orders in an array. Like this:
struct STRUCT_ORDER { double price; double stopLoss; double takeProfit; double volume; int ticket; int type; }; STRUCT_ORDER orders[];
Do you think "OrderSelect() + OrderClosePrice()" is better than "RefreshRates() + ternary_operator"?
- Vladislav Boyko #:
Is the "before" option much worse? Not only calling the server can take time. I prefer to update prices immediately before use (in trading functions).
Do you think "OrderSelect() + OrderClosePrice()" is better than "RefreshRates() + ternary_operator"?
On the first call, the refresh does nothing. After the last call, your code goes on; does the remaining code use any predefined variables and now require a refresh also? Are you sure?
-
Only blocking calls take time, what other calls do you have?
-
OCP does not require a refresh and a test. Simplify.
On the first call, the refresh does nothing. After the last call, your code goes on; does the remaining code use any predefined variables and now require a refresh also? Are you sure?
If I need the value of a predefined variable to be actual, then I call RefreshRates() before using the predefined variable. I understand you, thanks.
Usually I also do this check:
bool orderClose(int ticket, int type, double volume, int attempts = 3) { for(int i = 1; i <= attempts; i++) { if(isTradeAllowed() < 0) return(false); RefreshRates(); double price = type == 0 ? NormalizeDouble(Bid, Digits()) : NormalizeDouble(Ask, Digits()); ResetLastError(); if(OrderClose(ticket, volume, price, 999)) return(true); PrintFormat("OrderClose error %i. Ticket %i", GetLastError(), ticket); } return(false); } int isTradeAllowed(uint MaxWaiting_sec = 10) { if(!IsTradeAllowed()) { uint StartWaitingTime = GetTickCount(); Print("Trade flow is busy! Waiting..."); while(true) { if(IsStopped()) { Print("The expert has been stopped by the user!"); return(-1); } if(GetTickCount() - StartWaitingTime > MaxWaiting_sec * 1000) { Print(StringConcatenate("Waiting limit exceeded (", MaxWaiting_sec, " sec.)!")); return(-2); } if(IsTradeAllowed()) { Print("Trade flow is free!"); return(0); } Sleep(100); } } return(1); }
Unless you have analyzed the order history before. In this case, the OCP will contain the price of the closed order from the history. I find it much easier to use a bid/ask (+ RefreshRates()) than it is to consider cases like this. Or re-select an order whose properties are already known.
I'm just interested in your opinion. Thank you for your responses
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi everyone
I try to close some orders (buy/sell) with OrderClose()
My code is this:
I call functions like this:
As you can see firstly i try to close buy orders (that it work clearly)
but in next when i try to close sell orders i got invalid price error
Do you know whats wrong?