I’m not sure how to create code for an EA to increase lot size as an account grows and decrease lot size as the account shrinks. So if it starts at $1000 and moves to $2000 the lot size doubles where as if it falls to $500 it cuts in half.
double LotAdjust(){
string baseCurr = StringSubstr(Symbol(),0,3);
string crossCurr = StringSubstr(Symbol(),3,3);
double tickValue = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);
MqlTick last_tick;
SymbolInfoTick(_Symbol,last_tick);
double Ask=last_tick.ask;
// calculate risk based on free margin
//
double riskCapital = AccountInfoDouble(ACCOUNT_MARGIN_FREE) * Risk/100;
if(crossCurr == AccountInfoString(ACCOUNT_CURRENCY)) {
LTS = (riskCapital / StopLoss) / tickValue;
Print("Calculated lots (A/C currency = Quote currency): ", LTS);
} else if(baseCurr == AccountInfoString(ACCOUNT_CURRENCY)) {
//
LTS = ((Ask * riskCapital) / StopLoss) / tickValue;
Print("Calculated lots (A/C currency = Base currency): ", LTS);
} else if(crossCurr != AccountInfoString(ACCOUNT_CURRENCY)
&& baseCurr != AccountInfoString(ACCOUNT_CURRENCY)) {
LTS = riskCapital / (StopLoss * tickValue);
Print("Calculated lots (A/C currency neither Base nor Quote currency): ", LTS);
}
// snap lot size to broker-happy value
//
double lotStep = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);
double minLot = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
LTS = MathRound(LTS/lotStep) * lotStep;
if(LTS < minLot) {
LTS = minLot;
}
return(LTS);
// another look.....
/*if (UseMM)Lots=AccountEquity()/Close[0]/1000*Risk/100;
Lots = arrondirLots(Lots);
double minLot = MarketInfo(Symbol(), MODE_MINLOT);
double maxLot = MarketInfo(Symbol(), MODE_MAXLOT);
if (Lots<minLot)Lots=minLot;
if (Lots>maxLot)Lots=maxLot;
*/
}
this is what i use… see its mql5 though, but you can translate it to 4.
others can help improve it if it does not work for you or you find errors.
//---------------------------------------------- Input parameters ---!
input string Config_1 = ""; // Money Management
input double StartLotSize = 0.01; // The starting lot size
input double StartBalance = 50.0; // The starting balance
//---------------------------------------------- Money management ---!
double LotsOptimized(){
double LotStep=MarketInfo(Symbol(),MODE_LOTSTEP),LotSize=0;
if(LotStep==0.1){LotSize=NormalizeDouble(StartLotSize*(AccountFreeMargin()/StartBalance),1);}
if(LotStep==0.01){LotSize=NormalizeDouble(StartLotSize*(AccountFreeMargin()/StartBalance),2);}
if(LotSize<MarketInfo(Symbol(),MODE_MINLOT)){LotSize=MarketInfo(Symbol(),MODE_MINLOT);}
if(LotSize>MarketInfo(Symbol(),MODE_MAXLOT)){LotSize=MarketInfo(Symbol(),MODE_MAXLOT);}
return(LotSize);
}
Thanks for the reply! Is this mql4 or 5? If it’s 4, do I input this into the trade function section?