Risk % of account balance code modify

 

I have been trying to edit my code to use an exact % of the account balance and it nearly does, but is always slightly off... I have tried many different edits and the following code is the closest that I can get... An example would be that on a $50,000 size account a 2% risk loss is $1,500 when it should be around $1,000. Can anyone see what I am doing wrong and if so please advise?

    double lotsize = MarketInfo(Symbol(),MODE_LOTSIZE) / AccountLeverage();
    double pipsize = 1 * 10;
    double maxlots = AccountBalance() / 100 * RiskPercent / lotsize * pipsize;
    if (StopLoss == 0) Print("OrderSend() error - stoploss can not be zero");
    double lots = maxlots / StopLoss * 10;
    
    // calculate lot size based on current risk
    double lotvalue = 0.001;
    double minilot = MarketInfo(Symbol(), MODE_MINLOT);
    int powerscount = 0;
    while (minilot < 1)
    {
        minilot = minilot * MathPow(10, powerscount);
        powerscount++;
    }
    lotvalue = NormalizeDouble(lots, powerscount - 1);
 

https://www.mql5.com/en/forum/138613.

case 'r': if(Switch=='r'){Ans=AB*Flexable*_2Pct/Stop__Loss/Pip_Values;}

//Dont Want To Lose AnyMore Than *F% ... Where (F%_Flexable)= My_Risk_Per =2.0

 
thanks, I will try that :-)
 

That helped thanks, my solution for anyone who is interested was:

    double lotsize = MarketInfo(Symbol(),MODE_LOTSIZE) / 1;
    double pipsize = 100 * 10;
    double maxlots = AccountEquity() / 100 * CurrentRisk;
    if (StopLoss == 0) Print("OrderSend() error - stoploss can not be zero");
    double lots = maxlots / StopLoss / PipValue;
Reason: