Hello
Let "Risk" is margin limit of operation that is being computed.
Let "NominalMargin = MarketInfo ( Symbol (), MODE_MARGINREQUIRED )" is margin for standard lor size.
Then size limit of the operation "SizeLimit" will be equal "Risk / NominalMargin".
Then it is required to find valid size of the operation.
Size of operation is valid if it is aligned to the lot step.
Then size of operation is valid if it is greater or equal than "MinLots" and less or equal than "MaxLots".
Condition "if(lot<MinLots)lot=MinLots;" is weak because it may lead to wrong risk accept.
For example, let "lot = 0.02" and "MinLots = 0.10", then "lot=MinLots" enlarges risk to 5 times of the allowed one.
So if "lot<MinLots" then "lot=0" must be.
Lot size calculation sample:
Let "NominalMargin = MarketInfo ( Symbol (), MODE_MARGINREQUIRED )" is margin for standard lor size.
Then size limit of the operation "SizeLimit" will be equal "Risk / NominalMargin".
Then it is required to find valid size of the operation.
Size of operation is valid if it is aligned to the lot step.
Then size of operation is valid if it is greater or equal than "MinLots" and less or equal than "MaxLots".
Condition "if(lot<MinLots)lot=MinLots;" is weak because it may lead to wrong risk accept.
For example, let "lot = 0.02" and "MinLots = 0.10", then "lot=MinLots" enlarges risk to 5 times of the allowed one.
So if "lot<MinLots" then "lot=0" must be.
Lot size calculation sample:
double RiskRate = 0.05 ; // 01
// 02
int init () // 03
{ // 04
double MaxLots = MarketInfo ( Symbol () , MODE_MAXLOT ) ; // 05
double MinLots = MarketInfo ( Symbol () , MODE_MINLOT ) ; // 06
double LotStep = MarketInfo ( Symbol () , MODE_LOTSTEP ) ; // 07
double NominalMargin = MarketInfo ( Symbol () , MODE_MARGINREQUIRED ) ; // 08
// 09
double Risk = RiskRate * AccountEquity () ; // 10
double SizeLimit = Risk / NominalMargin ; // 11
double LotSize ; // 12
// 13
if ( SizeLimit >= MinLots ) // 14
{ int Steps = MathFloor ( ( SizeLimit - MinLots ) / LotStep ) ; // 15
LotSize = MinLots + Steps * LotStep ; } // 16
else LotSize = 0 ; // 17
// 18
if ( LotSize >= MaxLots ) // 19
LotSize = MaxLots ; // 20
// 21
Alert ( " " ) ; // 22
Alert ( "LotSize = " , LotSize , " " , "lots" ) ; // 23
Alert ( "Output :" ) ; // 24
Alert ( " " ) ; // 25
Alert ( "SizeLimit = " , SizeLimit , " " , "lots" ) ; // 26
Alert ( "LotStep = " , LotStep , " " , "lots" ) ; // 27
Alert ( "MinLots = " , MinLots , " " , "lots" ) ; // 28
Alert ( "MaxLots = " , MaxLots , " " , "lots" ) ; // 29
Alert ( "NominalMargin = " , NominalMargin , " " , AccountCurrency () ) ; // 30
Alert ( "Processing :" ) ; // 31
Alert ( " " ) ; // 32
Alert ( "Risk = " , Risk , " " , AccountCurrency () ) ; // 33
Alert ( "RiskRate = " , RiskRate * 100 , " " , " %" ) ; // 34
Alert ( "AccountEquity () = " , AccountEquity () , " " , AccountCurrency () ) ; // 35
Alert ( "Input :" ) ; // 36
} // 37
Link to "How do I calculate lot size?" may be useful in anyway.Best regards
Ais
thanks bro
regards,
MANSTIR
Welcome
Ais:
Hello
Let "Risk" is margin limit of operation that is being computed.
Let "NominalMargin = MarketInfo ( Symbol (), MODE_MARGINREQUIRED )" is margin for standard lor size.
Then size limit of the operation "SizeLimit" will be equal "Risk / NominalMargin".
Then it is required to find valid size of the operation.
Size of operation is valid if it is aligned to the lot step.
Then size of operation is valid if it is greater or equal than "MinLots" and less or equal than "MaxLots".
Condition "if(lot<MinLots)lot=MinLots;" is weak because it may lead to wrong risk accept.
For example, let "lot = 0.02" and "MinLots = 0.10", then "lot=MinLots" enlarges risk to 5 times of the allowed one.
So if "lot<MinLots" then "lot=0" must be.
Lot size calculation sample:
Link to "How do I calculate lot size?" may be useful in anyway.
Let "NominalMargin = MarketInfo ( Symbol (), MODE_MARGINREQUIRED )" is margin for standard lor size.
Then size limit of the operation "SizeLimit" will be equal "Risk / NominalMargin".
Then it is required to find valid size of the operation.
Size of operation is valid if it is aligned to the lot step.
Then size of operation is valid if it is greater or equal than "MinLots" and less or equal than "MaxLots".
Condition "if(lot<MinLots)lot=MinLots;" is weak because it may lead to wrong risk accept.
For example, let "lot = 0.02" and "MinLots = 0.10", then "lot=MinLots" enlarges risk to 5 times of the allowed one.
So if "lot<MinLots" then "lot=0" must be.
Lot size calculation sample:
Link to "How do I calculate lot size?" may be useful in anyway.
Best regards
Ais
This is beautiful, but for line 15, why not just do:
int Steps = MathFloor ( SizeLimit / LotStep ) ; // 15
LotSize = Steps * LotStep ;//16
Why did you include MinLots into the steps?
Hi Will
Thanks for attention.
My method returns correct "LotSize" for all possible combinations of "MinLots" and "LotStep".
For example, if "MinLots = 1.0" and "LotStep = 0.4" and "SizeLimit = 1.1" my method will return "LotSize = 1.0" when your method will return "LotSize = 0.8", that is less than minimal allowed lot size "MinLots = 1.0".
My method returns correct "LotSize" for all possible combinations of "MinLots" and "LotStep".
For example, if "MinLots = 1.0" and "LotStep = 0.4" and "SizeLimit = 1.1" my method will return "LotSize = 1.0" when your method will return "LotSize = 0.8", that is less than minimal allowed lot size "MinLots = 1.0".
Best regards
Ais
Ais:
Hi Will
Thanks for attention.
My method returns correct "LotSize" for all possible combinations of "MinLots" and "LotStep".
For example, if "MinLots = 1.0" and "LotStep = 0.4" and "SizeLimit = 1.1" my method will return "LotSize = 1.0" when your method will return "LotSize = 0.8", that is less than minimal allowed lot size "MinLots = 1.0".
My method returns correct "LotSize" for all possible combinations of "MinLots" and "LotStep".
For example, if "MinLots = 1.0" and "LotStep = 0.4" and "SizeLimit = 1.1" my method will return "LotSize = 1.0" when your method will return "LotSize = 0.8", that is less than minimal allowed lot size "MinLots = 1.0".
Best regards
Ais
Hello Ais,
Thanks. This code really helped me in the expert I am building.
Sincerely,
Will
Welcome
Attention
Aforeposted code sample demonstrates very simplified approach to control risk limits.
It is necessary to take in computing both margin limit and stop-loss limit simultaneously.
Improved risk control code sample see in second page of "How do I calculate lot size?".
Best regards
Ais
//+------------------------------------------------------------------+ //| Lot size computation. | //+------------------------------------------------------------------+ void OnInitLotSize(){ at.risk.equity = 0; at.risk.total = 0; at.risk.chart = 0; } double LotSize(double risk){ /*double at.risk.new; // Export to init/start //double TEF.value, // Import from ComputeTEF // at.risk.equity // \ Import from //double at.risk.chart, at.risk.total; // _> ModifyStops //int op.code; // OP_BUY/OP_SELL // Import from SetDIR */ /* 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() - at.risk.equity; switch(MM.F0M1G2){ case MMMODE_FIXED: double perChrt = MM.PerChart, maxRisk = MM.MaxRisk; break; case MMMODE_MODERATE: // See https://www.mql5.com/en/articles/1526 Fallacies, Part 1: Money // Management is Secondary and Not Very Important. maxRisk = MathSqrt(MM.MaxRisk * ab); perChrt = MathSqrt(MM.PerChart * ab); break; case MMMODE_GEOMETRICAL: perChrt = MM.PerChart * ab; maxRisk = MM.MaxRisk * ab; break; } ComputeTEF(); double minLot = MarketInfo(Symbol(), MODE_MINLOT), lotStep = MarketInfo(Symbol(), MODE_LOTSTEP), perLotPerPoint = PointValuePerLot(), maxLossPerLot = (risk+Slippage.Pips*pips2dbl) * perLotPerPoint, size = perChrt / maxLossPerLot; // Must still round to lotStep. /*---- Compute lot size based on account balance and MM mode*/} /* 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. Using the lesser of size vs * AccountFreeMargin / MODE_MARGINREQUIRED should have been sufficient, but * the tester was generating error 134 even when marginFree should have been * OK. So I also use AccountFreeMarginCheck < 0 which agrees with the * tester. Reported at https://forum.mql4.com/35056 * * Second problem, after opening the new order, if free margin then drops to * zero we get a margin call. In the tester, the test stops with: "EA: * stopped because of Stop Out" So I make sure that the free margin * after is larger then the equity risk so I never get a margin call. */ string status = "SL>AE"; // Assume size < minLot while (true){ // Adjust for broker, test for margin, combine with TEF... size = MathFloor(MathMax(0,size)/lotStep)*lotStep; at.risk.new = size * maxLossPerLot; // Export for Comment if (size < minLot){ at.risk.new = 0; EA.status = status; return(0); } /* at.risk.equity += Direction( OrderType() ) * * (OrderClosePrice()-OrderStopLoss())*perPoint; * Summed for all open orders. * at.risk.total is summed for all open orders below Break even. * at.risk.chart is summed for all open orders below BE, this pair/TF */ if (at.risk.new+at.risk.chart > perChrt){ // one pair, one TF size = (perChrt-at.risk.chart)/maxLossPerLot; // Re-adjust lotStep status = "MaxRisk"; continue; } if (at.risk.new+at.risk.total > maxRisk){ // All charts size = (maxRisk-at.risk.total)/maxLossPerLot; status = "TotalRisk"; continue; } double AFMC = AccountFreeMarginCheck(Symbol(), op.code, size), eRisk = at.risk.equity + at.risk.new; if (AFMC*0.99 <= eRisk){ size *= 0.95; status = "Free Margin"; continue; } // Prevent margin call if new trade goes against us. break; } if (TEF.Enable01>0){ size = MathFloor(size*MathMin(1, TEF.value)/lotStep)*lotStep; if (oo.count == 0 && size < minLot) size = minLot; // Not below min at.risk.new = size * maxLossPerLot; // Export for Comment if (size < minLot){ at.risk.new=0; EA.status = "TEF = "+TEF.value; return(0); } } return(size); // We're good to go. } // LotSize double PointValuePerLot() { // Value in 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. }
Airat Safin:
Many thanks!!!
Hello
Let "Risk" is margin limit of operation that is being computed.
Let "NominalMargin = MarketInfo ( Symbol (), MODE_MARGINREQUIRED )" is margin for standard lor size.
Then size limit of the operation "SizeLimit" will be equal "Risk / NominalMargin".
Then it is required to find valid size of the operation.
Size of operation is valid if it is aligned to the lot step.
Then size of operation is valid if it is greater or equal than "MinLots" and less or equal than "MaxLots".
Condition "if(lot<MinLots)lot=MinLots;" is weak because it may lead to wrong risk accept.
For example, let "lot = 0.02" and "MinLots = 0.10", then "lot=MinLots" enlarges risk to 5 times of the allowed one.
So if "lot<MinLots" then "lot=0" must be.
Lot size calculation sample:
Link to "How do I calculate lot size?" may be useful in anyway.
Let "NominalMargin = MarketInfo ( Symbol (), MODE_MARGINREQUIRED )" is margin for standard lor size.
Then size limit of the operation "SizeLimit" will be equal "Risk / NominalMargin".
Then it is required to find valid size of the operation.
Size of operation is valid if it is aligned to the lot step.
Then size of operation is valid if it is greater or equal than "MinLots" and less or equal than "MaxLots".
Condition "if(lot<MinLots)lot=MinLots;" is weak because it may lead to wrong risk accept.
For example, let "lot = 0.02" and "MinLots = 0.10", then "lot=MinLots" enlarges risk to 5 times of the allowed one.
So if "lot<MinLots" then "lot=0" must be.
Lot size calculation sample:
Link to "How do I calculate lot size?" may be useful in anyway.
Best regards
Ais

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
HEREs some example of MM & Leverage calculation that i've been use now...
take a good look.......
i've added " AccountLeverage() != ", do u think it can work like that?
please give me your wise comment..
regards,
MANSTIR