Dukascopy Support Board
http://www.dukascopy.com/swiss/english/forex/jforex/forum/

Universal getAmount() code
http://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=65&t=57764
Page 1 of 1

Author:  fprophet [ Fri 07 Jan, 2022, 08:24 ]
Post subject:  Universal getAmount() code

hi everyone - I'm struggling to develop getAmount() java code that returns number of Lots, or number of Contracts, given these input parameters:

Instrument to trade "XXX/YYY" (e.g. EUR/USD, GBP/JPY, XAU/USD, USA500.IDX/USD, etc.)
Account Currency "ZZZ" (e.g. NZD, EUR, etc.)
Account Leverage "L" (e.g. 100:1, 200:1, etc.)
Equity Risk Percent "EQpc" (e.g. 1%, 25%, etc.) = percent of available Equity willing to Risk on the Trade

I know I can use:
JFUtils utils = context.getUtils();
double amount1 = utils.convert(Instrument1, Instrument2, amount);
double amount2 = utils.convertPipToCurrency(Instrument1, context.getAccount().getCurrency());

plus of course I have:
lastTick.getAsk();
lastTick.getBid();
instrument.getPipValue();
instrument.getPipScale();
instrument.getAmountPerContract();
instrument.getMinTradeAmount();
instrument.getTradeAmountIncrement();

It's easy for trading EUR/USD with a different account Currency like NZD. I can work that out ...
But trading USA500.IDX/USD on a EUR Account is doing my head in.

Unhelpfully, for USA500.IDX/USD I see:
TradeAmountIncrement: 1
MinTradeAmount: 1
AmountPerContract: 1
pipScale: 2
pipValue: 0.01

Can anyone please share some Universal getAmount() code that works for all Instruments?

Author:  fprophet [ Fri 07 Jan, 2022, 21:05 ]
Post subject:  Re: Universal getAmount() code

the closest I have so far for my getAmount(instrument) code is:

double amount = (currentEquity/oneMill) * (EQpc/100) * Leverage * PipToCurrency / pipValue;

which seems to be fine for exchange rate instruments.
But then I add:

if (instrument.name().contains("IDX")) { amount /= lastBID; } /// or lastASK

for CFD Indexes like USA500 - which seems a bit inelegant / clunky,
and I don't know yet what adjustments I need for XAU or XAG ...
Thoughts?

Author:  fprophet [ Tue 11 Jan, 2022, 22:33 ]
Post subject:  Re: Universal getAmount() code

Oleksandr @ Dukascopy Support has pointed out that USA500IDX has max Leverage 50:1 so for has CFD Indices I now use:

amount = history.getEquity()/oneMill * (1/PipToCurrency*pipValue) * 50*((EQpc/100)*(Leverage/100)) / lastBID;

Multiply this by oneMill (1,000,000) to get the number of Contracts.
Hope this helps.

Author:  fprophet [ Mon 28 Mar, 2022, 02:41 ]
Post subject:  Re: Universal getAmount() code

also useful to get your Account Equity in USD first using the Account Currency to USD rate:

JFUtils utils = context.getUtils();
double ACC2USDrate = utils.getRate(JFCurrency.getInstance( String.valueOf(context.getAccount().getCurrency()) ), JFCurrency.getInstance("USD"));

double EquityInUSD = history.getEquity() * ACC2USDrate;

  Page 1 of 1