//++++ These are adjusted for 5 digit brokers. double pips2points, // slippage 3 pips 3=points 30=points pips2dbl; // Stoploss 15 pips 0.0015 0.00150 int Digits.pips; // DoubleToStr(dbl/pips2dbl, Digits.pips) int init(){ if (Digits == 5 || Digits == 3){ // Adjust for five (5) digit brokers. pips2dbl = Point*10; pips2points = 10; Digits.pips = 1; } else { pips2dbl = Point; pips2points = 1; Digits.pips = 0; } // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl ... //+------------------------------------------------------------------+ //| Lot size computation. | //+------------------------------------------------------------------+ double LotSize(double SLpoints){ /*double TSSF.value, TEF.value; // Import from ComputeTSSF //double at.risk; // Export to init/start/OpenNew */ /* This function computes the lot size for a trade. * Explicit inputs are SL relative to bid/ask (E.G. SL=30*points,) * Implicit inputs are the MM mode, the MM multiplier, count currently * filled orders by all EA's vs this EA/pair/period count and history. * Implicit inputs are all used to reduce available balance the maximum * dollar risk allowed. StopLoss determines the maximum dollar risk possible * per lot. Lots=maxRisk/maxRiskPerLot **************************************************************************/ /*++++ Compute lot size based on account balance and MM mode*/{ double ab = AccountBalance(); switch(Money.Management.F0M1G2){ case MMMODE_FIXED: at.risk = Money.Management.Multiplier; break; case MMMODE_MODERATE: // See https://www.mql5.com/en/articles/1526 Fallacies, Part 1: Money // Management is Secondary and Not Very Important. // %used/trade= at.risk = MathSqrt(Money.Management.Multiplier * ab)/ab; // ~const rate. at.risk = MathSqrt(Money.Management.Multiplier * ab * MathPow( 1 - at.risk, OrdersTotal() )); break; case MMMODE_GEOMETRICAL: at.risk = Money.Management.Multiplier * ab * MathPow(1 - Money.Management.Multiplier, OrdersTotal()); break; } double maxLossPerLot = SLpoints * PointValuePerLot(), /* Number of lots wanted = at.risk / maxLossPerLot rounded/truncated to /* nearest lotStep size. /* /* However, the broker doesn't care about the at.risk/account balance. They /* care about margin. Margin used=lots used*marginPerLot and that must be /* less than free margin available. */ marginFree = AccountFreeMargin()*0.95, // Allow some slack marginPerLot = MarketInfo( Symbol(), MODE_MARGINREQUIRED ), // So I use, the lesser of either. size = MathMin(marginFree / marginPerLot, at.risk / maxLossPerLot); /*---- Compute lot size based on account balance and MM mode*/} /*++++ Combine TSSF and trade size*/{ double minLot = MarketInfo(Symbol(), MODE_MINLOT); if (size < minLot){ // Multiple orders -> no free margin // [risk=9.48USD/40.80, margin=10.62/1334.48, MMM=1x1, ExtraOO=0] // 0.23 0.007 Print( "LotSize(SL=", DoubleToStr(SLpoints/pips2dbl, Digits.pips), ")=", size, " [risk=", at.risk, AccountCurrency(), "/", maxLossPerLot, ", margin=", marginFree, "/", marginPerLot, ", MMM=", Money.Management.F0M1G2,"x", Money.Management.Multiplier, ", ExtraOO=", OrdersTotal(), "]" ); return(0.0); // Risk limit. } if (!TSSF.Enable01) double adjFactTSSF = 1; else adjFactTSSF = MathMin(1,TSSF.value); if (!TEF.Enable01) double adjFactTEF = 1; else adjFactTEF = MathMin(1,TEF.value); double LotStep = MarketInfo(Symbol(), MODE_LOTSTEP); size = MathMax( minLot , MathFloor(size*adjFactTSSF*adjFactTEF/ LotStep)*LotStep ); /*---- Combine TSSF and trade size*/} at.risk = size * maxLossPerLot; // Export for Comment return(size); } // LotSize double PointValuePerLot() { // Value in the account currency of a Point of Symbol. /* In tester I had a sale: open=1.35883 close=1.35736 (0.00147) * gain$=97.32/6.62 lots/147 points=$0.10/point or $1.00/pip. * IBFX demo/mini EURUSD TICKVALUE=0.1 MAXLOT=50 LOTSIZE=10,000 * IBFX demo/standard EURUSD TICKVALUE=1.0 MAXLOT=50 LOTSIZE=100,000 * $1.00/point or $10.00/pip. * * https://forum.mql4.com/33975 CB: MODE_TICKSIZE will usually return the * same value as MODE_POINT (or Point for the current symbol), however, an * example of where to use MODE_TICKSIZE would be as part of a ratio with * MODE_TICKVALUE when performing money management calculations which need * to take account of the pair and the account currency. The reason I use * this ratio is that although TV and TS may constantly be returned as * something like 7.00 and 0.00001 respectively, I've seen this * (intermittently) change to 14.00 and 0.00002 respectively (just example * tick values to illustrate). */ return( MarketInfo(Symbol(), MODE_TICKVALUE) / MarketInfo(Symbol(), MODE_TICKSIZE) ); // Not Point. }
I posted my codes for doing this in this thread: https://www.mql5.com/en/forum/127798/page2#356736
It might help.
I tried yours, but had to adapt it to pass in the symbol (for other reasons). The numbers seem way off in the result though compared to the results I had with the above code. I can't decipher why, but the above code works 100% on regular brokers and yours seems to make too big of a trade.
I posted my codes for doing this in this thread: https://www.mql5.com/en/forum/127798/page2#356736
It might help.
Here is where I wonder if it all goes wrong:
int nDigits = 1; int _Digits = MarketInfo(symbol, MODE_DIGITS); if( _Digits == 3 || _Digits == 5 ) nDigits = 10; int POINTS = CalculatePoints(symbol, price, stoploss); double lotSize = CalculateLotSize(symbol, POINTS * nDigits, AccountEquity()*(RiskPercentage/100.0));
Should I even have the* nDigits? Does TICKVALUE need to be adjusted such as that or is that causing my lots to be too small?
As WHRoeder pointed in several of his posts - TICKVALUE should be always used in combination with TICKSIZE because sometimes TICKSIZE may not be equal to Point, e.g. your Point may be 0.00001 but TICKSIZE may be 0.00002. If that hapens than TICKVALUE will also be doubled so their ratio will also give you the correct result but you should always use them together.
PointValuePerLot()
///////////////////////////////////////////////////////// double perc_lot_by_stop_f(double Stop_points) { double lose_on_stop_lose=Stop_points*(MarketInfo(Symbol(),MODE_TICKVALUE)/100); double propotion=(AccountBalance()/100*Lot_perc_by_stop)/lose_on_stop_lose; double Loto_=MarketInfo(Symbol(),MODE_MINLOT)*propotion; if(Loto_>MarketInfo(Symbol(),MODE_MAXLOT)) Loto_=MarketInfo(Symbol(),MODE_MAXLOT); return(Loto_); }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I have created some code to determine a lot size based on risk and stoploss. However, it seems to calculate correctly on regular accounts, but incorrectly on a micro account (such as Alpari-UK) by a factor of 10. Can anyone see how to modify the code to fix this (or perhaps confirm it does work as expected in Alpari-UK micro account):