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.

Stoploss Break Even
 Post subject: Re: Stoploss Break Even Post rating: 0   New post Posted: Thu 15 Dec, 2011, 13:41 

User rating: 0
Joined: Thu 15 Dec, 2011, 13:31
Posts: 2
Location: PL
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?


 
 Post subject: Re: Stoploss Break Even Post rating: 0   New post Posted: Mon 19 Dec, 2011, 15:08 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
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 414 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: Stoploss Break Even Post rating: 0   New post Posted: Fri 16 Jan, 2015, 08:00 
User avatar

User rating: 0
Joined: Wed 04 Apr, 2012, 00:00
Posts: 1
Location: SpainSpain
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


 
 Post subject: Re: Stoploss Break Even Post rating: 0   New post Posted: Tue 20 Jan, 2015, 16:44 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hi!

Take a look at these break even examples: https://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.


 

Jump to:  

  © 1998-2026 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