JForex StopManager 1.0
I am building my real JForex automated trading system one feature at a time. The idea is to automate some of my most fundamental trading techniques on JForex so that I can trade forex semi-automatically on Dukascopy.
I am starting off here with a simple stop management strategy. In a nutshell, once the market price moves in your favour equidistant from your original stop loss, this strategy moves the stop to breakeven.
For example, if I go long EURUSD at 1.3500 with a stop at 1.3400 (100 pips stop). Then if the price breaks above 1.3600 (100 pips profit), the strategy moves the stop to breakeven.
This is a simplistic implementation of the saying that “never let profits turn into losses”.
You can download the source code here and the executable strategy here. Note that these files are made available through Dropbox.
Update: This logic is now integrated into the JFUtil open source project. You can also see a list of my latest JForex articles here.
/*
StopManager.java
version 1.0
Copyright 2010 Quantisan.com
Move stops to breakeven when equidistance to original stop loss
*/
package jforex;
import com.dukascopy.api.*;
public class StopManager implements IStrategy {
private IEngine engine;
private IConsole console;
private IContext context;
@Configurable("Lock-in Pips for Breakeven")
public int lockPip = 3;
@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, stop, diff, newStop;
String label = order.getLabel();
IChart chart;
isLong = order.isLong();
open = order.getOpenPrice();
stop = order.getStopLossPrice();
diff = open - stop; // stop loss distance
if (isLong) { // long side order
if (moveBE && diff > 0 && tick.getBid() > (open + diff)) {
// make it breakeven trade + lock in a few pips
newStop = open + instrument.getPipValue() * 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
// Move to breakeven
if (moveBE && diff < 0 && tick.getAsk() < (open + diff)) { // diff is negative
// make it breakeven trade + lock in a few pips
newStop = open - (instrument.getPipValue() * 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);
}
}
}
}
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
}
}
Related posts:

Hi,
your performance at the Dukascopy contest made me look at your blog, and I gotta say your approaches are very interesting. I play with the thought of developing a automated trading system too, I write in MQL, but it’s compatible with JTrader. So if u are interested in working together, just write me..
I gotta admit though that I’m not a good trader yet, I’m just a beginner. However, I’m developing my tool the same way as you – piece by piece according to what works in my manual trading.
Best regards
Alex
Hi Alex,
Thanks for the compliment! Do you write a blog or publish your work somewhere? I’d love to read about other trading ideas. Particularly since we have a similar approach.
Hi Paul,
Do you know any website where I can find more complicated JForex strategies/indicators? I am not looking for a profitable strategy, I am just trying to find a good comprehensive example covering all components of an automatic trading system such as historical bar analysis, order management, visualization, etc. so that I can modify it by replacing my own trading rules. The only thing I can find is the javadoc of JForex. It is a great pain to guess and try to figure everything out from scratch.
Cheers,
Mike
The dukascopy forum offers more examples. But most of them lack meaningful commentary.
I am writing a new series of posts explaining my new JForex strategy. It includes position management, visualization, use of historical bars, a few indicators, and multiple time frames. I think it will be what you are looking for.
Yes, it is exactly what I am looking for. I was told by Dukascopy that MT4 ECN is not even ready for live trading and it has no plan to support MT5 in the future. It seems the JForex api is pretty good because the user can use the full power of java. However reinventing everything from scratch is too much work for me. I am so glad you are going to add more examples.
Thank you for sharing this open source project. I am also a manual trader that switched to Jforex several months back for several reasons. Mostly, honest execution and ecn spreads. I am a scalper who looks for 4-15 pips at a time while using a stoploss to Breakeven +1,2,3 to offset any needed risk. Can you implement at Breakeven value of greater than 0? Moving our stoploss at BE, can cause slippage and still result in a small loss while locking in a few pips, BE +1,2,3 allows a higher possibility of a successful trade. I will be looking forward to your future projects and sharing ideas about automation without the use of candle formations.
Your download link is for the Riskmanager and not the StopManager. Are you aware of this?
Hi Will, there’s a configurable “lock-in pip” parameter when you start the StopManager strategy. Setting it should enable what you’re looking for.
Thanks for notifying me about the link. I wasn’t aware of it.
Paul, thanks for making this available. But how can this be used in a modular way? I’m not understanding if the intent is for the real strategy to extend this class or what.
@brian: You can copy and paste the code in onTick() into your own strategy to either onBar() or onTick(), then modify the logic as you see fit to suit your needs.
Paul, thanks for your quick response. Why did you bundle this functionality into a class? Do I understand correctly that you don’t intend to ever actually instantiate a StopManager?
@brian: You can run StopManager as a strategy in JForex platform to monitor your trades (just load the .jfx file). I made it so that it’s fully JForex API compliant so that it can be used by someone who doesn’t code.
Hi, thanks for this code. It has been very helpful. How would change it to move the stop loss up every other +2 pips since trailstep in jforex is a minimum of 10pips. I want to basically modify the setstoploss up every +2 pips. I hope I’m making sense. Thanks again.
if (isLong) { // long side order
if (moveBE && diff > 0 && tick.getBid() > (open + diff)) {
// make it breakeven trade + lock in a few pips
newStop = open + instrument.getPipValue() * 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);
}
Hi faru,
I no longer use JForex. Perhaps you can ask in the official forum?
Paul