Hi
Here is the code I use for my money management. - Actually it shouldn't be called MM, as it is only calculates how many lots can I buy so that when I hit my stop loss I only loss a certain percentage of my account.
I think though that is one of the crucial aspects of forex- trading - as it leads to compound growth. I have not seen many other money management functions - but would like to hear others opions on money management.
Thanks
Code:
///////////////////////////////////////////////////////////////////////
//call by
Lots = Lotsize1(50,0.1,Symbol(),"mini");
double Lotsize1(int stoploss1, double risk1, string symbol1, string std_or_mini)
{
/* returns the number of lots one can buy for a certain symbol
* for a given risk level and stoploss. Returns Max 100 lots for a std or demo account.
*Returns max 50 lots for a mini account.
* The pip values for the function come from http://www.interbankfx.com/calculator.htm
*Inputs
*1 stoploss1 - what is your stoploss i,e, 10, 20, 30 etc pips, ensure that trailingstop is
* - less then stoploss1
* 2 risk1 - how much of your accounts free margin are you willing to loss
* 0.03 for 3%, 0.1 for 10%
* 3 std_or_mini - enter "std" for a standard or demo account, where the minimum transaction size is
* 1 lot of 100,000 of the base currency or enter
* "mini" - where the minimum transaction size is 1/10th
* the size of a standard lot, or 10,000 of the base currency.
* 4 symbol2 : the symbol your dealing with e.i. "EURUSD"
* The function determines your accounts leverage
* and returns the number of lots that you can then purchase
*/
double AccountLeverage1; //the leverage of the current account
double pip; //one pip price - from http://www.interbankfx.com/calculator.htm
//this changes automatically based on either 'std' or 'mini'
double lots1;
//tocheck: before attaching EA - are your symbol definitions right?
if(symbol1 == "EURUSD" || symbol1 == "GBPUSD")
{
pip = 10;
}
else if(symbol1 == "USDCHF")
{
pip = 8.1;
}
else if(symbol1 == "USDJPY")
{
pip = 8.4;
}
//TODO: Actually if it can't find the right symbol - fail gracefully.
else return(0);// problem - just expand to include all symbols trading with
if (std_or_mini == "mini") // transaction size is 1/10th the size of a standard lot
{
pip = pip/10;
}
AccountLeverage1 = AccountLeverage();
pip = pip*AccountLeverage1/100;
//TODO: Don't loss more then risk% of AccountFreeMargin - not working right now
lots1 = NormalizeDouble(((AccountFreeMargin()/(stoploss1*pip))*risk1),2);
if (lots1 > 50)
{
if (std_or_mini == "mini")
{
lots1 = 50;
return(lots1);
}
if (lots1 > 100)
{
lots1 = 100;
return(lots1);
}
}
return(lots1);
}
/////////