package dukascopy.strategies.indicators; import com.dukascopy.api.*; import com.dukascopy.api.IEngine.OrderCommand; import com.dukascopy.api.IIndicators.AppliedPrice; public class SMARSI implements IStrategy { private IEngine engine; private IConsole console; private IHistory history; private IIndicators indicators; private int counter =0; private double [] filteredSma90; private double [] filteredEma10; private double rsi; private IOrder order = null; @Configurable("Instrument") public Instrument selectedInstrument = Instrument.EURUSD; @Configurable("Period") public Period selectedPeriod = Period.ONE_HOUR; @Configurable("SMA filter") public Filter indicatorFilter = Filter.NO_FILTER; @Configurable("Stop Loss") public int stopLoss = 70; @Configurable("RSI Overbought value") public double RSIBuy = 65; @Configurable("RSI Oversold value") public double RSISell = 35; @Configurable("Take profit Buy") public double TakeProfitBuy = 200; @Configurable("Take profit Sell") public double TakeProfitSell = 200; public void onStart(IContext context) throws JFException { this.engine = context.getEngine(); this.console = context.getConsole(); this.history = context.getHistory(); this.indicators = context.getIndicators(); } public void onAccount(IAccount account) throws JFException { } public void onMessage(IMessage message) throws JFException { } public void onStop() throws JFException { for (IOrder order : engine.getOrders()) { engine.getOrder(order.getLabel()).close(); } } public void onTick(Instrument instrument, ITick tick) throws JFException { if (!instrument.equals(selectedInstrument)) { return; } IBar prevBar = history.getBar(instrument, selectedPeriod, OfferSide.BID, 1); filteredSma90 = indicators.sma(instrument, selectedPeriod, OfferSide.BID, AppliedPrice.CLOSE, 90, indicatorFilter, 2, prevBar.getTime(), 0); filteredEma10 = indicators.ema(instrument, selectedPeriod, OfferSide.BID, AppliedPrice.CLOSE, 10, indicatorFilter, 2, prevBar.getTime(), 0); // if ((filteredEma10[1] < filteredEma10[0]) && (filteredEma10[1] < filteredSma90[1]) && (filteredEma10[0] >= filteredSma90[0])) { if (engine.getOrders().size() > 0) { for (IOrder orderInMarket : engine.getOrders()) { if (orderInMarket.isLong()) { print("Long position closed"); orderInMarket.close(); } } } if ((order == null) || (order.isLong() && order.getState().equals(IOrder.State.CLOSED)) ) { rsi= indicators.rsi(instrument, selectedPeriod, OfferSide.BID, AppliedPrice.CLOSE, 14, 0); if ( rsi < RSISell ) { System.out.println("SELL RSI: " + rsi + " EMA Prev" + filteredEma10[1] + " EMA Last" + filteredEma10[0] + " sma 90 Prev" + filteredSma90[1] + " sma 90 Last" + filteredSma90[0]); print("SELL"); order = engine.submitOrder(getLabel(instrument), instrument, OrderCommand.SELL, 2, 0, 20, tick.getBid() + instrument.getPipValue() * stopLoss, tick.getBid() - instrument.getPipValue() * TakeProfitSell); } } } // if ((filteredEma10[1] > filteredEma10[0]) && (filteredEma10[1] > filteredSma90[1]) && (filteredEma10[0] <= filteredSma90[0])) { if (engine.getOrders().size() > 0) { for (IOrder orderInMarket : engine.getOrders()) { if (!orderInMarket.isLong()) { print("Short position closed"); orderInMarket.close(); } } } if ((order == null) || (!order.isLong() && order.getState().equals(IOrder.State.CLOSED)) ) { rsi = indicators.rsi(instrument, selectedPeriod, OfferSide.BID, AppliedPrice.CLOSE, 14, 0); if ( rsi > RSIBuy) { System.out.println("BUY RSI: " + rsi + " EMA Prev" + filteredEma10[1] + " EMA Last" + filteredEma10[0] + " sma 90 Prev" + filteredSma90[1] + " sma 90 Last" + filteredSma90[0]); print ("BUY"); order = engine.submitOrder(getLabel(instrument), instrument, OrderCommand.BUY, 2, 0, 20, tick.getBid() - instrument.getPipValue() * stopLoss, tick.getBid() + instrument.getPipValue() * TakeProfitBuy); } } } } public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { } protected String getLabel(Instrument instrument) { String label = instrument.name(); label = label + (counter++); label = label.toUpperCase(); return label; } public void print(String message) { console.getOut().println(message); } }