Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Universal getAmount() code
 Post subject: Universal getAmount() code Post rating: 0   New post Posted: Fri 07 Jan, 2022, 08:24 
User avatar

User rating: 1
Joined: Fri 14 Sep, 2012, 02:25
Posts: 54
Location: New Zealand, Christchurch
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?


 
 Post subject: Re: Universal getAmount() code Post rating: 0   New post Posted: Fri 07 Jan, 2022, 21:05 
User avatar

User rating: 1
Joined: Fri 14 Sep, 2012, 02:25
Posts: 54
Location: New Zealand, Christchurch
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?


 
 Post subject: Re: Universal getAmount() code Post rating: 0   New post Posted: Tue 11 Jan, 2022, 22:33 
User avatar

User rating: 1
Joined: Fri 14 Sep, 2012, 02:25
Posts: 54
Location: New Zealand, Christchurch
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.


 
 Post subject: Re: Universal getAmount() code Post rating: 0   New post Posted: Mon 28 Mar, 2022, 02:41 
User avatar

User rating: 1
Joined: Fri 14 Sep, 2012, 02:25
Posts: 54
Location: New Zealand, Christchurch
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;


 

Jump to:  

  © 1998-2024 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com