Volume/Lots Calculation Incorrect/Failure on Index (SP500)

Created at 01 Jul 2020, 01:52
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!
RG

rgasch

Joined 14.12.2017

Volume/Lots Calculation Incorrect/Failure on Index (SP500)
01 Jul 2020, 01:52


I'm using the following code to calculate lots to open a trade: 

var equityLeveraged = this.Account.Equity * this.Account.PreciseLeverage;
var amt = equityLeveraged * (__EquityPercent * 0.01);
var lots = Symbol.VolumeInUnitsToQuantity(amt);

this.Print(string.Format("Open {0} position with: AccountEquity={1}, Leverage={2}, EquityLeveraged={3}, EquityPercentage={4}, TradeAmt={5}, TradeLots={6}, StopLossPipsParameter={7}, TakeProfitPipsParameter={8}", 
    type, account.Equity, account.PreciseLeverage, equityLeveraged, __EquityPercent, amt, lots, __DefaultStopLossPips, __DefaultTakeProfitPips));

I've used this code before on Forex pairs and it seemed to work well. However, on a demo account when this code is run against SP500, it prints the following output: 

01/07/2019 07:10:00.000 | Open Buy position with: AccountEquity=10000, Leverage=100, EquityLeveraged=1000000, EquityPercentage=1, TradeAmt=10000, TradeLots=10000, StopLossPipsParameter=0, TakeProfitPipsParameter=0

So when amt == 10000 (correct based on account balance, leverage and % of equity to use), VolumeInUnitsToQuantity(amt) returns 10000 ... my understanding is that this call should return the number of lots, so converting 10K USD to 10K lots is obviously not correct. 

Can anybody offer some advice as to what is going on here? 


@rgasch
Replies

PanagiotisCharalampous
01 Jul 2020, 08:43

Hi rgasch,

You assume that this part of the code is taking as input a value in USD and converts it into Lots

var lots = Symbol.VolumeInUnitsToQuantity(amt);

But this is wrong. VolumeInUnitsToQuantity() takes as input units of the traded symbol and returns the relevant Lot Size. Units always represent units of the base asset. So when you are trading SP5000 you are inputting 10000 contracts of this index and you are receiving as a return 10000 lots since 1 contract = 1 Lot. It works on some FX pairs by luck since the account's currency coincides with the base asset e.g. USD and USDJPY or EUR and EURUSD.

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

rgasch
01 Jul 2020, 15:53

RE:

Hi Panagiotis, 

thank you for your reply; it definitely clears up some confusion I had ...

Allow me to inquire, what would the correct API call be to convert a USD or EUR amount to a lot size?

Thank you very much

 

PanagiotisCharalampous said:

Hi rgasch,

You assume that this part of the code is taking as input a value in USD and converts it into Lots

var lots = Symbol.VolumeInUnitsToQuantity(amt);

But this is wrong. VolumeInUnitsToQuantity() takes as input units of the traded symbol and returns the relevant Lot Size. Units always represent units of the base asset. So when you are trading SP5000 you are inputting 10000 contracts of this index and you are receiving as a return 10000 lots since 1 contract = 1 Lot. It works on some FX pairs by luck since the account's currency coincides with the base asset e.g. USD and USDJPY or EUR and EURUSD.

Best Regards,

Panagiotis 

Join us on Telegram

 

 


@rgasch

PanagiotisCharalampous
01 Jul 2020, 16:02

Hi rgasch,

There is no API call for this. You will need to convert the relevant amount to base asset units. You need to calculate the rate between your account currency and your symbol's base asset. Here a trader tried to do it for calculating the required margin but it is a good starting point.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

rgasch
01 Jul 2020, 19:12

RE:

Thank you, I will check out the link you provided. 

Appreciate your help ...

 

PanagiotisCharalampous said:

Hi rgasch,

There is no API call for this. You will need to convert the relevant amount to base asset units. You need to calculate the rate between your account currency and your symbol's base asset. Here a trader tried to do it for calculating the required margin but it is a good starting point.

Best Regards,

Panagiotis 

Join us on Telegram

 


@rgasch