package jforex;
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.IIndicators;
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.IEngine.OrderCommand;
public class MovingAverageExample implements IStrategy {
// these are strategy interfaces which are used to execute trades, request indicators, print messages
private IEngine engine;
private IIndicators indicators;
private IConsole console;
// these are strategy parameters
private Instrument myInstrument = Instrument.EURUSD;
private Period myPeriod = Period.ONE_HOUR;
private int fastMAPeriod = 7;
private int slowMAPeriod = 14;
private boolean tradingAllowed;
private double takeProfit = 10 / Math.pow(10, myInstrument.getPipScale());
private double stopLoss = 20 / Math.pow(10, myInstrument.getPipScale());
private boolean loggingEnabled = true;
@Override
public void onStart(IContext context) throws JFException {
// we initialize interfaces in the onStart method
engine = context.getEngine();
indicators = context.getIndicators();
console = context.getConsole();
}
// we put the trading algorithms either in onBar or onTick methods. Once the bar is closed, the script will be run
@Override
public void onBar(Instrument instrument, Period period, IBar askBar,
IBar bidBar) throws JFException {
// in the line below we indicate that we are oging to trade on EURUSD and on 1 min chart. And we will trade only when the trade will be allowed
if (myPeriod == period && myInstrument == instrument && tradingAllowed) {
double fastMA = indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, fastMAPeriod, 0);
double fastMAonPreviousBar = indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, fastMAPeriod, 1);
double slowMA = indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, slowMAPeriod, 0);
double slowMAonPreviousBar = indicators.sma(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, slowMAPeriod, 1);
// condition when we BUY
if (fastMAonPreviousBar < slowMAonPreviousBar && fastMA > slowMA) {
engine.submitOrder("BUY", instrument, OrderCommand.BUY, 0.1, 0, 5, bidBar.getClose() - stopLoss, bidBar.getClose() + takeProfit);
print("BUY signal! " + stopLoss + takeProfit + new Date(bidBar.getTime()));
// condition when we SELL
} else if (fastMAonPreviousBar > slowMAonPreviousBar
&& fastMA < slowMA) {
engine.submitOrder("SELL", instrument, OrderCommand.SELL, 0.1, 0, 5, askBar.getClose() + stopLoss, askBar.getClose() - takeProfit);
print("SELL signal!" + stopLoss + takeProfit + new Date(bidBar.getTime()));
}
}
}
public void print(String string) {
if (loggingEnabled) console.getOut().println(string);
}
@Override
public void onAccount(IAccount account) throws JFException {
if (account.getUseOfLeverage()==0) tradingAllowed=true;
}
@Override
public void onMessage(IMessage message) throws JFException {
}
@Override
public void onStop() throws JFException {
}
@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {
}
}