Let's take this strategy.
With the previous JForex versions it was working fine.
I use optimization at the historical tester, I setup the stop loss from 10 to 100 step 10.
Take profit 200.
Trailing step 10.
You'll see it will open too many positions at the same time.
What's wrong?
package jforex.strategies;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.*;
import com.dukascopy.api.*;
import com.dukascopy.api.feed.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IIndicators.AppliedPrice;
public class trend implements IStrategy {
@Configurable("Instrument")
public Instrument instrument = Instrument.XAGUSD;
public Period period;
@Configurable("Amount")
public double amount = 0.001;
@Configurable("Stop loss")
public int stopLossPips = 10;
@Configurable("Take profit")
public int takeProfitPips = 200;
@Configurable("TrailingStep")
public int TrailingStep = 10;
private IEngine engine;
private IConsole console;
private IHistory history;
private IIndicators indicators;
private int orderCount;
@Override
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.console = context.getConsole();
this.history = context.getHistory();
console.getOut().println("start");
context.subscribeToBarsFeed(Instrument.XAGUSD, Period.createCustomPeriod(Unit.Hour, 4), OfferSide.BID,
new IBarFeedListener() {
public void onBar(Instrument instrument, Period period, OfferSide offerSide, IBar bidBar) {
try {
IBar prevBar = history.getBar(instrument, period, OfferSide.BID, 2);
OrderCommand cmd = bidBar.getClose() > prevBar.getClose()
? OrderCommand.BUY
: OrderCommand.SELL;
submitOrder(cmd);
}
catch (JFException jfe) {
console.getOut().println(jfe.toString());
}
}
}
);
}
private IOrder submitOrder(OrderCommand orderCmd) throws JFException {
double stopLossPrice, takeProfitPrice;
if (orderCmd == OrderCommand.BUY) {
stopLossPrice = history.getLastTick(this.instrument).getBid() - getPipPrice(this.stopLossPips);
takeProfitPrice = history.getLastTick(this.instrument).getBid() + getPipPrice(this.takeProfitPips);
for(IOrder o : engine.getOrders()){
if (!o.isLong() && o.getInstrument().equals(instrument)) {
print("Closing Short position");
o.close();
}
}
}
else {
stopLossPrice = history.getLastTick(this.instrument).getAsk() + getPipPrice(this.stopLossPips);
takeProfitPrice = history.getLastTick(this.instrument).getAsk() - getPipPrice(this.takeProfitPips);
for(IOrder o : engine.getOrders()){
if (o.isLong() && o.getInstrument().equals(instrument)) {
print("Closing Long position");
o.close();
}
}
}
// Submitting an order for the specified instrument at the current market price
IOrder order = engine.submitOrder("order" + ++orderCount , this.instrument, orderCmd, this.amount, 0, 10, stopLossPrice, takeProfitPrice);
//wait max 2 secs for order to get opened
order.waitForUpdate(2000);
//normally state here should be OPENED, but for a more general case we add also FILLED
if(order.getState() == IOrder.State.OPENED || order.getState() == IOrder.State.FILLED){
order.setStopLossPrice(stopLossPrice, OfferSide.BID, TrailingStep);
}
return order;
}
@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {
}
@Override
public void onBar(Instrument Instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
if( !instrument.equals(Instrument) || this.period != period){
return;
}
}
private double getPipPrice(int pips) {
return pips * this.instrument.getPipValue();
}
@Override
public void onMessage(IMessage message) throws JFException {
//if the message is related to order print its content
if (message.getOrder() != null){
// console.getOut().println(message.getOrder().getLabel() + " " + message.getType() + " " + message.getContent());
}
}
@Override
public void onAccount(IAccount account) throws JFException {
}
@Override
public void onStop() throws JFException {
}
public void print(String message) {
console.getOut().println(message);
}
}