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.

Breakeven closes my position in a time as it opened
 Post subject: Breakeven closes my position in a time as it opened Post rating: 0   New post Posted: Thu 23 Oct, 2014, 09:23 
User avatar

User rating: 6
Joined: Wed 05 Mar, 2014, 12:00
Posts: 116
Location: Ukraine, Odessa
Hi dear support.

I have a problem with my strategy.

IT opens 1 position,than closes on condition,and opens it in opposing direction.

But when I started to use a breakeven code, I met a problem of imidiately close of my position as it was opened.


Image

This Breakeven code works not only to bring a SL to an opened price but to bring it with some profit pips.
It works fine on a historical tester, but on a DEMO & LIVE trading gives an above mentioned picture,and writes a massage in log:

Image

So please sea part of code:
   // BreakEven
      if(_breakeven!=0) {         
      for (IOrder order : engine.getOrders(instr)) {

           if(order.getOrderCommand()==IEngine.OrderCommand.BUY) {
                if(tick.getBid()-order.getOpenPrice()>=_breakeven*instr.getPipValue()) {
                      if(round(order.getOpenPrice()+_breakeven_pips*instr.getPipValue(),5)>order.getStopLossPrice()) order.setStopLossPrice(round(order.getOpenPrice()+_breakeven_pips*instr.getPipValue(),5));
                }
           } 
           
           if(order.getOrderCommand()==IEngine.OrderCommand.SELL) {
               
               if(order.getOpenPrice()-tick.getAsk()>=_breakeven*instr.getPipValue()) {                   
                      if(round(order.getOpenPrice()-_breakeven_pips*instr.getPipValue(),5)<order.getStopLossPrice()) order.setStopLossPrice(round(order.getOpenPrice()-_breakeven_pips*instr.getPipValue(),5));                     
               }
           }         

      }     
      }   


What is wrong?

Please give advice.
Your faithfully,
Illya Budayev


Attachments:
breakeven false2.png [34.11 KiB]
Downloaded 508 times
breakeven false.png [39.9 KiB]
Downloaded 506 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: Breakeven closes my position in a time as it opened Post rating: 0   New post Posted: Mon 27 Oct, 2014, 14:24 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please, provide the strategy that replicates the case. If you don't want to post it on forum, you can send it to [email protected]


 
 Post subject: Re: Breakeven closes my position in a time as it opened Post rating: 0   New post Posted: Wed 11 Feb, 2015, 17:16 
User avatar

User rating: 6
Joined: Wed 05 Mar, 2014, 12:00
Posts: 116
Location: Ukraine, Odessa
Sent you an email. Pls confirm if you got received.


 
 Post subject: Re: Breakeven closes my position in a time as it opened Post rating: 0   New post Posted: Tue 24 Feb, 2015, 16:44 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
We have received your email and will answer as soon as possible.


 
 Post subject: Re: Breakeven closes my position in a time as it opened Post rating: 1   New post Posted: Tue 24 Feb, 2015, 18:21 
User avatar

User rating: 2
Joined: Wed 03 Sep, 2014, 09:11
Posts: 32
Location: GermanyGermany
please... ;)


//class variable
    private boolean breakEvenReached = false;

    //method
    private void updateBreakEven(IOrder order, double breakEvenPips, double breakEvenTriggerPips) throws JFException {

        if (breakEvenReached == false && order.getProfitLossInPips() >= breakEvenTriggerPips) {

            double breakEvenPrice = getPriceWithAddedPips(order.getInstrument(), order.getOpenPrice(),
                                                          order.isLong() ? breakEvenPips : -breakEvenPips);

            order.setStopLossPrice(breakEvenPrice);
            breakEvenReached = true;

        }

    }

    //helper
    private double getPriceWithAddedPips(Instrument instrument, double price, double pipsToAdd) {
        if (instrument == null) {

            return 0.0D;
        }
        return getRoundedDouble(price + (instrument.getPipValue() * pipsToAdd), instrument.getPipScale() + 1);
    }

    private double getRoundedDouble(double value, int precision) {
        double roundingFactor = Math.pow(10, precision);
        return Math.round(value * roundingFactor) / roundingFactor;
    }




 
 Post subject: Re: Breakeven closes my position in a time as it opened Post rating: 0   New post Posted: Fri 27 Feb, 2015, 13:37 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
We were unable to reproduce this problem. For us the strategy is setting break even orders correctly.
Does it occur in DEMO or LIVE, does it repeat in historical tester. What parameters did you use to call the strategy?
Also it would be great if you could give a simplified strategy code that would reproduce the problem.


 
 Post subject: Re: Breakeven closes my position in a time as it opened Post rating: 0   New post Posted: Sat 28 Feb, 2015, 12:14 
User avatar

User rating: 6
Joined: Wed 05 Mar, 2014, 12:00
Posts: 116
Location: Ukraine, Odessa
The code what I sent to you is original. I tried it on a historical tester, and it gives a predicted result, it works out. But when I try it on a DEMO account in remote and local mode,it shows an above mentioned picture. What I exactly see, my strategy opens a position then in a 1-2 seconds it changed a STOP LOSS and bring it higher than a open price if its BUY position, but some times it gives a correct stop loss if position is SELL. So it looks like a problem with a stop loss. This situation appeared if _breakeven and _breakeven_pips parameters are more than 0.


 
 Post subject: Re: Breakeven closes my position in a time as it opened Post rating: 0   New post Posted: Thu 05 Mar, 2015, 10:33 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
What are the parameters that are being used to run the strategy?


 
 Post subject: Re: Breakeven closes my position in a time as it opened Post rating: 0   New post Posted: Thu 19 Mar, 2015, 19:37 
User avatar

User rating: 6
Joined: Wed 05 Mar, 2014, 12:00
Posts: 116
Location: Ukraine, Odessa
Hi! Sorry for a long delay . I was bussy with a contests.

So , here they are my parameterse that I use, for checking my strategy on a DEMO remote and local run, both of them give same above mantion result : that in a time as it open a postion a strategy bring a stop loss above the open price ,if it BUY position, or it bring lower . So my position as well closes.
 @Configurable("Instrument:") public Instrument instr = Instrument.EURUSD;
    @Configurable(value="Period:") public Period per = Period.TEN_SECS;
    @Configurable(value="TE Time Period:", stepSize=1) public int TEtimePeriod = 19;
    @Configurable(value="TE Deviation:", stepSize=0.01) public double TEdeviation = 0.05;
    public double amount = 0.01;
    @Configurable(value="Take Profit:", stepSize=0.1) public double tp = 100;
    @Configurable(value="Stop Loss:", stepSize=0.1) public double sl = 200;
    @Configurable("Risk in %") public double lots = 25;
    @Configurable("Breakeven") public int _breakeven=5;   
    @Configurable("Breakeven pips") public int _breakeven_pips=2; 

So a same picture appeared.

But I changed my code , thanks to MR.nicejack . Danke )

It almost works.
But new problem appear,
17:54:50 java.lang.NullPointerException @ TE_BreakEven_2.onBar(TE_BreakEven_2.java:207)


As I got, this problem is connected with this line
 double breakEvenPrice = getPriceWithAddedPips(order.getInstrument(), order.getOpenPrice(),
                                                          order.isLong() ? breakEvenPips : -breakEvenPips);

if I make last line parameter breakEvenPips positive and it looks like this
order.isLong() ? breakEvenPips : breakEvenPips

a NullPointerException dissappear but a breakeven works only in one way.


 
 Post subject: Re: Breakeven closes my position in a time as it opened Post rating: 1   New post Posted: Fri 20 Mar, 2015, 13:21 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
A common solution could be to print out variable values to see which of them is null.


 
 Post subject: Re: Breakeven closes my position in a time as it opened Post rating: 0   New post Posted: Fri 20 Mar, 2015, 14:24 
User avatar

User rating: 6
Joined: Wed 05 Mar, 2014, 12:00
Posts: 116
Location: Ukraine, Odessa
Got it. At last I did it, by the example from dukascopy wiki paragraph named Break even for multiple orders

and with a big thanks to MR.nicejack .

I got a code that has this view:

 

//CLASS

    private List<IOrder> myOrders = new ArrayList<IOrder>();
    private IOrder order;


public void onTick(Instrument instrument, ITick tick) throws JFException {
// BreakEven
     
          if(instrument != this.instr
            || myOrders.isEmpty() //all BE's reached, nothing to check
           
        ){
         return ;
        }
       List<IOrder> ordersBeReached = new ArrayList<IOrder>();
    for(IOrder order  : myOrders){
          if( order.getProfitLossInPips() >= _breakeven ){                 
       
             double breakEvenPrice = getPriceWithAddedPips(order.getInstrument(), order.getOpenPrice(),order.isLong() ? _breakeven_pips : -_breakeven_pips);
             order.setStopLossPrice(breakEvenPrice);
             ordersBeReached.add(order);           
            console.getOut().println("breakEvenReached");     
          }
    }
           for(IOrder order  : ordersBeReached){
        myOrders.remove(order);
    }
         }



Tnank you very much ;)


 

Jump to:  

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