I have created this function to update the TP/SL of market orders. In tester it sometimes shows error 1 whereas the TP and new price are the same values. if TakeProfit() and _precio are the same, I expect the function to skip OrderModify().
Most probably it is a stupid error from my side, but after a whole day working on this I am unable to find the error in this code.
I would appreciate any help. Thanks
Here is the output
Hello,
separate this line...
if(OrderSelect(pos, SELECT_BY_POS) && OrderType() == _tipoOrden && OrderSymbol() == _Symbol && OrderMagicNumber() == MagicNumber)
make 2 lines like example...
if(OrderSelect(pos, SELECT_BY_POS)) { if(OrderType() == _tipoOrden && OrderSymbol() == _Symbol && OrderMagicNumber() == MagicNumber) {
I have created this function to update the TP/SL of market orders. In tester it sometimes shows error 1 whereas the TP and new price are the same values. if TakeProfit() and _precio are the same, I expect the function to skip OrderModify().
Most probably it is a stupid error from my side, but after a whole day working on this I am unable to find the error in this code.
I would appreciate any help. Thanks
Here is the output
When you want to compare given
price for TP or SL, with OrderTakeProfit() or OrderStopLoss(), you need to round that
price to TickSize.
Here is a function
for this purpose:
double RoundToTickSize(double price, string symbol) { if(price <= 0) { return(0); } double tickSize = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE); if(tickSize != 0) { price = MathRound(price / tickSize) * tickSize; } return(price); }
And here is how to use it:
if(_precio > Bid && RoundToTickSize(_precio, OrderSymbol())!= OrderTakeProfit()) // For TakeProfit { OrderModify( OrderTicket(), OrderOpenPrice(), OrderStopLoss(), RoundToTickSize(_precio, OrderSymbol()), OrderExpiration() ); }
Those two are identical. There is no difference.
OrderModify gives error 1 even checking the TP values before
You | Server |
---|---|
Change the SL to X | It is at X! |
Change the SL to X | It is at X! |
Change the SL to X | You are insane |
Unknown
What is a TICK? - MQL4 programming forum
When you want to compare given
price for TP or SL, with OrderTakeProfit() or OrderStopLoss(), you need to round that
price to TickSize.
Here is a function
for this purpose:
And here is how to use it:
The price passed to the function comes normalized
NormalizeDouble ( price, _Digits );
Is this different from your rounding function?
ERR_NO_RESULT
You | Server |
---|---|
Change the SL to X | It is at X! |
Change the SL to X | It is at X! |
Change the SL to X | You are insane |
What is a TICK? - MQL4 programming forum
The price value calculated as new TP is normalized and then passed to the function.
I print the price and TP values up to 7th decimal and divide them to confirm they are same values. What I am missing here?
if(DEBUG) printf(">>>> Ajustando TP de SELL #%d: TP=%.7f :: NewPrice=%.7f :: Div=%.7f", OrderTicket(), OrderTakeProfit(), _precio, OrderTakeProfit()/_precio);
They are the same values. Therefor you get "no result." What part of "check that you are moving the existing value at least a tick" was unclear?
This is the check that results TRUE when should not
if(_precio < Bid && _precio != OrderTakeProfit()) // For TakeProfit { if(DEBUG) printf(">>>> Ajustando TP de SELL #%d: TP=%.7f :: NewPrice=%.7f :: Div=%.7f", OrderTicket(), OrderTakeProfit(), _precio, OrderTakeProfit()/_precio); OrderModify( OrderTicket(), OrderOpenPrice(), OrderStopLoss(), _precio, OrderExpiration() ); continue; }
_precio != OrderTakeProfit()
Doubles are rarely equal. Understand the links in:
The ==
operand. - MQL4 programming forum
And for the third time: What part of "check that you are moving the existing value at least a tick" was unclear?
The price passed to the function comes normalized
Is this different from your rounding function?
Yes, different.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I have created this function to update the TP/SL of market orders. In tester it sometimes shows error 1 whereas the TP and new price are the same values. if TakeProfit() and _precio are the same, I expect the function to skip OrderModify().
Most probably it is a stupid error from my side, but after a whole day working on this I am unable to find the error in this code.
I would appreciate any help. Thanks
Here is the output