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.

RIsk Management Using convertPipToCurrency()
 Post subject: RIsk Management Using convertPipToCurrency() Post rating: 0   New post Posted: Thu 12 Apr, 2012, 02:00 

User rating: 3
Joined: Thu 17 Nov, 2011, 04:06
Posts: 39
Location: Canada, Ottawa
Given a 2% risk of account balance, an entry price, and a stop loss price, can someone give me an example of the function that would calculate the lots required to manage risk for each position? I would like to use the function convertPipToCurrency() to achieve this.

Thanks,

Brad


 
 Post subject: Re: RIsk Management Using convertPipToCurrency() Post rating: 2   New post Posted: Thu 12 Apr, 2012, 10:23 

User rating: 5
Joined: Thu 29 Mar, 2012, 15:00
Posts: 19
I use the following in my strategy... I only recently wrote this myself so it's possible that it's not fool-proof or perhaps even just wrong.

private double CalculateOrderAmount(Instrument instrument, double closePrice, double riskPercentage, double stopLossPips) {
        double amountToUseTemp = this.account.getBalance() * riskPercentage; // 10000 * 0.01 = 100
        double amountToUseTemp2 = amountToUseTemp / stopLossPips; // 100 / 200 = 0.5
        double amountToUseTemp3 = amountToUseTemp2 * 10000; // 0.5 / 0.0001 = 5000

        double amountToUse = amountToUseTemp3;

        if (instrument.getSecondaryCurrency().getCurrencyCode() != "USD") {
            int mod = 1;
            if (instrument.getSecondaryCurrency().getCurrencyCode() == "JPY")
                mod = 100;
            if (instrument.getPrimaryCurrency().getCurrencyCode() == "USD") {
                amountToUse /= (closePrice / mod);
            } else {
                for (Instrument ins : this.latestConversions.keySet()) {
                    if (ins.getPrimaryCurrency() == instrument.getSecondaryCurrency() && ins.getSecondaryCurrency().getCurrencyCode() == "USD") {
                        amountToUse /= this.latestConversions.get(ins);
                    } else if (ins.getSecondaryCurrency() == instrument.getSecondaryCurrency() && ins.getPrimaryCurrency().getCurrencyCode() == "USD") {
                        amountToUse /= (1 / this.latestConversions.get(ins));
                    }
                }
            }
        }

        double correctedAmount = amountToUse / 1000000; // 0.005

        return correctedAmount;
    }


 
 Post subject: Re: RIsk Management Using convertPipToCurrency() Post rating: 0   New post Posted: Thu 12 Apr, 2012, 10:24 

User rating: 5
Joined: Thu 29 Mar, 2012, 15:00
Posts: 19
And just a quick note. As you can probably see I am passing in 1% risk as 0.01 rather than 1.


 

Jump to:  

cron
  © 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