#property copyright"forum thread"#property link"https://www.mql5.com/en/forum/448801"#property version"1.00"#property strictinputint bar_offset=1;//bar offset to acquire size inputdouble risk=1;//risk %double get_lot_by_bar_size_and_risk(int _bar,double _risk){
//error collectorint errors=0;
//get tick value for one lot for assetdouble tvol=(double)SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
//collect errors
errors+=GetLastError();
//get min and max lotdouble minlot=(double)SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);
//collect errors
errors+=GetLastError();
double maxlot=(double)SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);
//collect errors
errors+=GetLastError();
//lot stepdouble lotstep=(double)SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
//collect errors
errors+=GetLastError();
//if no errors if(errors==0){
//get size of bar if(_bar<Bars){
double size_in_ticks=(High[_bar]-Low[_bar])/_Point;
double amount_to_lose=(AccountBalance()/100.0)*_risk;
//so you are intending to lose the above amount in the above ticks// you can calculate the target value per tick based on the above double tick_value_target=amount_to_lose/size_in_ticks;
//we know the value of 1 tick if we had a trade of 1 lot//so if we divide the target tick value by the one lot tick value we get the lotsdouble lot=tick_value_target/tvol;
//check if within limits if(lotstep>0.0){
int steps=(int)MathFloor(lot/lotstep);
lot=((double)steps)*lotstep;
}
if(lot<minlot){lot=minlot;}
if(lot>maxlot){lot=maxlot;}
return(lot);
}
}
return(0.0);
}
intOnInit()
{
//---//don't call oninit while(!EventSetMillisecondTimer(44)){
Print("Setting timer please wait");
Sleep(44);
}
//---return(INIT_SUCCEEDED);
}
voidOnTimer(){
EventKillTimer();
double lot=get_lot_by_bar_size_and_risk(bar_offset,risk);
Print("Lot to use ("+DoubleToString(lot,2)+")");
ExpertRemove();
}
voidOnDeinit(constint reason)
{
}
voidOnTick()
{
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Ah , yeah it sets it to zero
Thanks @William Roeder
Here is the updated code @aliasgari524
You read last error, set it to zero, checked if it was non-zero, added zero, and set it to zero again.
Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error.
Thank you bro
Ah , yeah it sets it to zero
Thanks @William Roeder
Here is the updated code @aliasgari524