Last year I published on ordersend-script which computes the lotsize based on risk.
In the meantime I have found a nicer way of doing the same, so I decided to use jmw1970's way in my stuff (cui honorem, honorem

).
Attached you will find a mql4-file. This file consists all parts you have to insert to your existing EA or ordersend-script to computed lots based on risk and stoploss (not pips, the price!).
If you do not know the price and want to deal with fixed pips, change the external variable "CalcWithSlPipsIfNoSlGiven" to your needs.
That are the external vars, find the functions / instructions in the attached mql4-file (in fact you only have to copy&paste all to your exisiting ea)
Code: Select all
//calculate lots based on risk and stoploss, fxdaytrader (calclots function by jmw1970, other parts by WHRoeder, mod./adapted by fxdaytrader, see code for details)
extern double lots = 0.1;
extern string rbclh0="Use risk based lotsize, instead of fixed lots?";
extern string rbclh1=" if no SL is specified, the script calculates with that pips";
extern bool UseLotsPerRiskCalculation=TRUE;
extern double RiskPercentage=2.5;
extern string rbclh2="----: advanced lotsize calculation settings:";
extern double CalcWithSlPipsIfNoSlGiven = 30.0;//calc with this stoploss-pips distance if no sl-price is given
extern bool DoFreeMarginCheck=true; //should check if there is enough free margin to open the trade
extern bool DoFreeMarginForSlAlsoCheck=true;//checks if there is also enough free margin for a (potentially) SL-hit
To compute the lotsize based on risk and SL, you could insert right before the ordersend(-command:
lots = CalcLotsAndCheckMargin(Symbol(),OP_BUY,lots,RiskPercentage,Bid,0.0);
if the last var is 0, then a distance of price-CalcWithSlPipsIfNoSlGiven pips will be used, if you want to specify a certain price it goes analogue, for example:
double stoplossprice=1.3590;
lots = CalcLotsAndCheckMargin(Symbol(),OP_BUY,lots,RiskPercentage,Bid,stoplossprice);
please try

You do not have the required permissions to view the files attached to this post.