Hi everyone,
----------
First of all, I could not see a sub-forum specifically for programming, so I thought I would post my query in this section. If there is a programming section, please let me know for future reference.
----------
I have been asked to help a user with his strategy - by looking at the Java code he implemented and make some minor tweaks to suit his needs. Basically, he wants to make a trade for every 1 hour period. At the moment, the code I do have is running, but it is not making any trades whatsoever. Because I have very limited knowledge on this particular program, I thought someone here might be able to see what is wrong. Here is the code of this particular strategy:
package jforex;
import com.dukascopy.api.*;
import com.dukascopy.api.Configurable;
import com.dukascopy.api.Filter;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IFillOrder.*;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IIndicators.MaType;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.IUserInterface;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import java.util.*;
public class Strategy implements IStrategy {
private IEngine engine;
private IConsole console;
private IHistory history;
private IContext context;
private IIndicators indicators;
private IUserInterface userInterface;
private int counter = 0;
@Configurable("Instrument")
public Instrument instrument = Instrument.EURUSD;
@Configurable("Period")
public Period selectedPeriod = Period.ONE_HOUR;
@Configurable("G")
public double g = 0.1;
@Configurable("H")
public double h = 0.1;
@Configurable("Fast K period")
public int fastKPeriod = 3;
@Configurable("Slow K Period")
public int slowKPeriod = 5;
@Configurable("K MA Type")
public MaType slowKMaType = MaType.SMA;
@Configurable("Slow D Period")
public int slowDPeriod = 5;
@Configurable("D MA Type")
public MaType slowDMaType = MaType.SMA;
@Configurable("DMI")
public int DMI = 14;
@Configurable("Amount")
public double amount = 0.1;
@Configurable("Stop loss")
public int stopLossPips = 30;
@Configurable("Take profit")
public int takeProfitPips = 90;
private IOrder order;
private Filter filter = Filter.ALL_FLATS;
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.console = context.getConsole();
this.history = context.getHistory();
this.context = context;
this.indicators = context.getIndicators();
this.userInterface = context.getUserInterface();
}
public void onAccount(IAccount account) throws JFException {
}
public void onMessage(IMessage message) throws JFException {
}
public void onStop() throws JFException {
if (order != null) {
order.close();
}
}
public void onTick(Instrument barInstrument, ITick tick) throws JFException {
}
public void onBar(Instrument barInstrument, Period period, IBar askBar, IBar bidBar) throws JFException {
if (order != null && order.getState() == IOrder.State.CLOSED) {
order = null;
}
if (!instrument.equals(barInstrument) || (period != this.selectedPeriod)) {
return;
}
IBar prevBar = history.getBar(instrument, selectedPeriod, OfferSide.BID, 1);
double[] dPlusDI = indicators.plusDi(instrument, selectedPeriod, OfferSide.BID, DMI, filter, 2, bidBar.getTime(), 0);
double[] dMinusDI = indicators.minusDi(instrument, selectedPeriod, OfferSide.BID, DMI, filter, 2, bidBar.getTime(), 0);
double[] dStoch = indicators.stoch(instrument, period, OfferSide.BID, fastKPeriod, slowKPeriod, slowKMaType, slowDPeriod, slowDMaType, 0);
if (period != selectedPeriod) {
//GO LONG
if (order == null && dPlusDI[1] > dPlusDI[0] * (1 + g) && dMinusDI[1] < dMinusDI[0] * (1 - g) && dStoch[0] > dStoch[1] * (1 + h)) {
//if all conditions are satisfied, make a trade (buy currency using specified instrument)
order = engine.submitOrder("EUR/USD", instrument, IEngine.OrderCommand.BUY, amount);
console.getOut().println("BUY");
} else {
//if any condition fails, close the trade
order.close();
console.getOut().println("CLOSE");
}
//GO SHORT
if (order == null && dPlusDI[1] < dPlusDI[0] * (1 - g) && dMinusDI[1] < dMinusDI[0] * (1 + g) && dStoch[0] < dStoch[1] * (1 - h)) {
//if all conditions are satisfied, make a trade (sell currency using specified instrument)
order = engine.submitOrder("EUR/USD", instrument, IEngine.OrderCommand.SELL, amount);
console.getOut().println("SELL");
} else {
//if any condition fails, close the trade
order.close();
console.getOut().println("CLOSE");
}
if (order != null && order.getState() == IOrder.State.FILLED) {
if (order.isLong() == true && (dPlusDI[1] < dPlusDI[0] || dMinusDI[1] > dMinusDI[0] || dStoch[0] < dStoch[1])) {
order.close();
console.getOut().println("CLOSE");
order = null;
} else if (order.isLong() == false && (dPlusDI[1] > dPlusDI[0] || dPlusDI[1] < dMinusDI[0] || dStoch[0] > dStoch[1])) {
order.close();
console.getOut().println("CLOSE");
order = null;
}
}
}
}
}
I have enclosed the executing code in an if block (which begins with
if (period != selectedPeriod) {), assuming that a trade will be made only every hour. I think an else block should accompany this if block, but I wouldn't know what to put in it.
I will be grateful for any help received.