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.

Problem with changing of TP for buy orders
 Post subject: Problem with changing of TP for buy orders Post rating: 0   New post Posted: Fri 22 Mar, 2013, 07:02 

User rating: 0
Joined: Wed 02 Nov, 2011, 15:45
Posts: 6
Location: Ukraine,
Hello.
I am using a stratedy on AUDNZD on H1. I place limit orders and on each tick try to correct the TP if it not corresponding with calculated price.
TP - is a rounded EMA:

private static double round(double amount, int decimalPlaces) {
        int tmp = 1;
        for(int k=1;k <= decimalPlaces;k++)
            tmp*=10;
        double result = amount*tmp;
        result = Math.round(result);
        result =  result/tmp;
        return result;
    }
TP = round(TPEMA, _MyInstrument.getPipScale()+1);


I try to correct TP in onTick(), when EMA recalculated:

delta=1;
        for(int k=1;k<=MyInstrument.getPipScale();k++)
            delta/=10;
spread=0.0005;
if (order.getState() == IOrder.State.FILLED)
                    {
                        if(order.isLong() == true)
                        {   
                                bought = true;
                                if (Math.abs(order.getTakeProfitPrice() - TP) > delta);
                                {
                                    order.setTakeProfitPrice(TP);
                                }
                         }
                        else
                        {
                                sold = true;
                                if (Math.abs(order.getTakeProfitPrice() - TP - spread) > delta)
                                {
                                    order.setTakeProfitPrice(TP + spread);
                                }
                         }
                    }

Changing of TP for the sell orders is correct (first correcting is good and on next ticks correcting is ignored: difference between existing and calculated TP less then delta), but for the buy orders on each tick robot try to change current TP to a new TP even if they are equal. This correcting ends after order is closed (it can be after several bars H1), or robot crashed.
Help me, please: where can be the problem?


 
 Post subject: Re: Problem with changing of TP for buy orders Post rating: 0   New post Posted: Fri 22 Mar, 2013, 10:21 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
bootor wrote:
but for the buy orders on each tick robot try to change current TP to a new TP even if they are equal
Use Double.copare for double value comparisons.


 
 Post subject: Re: Problem with changing of TP for buy orders Post rating: 0   New post Posted: Mon 25 Mar, 2013, 21:28 

User rating: 0
Joined: Wed 02 Nov, 2011, 15:45
Posts: 6
Location: Ukraine,
Tell me, please, how can I round the price?
I am using the folowing method, is it correct? Sometimes robots are halted on this function...

private static double round(double amount, int decimalPlaces) {
        return (new BigDecimal(amount)).setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP).doubleValue();
    }


 
 Post subject: Re: Problem with changing of TP for buy orders Post rating: 0   New post Posted: Tue 26 Mar, 2013, 08:14 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
bootor wrote:
Tell me, please, how can I round the price?
I am using the folowing method, is it correct?
Depends on the instrument, see:
https://www.dukascopy.com/wiki/#Rounding_prices
bootor wrote:
Sometimes robots are halted on this function...
Please provide the exception details if any.


 

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