here is the strategy
package singlejartest;
import com.dukascopy.api.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IIndicators.MaType;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.OfferSide;
public class test implements IStrategy {
private IEngine engine = null;
private IIndicators indicators = null;
private IConsole console = null;
private double volume = 0.01;
private int profitLimit = 100;
private int lossLimit = 150;
private double bidPrice;
private double askPrice;
public Instrument instrument= Instrument.EURUSD;
public Period period = Period.FIFTEEN_MINS;
public IOrder marketorder;
public boolean tradeallowed;
public void onStart(IContext context) throws JFException {
Set<Instrument> subscribedInstruments = new HashSet<Instrument>();
subscribedInstruments.add(Instrument.EURUSD);
context.setSubscribedInstruments(subscribedInstruments);
engine = context.getEngine();
indicators = context.getIndicators();
context.getHistory();
console = context.getConsole();
indicators = context.getIndicators();
}
public void onStop() throws JFException {
}
public void onTick(Instrument instrument, ITick tick) throws JFException {
if (instrument != Instrument.EURUSD || period != Period.FIFTEEN_MINS) return;
}
// opened positions counted
protected int positionsTotal(Instrument instrument) throws JFException {
int counter = 0;
for (IOrder order : engine.getOrders(instrument)) {
if (order.getState() == IOrder.State.FILLED) {
counter++;
}
}
return counter;
}
protected String getLabel(Instrument instrument) {
String label = instrument.name();
long time = new java.util.Date().getTime();
label = label.substring(0, 2) + label.substring(3, 5);
label = label + time;
label = label.toLowerCase();
return label;
}
public void onBar(Instrument instrument, Period period, IBar askbar, IBar bidbar) throws JFException {
if(period == Period.FIFTEEN_MINS) {
// Time of opening trade
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT 0"));
int DayOfWeek=calendar.get(Calendar.DAY_OF_WEEK);
int Hour=calendar.get(Calendar.HOUR_OF_DAY);
if (Hour>9 && Hour<16){
tradeallowed = true;
}
else {
tradeallowed = false;
}
// End of opening trade
profitLimit = 10;
lossLimit = 250;
if (askbar.getVolume() == 0) return;
askPrice = askbar.getClose();
bidPrice = bidbar.getClose();
double tvs = this.indicators.tvs(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 18, 0);
double tvs1 = this.indicators.tvs(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 18, 1);
double sma14 = this.indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 14, 0);
double sma14h = this.indicators.sma(instrument, Period.FOUR_HOURS, OfferSide.BID, IIndicators.AppliedPrice.CLOSE,14, 0);
double sma14ha = this.indicators.sma(instrument, Period.ONE_HOUR, OfferSide.BID, IIndicators.AppliedPrice.CLOSE,14, 0);
double rsi = indicators.rsi(instrument, Period.ONE_HOUR, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 14, 0);
if (positionsTotal(instrument) == 0) {
if (bidPrice > sma14 && tvs > 0 && tvs1 > 0 && tvs > tvs1 ) {
marketorder = buy(instrument, engine, profitLimit, lossLimit, volume);
}
else if (bidPrice < sma14 && tvs < 0 && tvs1 < 0 && tvs < tvs1 ) {
marketorder = sell(instrument, engine, profitLimit, lossLimit, volume);
}
}
}
}
public void onMessage(IMessage message) throws JFException {
if (message != null && message.getType() == IMessage.Type.ORDER_CLOSE_OK) {
IOrder lastOne = message.getOrder();
double profitsLoss = lastOne.getProfitLossInPips();
console.getOut().println("Order : "+lastOne.getLabel()+ " "+ lastOne.getOrderCommand()+ " Pips: "+profitsLoss);
}
}
@Override
public void onAccount(IAccount account) throws JFException {
}
public IOrder sell(Instrument instrument, IEngine engine, int takeProfitPipLevel, int endLossPipLevel, double volumeParam) throws JFException {
return engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.SELL, volumeParam, 0, 3, bidPrice
+ instrument.getPipValue() *endLossPipLevel, bidPrice - instrument.getPipValue() * takeProfitPipLevel);
}
public IOrder buy(Instrument instrument, IEngine engine, int takeProfitPipLevel, int endLossPipLevel, double volumeParam) throws JFException {
return engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.BUY, volumeParam, 0, 3, askPrice
- instrument.getPipValue() * endLossPipLevel, askPrice + instrument.getPipValue() * takeProfitPipLevel);
}
}