package com.dukascopy.visualforex.vadim_berezhnoj; import java.util.*; import com.dukascopy.api.*; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.concurrent.CopyOnWriteArrayList; import java.lang.reflect.*; import java.math.BigDecimal; /* * Created by VisualJForex Generator, version 1.65 * Date: 27.01.2015 10:09 */ public class A1234 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("_Step:") public int _Step = 15; @Configurable("defaultPeriod:") public Period defaultPeriod = Period.FIFTEEN_MINS; @Configurable("defaultTradeAmount:") public double defaultTradeAmount = 0.001; @Configurable("defaultStopLoss:") public int defaultStopLoss = 25; @Configurable("defaultInstrument:") public Instrument defaultInstrument = Instrument.EURUSD; private String AccountCurrency = ""; private double _NextLvl; private double Leverage; private double _Dn; private Tick LastTick = null ; private boolean _NoPos = true; private boolean _Direction = true; private double _Shift; private IOrder _Pos = null ; private String AccountId = ""; private double Equity; private double UseofLeverage; private double _TP; private List PendingPositions = null ; private double _Up; private double _SumVol; private List AllPositions = null ; private IOrder _Sell = null ; private int OverWeekendEndLeverage; private int MarginCutLevel; private Candle LastAskCandle = null ; private double _CurrLvl; private boolean _Init = true; private boolean GlobalAccount; private List OpenPositions = null ; private IMessage LastTradeEvent = null ; private IOrder _Buy = null ; 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, defaultPeriod, OfferSide.BID, 1); IBar askBar = context.getHistory().getBar(defaultInstrument, defaultPeriod, OfferSide.ASK, 1); LastAskCandle = new Candle(askBar, defaultPeriod, defaultInstrument, OfferSide.ASK); LastBidCandle = new Candle(bidBar, defaultPeriod, defaultInstrument, OfferSide.BID); 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(Instrument instrument) { try { AllPositions = engine.getOrders(instrument); 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) { updateVariables(message.getOrder().getInstrument()); 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); } } If_block_35(2); } } public void onStop() throws JFException { } public void onTick(Instrument instrument, ITick tick) throws JFException { LastTick = new Tick(tick, instrument); updateVariables(instrument); } public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { LastAskCandle = new Candle(askBar, period, instrument, OfferSide.ASK); LastBidCandle = new Candle(bidBar, period, instrument, OfferSide.BID); updateVariables(instrument); If_block_23(1); } 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(); } } public double round(double price, Instrument instrument) { BigDecimal big = new BigDecimal("" + price); big = big.setScale(instrument.getPipScale() + 1, BigDecimal.ROUND_HALF_UP); return big.doubleValue(); } public ITick getLastTick(Instrument instrument) { try { return (context.getHistory().getTick(instrument, 0)); } catch (JFException e) { e.printStackTrace(); } return null; } private void If_block_10(Integer flow) { Period argument_1 = LastAskCandle.getPeriod(); Period argument_2 = defaultPeriod; if (!argument_1.equals(argument_2)) { } else if (argument_1.equals(argument_2)) { If_block_18(flow); } } private void SMA_block_12(Integer flow) { Instrument argument_1 = defaultInstrument; Period argument_2 = defaultPeriod; int argument_3 = 1; int argument_4 = 20; 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] = 20; try { subscriptionInstrumentCheck(argument_1); long time = context.getHistory().getBar(argument_1, argument_2, OfferSide.BID, argument_3).getTime(); Object[] indicatorResult = context.getIndicators().calculateIndicator(argument_1, argument_2, offerside, "SMA", appliedPrice, params, Filter.WEEKENDS, 1, time, 0); if ((new Double(((double [])indicatorResult[0])[0])) == null) { this._CurrLvl = Double.NaN; } else { this._CurrLvl = (((double [])indicatorResult[0])[0]); } } catch (JFException e) { e.printStackTrace(); } Calculation_block_16(flow); } private void Calculation_block_15(Integer flow) { double argument_1 = _CurrLvl; double argument_2 = _Shift; _Dn = argument_1 - argument_2; If_block_19(flow); } private void Calculation_block_16(Integer flow) { double argument_1 = _CurrLvl; double argument_2 = _Shift; _Up = argument_1 + argument_2; Calculation_block_15(flow); } private void If_block_18(Integer flow) { Object argument_1 = _NoPos; Object argument_2 = true; if (argument_1!= argument_2) { } else if (argument_1== argument_2) { SMA_block_12(flow); } } private void If_block_19(Integer flow) { double argument_1 = LastTick.getBid(); double argument_2 = _Up; if (argument_1< argument_2) { If_block_20(flow); } else if (argument_1> argument_2) { } else if (argument_1== argument_2) { } } private void If_block_20(Integer flow) { double argument_1 = LastTick.getAsk(); double argument_2 = _Dn; if (argument_1< argument_2) { } else if (argument_1> argument_2) { PendingOpen_block_21(flow); } else if (argument_1== argument_2) { } } private void PendingOpen_block_21(Integer flow) { double argument_1 = _Up; Instrument argument_2 = defaultInstrument; double argument_3 = defaultTradeAmount; int argument_4 = 0; double argument_5 = 0; double argument_6 = 0; String argument_7 = ""; long argument_8 = 0; IEngine.OrderCommand command = IEngine.OrderCommand.SELLLIMIT; double stopLoss = round(argument_5, argument_2); double takeProfit = round(argument_6, argument_2); try { String label = getLabel(); _Sell = context.getEngine().submitOrder(label, argument_2, command, argument_3, argument_1, (double)argument_4, stopLoss, takeProfit, argument_8, argument_7); TradeEventAction event = new TradeEventAction(); event.setMessageType(IMessage.Type.ORDER_SUBMIT_OK); event.setNextBlockId("PendingOpen_block_22"); event.setPositionLabel(_Sell.getLabel()); event.setFlowId(flow); tradeEventActions.add(event); } catch (JFException e) { e.printStackTrace(); } } private void PendingOpen_block_22(Integer flow) { double argument_1 = _Dn; Instrument argument_2 = defaultInstrument; double argument_3 = defaultTradeAmount; int argument_4 = 0; double argument_5 = 0; double argument_6 = 0; String argument_7 = ""; long argument_8 = 0; IEngine.OrderCommand command = IEngine.OrderCommand.BUYLIMIT; double stopLoss = round(argument_5, argument_2); double takeProfit = round(argument_6, argument_2); try { String label = getLabel(); _Buy = context.getEngine().submitOrder(label, argument_2, command, argument_3, argument_1, (double)argument_4, stopLoss, takeProfit, argument_8, argument_7); TradeEventAction event = new TradeEventAction(); event.setMessageType(IMessage.Type.ORDER_SUBMIT_OK); event.setNextBlockId("Assign_block_26"); event.setPositionLabel(_Buy.getLabel()); event.setFlowId(flow); tradeEventActions.add(event); } catch (JFException e) { e.printStackTrace(); } } private void If_block_23(Integer flow) { Object argument_1 = _Init; Object argument_2 = true; if (argument_1!= argument_2) { If_block_10(flow); } else if (argument_1== argument_2) { Calculation_block_24(flow); } } private void Calculation_block_24(Integer flow) { int argument_1 = _Step; double argument_2 = defaultInstrument.getPipValue(); _Shift = argument_1 * argument_2; Assign_block_25(flow); } private void Assign_block_25(Integer flow) { boolean argument_1 = false; _Init = argument_1; If_block_10(flow); } private void Assign_block_26(Integer flow) { boolean argument_1 = false; _NoPos = argument_1; } private void If_block_27(Integer flow) { String argument_1 = LastTradeEvent.getType().name(); String argument_2 = "ORDER_FILL_OK"; if (!argument_1.equals(argument_2)) { } else if (argument_1.equals(argument_2)) { If_block_41(flow); } } private void If_block_35(Integer flow) { String argument_1 = LastTradeEvent.getType().name(); String argument_2 = "ORDER_CLOSE_OK"; if (!argument_1.equals(argument_2)) { If_block_27(flow); } else if (argument_1.equals(argument_2)) { If_block_36(flow); } } private void If_block_36(Integer flow) { int argument_1 = OpenPositions.size(); int argument_2 = 0; if (argument_1< argument_2) { } else if (argument_1> argument_2) { } else if (argument_1== argument_2) { PositionsViewer_block_37(flow); } } private void PositionsViewer_block_37(Integer flow) { List argument_1 = PendingPositions; for (IOrder order : argument_1){ if (order.getState() == IOrder.State.OPENED||order.getState() == IOrder.State.FILLED){ _Pos = order; CloseandCancelPosition_block_38(flow); } } Assign_block_40(flow); } private void CloseandCancelPosition_block_38(Integer flow) { try { if (_Pos.getState() == IOrder.State.OPENED||_Pos.getState() == IOrder.State.FILLED){ _Pos.close(); } } catch (JFException e) { e.printStackTrace(); } } private void Assign_block_40(Integer flow) { boolean argument_1 = true; _NoPos = argument_1; } private void If_block_41(Integer flow) { int argument_1 = OpenPositions.size(); int argument_2 = 1; if (argument_1< argument_2) { } else if (argument_1> argument_2) { If_block_47(flow); } else if (argument_1== argument_2) { PositionsViewer_block_42(flow); } } private void PositionsViewer_block_42(Integer flow) { List argument_1 = PendingPositions; for (IOrder order : argument_1){ if (order.getState() == IOrder.State.OPENED||order.getState() == IOrder.State.FILLED){ _Pos = order; CloseandCancelPosition_block_43(flow); } } If_block_44(flow); } private void CloseandCancelPosition_block_43(Integer flow) { try { if (_Pos.getState() == IOrder.State.OPENED||_Pos.getState() == IOrder.State.FILLED){ _Pos.close(); } } catch (JFException e) { e.printStackTrace(); } } private void If_block_44(Integer flow) { Object argument_1 = LastTradeEvent.getOrder().isLong(); Object argument_2 = true; if (argument_1!= argument_2) { Assign_block_45(flow); } else if (argument_1== argument_2) { Assign_block_46(flow); } } private void Assign_block_45(Integer flow) { boolean argument_1 = true; _Direction = argument_1; If_block_47(flow); } private void Assign_block_46(Integer flow) { boolean argument_1 = false; _Direction = argument_1; If_block_47(flow); } private void If_block_47(Integer flow) { Object argument_1 = _Direction; Object argument_2 = true; if (argument_1!= argument_2) { Calculation_block_52(flow); } else if (argument_1== argument_2) { Calculation_block_68(flow); } } private void Calculation_block_52(Integer flow) { double argument_1 = _CurrLvl; double argument_2 = _Shift; _CurrLvl = argument_1 - argument_2; Calculation_block_53(flow); } private void Calculation_block_53(Integer flow) { double argument_1 = _CurrLvl; double argument_2 = _Shift; _TP = argument_1 + argument_2; Calculation_block_54(flow); } private void Calculation_block_54(Integer flow) { double argument_1 = _CurrLvl; double argument_2 = _Shift; _NextLvl = argument_1 - argument_2; Assign_block_58(flow); } private void PositionsViewer_block_55(Integer flow) { List argument_1 = OpenPositions; for (IOrder order : argument_1){ if (order.getState() == IOrder.State.OPENED||order.getState() == IOrder.State.FILLED){ _Pos = order; Calculation_block_59(flow); } } PendingOpen_block_57(flow); } private void SetTakeProfit_block_56(Integer flow) { IOrder argument_1 = _Pos; double argument_2 = _TP; boolean isLong = argument_1.isLong(); double takeProfit = round(argument_2, argument_1.getInstrument()); try { argument_1.setTakeProfitPrice(takeProfit); } catch (JFException e) { e.printStackTrace(); } } private void PendingOpen_block_57(Integer flow) { double argument_1 = _NextLvl; Instrument argument_2 = defaultInstrument; double argument_3 = _SumVol; int argument_4 = 0; double argument_5 = 0; double argument_6 = 0; String argument_7 = ""; long argument_8 = 0; IEngine.OrderCommand command = IEngine.OrderCommand.BUYLIMIT; double stopLoss = round(argument_5, argument_2); double takeProfit = round(argument_6, argument_2); try { String label = getLabel(); IOrder order = context.getEngine().submitOrder(label, argument_2, command, argument_3, round(argument_1, argument_2), (double)argument_4, stopLoss, takeProfit, argument_8, argument_7); } catch (JFException e) { e.printStackTrace(); } } private void Assign_block_58(Integer flow) { double argument_1 = 0.0; _SumVol = argument_1; PositionsViewer_block_55(flow); } private void Calculation_block_59(Integer flow) { double argument_1 = _SumVol; double argument_2 = _Pos.getAmount(); _SumVol = argument_1 + argument_2; SetTakeProfit_block_56(flow); } private void Calculation_block_68(Integer flow) { double argument_1 = _CurrLvl; double argument_2 = _Shift; _CurrLvl = argument_1 + argument_2; Calculation_block_69(flow); } private void Calculation_block_69(Integer flow) { double argument_1 = _CurrLvl; double argument_2 = _Shift; _TP = argument_1 - argument_2; Calculation_block_70(flow); } private void Calculation_block_70(Integer flow) { double argument_1 = _CurrLvl; double argument_2 = _Shift; _NextLvl = argument_1 + argument_2; Assign_block_74(flow); } private void PositionsViewer_block_71(Integer flow) { List argument_1 = OpenPositions; for (IOrder order : argument_1){ if (order.getState() == IOrder.State.OPENED||order.getState() == IOrder.State.FILLED){ _Pos = order; Calculation_block_75(flow); } } PendingOpen_block_73(flow); } private void SetTakeProfit_block_72(Integer flow) { IOrder argument_1 = _Pos; double argument_2 = _TP; boolean isLong = argument_1.isLong(); double takeProfit = round(argument_2, argument_1.getInstrument()); try { argument_1.setTakeProfitPrice(takeProfit); } catch (JFException e) { e.printStackTrace(); } } private void PendingOpen_block_73(Integer flow) { double argument_1 = _NextLvl; Instrument argument_2 = defaultInstrument; double argument_3 = _SumVol; int argument_4 = 0; double argument_5 = 0; double argument_6 = 0; String argument_7 = ""; long argument_8 = 0; IEngine.OrderCommand command = IEngine.OrderCommand.SELLLIMIT; double stopLoss = round(argument_5, argument_2); double takeProfit = round(argument_6, argument_2); try { String label = getLabel(); IOrder order = context.getEngine().submitOrder(label, argument_2, command, argument_3, round(argument_1, argument_2), (double)argument_4, stopLoss, takeProfit, argument_8, argument_7); } catch (JFException e) { e.printStackTrace(); } } private void Assign_block_74(Integer flow) { double argument_1 = 0.0; _SumVol = argument_1; PositionsViewer_block_71(flow); } private void Calculation_block_75(Integer flow) { double argument_1 = _SumVol; double argument_2 = _Pos.getAmount(); _SumVol = argument_1 + argument_2; SetTakeProfit_block_72(flow); } 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; } } }