
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
{
double minlot = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN),
maxlot = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX),
lotstep = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
lots = MathFloor(lots/lotstep)*lotstep;
lots = MathMax(lots,minlot);
lots = MathMin(lots,maxlot);
return(NormalizeDouble(lots,2));
}
Much appreciate.
I just want to start write that code :)
You saved my more times.
Big thanks!
I am looking for good forum for 'MarketInfo & LotSize' computation.
Who know that type of good forum please share with me.
Thanks.
So, I need good advice, please.
02:00:00.069 - custom expert EURUSD,H1: | _lotSize - DoubleToString : 0.07
I believe you do still need NormalizeDouble().
Here is an example, using your code snippet (same applies to my example using MathFloor):
if (pair == "") pair = Symbol();
double lotStep = MarketInfo(pair, MODE_LOTSTEP),
minLot = MarketInfo(pair, MODE_MINLOT);
lots = MathRound(lots/lotStep) * lotStep;
if (lots < minLot) lots = 0; // or minLot
return(lots);
}
Called:
Result:
Now code adjusted:
if (pair == "") pair = Symbol();
double lotStep = MarketInfo(pair, MODE_LOTSTEP),
minLot = MarketInfo(pair, MODE_MINLOT);
lots = MathRound(lots/lotStep) * lotStep;
if (lots < minLot) lots = 0; // or minLot
return(NormalizeDouble(lots,2));
}
Result:
Result: 0.7000000000000001
What part of "your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.) Double-precision floating-point format - Wikipedia, the free encyclopedia" was unclear?
NormalizeDouble(0.7, 2) will produce the same exact result (or possibily 0.699999999999999.)
What part of "your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.) Double-precision floating-point format - Wikipedia, the free encyclopedia" was unclear?
NormalizeDouble(0.7, 2) will produce the same exact result (or possibily 0.699999999999999.)
I am not saying NormalizeDouble() is required for a valid OrderSend. That is why my first code snippet excluded it. Nor am I unclear about the manner in which floating point numbers are represented.
I edited the snippet to include it because I felt it better addressed my understanding of the OP's issue (essentially a display issue, which can be resolved either by using NormalizeDouble() in the "lot normalizer" code, or by using printf or DoubleToStr when the value needs to be displayed).
Maybe I have misunderstood the OP.
02:00:00.069 - custom expert EURUSD,H1: | _lotSize - DoubleToString : 0.07
(edited to remove unnecessary terseness)
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- get minimum stop level
double minstoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
Print("Minimum Stop Level=",minstoplevel," points");
double price=Ask;
//--- calculated SL and TP prices must be normalized
double stoploss=NormalizeDouble(Bid-minstoplevel*Point,Digits);
double takeprofit=NormalizeDouble(Bid+minstoplevel*Point,Digits);
//--- place market order to buy 1 lot
int ticket=OrderSend(Symbol(),OP_BUY,1,price,3,stoploss,takeprofit,"My order",16384,0,clrGreen);
if(ticket<0)
{
Print("OrderSend failed with error #",GetLastError());
}
else
Print("OrderSend placed successfully");
//---
}
This might be confusing to people because the MQL4 documentation uses NormalizeDouble() function in most of the examples.
It even goes as far as warning that unnormalized price can not be applied:
Note
At opening of a market order (OP_SELL or OP_BUY), only the latest prices of Bid (for selling) or Ask (for buying) can be used as open price. If operation is performed with a security differing from the current one, the MarketInfo() function must be used with MODE_BID or MODE_ASK parameter for the latest quotes for this security to be obtained.
Calculated or unnormalized price cannot be applied. If there has not been the requested open price in the price thread or it has not been normalized according to the amount of digits after decimal point, the error 129 (ERR_INVALID_PRICE) will be generated. If the requested open price is fully out of date, the error 138 (ERR_REQUOTE) will be generated independently on the slippage parameter. If the requested price is out of date, but present in the thread, the order will be opened at the current price and only if the current price lies within the range of price+-slippage.
And even in some of the better books on MQL they use it quite heavily.
It might depends on it's application a display issue is less dramatic then order or modification failures.
Personally i always upconvert to whole integer values so i rarely deal with it.