Replies

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