package jforex; import java.util.*; import com.dukascopy.api.*; public class A_0 implements IStrategy { private IEngine engine; private IConsole console; private IHistory history; private IContext context; private IIndicators indicators; private IUserInterface userInterface; public GregorianCalendar currentDate = new GregorianCalendar(); public Instrument instrument = Instrument.EURUSD; public Period period = Period.ONE_HOUR; public double AccEquity; public int numberOfBars=24; public double[][] data = new double[4][numberOfBars]; public int t; public int t_1; public int start_hour=16; public int end_hour=8; public double StopLoss=70; public double TakeProfit =200; public double SLp; public double TPp; public int Leverage=28; public int p=10000; public int profit; public double Open_0, Open_1, Open_2, Open_3, Open_11, Open_22; public double Close_1; public double High_1; public double Low_1; public double Highest_6, Highest_8, Highest_14, Highest_17, Highest_23; public double Lowest_6, Lowest_8, Lowest_14, Lowest_17, Lowest_23; //+------------------------------------------------------------------+ 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(); currentDate.setTimeZone (TimeZone.getTimeZone("GMT+1")); Set subscribedInstruments = new HashSet(); subscribedInstruments.add(Instrument.EURUSD); context.setSubscribedInstruments(subscribedInstruments); } //+------------------------------------------------------------------+ public void onAccount(IAccount account) throws JFException { AccEquity = account.getEquity(); } //+------------------------------------------------------------------+ public void onMessage(IMessage message) throws JFException { } public void onStop() throws JFException { } //+------------------------------------------------------------------+ public void onTick(Instrument instrument, ITick tick) throws JFException { } //+------------------------------------------------------------------+ public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { if(instrument == this.instrument && period == this.period) { currentDate.setTimeInMillis(bidBar.getTime()); int hour = currentDate.get (Calendar.HOUR_OF_DAY); int trades=tradesTotal(instrument); t=0; t_1=0; data = null; data=GetBars(numberOfBars); if (data== null)return; Open_0=data[0][0];Close_1=data[3][1];Open_1=data[0][1]; Open_2=data[0][2]; Open_3=data[0][3]; Open_11=data[0][11]; Open_22=data[0][22];High_1=data[1][1];Low_1=data[2][1]; Highest_6=Highest(6); Highest_8=Highest(8); Highest_14=Highest(14); Highest_17=Highest(17); Highest_23=Highest(23);Lowest_6=Lowest(6); Lowest_8=Lowest(8);Lowest_14=Lowest(14); Lowest_17=Lowest(17); Lowest_23=Lowest(23); if((Open_0-Open_22)*p>65&&(Open_0-Open_22)*p<100)StopLoss=32; if((Open_22-Open_0)*p>65&&(Open_22-Open_0)*p<100)StopLoss=32; if((Open_0-Open_11)*p>50&&(Open_0-Open_11)*p<85)StopLoss=30; if((Open_11-Open_0)*p>50&&(Open_11-Open_0)*p<85)StopLoss=30; if((Open_0-Open_1)*p>10&&(Open_0-Open_2)*p>7&&(hour>start_hour || hourstart_hour || hour8&&(Open_0-Open_2)*p>5&&(Open_0-Open_3)*p>14&&(hour>start_hour || hourstart_hour || hour4&&(Open_0-Open_2)*p>5&&(Open_0-Lowest_23)*p>54&&(Highest_23-Open_0)*p>54&&(hour>start_hour || hour54&&(Highest_23-Open_0)*p>54&&(hour>start_hour || hour11&&(Open_0-Open_2)*p>3&&(Open_0-Lowest_6)*p>28&&(Highest_6-Open_0)*p>28&&(hour>start_hour || hour28&&(Highest_6-Open_0)*p>28&&(hour>start_hour || hour9&&(Open_0-Open_2)*p>9&&(Open_0-Lowest_14)*p>26&&(Highest_14-Open_0)*p>26&&(hour>start_hour || hour26&&(Highest_14-Open_0)*p>26&&(hour>start_hour || hour23&&(Open_1-Open_2)*p>3&&High_1-Close_1<(High_1-Low_1)/5&&(hour>end_hour+1&&hour23&&(Open_2-Open_1)*p>3&&Close_1-Low_1<(High_1-Low_1)/5&&(hour>end_hour+1&&hour27&&(Open_0-Open_2)*p>15&&High_1-Close_1<(High_1-Low_1)/5&&(Open_0-Lowest_8)*p<58&&(Highest_8-Open_0)*p<58&&(hour>end_hour+1&&hourend_hour+1&&hour16&&(Open_1-Open_2)*p>3&&High_1-Close_1<(High_1-Low_1)/5&&(Open_0-Lowest_17)*p<56&&(Highest_17-Open_0)*p<56&&(hour>end_hour+1&&hourend_hour+1&&hour0) { int orderType=tradeType(instrument); double pips=tradePips(instrument); if(orderType==1) { if(Open_010) { for (IOrder order : engine.getOrders(instrument)) { if (order.getState() == IOrder.State.FILLED) { order.close();return;}} } if(t==-1||(t_1==-1&&pips>-21)) { for (IOrder order : engine.getOrders(instrument)) { if (order.getState() == IOrder.State.FILLED) { order.close(); while (order.getState() != IOrder.State.CLOSED) { order.waitForUpdate(10000);} if (order.getState() == IOrder.State.CLOSED) { Sell(instrument, engine, StopLoss, TakeProfit, Lots()); return; } } } } } if(orderType==-1) { if(Open_0>Open_1&&pips>10) { for (IOrder order : engine.getOrders(instrument)) { if (order.getState() == IOrder.State.FILLED) { order.close();return;}} } if(t==1||(t_1==1&&pips>-21)) { for (IOrder order : engine.getOrders(instrument)) { if (order.getState() == IOrder.State.FILLED) { order.close(); while (order.getState() != IOrder.State.CLOSED) { order.waitForUpdate(10000);} if (order.getState() == IOrder.State.CLOSED) { Buy(instrument, engine, StopLoss, TakeProfit, Lots()); return; } } } } } } } } //+------------------------------------------------------------------+ // opened trades public int tradesTotal(Instrument instrument) throws JFException { int counter = 0; for (IOrder order : engine.getOrders(instrument)) { if (order.getState() == IOrder.State.FILLED) { counter++; } } return counter; } //+------------------------------------------------------------------+ // trade type public int tradeType(Instrument instrument) throws JFException { int type = 0; for (IOrder order : engine.getOrders(instrument)) { if (order.getState() == IOrder.State.FILLED) { type=-1; if (order.isLong())type = 1; } } return type; } //+------------------------------------------------------------------+ // trade pips P/L public double tradePips(Instrument instrument) throws JFException { double pips = 0; for (IOrder order : engine.getOrders(instrument)) { if (order.getState() == IOrder.State.FILLED) { pips=order.getProfitLossInPips(); } } return pips; } //+------------------------------------------------------------------+ public double [][] GetBars(int ArraySize) { double [][] ohlc = new double[4][ArraySize]; int Bars= ArraySize; int cnt=0; for (int i = 0; i < Bars; i++){ IBar bar = null; try {bar = history.getBar(instrument, period, OfferSide.BID, i);} catch (JFException e) {return null;} if (bar != null) { if(bar.getVolume()==0&&cnt!=0){Bars++;continue;} ohlc[0][cnt] = bar.getOpen(); ohlc[1][cnt] = bar.getHigh(); ohlc[2][cnt] = bar.getLow(); ohlc[3][cnt] = bar.getClose(); cnt++; } } return ohlc; } //+------------------------------------------------------------------+ public double Highest(int shift){ double highest=0; for(int i=0;ihighest)highest= data[1][i]; } return highest; } //+------------------------------------------------------------------+ public double Lowest(int shift){ double lowest=Double.MAX_VALUE; for(int i=0;iMath.abs(c1-o1)*f)return(-1); } return(1); } //+------------------------------------------------------------------+ protected void Buy(Instrument instrument, IEngine engine, double SLp, double TPp, double Lots) throws JFException { SLp = history.getLastTick(instrument).getBid() - StopLoss * instrument.getPipValue(); TPp = history.getLastTick(instrument).getBid() + TakeProfit * instrument.getPipValue(); engine.submitOrder(getLabel(instrument) , instrument, IEngine.OrderCommand.BUY, Lots, 0, 20,SLp,TPp); } //+------------------------------------------------------------------+ protected void Sell(Instrument instrument, IEngine engine, double SLp, double TPp, double Lots) throws JFException { SLp = history.getLastTick(instrument).getAsk() + StopLoss * instrument.getPipValue(); TPp = history.getLastTick(instrument).getAsk() - TakeProfit * instrument.getPipValue(); engine.submitOrder(getLabel(instrument) , instrument, IEngine.OrderCommand.SELL, Lots, 0, 20, SLp ,TPp); } //+------------------------------------------------------------------+ protected double Lots() { double Lot; Lot=AccEquity*Leverage/1000000; Lot = Math.floor(Lot*100)/100; return Lot; } //+------------------------------------------------------------------+ protected String getLabel(Instrument instrument) { String label = instrument.name(); long time = new java.util.Date().getTime(); label = label + time; return label; } //+------------------------------------------------------------------+ }