1 (edited by Roughey 2024-09-09 13:25:03)

Topic: LotSize Calculation Percentage of Balance

I try to find out how we can use Lotsize calculation of x percent of Account Balance.

Here is my code

// Function to calculate lot size based on account balance and risk percentage
double CalculateLotSize(double risk_percentage)
{
    // Get the current account balance
    double account_balance = AccountInfoDouble(ACCOUNT_BALANCE);
    
    // Define the amount of balance to risk (risk_percentage of the balance)
    double risk_amount = (risk_percentage / 100.0) * account_balance;

    // Get the tick value and point size for the symbol
    double tick_value = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
    double tick_size = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
    
    // Risk per lot (pip value per lot)
    double risk_per_lot = tick_value / tick_size;

    // Assuming 100 pips as stop loss (modify this based on your strategy)
    double stop_loss_pips = 100;
    
    // Calculate lot size based on risk amount and stop loss
    double lot_size = risk_amount / (stop_loss_pips * risk_per_lot);
    
    // Ensure the lot size is within the broker's minimum and maximum allowed limits
    double min_lot = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MIN);
    double max_lot = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MAX);
    double step_lot = SymbolInfoDouble(symbol, SYMBOL_VOLUME_STEP);
    
    // Round the lot size to the nearest lot step
    lot_size = MathMax(min_lot, MathMin(max_lot, NormalizeDouble(lot_size, int(log10(1.0/step_lot)))));

    return lot_size;
}

The problem here is that the Lotsize Calculation depends of the stopLoss. So i ask how i can get the stoploss of the strategy which will open a trade next time. Maybe someone have idea or a better solution

    // Calculate lot size based on risk amount and stop loss
    double lot_size = risk_amount / (stop_loss_pips * risk_per_lot);

How can i get the stopLoss out of the signal ? someone have an idea..

Re: LotSize Calculation Percentage of Balance

damn NOBODY AN IDEA?
I try 1000 times. i need the Stoploss of an signal but i dont know how to fetch it.

it must be something with this in OPENpOsition

         double stopLoss   = GetStopLossPrice(command, signal.StopLossPips);

i think it fetches the stoploss but i dont know how to implement

Re: LotSize Calculation Percentage of Balance

Você precisa ir na opções create positivo signal e signal e chamar um. Função com stop loss de 500000 pontos assim fica o mesmo que operar sem. Stop e super fácil

Re: LotSize Calculation Percentage of Balance

Side note!

I'll add a Risk calculation functionality to the "Top 10 Robots App".
It will be ready next week.

If it works fine, I may add such options to EA Studio.

It works as follows:
  - The strategy needs Stop Loss (if not, we may assume 100 pips loss)
  - Select Rsik percent (let's say 1% of the account)
  - The program will calculate the proper Entry Lots depending on the Initial Account, the Stop Loss pips the spread and the commission.

Here is an example from the Prop Firm app (it also show the Stop Loss in dollars)

https://image-holder.forexsb.com/store/prop-app-risk-to-lot-size-thumb.png

5 (edited by mentosan 2024-11-17 18:47:51)

Re: LotSize Calculation Percentage of Balance

wow, thanks Mr. Popov !

late edit: oh, it is not for expert advisor studio. I red it very fast, too fast! smile

anyway, good things are coming in the future also for the eas, for sure!