Bumping this up so others might help
Bumping this up so others might help
Please don't as instead of making it more likely that you will get help, it annoys people to go to an unread post that they have already read.
FYI
it may be just me, but I often cannot be bothered with reading code that is full of unnecessary line spaces.
I actually put the line spaces there so it wouldn't be difficult to read. I actually find reading text without spacing very annoying. Perhaps coding is a different ball game.
As I said, it might just be me, but I prefer to read posted code without scrolling.
I agree with @Keith Watford ! Too much white-space makes it unreadable!
The following post is a very old and the code is also very old, but it can give you an idea of what you should be doing:
PS! I altered and simplified the code here a bit in this post as apposed to the original post in the reference thread, so as to make it easier for you to understand it.Forum on trading, automated trading systems and testing trading strategies
Need moneymanagement LOT size formula based on SL and Account Risk!
Fernando Carreiro, 2014.01.09 02:37
OK guys! This code was written by me and it is what I use in my own EA's. It is quite complex because, besides the Risk% and StopLoss, it also takes into account the Margin Risk% as well as correct the Lot Size based on Minimum, Maximum and Step Size. It also always uses the minimum value of Current Balance and Equity instead of just using one of them. I feel it is safer that way.
Please note, however, that it does not use the spread in the calculation, because I calculate that separately when calculating the StopLoss to be used. The below function, thus uses a relative stop loss size. In my EA's the argument dblStopLossPips is calculated beforehand depending on various factors such as the strategy, spread, ATR, etc.; so the final value passed on to the dblLotsRisk() function is already a final value in order to calculate the Lot size to be used.
// Function to Determine Tick Point Value in Account Currency double dblTickValue( string strSymbol ) { return( MarketInfo( strSymbol, MODE_TICKVALUE ) ); } // Function to Determine Pip Point Value in Account Currency double dblPipValue( string strSymbol ) { double dblCalcPipValue = dblTickValue( strSymbol ); switch ( MarketInfo( strSymbol, MODE_DIGITS ) ) { case 3: case 5: dblCalcPipValue *= 10; break; } return( dblCalcPipValue ); } // Calculate Lot Size based on Maximum Risk & Margin double dblLotsRisk( string strSymbol, double dblStopLossPips, double dblRiskMaxPercent, double dblMarginMaxPercent ) { double dblValueAccount = MathMin( AccountEquity(), AccountBalance() ), dblValueRisk = dblValueAccount * dblRiskMaxPercent / 100.0, dblValueMargin = AccountFreeMargin() * dblMarginMaxPercent / 100.0, dblLossOrder = dblStopLossPips * dblPipValue( strSymbol ), dblMarginOrder = MarketInfo( strSymbol, MODE_MARGINREQUIRED ), dblCalcLotMin = MarketInfo( strSymbol, MODE_MINLOT ), dblCalcLotMax = MarketInfo( strSymbol, MODE_MAXLOT ), dblCalcLotStep = MarketInfo( strSymbol, MODE_LOTSTEP ), dblCalcLotLoss = MathRound( dblValueRisk / dblLossOrder / dblCalcLotStep ) * dblCalcLotStep, dblCalcLotMargin = MathRound( dblValueMargin / dblMarginOrder / dblCalcLotStep ) * dblCalcLotStep, dblCalcLot = MathMin( dblCalcLotLoss, dblCalcLotMargin ); if ( dblCalcLot < dblCalcLotMin ) dblCalcLot = dblCalcLotMin; if ( dblCalcLot > dblCalcLotMax ) dblCalcLot = dblCalcLotMax; return ( dblCalcLot ); }
Thank you very much. This was helpful!
p.s: Sorry about the white space

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I have written a script below to determine the lot size of an order based upon the stoploss size. I am using mt4 pepperstone using the standard demo account (leverage 500, if that matters). However, I get the same lot size, irrespective of the stoploss value.
Can someone tell me what I am doing wrong?