Convert quantity to volume

Created at 10 Oct 2017, 11:02
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!
PI

pipsniper123

Joined 21.12.2016

Convert quantity to volume
10 Oct 2017, 11:02


I have the following problem.

I want to convert the double quantity to a volume (a volume needs to be in format long to be able to be fed to the executeMarketOrder function)

So, I use:   volume = Symbol.QuantityToVolume(quantity), where quantity is a double specified beforehand. 

This function does exactly what I want, namely converting a double quantity to a long volume.

When, for example quantity = 13.64, volume becomes: volume = 1.364.000. Perfect.

However, when for example quantity = 130.64, volume suddenly becomes: volume = 13.063.999. Why? Why isn't it transformed to 13.064.000? Because that is a tradable volume, and 13.063.999 is not.

So then I decided to transform the volume again, using the function Symbol.NormalizeVolume(), which transforms a non-tradable volume into a tradable volume.

So I do: volume = Symbole.NormalizeVolume(volume)

It then coverts the number 13.063.999 to 10.000.000. However, this is not what I want it to be converted to. I want it to be converted to 13.064.000. So I do the following instead:

volume = Symbole.NormalizeVolume(volume, RoundingMode.ToNearest), which is supposed to transform the amount 'volume' to the nearest tradable volume.

But then again, 13.063.999 is transformed to 10.000.000, which doesn't make sense since it is not the nearest tradable volume. 13.064.000 is perfectly tradable. 

 

So, could somebody help me convert double quantity = 130.64 to long volume 13.064.000?

Thanks in advance!

 

 

 


@pipsniper123
Replies

PanagiotisCharalampous
10 Oct 2017, 11:43

Hi pipsiper123,

The following code should do the work for you

var volume = Math.Ceiling((Symbol.QuantityToVolume(130.64) / (double)Symbol.VolumeStep)) * Symbol.VolumeStep;

Use Math.Ceiling or Math.Floor based on how you would like to round your volume.

Best Regards,

Panagiotis


@PanagiotisCharalampous

pipsniper123
10 Oct 2017, 15:34

RE:

Panagiotis Charalampous said:

Hi pipsiper123,

The following code should do the work for you

var volume = Math.Ceiling((Symbol.QuantityToVolume(130.64) / (double)Symbol.VolumeStep)) * Symbol.VolumeStep;

Use Math.Ceiling or Math.Floor based on how you would like to round your volume.

Best Regards,

Panagiotis

 

Works perfectly, thanks!


@pipsniper123