Trade Size = x% of account balance | Stop Loss = y% of account balance

 

Hi all,

‌I am trying to :

1. set the trade size to x% of account balance and

2‌. set the stop loss to y% of account balance

However, I found that trade size formula depends on stop loss and vice versa. This creates dependent loop between them.

‌‌This is the function that I found in another thread:

double AccountPercentStopPips(string symbol, double percent, double lots)
{
    double balance   = AccountBalance();
    double tickvalue = MarketInfo(symbol, MODE_TICKVALUE);
    double lotsize   = MarketInfo(symbol, MODE_LOTSIZE);
    double spread    = MarketInfo(symbol, MODE_SPREAD);
    double point     = MarketInfo(symbol, MODE_POINT);
    double ticksize  = MarketInfo(symbol, MODE_TICKSIZE);
    
    // fix for extremely rare occasion when a change in ticksize leads to a change in tickvalue
    double reliable_tickvalue = tickvalue * point / ticksize;           
    
    double stopLossPips = percent * balance / (lots * lotsize * reliable_tickvalue ) - spread;

    return (stopLossPips); 
}

Is my understanding correct?

H‌ow can I achieve setting the trade size and the stop loss base on the account balance, if it is at all possible?

‌‌Thanks.

 
Yes, you can use either one of the options 1 or 2, but not both at the same time. To overcome this problem you could probably neglect the loss from possible stoploss and use x% as a fraction of free margin to determine lots without a stoploss, and only then apply the given function for the option 2.
 
Got it. Thanks Stanislav!
Reason: