Dukascopy Support Board
http://www.dukascopy.com/swiss/english/forex/jforex/forum/

Stoploss Break Even
http://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=65&t=44322
Page 2 of 2

Author:  klint [ Thu 15 Dec, 2011, 13:41 ]
Post subject:  Re: Stoploss Break Even

Hello:)
Can you fix that bug?: StopManager is trying to set BE every one tick, so I get tons of messages like these:

12:35:15 Order REJECTED: STOP LOSS SELL EUR/USD @MKT IF BID <= 1.29972 - Position #31563296, REASON: can't change stop loss price more than once in a second
12:35:13 Order REJECTED: STOP LOSS SELL EUR/USD @MKT IF BID <= 1.29972 - Position #31563296, REASON: can't change stop loss price more than once in a second
12:35:13 Order REJECTED: STOP LOSS SELL EUR/USD @MKT IF BID <= 1.29972 - Position #31563296, REASON: can't change stop loss price more than once in a second
12:35:12 Order REJECTED: STOP LOSS SELL EUR/USD @MKT IF BID <= 1.29972 - Position #31563296, REASON: can't change stop loss price more than once in a second
12:35:11 Order REJECTED: STOP LOSS SELL EUR/USD @MKT IF BID <= 1.29972 - Position #31563296, REASON: can't change stop loss price more than once in a second
.............

Once BE is set, it should stop trying doing it again:)


Another question - is it possible to implement to this strategy one importat thing - setting the SL itself.
I use BID/OFFER orders, which are sent without predefined SL. I would like the Strategy to check if there is any
order without SL, and if there is - it would then automatically set it to predefined SL in Strategy.
I wonder why such important thing is not implemented into JForex platform?

Author:  API Support [ Mon 19 Dec, 2011, 15:08 ]
Post subject:  Re: Stoploss Break Even

Quote:
I wonder why such important thing is not implemented into JForex platform?

When trading manually default stop loss can be enabled in settings: Tools > Preferences > General > "Apply defoult stop loss to all market orders".

Here is modified strategy.
It updates stop loss only once and automatically sets stop loss to orders that do not have stop loss.

package jforex.strategies;

import com.dukascopy.api.*;

public class StopManager implements IStrategy {

    private IEngine engine;
    private IConsole console;
    private IContext context;
    @Configurable("Lock-in Pips for Breakeven (SLBE)")
    public int lockPip = 1;
    @Configurable("Stoploss Break Even Trigger (SLBET)")
    public int triggerPips = 3;
    @Configurable("Auto stop loss pips")
    public int autoStopLossPips = 10;
    @Configurable("Move stop to breakeven?")
    public boolean moveBE = true;

    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.context = context;
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
       
        for (IOrder order : engine.getOrders(instrument)) {
            if (order.getState() == IOrder.State.FILLED) {
                boolean isLong;
                double open, newStop, stopLoss;
                String label = order.getLabel();
                IChart chart;

                isLong = order.isLong();
                open = order.getOpenPrice();
                stopLoss = order.getStopLossPrice();
               
                // auto set stop loss
                if( stopLoss == 0.0 ){
                    if(isLong) {
                        order.setStopLossPrice(open - toPrice(instrument, autoStopLossPips) );
                    } else {
                        order.setStopLossPrice(open + toPrice(instrument, autoStopLossPips) );
                    }
                }
               
               
                if (isLong) { // long side order
                    if(open < stopLoss){
                        return;
                    }
                   
                    if (moveBE && tick.getBid() > (open + toPrice(instrument, triggerPips))) {
                        // make it breakeven trade + lock in a few pips
                        newStop = open + toPrice(instrument, lockPip);
                        order.setStopLossPrice(newStop);
                        console.getOut().println(label + ": Moved stop to breakeven");

                        chart = this.context.getChart(instrument);
                        chart.draw(label + "_BE", IChart.Type.SIGNAL_UP, tick.getTime(), newStop);
                    }
                } else { // short side order
                    if(open > stopLoss){
                        return;
                    }
                   
                    // Move to breakeven
                    if (moveBE && tick.getAsk() < (open - toPrice(instrument, triggerPips))) { // diff is negative
                        // make it breakeven trade + lock in a few pips
                        newStop = open - toPrice(instrument, lockPip);
                        order.setStopLossPrice(newStop);
                        console.getOut().println(label + ": Moved stop to breakeven");

                        chart = this.context.getChart(instrument);
                        chart.draw(label + "_BE", IChart.Type.SIGNAL_DOWN, tick.getTime(), newStop);
                    }
                }
            }
        }
    }
   
    private double toPrice(Instrument instr, int pips) {
        return instr.getPipValue() * pips;
    }

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
}


Attachments:
StopManager.java [3.56 KiB]
Downloaded 362 times

Author:  jguellbo [ Fri 16 Jan, 2015, 08:00 ]
Post subject:  Re: Stoploss Break Even

Hi, I have some problems implementing strategies in jforex. For example, the one in this thread called stopManager. I want :
1- Stop order at 5 pips
2-Once my position is +5 pips, I need the stop to breakeven, nothing else. I don't want the trailing stop.

How I can do that?
In other way, there is an option called "remote run". How this works?

Thank you very much

Author:  API Support [ Tue 20 Jan, 2015, 16:44 ]
Post subject:  Re: Stoploss Break Even

Hi!

Take a look at these break even examples: http://www.dukascopy.com/wiki/#Set_Stop ... Break_even

Remote run means that your strategy will be running on Dukascopy servers, not on your local PC. If you run your strategy remotely, then you can close JForex Platform and your strategy will still be running.

  Page 2 of 2