JForex Example: Not letting profit turn to losses with a breakeven stop

While the dual-timeframe trading setup and much of the algorithm in my contest strategy are for competition only (read: not for use with real money) as I've warned numerous times, some of the risk management techniques used there are really what I use in real life. This being one of them. There's a saying in trading that "never let profits turns into losses." That is exactly the motivation behind this JForex code snippet. The following source code are excerpts from my Dukascopy JForex contest's July strategy. The complete source code file is available in that link. Back to this concept of not letting profits turn to losses. This is a matter of balancing between giving room for your trade to reach its potential and limiting your drawdown. If you're too careful, you may find yourself being whipsawed out before a price move can materialize. If you keep your leash too loose though, well, you may watch your profits turn into losses. The way I intrepret the saying is that once your trade is profitable enough, you shouldn't let it slip back into a loss. I implement the above statement in my automated strategy as follows. Note that everything goes in the onTick() method so that it watches your position on every tick. The line numbers correspond to the complete source code of my automated strategy. [java firstline="177"] boolean isLong; double open, stop, diff, newStop; for (IOrder order : engine.getOrders(instrument)) { if (order.getState() == IOrder.State.FILLED) { [/java] [java firstline="191"] isLong = order.isLong(); open = order.getOpenPrice(); stop = order.getStopLossPrice(); diff = (open - stop); // ********* BREAKEVEN *********************** if (isLong && diff > 0 && tick.getBid() > (open + diff)) { order.close(roundLot(order.getAmount() * beRatio)); // close a portion newStop = open + instrument.getPipValue() * LOCKPIP; order.setStopLossPrice(newStop); print(order.getLabel() + ": Moved STOP to breakeven"); } else if (!isLong && diff \< 0 && tick.getAsk() \< (open + diff)) { order.close(roundLot(order.getAmount() * beRatio)); newStop = open - (instrument.getPipValue() * LOCKPIP); order.setStopLossPrice(newStop); print(order.getLabel() + ": Moved STOP to breakeven"); } [/java] What this does is that it will partially exit a position and move the stop to breakeven once the price is equidistantly positive from your initial stop loss. For example, if my stop loss is 100 pips, then once the position is 100 pips in profit, it will exit partially and set the new stop to breakeven. Update: This is now integrated into the JFUtil open source project.