Sunteți pe pagina 1din 4

package jforex; import java.util.*; import java.math.*; import java.text.*; import java.awt.*; import java.awt.Color; import java.awt.geom.

AffineTransform; import javax.swing.*; import javax.swing.filechooser.FileSystemView; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.LinkedList; import java.util.Hashtable; import java.util.Iterator; import java.util.Vector; import java.io.*; import com.dukascopy.api.*; import java.lang.*; public class contestClassicsEURJPYMCE6 implements IStrategy { private IEngine engine; private IConsole console; private IHistory history; private IContext context; private IIndicators indicators; private IUserInterface userInterface; private IChart userChart; private IAccount account; private IOrder message_order; public Instrument currentInstrument = Instrument.EURJPY; public Period currentPeriod = Period.DAILY; public OfferSide indOfferside = OfferSide.BID; public double TP =10; public double StopLoss = 400; public double volume = 5; public String order_label=""; public IOrder order; public int protectionFlag=0; public double rsi1=0; public double rsi2=0; public double rsiBase=0; public double rsiD=16; public int todayMood=-1; public int changeMood=0; public int baseTime=84; public int open_day=0; public int close_day=0; public Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")) ; public void onStart(IContext context) throws JFException { Set subscribedInstruments = new HashSet(); subscribedInstruments.add(currentInstrument); context.setSubscribedInstruments(subscribedInstruments ); this.engine = context.getEngine();

this.console = context.getConsole(); this.history = context.getHistory(); this.context = context; this.indicators = context.getIndicators(); this.userInterface = context.getUserInterface(); this.userChart = context.getChart(currentInstrument); rsiBase=indicators.rsi(currentInstrument, Period.DAILY_SUNDAY_IN_MONDAY, indOfferside,IIndicators.AppliedPrice.CLOSE, 14, Filter.WEEKENDS, 4,history.get Bar(currentInstrument, Period.DAILY, indOfferside, 0).getTime(),0)[2]; if(rsiBase>30)todayMood=-1; } public void onAccount(IAccount account) throws JFException { if (this.account == null) { this.account = account; } } public void onMessage(IMessage message) throws JFException { message_order=message.getOrder(); String status=new String(); switch(message.getType()) { case ORDER_CLOSE_OK:// when a position is closed we have to calculat e lots for the next trade if(message_order.getInstrument()!=currentInstrument) break; IOrder lastTwo = message.getOrder(); calendar.setTimeInMillis(lastTwo.getCloseTime()); close_day=calendar.get(Calendar.DAY_OF_WEEK)-1; calendar.setTimeInMillis(lastTwo.getFillTime()); open_day=calendar.get(Calendar.DAY_OF_WEEK)-1; if(changeMood==0&&((lastTwo.getCloseTime()-lastTwo.getFillTime()> 3600000*baseTime&&open_day<close_day)||(lastTwo.getCloseTime()-lastTwo.getFillTi me()>172800000+(3600000*baseTime)&&open_day>close_day))){ if(lastTwo.isLong()==true){todayMood=-1;}else{todayMood=1;} } if(changeMood!=0){todayMood=changeMood;changeMood=0;} break; } } public void onTick(Instrument instrument, ITick tick) throws JFException { if(protectionFlag==1)return; protectionFlag=1; if(instrument!=currentInstrument){protectionFlag=0;return;} if(todayMood==1){ if(positionsTotal(currentInstrument)==0){ openBuyOrder("BUY"); wait5sec(); } } if(todayMood==-1){ if(positionsTotal(currentInstrument)==0){ openSellOrder("SELL"); wait5sec(); }

} protectionFlag=0; } protected void openBuyOrder(String SignalName) throws JFException { double bar_CLOSE=history.getLastTick(currentInstrument).getAsk(); double tempSL=0; double tempTP=0; double _lots=0; tempTP=bar_CLOSE + currentInstrument.getPipValue() * TP; tempSL=bar_CLOSE - currentInstrument.getPipValue() * StopLoss; order_label=SignalName+history.getTimeOfLastTick(currentInstrument); _lots=volume; try{ order = engine.submitOrder(order_label, currentInstrument, IEngine.OrderCommand.BUY, _lots, bar_CLOSE, 10, tempSL, tempTP,0,"B"); } catch (JFException e) { console.getOut().println("opaaaa"); } } protected void openSellOrder(String SignalName) throws JFException { double bar_CLOSE=history.getLastTick(currentInstrument).getBid(); double tempSL=0; double tempTP=0; double _lots=0; tempTP=bar_CLOSE - currentInstrument.getPipValue() * TP; tempSL=bar_CLOSE + currentInstrument.getPipValue() * StopLoss; order_label=SignalName+history.getTimeOfLastTick(currentInstrument); _lots=volume; try{order = engine.submitOrder(order_label, currentInstrument, IEngine.OrderCommand.SELL, _lots, bar_CLOSE, 10,tempSL, tempTP,0,"S"); } catch (JFException e) { } } protected int positionsTotal(Instrument instrument) throws JFException { int counter = 0; for (IOrder order : engine.getOrders(instrument)) { if (order.getState() == IOrder.State.FILLED) { counter++; } } return counter; } public void onStop() throws JFException { } public void onBar(Instrument instrument, Period period, IBar askBar, IBar bi dBar) throws JFException { if(instrument==currentInstrument && period==Period.FIVE_MINS){ //cach ing some data rsi1=indicators.rsi(currentInstrument, Period.DAILY_SUNDAY_IN_MONDAY, in dOfferside,IIndicators.AppliedPrice.CLOSE, 14, Filter.WEEKENDS, 4,history.getBar (currentInstrument, Period.DAILY, indOfferside, 0).getTime(),0)[2]; rsi2=indicators.rsi(currentInstrument, Period.DAILY_SUNDAY_IN_MONDAY, in dOfferside,IIndicators.AppliedPrice.CLOSE, 14, Filter.WEEKENDS, 4,history.getBar (currentInstrument, Period.DAILY, indOfferside, 0).getTime(),0)[1]; if((rsi1>rsiBase+rsiD&&rsi2<rsiBase+rsiD))changeMood=-1; if((rsi1<rsiBase-rsiD&&rsi2>rsiBase-rsiD))changeMood=1; } } public void wait5sec(){//just waits 5 seconds - when we place an order try { Thread.currentThread().sleep(5000); //wait 5 secs

} catch (InterruptedException e) { } } }

S-ar putea să vă placă și