Try printing the values of marginAllot, PercentRisk and pipsToSL to narrow down where the negative number is coming from. My speculative guess is pipsToSL is being passed as a negative number under certain circumstances.
NormalizeDouble(posSize,2);
This line does nothing.- Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
- SL/TP (stops) need to be normalized to tick size. (On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 forum) and abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 forum
- Only the open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on Metals. So do it right: Trailing Bar Entry EA - MQL4 forum or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 forum
- Lot size must also be adjusted to a multiple of LotStep. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
Try printing the values of marginAllot, PercentRisk and pipsToSL to narrow down where the negative number is coming from. My speculative guess is pipsToSL is being passed as a negative number under certain circumstances.
I was actually able to find some places in the rest of the code where pipsToSL was incorrectly calculated as negative. Thanks.
- This line does nothing.
- Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
- SL/TP (stops) need to be normalized to tick size. (On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 forum) and abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 forum
- Only the open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on Metals. So do it right: Trailing Bar Entry EA - MQL4 forum or Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 forum
- Lot size must also be adjusted to a multiple of LotStep. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
I've read of you posting about this before, but I didn't understand your point until your very last link. Thanks.
Why would I still be getting answers to multiple decimal places if I'm using MathRound()? I've tried using DoubleToString but that didn't help at all. Is that due entirely to the limitations of Print() or is there something else wrong?
posSize = posSize * 10; MathRound(posSize); posSize = posSize / 10;
NoLimitations: Why would I still be getting answers to multiple decimal places if I'm using MathRound() | MathRound returns a double; infinite number of decimal places. |
NoLimitations: Why would I still be getting answers to multiple decimal places if I'm using MathRound() | MathRound returns a double; infinite number of decimal places. |
Not according to the doc page for it. https://docs.mql4.com/math/mathround
"Return Value
Value rounded till to the nearest integer."
If it is supposed to but doesn't, then MQ needs to be notified, or they need to put a warning label on the function.
JD4 is right, the doc page says it rounds to the nearest integer. I'm not sure of another way to accomplish what I was trying to above without rounding or cutting off a certain number of decimal places. What would be the point of MathRound() or round() if they don't return a whole number?
I also just tried using this to no avail. Possibly DTS simply changes the accuracy of a value for appearance and not the actual value? I don't see any other options.
posSize = posSize * 100; DoubleToString(posSize,0); StringToDouble(posSize); posSize = posSize / 100;
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Within my strategy, I use a function GetPosSize() to calculate position sizes. I'm only using the strategy with EURUSD at the time being and for the life of me I can't figure out why posSize is returning a negative value, resulting in an OrderSend error 4051. Any help would be greatly appreciated. I'll post all relevant code here. No other calculations towards posSize are done throughout the code, only the function is called. All three separate pieces of code are within the function.