In different forum I've recieved something similar. So here it:
"Here's my lot sizing script. It is a lot more versatile than you really need for your requirement but it should be very easy to tweak if you want to adapt it. ATR based stops (in your case an ATR with a single period) are, in my opinion, the only real way to calculate stops:
/***
* GetUnits
* ========
* Parameters: |
* dblATR - The ATR upon which to base the calculation |
* dblStopLossMultiplier - the number of ATR's away from the entry price to use as a stop | |
* dblNominalCapital - the assumed amount of capital in US Dollars upon which to calculate a 1% maximum loss
*
* Returns: The number of units (lots) to trade that will only incur a maximum loss of 1% of capital if the order is stopped out |
*
* Description:
* When setting a specific stop-loss it is important to size a postion so that the loss does not exceed one percent of capital.
* This function uses the formula:
* Unit = 1% of Account divided by ATR * Dollars Per Point
*
* Example:
* We have $100,000 of capital and are prepared to risk 2% of this on each order. Our strategy places stops at 2.0 * ATR from the entry price.
*
* dblUnits = GetUnits( dblATR, 2.0, 100000, 2); // Returns number of lots to trade.
*
*/
double GetUnits( double dblATR, double dblStopLossMultiplier, double dblNominalCapital, double dblRiskPercent )
{
ErrorPush( "GetUnits");
double dblUnit, dblDollarsPerPoint, dblRiskOnOneContract, dblValueOfOneContract ;
int intMaxLots;
// On EURUSD this returns $1 for a 5-digit broker and $10 for a 4-digit broker as it is per point and not per pip (pip widely accepted as $.0001
dblDollarsPerPoint = MarketInfo(Symbol(),MODE_TICKVALUE) ;
// For EURUSD this returns a $100,000 contract size but by not hard-coding it we should get resilience for other currency pairs too
dblValueOfOneContract = dblDollarsPerPoint * MathPow(10,Digits);
// Now we calculate the risk if a gATR * dblStopLossMultiplier stop was applied to find the real risk in a stop-out situation
dblRiskOnOneContract = dblValueOfOneContract * dblATR * dblStopLossMultiplier;
// So now we know what could potentially be lost on one contract we size contracts to one per cent of our capital.
dblUnit = ((dblRiskPercent/100)*dblNominalCapital)/dblRiskOnOneContract;
// And because you can only trade in tenths of a contract with some brokers (mini lots) lets not support micro lots and round to one decimal place
dblUnit = NormalizeDouble( dblUnit, 1 );
// We return how many lots it is safe to trade for the current ATR, ATR Multiplier, current capital and % of capital we want to risk
/*********************************** *********************************** *********
* BROKER DEPENDENT RULES - DO THE LOTS HAVE TO BE ADJUSTED? *
* *
* If the proposed update breaks the broker's rules we have to adjust them *
*********************************** *********************************** *********
* These may differ from broker to broker
*/
//First define the rules
if ( StringFind(AccountCompany(), "OANDA")!=-1 || StringFind(AccountCompany(), "Oanda")!=-1 || StringFind(AccountCompany(), "oanda")!=-1 )
{
intMaxLots = 100;
}
if ( StringFind(AccountCompany(), "FXCM")!=-1 || StringFind(AccountCompany(), "fxcm")!=-1 )
{
intMaxLots = 100;
}
if ( StringFind(AccountCompany(), "Forex.com")!=-1 || StringFind(AccountCompany(), "forex.com")!=-1 )
{
intMaxLots = 50;
}
if (dblUnit > intMaxLots)
{
dblUnit = intMaxLots;
}
ErrorPop();
return (dblUnit);
}
Looking for BUY/SELL script with lot size calculator.
Example:
BUY/SELL 2% based on previous candle's HIGH/LOW.
If HIGH/LOW is above 50 pips we devide by 2. So SL would be 25 pips. if hits our SL of 25 pips we lose 2%.
If HIGH/LOW is below 49 pips we stick with the 49 pips instead of deviding.
Attaching a screenshot...
Two red lines represents the openning a trade and SL.
69pips between the Openning trade and previous candle's High. It is above 50 pips so we devide by 2 which we will get around 34 pips of Stop Loss.
If our balance is 200$ then we risk 2% =4$ for 34 pips.
I hope I was clear
![](https://c.mql5.com/forextsd/forum/165/chart_3.gif)
What you asked can be done easily. But since you specified a script, how do you make the decision when to buy and sell? Is it simply the time you drop the script on the chart? Then compare the price at the time to previous bar's hi/lo?
What you asked can be done easily. But since you specified a script, how do you make the decision when to buy and sell? Is it simply the time you drop the script on the chart? Then compare the price at the time to previous bar's hi/lo?
Yep exactly that. BUY/SELL when you drop on the chart. It is for my manual trading, if you want to know the details I'll be glad to share it with you but winning percentages isn't that great. I try to ride my profits and cut losses sooner.
Yep exactly that. BUY/SELL when you drop on the chart. It is for my manual trading, if you want to know the details I'll be glad to share it with you but winning percentages isn't that great. I try to ride my profits and cut losses sooner.
Sure I can write this for you in the next couple days when I find some time.
Yep exactly that. BUY/SELL when you drop on the chart. It is for my manual trading, if you want to know the details I'll be glad to share it with you but winning percentages isn't that great. I try to ride my profits and cut losses sooner.
By the way, when you drop the script on the chart, how does MT4 know if you want to buy or sell? From what I can see, it can be either way.
Or you can define something like if current price is closer to previous low then sell, mirror for buy. This is when the logic comes into play.
By the way, when you drop the script on the chart, how does MT4 know if you want to buy or sell? From what I can see, it can be either way. Or you can define something like if current price is closer to previous low then sell, mirror for buy. This is when the logic comes into play.
Thanks for the interest on the subject. But with the logic above you have mentioned I don't think it will work properly because if the previous bar's High and Low equals exactly it wouldn't work. Or perhaps if there is a gap on the next candle sure it would mess up the calculations. If you look at the screenshot and placed with your logic then the current price would have been closer to previous bar's High so it would place a buy order instead of a sell. Two identical scripts with buy and sell would do it, don't you think so?
Can you make the risk parameters changeable too? I have an accounts which trades 0.01lots (1cent per pip) and 0.01(10 cents per pip)
UPDATE: I think I've found another solution. The trade will place buy or sell based on the previous bar, so If I want to place sell trade then the previous bar is also closed lower. So it means we trade in the same direction as the previous bar.
Thanks for the interest on the subject. But with the logic above you have mentioned I don't think it will work properly because if the previous bar's High and Low equals exactly it wouldn't work. Or perhaps if there is a gap on the next candle sure it would mess up the calculations. If you look at the screenshot and placed with your logic then the current price would have been closer to previous bar's High so it would place a buy order instead of a sell. Two identical scripts with buy and sell would do it, don't you think so?
Can you make the risk parameters changeable too? I have an accounts which trades 0.01lots (1cent per pip) and 0.01(10 cents per pip)
UPDATE: I think I've found another solution. The trade will place buy or sell based on the previous bar, so If I want to place sell trade then the previous bar is also closed lower. So it means we trade in the same direction as the previous bar.![](https://c.mql5.com/forextsd/smiles/biggrin.png)
No, I didn't say that is your logic. My point is that you need to have to logic for the EA to determine what kind of orders it needs to submit. You just need to describe what your actual logic is.
Your logic needs to cover all possibility. If current price (which could be bid, ask or last price, you have to decide) is equal distance to hi/lo what to do?
No, I didn't say that is your logic. My point is that you need to have to logic for the EA to determine what kind of orders it needs to submit. You just need to describe what your actual logic is. Your logic needs to cover all possibility. If current price (which could be bid, ask or last price, you have to decide) is equal distance to hi/lo what to do?
I don't know if I understood you correctly, if buy then bid price, if sell then ask price? Does it have to that complicated? We calculate the lot size if below 50 pips then we don't devide by 2. if it is creater than 50 pips we devide by 2. The reason I think the EA won't work is that the trades are based on moving averages and there are instances that those moving averages whipsaws the trade on some occassions, so maybe we could end up losing more.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Looking for BUY/SELL script with lot size calculator.
Example:
BUY/SELL 2% based on previous candle's HIGH/LOW.
If HIGH/LOW is above 50 pips we devide by 2. So SL would be 25 pips. if hits our SL of 25 pips we lose 2%.
If HIGH/LOW is below 49 pips we stick with the 49 pips instead of deviding.
Attaching a screenshot...
Two red lines represents the openning a trade and SL.
69pips between the Openning trade and previous candle's High. It is above 50 pips so we devide by 2 which we will get around 34 pips of Stop Loss.
If our balance is 200$ then we risk 2% =4$ for 34 pips.
I hope I was clear