package com.dukascopy.visualforex.vf1; import java.util.*; import com.dukascopy.api.*; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.concurrent.CopyOnWriteArrayList; import java.lang.reflect.*; /* * Created by VisualJForex Generator, version 1.12 * Date: 11.12.2012 13:01 */ public class SMASmallExample implements IStrategy { private CopyOnWriteArrayList tradeEventActions = new CopyOnWriteArrayList(); private static final String DATE_FORMAT_NOW = "yyyyMMdd_HHmmss"; private IEngine engine; private IConsole console; private IHistory history; private IContext context; private IIndicators indicators; private IUserInterface userInterface; @Configurable("defaultSlippage:") public int defaultSlippage = 5; @Configurable("defaultTakeProfit:") public int defaultTakeProfit = 50; @Configurable("defaultPeriod:") public Period defaultPeriod = Period.TEN_MINS; @Configurable("defaultTradeAmount:") public double defaultTradeAmount = 0.0010; @Configurable("defaultStopLoss:") public int defaultStopLoss = 25; @Configurable("defaultInstrument:") public Instrument defaultInstrument = Instrument.EURUSD; private String AccountCurrency = ""; private double Leverage; private Tick LastTick = null ; private String AccountId = ""; private double Equity; private double UseofLeverage; private List PendingPositions = null ; private double smaSmallCurrent; private double smaBigCurrent; private List AllPositions = null ; private int OverWeekendEndLeverage; private int MarginCutLevel; private Candle LastAskCandle = null ; private boolean GlobalAccount; private List OpenPositions = null ; private IMessage LastTradeEvent = null ; private double smaSmallPrev; private double smaBigPrev; private Candle LastBidCandle = null ; 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(); subscriptionInstrumentCheck(defaultInstrument); ITick lastITick = context.getHistory().getLastTick(defaultInstrument); LastTick = new Tick(lastITick, defaultInstrument); IBar bidBar = context.getHistory().getBar(defaultInstrument, Period.ONE_MIN, OfferSide.BID, 1); IBar askBar = context.getHistory().getBar(defaultInstrument, Period.ONE_MIN, OfferSide.ASK, 1); LastAskCandle = new Candle(askBar, Period.ONE_MIN, defaultInstrument, OfferSide.ASK); LastBidCandle = new Candle(bidBar, Period.ONE_MIN, defaultInstrument, OfferSide.BID); if (indicators.getIndicator("SMA") == null) { indicators.registerDownloadableIndicator("431","SMA"); } if (indicators.getIndicator("SMA") == null) { indicators.registerDownloadableIndicator("431","SMA"); } if (indicators.getIndicator("SMA") == null) { indicators.registerDownloadableIndicator("431","SMA"); } if (indicators.getIndicator("SMA") == null) { indicators.registerDownloadableIndicator("431","SMA"); } subscriptionInstrumentCheck(Instrument.fromString("EUR/USD")); } public void onAccount(IAccount account) throws JFException { AccountCurrency = account.getCurrency().toString(); Leverage = account.getLeverage(); AccountId= account.getAccountId(); Equity = account.getEquity(); UseofLeverage = account.getUseOfLeverage(); OverWeekendEndLeverage = account.getOverWeekEndLeverage(); MarginCutLevel = account.getMarginCutLevel(); GlobalAccount = account.isGlobal(); } private void updateVariables() { try { AllPositions = engine.getOrders(defaultInstrument); List listMarket = new ArrayList(); for (IOrder order: AllPositions) { if (order.getState().equals(IOrder.State.FILLED)){ listMarket.add(order); } } List listPending = new ArrayList(); for (IOrder order: AllPositions) { if (order.getState().equals(IOrder.State.OPENED)){ listPending.add(order); } } OpenPositions = listMarket; PendingPositions = listPending; } catch(JFException e) { e.printStackTrace(); } } public void onMessage(IMessage message) throws JFException { if (message.getOrder() != null && message.getOrder().getInstrument().equals(defaultInstrument)) { updateVariables(); LastTradeEvent = message; for (TradeEventAction event : tradeEventActions) { IOrder order = message.getOrder(); if (order != null && event != null && message.getType().equals(event.getMessageType())&& order.getLabel().equals(event.getPositionLabel())) { Method method; try { method = this.getClass().getDeclaredMethod(event.getNextBlockId(), Integer.class); method.invoke(this, new Integer[] {event.getFlowId()}); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } tradeEventActions.remove(event); } } } } public void onStop() throws JFException { } public void onTick(Instrument instrument, ITick tick) throws JFException { if (instrument.equals(defaultInstrument)) { LastTick = new Tick(tick, defaultInstrument); updateVariables(); If_block_10(0); } } public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { if (instrument.equals(defaultInstrument)) { LastAskCandle = new Candle(askBar, period, instrument, OfferSide.ASK); LastBidCandle = new Candle(bidBar, period, instrument, OfferSide.BID); updateVariables(); } } public void subscriptionInstrumentCheck(Instrument instrument) { try { if (!context.getSubscribedInstruments().contains(instrument)) { Set instruments = new HashSet(); instruments.add(instrument); context.setSubscribedInstruments(instruments, true); Thread.sleep(100); } } catch (InterruptedException e) { e.printStackTrace(); } } private void If_block_10(Integer flow) { int argument_1 = AllPositions.size(); double argument_2 = 1.0; if (argument_1< argument_2) { SMA_block_11(flow); } else if (argument_1> argument_2) { } else if (argument_1== argument_2) { } } private void SMA_block_11(Integer flow) { Instrument argument_1 = defaultInstrument; Period argument_2 = Period.TEN_MINS; int argument_3 = 0; int argument_4 = 30; OfferSide[] offerside = new OfferSide[1]; IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1]; offerside[0] = OfferSide.BID; appliedPrice[0] = IIndicators.AppliedPrice.CLOSE; Object[] params = new Object[1]; params[0] = 30; try { subscriptionInstrumentCheck(argument_1); Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside, "SMA", appliedPrice, params, argument_3); if (indicatorResult[0] == null) { this.smaSmallCurrent = Double.NaN; } else { this.smaSmallCurrent = (Double) indicatorResult[0]; } } catch (JFException e) { e.printStackTrace(); } SMA_block_13(flow); } private void SMA_block_12(Integer flow) { Instrument argument_1 = defaultInstrument; Period argument_2 = Period.ONE_HOUR; int argument_3 = 0; int argument_4 = 30; OfferSide[] offerside = new OfferSide[1]; IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1]; offerside[0] = OfferSide.BID; appliedPrice[0] = IIndicators.AppliedPrice.CLOSE; Object[] params = new Object[1]; params[0] = 30; try { subscriptionInstrumentCheck(argument_1); Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside, "SMA", appliedPrice, params, argument_3); if (indicatorResult[0] == null) { this.smaBigCurrent = Double.NaN; } else { this.smaBigCurrent = (Double) indicatorResult[0]; } } catch (JFException e) { e.printStackTrace(); } SMA_block_14(flow); } private void SMA_block_13(Integer flow) { Instrument argument_1 = defaultInstrument; Period argument_2 = Period.TEN_MINS; int argument_3 = 1; int argument_4 = 30; OfferSide[] offerside = new OfferSide[1]; IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1]; offerside[0] = OfferSide.BID; appliedPrice[0] = IIndicators.AppliedPrice.CLOSE; Object[] params = new Object[1]; params[0] = 30; try { subscriptionInstrumentCheck(argument_1); Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside, "SMA", appliedPrice, params, argument_3); if (indicatorResult[0] == null) { this.smaSmallPrev = Double.NaN; } else { this.smaSmallPrev = (Double) indicatorResult[0]; } } catch (JFException e) { e.printStackTrace(); } SMA_block_12(flow); } private void SMA_block_14(Integer flow) { Instrument argument_1 = defaultInstrument; Period argument_2 = Period.ONE_HOUR; int argument_3 = 1; int argument_4 = 30; OfferSide[] offerside = new OfferSide[1]; IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1]; offerside[0] = OfferSide.BID; appliedPrice[0] = IIndicators.AppliedPrice.CLOSE; Object[] params = new Object[1]; params[0] = 30; try { subscriptionInstrumentCheck(argument_1); Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside, "SMA", appliedPrice, params, argument_3); if (indicatorResult[0] == null) { this.smaBigPrev = Double.NaN; } else { this.smaBigPrev = (Double) indicatorResult[0]; } } catch (JFException e) { e.printStackTrace(); } If_block_15(flow); } private void If_block_15(Integer flow) { double argument_1 = smaSmallCurrent; double argument_2 = smaBigCurrent; if (argument_1< argument_2) { If_block_16(flow); } else if (argument_1> argument_2) { If_block_17(flow); } else if (argument_1== argument_2) { } } private void If_block_16(Integer flow) { double argument_1 = smaBigCurrent; double argument_2 = smaBigPrev; if (argument_1< argument_2) { } else if (argument_1> argument_2) { OpenatMarket_block_19(flow); } else if (argument_1== argument_2) { } } private void If_block_17(Integer flow) { double argument_1 = smaBigCurrent; double argument_2 = smaBigPrev; if (argument_1< argument_2) { OpenatMarket_block_18(flow); } else if (argument_1> argument_2) { } else if (argument_1== argument_2) { } } private void OpenatMarket_block_18(Integer flow) { Instrument argument_1 = defaultInstrument; double argument_2 = defaultTradeAmount; int argument_3 = defaultSlippage; int argument_4 = defaultStopLoss; int argument_5 = defaultTakeProfit; String argument_6 = ""; IEngine.OrderCommand command = IEngine.OrderCommand.BUY; double stopLoss = LastTick.getBid() - defaultInstrument.getPipValue() * argument_4; double takeProfit = LastTick.getBid() + defaultInstrument.getPipValue() * argument_5; try { String label = getLabel(); IOrder order = context.getEngine().submitOrder(label, argument_1, command, argument_2, 0, argument_3, stopLoss, takeProfit, 0, argument_6); } catch (JFException e) { e.printStackTrace(); } } private void OpenatMarket_block_19(Integer flow) { Instrument argument_1 = defaultInstrument; double argument_2 = defaultTradeAmount; int argument_3 = defaultSlippage; int argument_4 = defaultStopLoss; int argument_5 = defaultTakeProfit; String argument_6 = ""; IEngine.OrderCommand command = IEngine.OrderCommand.SELL; double stopLoss = LastTick.getAsk() + defaultInstrument.getPipValue() * argument_4; double takeProfit = LastTick.getAsk() - defaultInstrument.getPipValue() * argument_5; try { String label = getLabel(); IOrder order = context.getEngine().submitOrder(label, argument_1, command, argument_2, 0, argument_3, stopLoss, takeProfit, 0, argument_6); } catch (JFException e) { e.printStackTrace(); } } class Candle { IBar bar; Period period; Instrument instrument; OfferSide offerSide; public Candle(IBar bar, Period period, Instrument instrument, OfferSide offerSide) { this.bar = bar; this.period = period; this.instrument = instrument; this.offerSide = offerSide; } public Period getPeriod() { return period; } public void setPeriod(Period period) { this.period = period; } public Instrument getInstrument() { return instrument; } public void setInstrument(Instrument instrument) { this.instrument = instrument; } public OfferSide getOfferSide() { return offerSide; } public void setOfferSide(OfferSide offerSide) { this.offerSide = offerSide; } public IBar getBar() { return bar; } public void setBar(IBar bar) { this.bar = bar; } public long getTime() { return bar.getTime(); } public double getOpen() { return bar.getOpen(); } public double getClose() { return bar.getClose(); } public double getLow() { return bar.getLow(); } public double getHigh() { return bar.getHigh(); } public double getVolume() { return bar.getVolume(); } } class Tick { private ITick tick; private Instrument instrument; public Tick(ITick tick, Instrument instrument){ this.instrument = instrument; this.tick = tick; } public Instrument getInstrument(){ return instrument; } public double getAsk(){ return tick.getAsk(); } public double getBid(){ return tick.getBid(); } public double getAskVolume(){ return tick.getAskVolume(); } public double getBidVolume(){ return tick.getBidVolume(); } public long getTime(){ return tick.getTime(); } public ITick getTick(){ return tick; } } public class AssertException extends RuntimeException { public AssertException(Object primary, Object compared) { super("Primary object : " + primary.toString() + " is different from " + compared.toString()); } } protected String getLabel() { String label; label = "IVF" + getCurrentTime(LastTick.getTime()) + generateRandom(10000) + generateRandom(10000); return label; } private String getCurrentTime(long time) { SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); return sdf.format(time); } private static String generateRandom(int n) { int randomNumber = (int) (Math.random() * n); String answer = "" + randomNumber; if (answer.length() > 3) { answer = answer.substring(0, 4); } return answer; } class TradeEventAction { private IMessage.Type messageType; private String nextBlockId = ""; private String positionLabel = ""; private int flowId = 0; public IMessage.Type getMessageType() { return messageType; } public void setMessageType(IMessage.Type messageType) { this.messageType = messageType; } public String getNextBlockId() { return nextBlockId; } public void setNextBlockId(String nextBlockId) { this.nextBlockId = nextBlockId; } public String getPositionLabel() { return positionLabel; } public void setPositionLabel(String positionLabel) { this.positionLabel = positionLabel; } public int getFlowId() { return flowId; } public void setFlowId(int flowId) { this.flowId = flowId; } } }