How to calculate Volume/ Lot Size exactly

Created at 12 Jul 2024, 12:14
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
GO

GonzaloBlanco

Joined 18.04.2024

How to calculate Volume/ Lot Size exactly
12 Jul 2024, 12:14


Hi, I am trying to calculate Volume/ Lot Size exactly risking 1% of balance with a 1:1 ratio according to StopLoss in pips. In in theory it would work but the logs gives me another P&L. I guess that something in the function code is wrong or I must consider the spread, comission etc… I attached img where you will see that the profit or loss not is exactly 1% of balance but the chart shows a close value to the 1% (100$). Can someone help me or give me the good function? Thanks

       

public double CalculateVolume(double StopLossInPips, double Risk)
       {
           double VolumeGross = (Risk / (0.01 * StopLossInPips)) * InicialBalance;
           double Volume = Math.Round(VolumeGross/100000, 2)*100000;
           // double Comission = 4 * LotSize;
           return Volume;
       }


@GonzaloBlanco
Replies

PanagiotisCharalampous
12 Jul 2024, 12:36

Hi there,

Here you go

        private double GetVolume(double sl)
        {
            var maxAmountRisked = Account.Equity * (RiskPerTrade / 100);

            return Symbol.NormalizeVolumeInUnits(maxAmountRisked / (((sl + CommissionCostInPips()) * Symbol.PipValue)), RoundingMode.Down);
        }

        private double CommissionCostInPips()
        {
            if (!UseCommissions)
                return 0;

            // We get the symbol conditions
            switch (Symbol.CommissionType)
            {
                case SymbolCommissionType.QuoteCurrencyPerOneLot:
                    {
                        return (Symbol.Commission * 2 / Symbol.Bid) / Symbol.PipSize;
                    }
                case SymbolCommissionType.PercentageOfTradingVolume:
                    {
                        return (Symbol.Commission * 100) * 2 / Symbol.PipSize;
                    }
                case SymbolCommissionType.UsdPerMillionUsdVolume:
                    {
                        return AssetConverter.Convert(Symbol.Commission * 2, "USD", Symbol.QuoteAsset) / 1000000 / Symbol.PipSize;
                    }

                case SymbolCommissionType.UsdPerOneLot:
                    {
                        return AssetConverter.Convert(Symbol.Commission * 2, "USD", Symbol.QuoteAsset) / Symbol.LotSize / Symbol.PipSize;
                    }
                default: return 0;
            }
        }

But note that the risk will never be exact due to the fact that the volume needs to be rounded to the closest valid value

Best regards,


@PanagiotisCharalampous

GonzaloBlanco
12 Jul 2024, 12:45 ( Updated at: 13 Jul 2024, 06:46 )

RE: How to calculate Volume/ Lot Size exactly

PanagiotisCharalampous said: 

Hi there,

Here you go

        private double GetVolume(double sl)        {            var maxAmountRisked = Account.Equity * (RiskPerTrade / 100);            return Symbol.NormalizeVolumeInUnits(maxAmountRisked / (((sl + CommissionCostInPips()) * Symbol.PipValue)), RoundingMode.Down);        }        private double CommissionCostInPips()        {            if (!UseCommissions)                return 0;            // We get the symbol conditions            switch (Symbol.CommissionType)            {                case SymbolCommissionType.QuoteCurrencyPerOneLot:                    {                        return (Symbol.Commission * 2 / Symbol.Bid) / Symbol.PipSize;                    }                case SymbolCommissionType.PercentageOfTradingVolume:                    {                        return (Symbol.Commission * 100) * 2 / Symbol.PipSize;                    }                case SymbolCommissionType.UsdPerMillionUsdVolume:                    {                        return AssetConverter.Convert(Symbol.Commission * 2, "USD", Symbol.QuoteAsset) / 1000000 / Symbol.PipSize;                    }                case SymbolCommissionType.UsdPerOneLot:                    {                        return AssetConverter.Convert(Symbol.Commission * 2, "USD", Symbol.QuoteAsset) / Symbol.LotSize / Symbol.PipSize;                    }                default: return 0;            }        }

But note that the risk will never be exact due to the fact that the volume needs to be rounded to the closest valid value

Best regards,

Ok, thanks, I am going to backtest


@GonzaloBlanco

PanagiotisCharalampous
14 Jul 2024, 06:33

RE: RE: How to calculate Volume/ Lot Size exactly

GonzaloBlanco said: 

PanagiotisCharalampous said: 

Hi there,

Here you go

        private double GetVolume(double sl)        {            var maxAmountRisked = Account.Equity * (RiskPerTrade / 100);            return Symbol.NormalizeVolumeInUnits(maxAmountRisked / (((sl + CommissionCostInPips()) * Symbol.PipValue)), RoundingMode.Down);        }        private double CommissionCostInPips()        {            if (!UseCommissions)                return 0;            // We get the symbol conditions            switch (Symbol.CommissionType)            {                case SymbolCommissionType.QuoteCurrencyPerOneLot:                    {                        return (Symbol.Commission * 2 / Symbol.Bid) / Symbol.PipSize;                    }                case SymbolCommissionType.PercentageOfTradingVolume:                    {                        return (Symbol.Commission * 100) * 2 / Symbol.PipSize;                    }                case SymbolCommissionType.UsdPerMillionUsdVolume:                    {                        return AssetConverter.Convert(Symbol.Commission * 2, "USD", Symbol.QuoteAsset) / 1000000 / Symbol.PipSize;                    }                case SymbolCommissionType.UsdPerOneLot:                    {                        return AssetConverter.Convert(Symbol.Commission * 2, "USD", Symbol.QuoteAsset) / Symbol.LotSize / Symbol.PipSize;                    }                default: return 0;            }        }

But note that the risk will never be exact due to the fact that the volume needs to be rounded to the closest valid value

Best regards,

Ok, thanks, I am going to backtest

Did you read my response carefully?


@PanagiotisCharalampous