Please see code below (forum gave me error regarding attachment quota). All logic related to positions has been cleared, but if I run this strategy on my LIVE account, I frequently get "Incomplete data" in the log files.
package forex.strategies;
import java.util.Date;
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;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.PriceRange;
import com.dukascopy.api.feed.IFeedDescriptor;
import com.dukascopy.api.feed.IRangeBar;
import com.dukascopy.api.feed.IRangeBarFeedListener;
import com.dukascopy.api.feed.util.RangeBarFeedDescriptor;
public class DukascopySampleStrategy implements IStrategy {
protected IEngine engine;
protected IIndicators indicators;
protected IHistory history;
protected IConsole console;
protected IContext context;
private Instrument instrument = Instrument.EURCAD;
private int minmax = 30;
private int ema = 100;
private IRangeBarFeedListener listener = null;
private PriceRange priceRange = PriceRange.valueOf(10);
public String getDescription() {
return "";
}
protected Instrument getInstrument() {
return instrument;
}
protected long getLogTime() {
try {
return history.getLastTick(getInstrument()).getTime();
} catch (Exception e) {
return new Date().getTime();
}
}
// ** onBar used for entries **
public void onBar(Instrument instrument, Period period, IBar askBar,
IBar bidBar) {
}
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.indicators = context.getIndicators();
this.history = context.getHistory();
this.console = context.getConsole(); // allows printing to console
this.context = context;
subscribe();
}
private void subscribe() {
if(listener != null) {
context.unsubscribeFromRangeBarFeed(listener);
}
listener = new IRangeBarFeedListener() {
@Override
public void onBar(Instrument arg0, OfferSide arg1, PriceRange arg2,
IRangeBar arg3) {
// TODO Auto-generated method stub
try {
DukascopySampleStrategy.this.setSentiment(arg0, arg3);
} catch (Exception e) {
log("Got exception in setSentiment(): " + e.getMessage());
e.printStackTrace();
}
}
};
context.subscribeToRangeBarFeed(getInstrument(), OfferSide.BID, priceRange, listener);
log("Subscribed to " + priceRange + " feed");
}
// ** onTICK: for open position management **
public void onTick(Instrument instrument, ITick tick) {
}
private void setSentiment(Instrument instrument, IRangeBar selectedRangeBar)
throws JFException {
IFeedDescriptor feedDescriptor = new RangeBarFeedDescriptor(instrument, priceRange, OfferSide.BID);
Object[] minMaxFeed = indicators.calculateIndicator(feedDescriptor, new OfferSide[] { OfferSide.BID }, "MinMax",
new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { minmax }, 20, selectedRangeBar.getTime(), 0);
double[] minFeed = (double[]) minMaxFeed[0];
double[] maxFeed = (double[]) minMaxFeed[1];
Object[] macdFeedObj = indicators.calculateIndicator(feedDescriptor, new OfferSide[] { OfferSide.BID }, "MACD",
new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { 12, 26, 9 }, 3, selectedRangeBar.getTime(), 0);
double[] macdFeed = (double[]) macdFeedObj[0];
double[] macdHistFeed = (double[]) macdFeedObj[2];
Object[] maLngFeed = indicators.calculateIndicator(feedDescriptor, new OfferSide[] { OfferSide.BID }, "EMA",
new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { ema }, 2, selectedRangeBar.getTime(), 0);
double[] emaLng = (double[]) maLngFeed[0];
if(minFeed.length < 1 || maxFeed.length < 1 || emaLng.length < 2 || macdFeed.length < 2) {
///// <---- THIS HAPPENS A LOT ----->
log("Incomplete data!");
return;
}
}
@Override
public void onAccount(IAccount arg0) throws JFException {
// TODO Auto-generated method stub
}
@Override
public void onMessage(IMessage arg0) throws JFException {
// TODO Auto-generated method stub
}
@Override
public void onStop() throws JFException {
// TODO Auto-generated method stub
}
protected void log(String string) {
this.console.getOut().println(string);
}
}