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 of capital
 Post subject: Risk of capital Post rating: 0   New post Posted: Wed 17 Jul, 2013, 08:35 
User avatar

User rating: 0
Joined: Thu 16 Feb, 2012, 19:18
Posts: 15
Location: Czech Republic, Prague
Hey all,
I would like to inform whether there is any possibility to make/have some kind of script which automatically calculates Risk in % of capital. I mean if I click on Short/Long it automatically calculate and open position in "10%" of my capital.

Thanks.


 
 Post subject: Re: Risk of capital Post rating: 1   New post Posted: Wed 17 Jul, 2013, 12:19 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
edit: Why is this topic created (moved?) to Bug Reports?

I don't think so that the platform is offering such feature. However with coding (automated strategy) you can achieve this.
You could run a strategy that does nothing but checking for orders without SL, and if it founds one, it sets the SL level based on % of the (initial) account balance.

Here is a code snippet from a strategy that does this:
private double accountStartBalance;
private double SLInCurrency; // SL in account currency
private static HashMap<Currency, Instrument> pairs = new HashMap<Currency, Instrument>();

@Configurable("Instrument")
public Instrument selectedInstrument = Instrument.EURUSD;
@Configurable("Stop Loss Percent")
public int stopLossPercent = 10;

private void initializeCurrencyPairs() {
    pairs.put(Currency.getInstance("AUD"), Instrument.AUDUSD);
    pairs.put(Currency.getInstance("CAD"), Instrument.USDCAD);
    pairs.put(Currency.getInstance("CHF"), Instrument.USDCHF);
    pairs.put(Currency.getInstance("DKK"), Instrument.USDDKK);
    pairs.put(Currency.getInstance("EUR"), Instrument.EURUSD);
    pairs.put(Currency.getInstance("GBP"), Instrument.GBPUSD);
    pairs.put(Currency.getInstance("HKD"), Instrument.USDHKD);
    pairs.put(Currency.getInstance("JPY"), Instrument.USDJPY);
    pairs.put(Currency.getInstance("NOK"), Instrument.USDNOK);
    pairs.put(Currency.getInstance("NZD"), Instrument.NZDUSD);
    pairs.put(Currency.getInstance("SEK"), Instrument.USDSEK);
    pairs.put(Currency.getInstance("SGD"), Instrument.USDSGD);
}

// This function is calculating the value (in currency) of pipCount pips, using the given amount (orderAmount) for the given instrument.
private double pipValueInCurrency(Instrument instrument, double pipCount, double orderAmount) throws JFException
{
    double profit
         , transitionalPrice
         , buyPrice
         , sellPrice
         , payedAmount
         , soldPrice
         , soldAmount
         , profitInInitialCurrncy;
    Instrument transitionalInstrument;
   
    // multiple up orderAmount
    orderAmount *= 1000000;
    buyPrice = history.getLastTick(instrument).getAsk();
    sellPrice = history.getLastTick(instrument).getBid();
    payedAmount = buyPrice * orderAmount;
    soldPrice = (buyPrice + (selectedInstrument.getPipValue()*pipCount));
    soldAmount = soldPrice * orderAmount;
    profitInInitialCurrncy = soldAmount - payedAmount;
   
    //If second currency in the instrument is account currency, then print amount difference
    if (instrument.getSecondaryCurrency().equals(account.getCurrency()))
    {
        profit = soldAmount - payedAmount;
    //If primary currency in the instrument is account currency, sold instrument secondary currency to buy primary(account) currency
    }
    else if (instrument.getPrimaryCurrency().equals(account.getCurrency()))
    {
        profit = (soldAmount - payedAmount) / sellPrice;
    // Secondary instrument is USD, convert to account currency.   
    //If secondary currency is USD, get instrument from map by account key and buy account currency.
    }
    else if (instrument.getSecondaryCurrency().equals(USD))
    {
        transitionalInstrument = pairs.get(account.getCurrency());
        transitionalPrice = history.getLastTick(transitionalInstrument).getBid();
        profit = profitInInitialCurrncy * transitionalPrice ;
    // Secondary instrument is not USD, must convert to USD, then to Account currency
    //If secondary currency not equal to USD, and primary or secondary currency not equal to account currency. Then get transitional instrument from map, if USD is a primary currency in the instrument then divide profit to bid price, if USD is a secondary currency in the instrument, then multiple profit to bid price.
    }
    else
    {
        transitionalInstrument = pairs.get(instrument.getSecondaryCurrency());
        Currency workInstrumentSecondaryCurrency = instrument.getSecondaryCurrency();
        // USD is primary currency in transitional instrument
        if (transitionalInstrument.getPrimaryCurrency().equals(USD))
        {
            // Sell Primary(USD) currency
            transitionalPrice = history.getLastTick(pairs.get(workInstrumentSecondaryCurrency)).getBid();
            profit = profitInInitialCurrncy / transitionalPrice;
        }
        // USD is secondary currency in transitional instrument
        else
        {
            transitionalPrice = history.getLastTick(pairs.get(workInstrumentSecondaryCurrency)).getBid();
            profit = profitInInitialCurrncy * transitionalPrice;
        }
    }
   
    return profit;
}

public void onStart(final IContext context) throws JFException {
    accountStartBalance = context.getAccount().getBalance();       
    SLInCurrency = accountStartBalance * (stopLossPercent*0.01);
    initializeCurrencyPairs();
}

public void onMessage(IMessage message) throws JFException {
    IOrder anOrder;
    double price
         , slPrice
         , aPipValue
         , stopLossInPips;

    switch(message.getType()) {
        case ORDER_FILL_OK:
            anOrder = message.getOrder();
            if (Double.compare(anOrder.getStopLossPrice(),0) == 0) {
                price = anOrder.getOpenPrice();
                aPipValue = pipValueInCurrency(selectedInstrument, 1, anOrder.getAmount());
                stopLossInPips = SLInCurrency / aPipValue;
                if (anOrder.isLong())
                    slPrice = price - stopLossInPips*tickSize;
                else
                    slPrice = price + stopLossInPips*tickSize;

                anOrder.setStopLossPrice(slPrice);
                anOrder.waitForUpdate(2000);
            }
            break;
    }
}


 
 Post subject: Re: Risk of capital Post rating: 0   New post Posted: Mon 22 Jul, 2013, 14:27 
User avatar

User rating: 0
Joined: Thu 16 Feb, 2012, 19:18
Posts: 15
Location: Czech Republic, Prague
where is original strategy code pls?


 
 Post subject: Re: Risk of capital Post rating: 0   New post Posted: Sun 28 Jul, 2013, 22:56 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
It is confidential at this moment, sorry.


 

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