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.

Dynamically calculating lot size
 Post subject: Dynamically calculating lot size Post rating: 0   New post Posted: Wed 26 Feb, 2014, 11:25 
User avatar

User rating: 2
Joined: Wed 29 Jan, 2014, 10:18
Posts: 30
Location: Austria,
I'd like to dynamically calculate the lot size for a buy or sell order dependent on equity, stop loss and risk:

double calculateLotSize(Instrument instrument, IContext context, double entryPrice, double stopLoss, int risk) {

       double stopLossPips = Math.abs(stopLoss-entryPrice) / instrument.getPipValue();
       double riskPercentage =  (double) risk / 100;
       double RiskAmount = context.getAccount().getEquity() * riskPercentage;
       double PipValue = 100000 * instrument.getPipValue();   // calculated for 1 standard lot
       double lotSize = RiskAmount / (stopLossPips * PipValue);

       return lotSize;
   }


Is my code above is correct? if not, is there any better example for how to do this?

Thanks


 
 Post subject: Re: Dynamically calculating lot size Post rating: 1   New post Posted: Wed 26 Feb, 2014, 11:30 
User avatar

User rating: 96
Joined: Mon 09 Sep, 2013, 07:09
Posts: 287
Location: Ukraine, SHostka
1 lot = 1000000 (a million) currency units, not 100000.


 
 Post subject: Re: Dynamically calculating lot size Post rating: 1   New post Posted: Wed 26 Feb, 2014, 11:42 
User avatar

User rating: 2
Joined: Wed 29 Jan, 2014, 10:18
Posts: 30
Location: Austria,
hebasto wrote:
1 lot = 1000000 (a million) currency units, not 100000.


Sorry, I don't agree ;)

Basically, in forex a standard trading contract equates to 100,000 units of the base currency. This means that 1 standard lot has a value of roughly $10 per pip (depending on the currency pair you are trading).

A mini lot is equal to 10,000 units of currency.

A micro lot is equal to 1,000 units of currency.


 
 Post subject: Re: Dynamically calculating lot size Post rating: 1   New post Posted: Wed 26 Feb, 2014, 13:03 
User avatar

User rating: 96
Joined: Mon 09 Sep, 2013, 07:09
Posts: 287
Location: Ukraine, SHostka
https://www.dukascopy.com/wiki/#Order_Amounts


 
 Post subject: Re: Dynamically calculating lot size Post rating: 0   New post Posted: Wed 26 Feb, 2014, 13:22 
User avatar

User rating: 2
Joined: Wed 29 Jan, 2014, 10:18
Posts: 30
Location: Austria,
hebasto wrote:


Aha! I see your point. So 1 standard lot in Metatrader is actually 0,1 lot in JForex :o

Thank you!


 
 Post subject: Re: Dynamically calculating lot size Post rating: 0   New post Posted: Wed 26 Feb, 2014, 13:44 
User avatar

User rating: 2
Joined: Wed 29 Jan, 2014, 10:18
Posts: 30
Location: Austria,
Fx18 wrote:
I'd like to dynamically calculate the lot size for a buy or sell order dependent on equity, stop loss and risk:

double calculateLotSize(Instrument instrument, IContext context, double entryPrice, double stopLoss, int risk) {

       double stopLossPips = Math.abs(stopLoss-entryPrice) / instrument.getPipValue();
       double riskPercentage =  (double) risk / 100;
       double RiskAmount = context.getAccount().getEquity() * riskPercentage;
       double PipValue = 1000000 * instrument.getPipValue();   // calculated for 1 standard lot
       double lotSize = RiskAmount / (stopLossPips * PipValue);

       return lotSize;
   }


Is my code above is correct? if not, is there any better example for how to do this?

Thanks



instead of

 double PipValue = 1000000 * instrument.getPipValue(); 


I think, it would be better to use convertPipToCurrency method:

PipValue =  context.getUtils().convertPipToCurrency(instrument, context.getAccount().getCurrency());


 

Jump to:  

  © 1998-2025 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