NormalizeVolumeInUnits and precise Lot size after decimal number

Created at 19 Aug 2022, 08:56
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!
RO

ronnjinn

Joined 05.10.2020

NormalizeVolumeInUnits and precise Lot size after decimal number
19 Aug 2022, 08:56


I want NormalizeVolumeInUnits to calculate precise Lot size after decimal number with my code below, but it doesn't. Does anyone know how to fix this?

[Parameter("Return Ratio", DefaultValue = 1)]
public int ReturnRatio { get; set; }

protected override void OnBar()
        {
            if (rsi.Result.Last(2) > MaxLevel && rsi.Result.Last(1) < MaxLevel)
            {
                double lastHigh = double.MinValue;

                for (int i = 0; i <= 5; i++)
                {
                    lastHigh = Math.Max(Bars.HighPrices.Last(i + 1), lastHigh);
                }

                var stopLoss = Math.Abs((lastHigh - Symbol.Bid) / Symbol.PipSize);
                var takeProfit = stopLoss * ReturnRatio;
                var maxAmountRisked = Account.Equity * (RiskPercentage / 100);
                var volume = maxAmountRisked / (stopLoss * Symbol.PipValue);
                Print("stopLoss={0}, takeProfit={1}, maxAmountRisked={2}", stopLoss, takeProfit, maxAmountRisked);
                Print("volume={0}", volume);
                volume = Symbol.NormalizeVolumeInUnits(volume, RoundingMode.Down);
                Print("normalized volume={0}", volume);
                ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volume, "Mybot", stopLoss, takeProfit);
            }
	}

and my log said;

29/07/2022 19:00:00.000 | stopLoss=772.000000000003, takeProfit=2316.00000000001, maxAmountRisked=912.27
    28/07/2022 20:00:00.000    normalized volume=1    
    28/07/2022 20:00:00.000    volume=0.841610963951071    
    28/07/2022 20:00:00.000    stopLoss=823.000000000002, takeProfit=2469.00000000001, maxAmountRisked=923.38    
    25/07/2022 20:00:00.000    normalized volume=1    
    25/07/2022 20:00:00.000    volume=1.54308959096375    
    25/07/2022 20:00:00.000    stopLoss=445.000000000005, takeProfit=1335.00000000001, maxAmountRisked=915.42    
    23/07/2022 02:00:00.000    normalized volume=1    
    23/07/2022 02:00:00.000    volume=0.675526791436481    
    23/07/2022 02:00:00.000    stopLoss=1021, takeProfit=3063.00000000001, maxAmountRisked=919.47    
    22/07/2022 22:00:00.000    normalized volume=1    
    22/07/2022 22:00:00.000    volume=1.14084646986138    

 

 


@ronnjinn
Replies

PanagiotisCharalampous
19 Aug 2022, 11:02

Hi ronnjinn,

You need to explain better what do you mean. Are you aware what NormalizeVolumeInUnits() does?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous