![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
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
Im curious as to why you chose .5 to add to the Step. Where did you get that number?
If it helps to clarify Irtron's reply, Lots * Percent / step + 0.5 is equivalent to MathRound(Lots * Percent / step). If you want to round up to the next step, rather than up or down to the nearest step, use MathCeil() rather than MathRound(). The use of rounding actually gives rise to a problem in the original code which I hadn't spotted - if Percent leads to a lot size which is less than half of LOTSTEP (e.g. 3% of 1 lot versus a LOTSTEP of 0.1), the original version of the code rounds down to zero.
If you can assume that MINLOT will always be a multiple of LOTSTEP - and I think that this assumption is safe, because it leads to all sorts of ambiguities in the interpretation of MINLOT if it's not true - then you could handle MINLOT just by modifying Irtron's original code so that it does return (MathMax(l * step, MarketInfo(symbol, MODE_MINLOT))); instead of return (l * step);. In other words, the original calculation except that it never returns a value less than MINLOT.
If it helps to clarify Irtron's reply, Lots * Percent / step + 0.5 is equivalent to MathRound(Lots * Percent / step). If you want to round up to the next step, rather than up or down to the nearest step, use MathCeil() rather than MathRound(). The use of rounding actually gives rise to a problem in the original code which I hadn't spotted - if Percent leads to a lot size which is less than half of LOTSTEP (e.g. 3% of 1 lot versus a LOTSTEP of 0.1), the original version of the code rounds down to zero.
If you can assume that MINLOT will always be a multiple of LOTSTEP - and I think that this assumption is safe, because it leads to all sorts of ambiguities in the interpretation of MINLOT if it's not true - then you could handle MINLOT just by modifying Irtron's original code so that it does return (MathMax(l * step, MarketInfo(symbol, MODE_MINLOT))); instead of return (l * step);. In other words, the original calculation except that it never returns a value less than MINLOT.
Right, I would hate to inject some arbitrary number into the mix. Assumptions kill.
Thanks for all your efforts. Looks like I can use this.
Jonus
Yes, I am using a broker now whos Minlot is .20 an the Lotstep is .10.
However, in manual trading they Dont allow closing anything less than .20.
Out of sheer curiosity, what happens if you do a partial close which leaves you with a remaining position which is smaller then minlot?
For example, let's say that you go long 1.00 lots and then close 0.90 lots, leaving you long 0.10 versus a minlot of 0.20. I can think of three things which might happen here, and I'd be interested to know which the broker is enforcing:
In all these cases, a simple function which calculates an amount of lots to close based on minlot and lotstep potentially isn't going to be sufficient. The function would potentially need to take into account your open positions and their implications, rather than just minlot and lotstep. The only way round this would be to make sure that you never place opening or closing orders which aren't a multiple of minlot.
Its indeed a conundrum. Once while trading manually I got myself into a situation where I could not close the remaining part of the trade. Yeh go figure!
Anyway Im looking for a new Broker. Preferably an ECN with tight spreads and small comissions. And of course using MT4. Any suggestions?
Its indeed a conundrum. Once while trading manually I got myself into a situation where I could not close the remaining part of the trade. Yeh go figure!
Anyway Im looking for a new Broker. Preferably an ECN with tight spreads and small comissions. And of course using MT4. Any suggestions?
After many tests and trials, this is the final solution I came up with. It handles all cases pretty well, with some assumptions. One assumption is that minlot is => lotstep and is a multiple of lotstep. It will try to return at least 1 lotstep regardless of the percentage passed to the routine (unless zero). However if that doesnt leave the minlot size, it will subtract 1 lotstep from the value. This could return zero and is an arbitrary behavior, but my preference.
double CloseSize(double Percent, double Lots)
{
double lotstep, minlot, Z;
int steps;
if (Percent==1) return(Lots); //close all !
if (Percent==0) return(0); //close none !
lotstep = MarketInfo(Symbol(), MODE_LOTSTEP);
minlot = MarketInfo(Symbol(), MODE_MINLOT);
steps = MathRound(NormalizeDouble(Lots * Percent,2) / lotstep);
if (steps<1) steps=1; //force the minimum size
if (Lots - (steps * lotstep) >= minlot) //compare remainder to minlot
{return(steps * lotstep);} //OK!
else //not OK!
{return((steps * lotstep)-lotstep);} //could be zero if steps=1
}