VOLUME CALCULATION ON XAUUSD NOT CALCULATING CORRECTLY AFTER UPDATE TO 4.1

Created at 10 Aug 2021, 15:55
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!
HE

herculesone

Joined 02.11.2020

VOLUME CALCULATION ON XAUUSD NOT CALCULATING CORRECTLY AFTER UPDATE TO 4.1
10 Aug 2021, 15:55


Hello there, 

 

After the latest update, one of the trading bots I have created does not return the correct value on one particular instrument which is the XAUUSD.

Here is function that calculates the volume that is the entered in the methods that place and execute orders:

 

Function Inputs:

percent of the volume I want since I split the orders in multiples

stop loss of the position to calculate the lots based on a fixed risk % of the account

Parameters used:

        [Parameter("Risk (%)", Group = "Risk Management", DefaultValue = 1, MinValue = 0.1, MaxValue = 100, Step = 0.05)]
        public double Risk { get; set; }

        [Parameter("Fixed Volume", Group = "Risk Management", DefaultValue = true)]
        public bool FixedVolume { get; set; }

        [Parameter("Volume (in lots)", Group = "Risk Management", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Volume { get; set; }

Function: 

       protected double CalculateRisk(double percent, double stoploss)
        {

            if (!FixedVolume && stoploss != 0)
            {

                double risk = Account.Balance * (Risk / 100);
                double riskPerTrade = risk * (percent / 100);
                double lots = Math.Round(riskPerTrade / (Symbol.PipValue * stoploss), 2);

                Print("Quantity in lots = ", lots);

                double volume = Symbol.QuantityToVolumeInUnits(lots);
                Print("Volume = ", volume);

                if (volume < Symbol.VolumeInUnitsMin)
                {
                    volume = Symbol.VolumeInUnitsMin;
                }

                if (volume > Symbol.VolumeInUnitsMax)
                {
                    volume = Symbol.VolumeInUnitsMax;
                }

                return Symbol.NormalizeVolumeInUnits(volume, RoundingMode.ToNearest);

            }
            else
            {

                double lts = Volume * (percent / 100);
                Print("Quantity in lots = ", lts);

                double vol = Symbol.QuantityToVolumeInUnits(lts);
                Print("Volume = ", vol);

                if (stoploss == 0 || vol < Symbol.VolumeInUnitsMin)
                {
                    vol = Symbol.VolumeInUnitsMin;
                }

                if (vol > Symbol.VolumeInUnitsMax)
                {
                    vol = Symbol.VolumeInUnitsMax;
                }

                return Symbol.NormalizeVolumeInUnits(vol, RoundingMode.ToNearest);

            }

        }

 

Usage:

double volume = CalculateRisk(50, stoploss);

var trade = ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, label);

 

Every instrument or when Fixed Lots are used, calculates correctly except on the XAUUSD when the risk is used, where it calculates maximum lots.

 

Your help is appreciated.

 

Thank you!


@herculesone
Replies

amusleh
10 Aug 2021, 16:18 ( Updated at: 10 Aug 2021, 16:20 )

Hi,

Not sure what's your exact problem, but you can try this method to get volume in percentage:

        /// <summary>
        /// Returns the amount of volume based on your provided risk percentage and stop loss
        /// </summary>
        /// <param name="symbol">The symbol</param>
        /// <param name="riskPercentage">Risk percentage amount</param>
        /// <param name="accountBalance">The account balance</param>
        /// <param name="stopLossInPips">Stop loss amount in Pips</param>
        /// <returns>double</returns>
        public double GetVolume(Symbol symbol, double riskPercentage, double accountBalance, double stopLossInPips)
        {
            return symbol.NormalizeVolumeInUnits(riskPercentage / (Math.Abs(stopLossInPips) * symbol.PipValue / accountBalance * 100));
        }

 


@amusleh

herculesone
10 Aug 2021, 16:31

RE:

amusleh said:

Hi,

Not sure what's your exact problem, but you can try this method to get volume in percentage:

        /// <summary>
        /// Returns the amount of volume based on your provided risk percentage and stop loss
        /// </summary>
        /// <param name="symbol">The symbol</param>
        /// <param name="riskPercentage">Risk percentage amount</param>
        /// <param name="accountBalance">The account balance</param>
        /// <param name="stopLossInPips">Stop loss amount in Pips</param>
        /// <returns>double</returns>
        public double GetVolume(Symbol symbol, double riskPercentage, double accountBalance, double stopLossInPips)
        {
            return symbol.NormalizeVolumeInUnits(riskPercentage / (Math.Abs(stopLossInPips) * symbol.PipValue / accountBalance * 100));
        }

 

 

Thank you for your response! :) 

 

I will give this a try.

 

The problem is that on cTrader you have 2 different values for trade volume.

Quantity (lots)

Volume (in units)

On XAUUSD specifically, the conversion from Quantity to Volume does not work correctly.

 


@herculesone