sorry someone can help me? remote server did not open positions, but in the test strategy is fine, this is the code
package jforex.strategies.indicators;
import com.dukascopy.api.*; import com.dukascopy.api.Configurable; import com.dukascopy.api.IAccount; import com.dukascopy.api.IBar; import com.dukascopy.api.IConsole; import com.dukascopy.api.IContext; import com.dukascopy.api.IEngine.OrderCommand; import com.dukascopy.api.IEngine; import com.dukascopy.api.IHistory; import com.dukascopy.api.IIndicators.AppliedPrice; import com.dukascopy.api.IIndicators; import com.dukascopy.api.IMessage; import com.dukascopy.api.IOrder.State; import com.dukascopy.api.IOrder; import com.dukascopy.api.IStrategy; import com.dukascopy.api.ITick; import com.dukascopy.api.Instrument; import com.dukascopy.api.JFException; import com.dukascopy.api.OfferSide; import com.dukascopy.api.Period; import java.awt.*; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.*;
public class COIN_FLIP implements IStrategy { private String StrategyID = "COIN_FLIP"; public Instrument instrument = Instrument.EURJPY; public Period selectedPeriod = Period.FIFTEEN_MINS; public boolean Use_CH = true; public boolean Use_NATR = false; public boolean Use_TVS = true; public boolean Use_BB = true; public boolean Use_MACD = false; public boolean Use_CCIRSI = true; public int takeProfit = 70; public int stopLoss = 40; public double fixed_Amount = 0; public double Equity = 40; public int NATR_Period1 = 21; public int NATR_Period2 = 30; public int NATR_emaPeriod = 50; public AppliedPrice NATR_appliedPrice = AppliedPrice.CLOSE; public boolean Use_ATR = true; public int ATR_Period = 15; public double ATR_base = 0.26; public Filter filter = Filter.NO_FILTER; public int MACD_Mode = 0; // 0=filter, 1=signal public int MACD_trigger = 10; public int fast_Period = 12; public IIndicators.MaType fastMA_Type = IIndicators.MaType.DEMA; public int slow_Period = 26; public IIndicators.MaType slowMA_Type = IIndicators.MaType.DEMA; public int sign_Period = 9; public IIndicators.MaType signMA_Type = IIndicators.MaType.DEMA; public AppliedPrice MACD_appliedPrice = AppliedPrice.CLOSE; public boolean OR_Signals = true; public boolean AND_Signals = false; private double askPrice; private double bidPrice; private static Instrument GlobalInstrument; private IEngine engine; private IConsole console; private IHistory history; private IIndicators indicators; private IContext context; private IAccount account; private int lcounter = 0; private double pp = 0.0001; private double slippage = 0; private int shift = 1; private String Run_ID; private void printMe(String toPrint){console.getOut().println(toPrint);} private IOrder currentOrder = null;
@Override public void onStart(IContext context) throws JFException { engine = context.getEngine(); history = context.getHistory(); console = context.getConsole(); account = context.getAccount(); indicators = context.getIndicators();
Set<Instrument> instruments = new HashSet<Instrument>(); instruments.add(instrument); context.setSubscribedInstruments(instruments); Calendar calend = new GregorianCalendar(); Run_ID = String.valueOf(calend.getTimeInMillis()).substring(8); }
@Override public void onAccount(IAccount account) throws JFException { this.account = account; }
@Override public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
if (this.instrument != instrument) return;
OfferSide offerSide = OfferSide.BID; AppliedPrice appliedPrice = AppliedPrice.CLOSE; Object[] ichimoku1 = indicators.calculateIndicator(instrument, period, new OfferSide[]{ OfferSide.BID }, "ICHIMOKU", new IIndicators.AppliedPrice[]{ IIndicators.AppliedPrice.CLOSE }, new Object[]{ 7,21,49 }, 1); Object[] ichimoku2 = indicators.calculateIndicator(instrument, period, new OfferSide[]{ OfferSide.BID }, "ICHIMOKU", new IIndicators.AppliedPrice[]{ IIndicators.AppliedPrice.CLOSE }, new Object[]{ 7,21,49}, 2);
try { if ((period == selectedPeriod) && isNoOpenPosition()) { boolean CH_Buy = false; boolean CH_Sell = false; if (Use_CH) { double tenkansencurr = ((Double)ichimoku1[0]).doubleValue(); double kijunsencurr = ((Double)ichimoku1[1]).doubleValue(); double tenkansenprev = ((Double)ichimoku2[0]).doubleValue(); double kijunsenprev = ((Double)ichimoku2[1]).doubleValue(); double EMA_1 = indicators.ema(instrument, selectedPeriod, offerSide, NATR_appliedPrice, NATR_emaPeriod, shift);
if (EMA_1 < bidBar.getOpen() && (tenkansenprev > kijunsenprev && tenkansencurr < kijunsencurr)) CH_Buy = true; if (EMA_1 > bidBar.getOpen() && (tenkansenprev < kijunsenprev && tenkansencurr > kijunsencurr)) CH_Sell = true; } boolean NATR_Buy = false; boolean NATR_Sell = false; if (Use_NATR) { double NATR_11 = indicators.natr(instrument, selectedPeriod, offerSide, NATR_Period1, shift); double NATR_12 = indicators.natr(instrument, selectedPeriod, offerSide, NATR_Period1, shift+1); double NATR_21 = indicators.natr(instrument, selectedPeriod, offerSide, NATR_Period2, shift); double NATR_22 = indicators.natr(instrument, selectedPeriod, offerSide, NATR_Period2, shift+1); double EMA_1 = indicators.ema(instrument, selectedPeriod, offerSide, NATR_appliedPrice, NATR_emaPeriod, shift); double EMA_2 = indicators.ema(instrument, selectedPeriod, offerSide, NATR_appliedPrice, NATR_emaPeriod, shift+1);
if ((NATR_11 > NATR_21 && NATR_12 < NATR_22) || (NATR_11 < NATR_21 && NATR_12 > NATR_22)) { if (EMA_1 < bidBar.getOpen() && EMA_1 > EMA_2) NATR_Buy = true; if (EMA_1 > bidBar.getOpen() && EMA_1 < EMA_2) NATR_Sell = true; } } boolean BB_Buy = false; boolean BB_Sell = false; if (Use_BB) { IBar prevBar = history.getBar(instrument, selectedPeriod, offerSide, 1); double oc = bidBar.getOpen() - bidBar.getClose(); double co = bidBar.getClose()- bidBar.getOpen(); double hc = bidBar.getHigh() - bidBar.getClose(); double cl = bidBar.getClose()- bidBar.getLow(); askPrice = askBar.getClose(); bidPrice = bidBar.getClose(); double [] atr1 = indicators.atr(instrument, selectedPeriod, offerSide, ATR_Period, filter, 12, prevBar.getTime(), 1); double sma50 = this.indicators.sma(instrument, Period.ONE_HOUR, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 80, 0); double [] rvi = this.indicators.rvi(instrument, Period.ONE_HOUR, OfferSide.BID, 10, 0); double [] bands = this.indicators.bbands(instrument, period.ONE_HOUR,OfferSide.BID, IIndicators.AppliedPrice.CLOSE,30,0.8,0.8,IIndicators.MaType.DEMA,0); double openPrice = bidBar.getOpen(); if ((co > ATR_base*atr1[5])&& ( bidPrice < bands[0]+0.004 && bidPrice > bands[0] && bidPrice > sma50 &&bidPrice>openPrice && rvi[1]>0)) BB_Buy = true; if ((oc > ATR_base*atr1[5])&&( bidPrice> bands[2]-0.004 && bidPrice< bands[2] && bidPrice < sma50 &&bidPrice<openPrice && rvi[1]<0 )) BB_Sell = true; } boolean TVS_Buy=false; boolean TVS_Sell=false; if (Use_TVS) { double openPrice = bidBar.getOpen(); askPrice = askBar.getClose(); bidPrice = bidBar.getClose(); double ema = this.indicators.ema(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 34, 0); double tvs = this.indicators.tvs(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 24, 0); double tvs1 = this.indicators.tvs(instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 24, 1); if (bidPrice > ema && tvs > tvs1 && tvs > 0 && tvs1 < 0) TVS_Buy=true; if (bidPrice < ema && tvs < tvs1 && tvs < 0 && tvs1 > 0) TVS_Sell=true; } boolean CCIRSI_Buy=false; boolean CCIRSI_Sell=false; if (Use_CCIRSI) { double cci = this.indicators.cci(instrument, selectedPeriod, OfferSide.BID, 12, 0); double rsi = indicators.rsi(GlobalInstrument, selectedPeriod, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 14, 0); double ema1 = this.indicators.ema(instrument, selectedPeriod, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 32, 0); double openPrice = bidBar.getOpen(); askPrice = askBar.getClose(); bidPrice = bidBar.getClose(); if ((bidPrice > ema1)&& (rsi < 50) && (cci < -100)) CCIRSI_Buy=true; if ((bidPrice < ema1)&& (rsi > 50) && (cci > 100)) CCIRSI_Sell=true; } boolean MACD_Buy = false; boolean MACD_Sell = false; int LINE = 0; int SIGN = 1; int HIST = 2; if (Use_MACD) { double[] macd1 = indicators.macdExt(instrument, selectedPeriod, offerSide, MACD_appliedPrice, fast_Period, fastMA_Type, slow_Period, slowMA_Type, sign_Period, signMA_Type, shift); double[] macd2 = indicators.macdExt(instrument, selectedPeriod, offerSide, MACD_appliedPrice, fast_Period, fastMA_Type, slow_Period, slowMA_Type, sign_Period, signMA_Type, shift+1); switch (MACD_Mode) { case 0: if (macd2[LINE] < macd1[LINE] && macd2[SIGN] < macd1[SIGN]) MACD_Buy = true; if (macd2[LINE] > macd1[LINE] && macd2[SIGN] > macd1[SIGN]) MACD_Sell = true; break; case 1: if (macd1[HIST] > 0 && macd2[HIST] <= 0 && macd1[LINE] < (-MACD_trigger * pp)) MACD_Buy = true; if (macd1[HIST] < 0 && macd2[HIST] >= 0 && macd1[LINE] > (MACD_trigger * pp)) MACD_Sell = true; break; } } if (OR_Signals) { if (CH_Buy || NATR_Buy || BB_Buy || CCIRSI_Buy || MACD_Buy || TVS_Buy ) goLong(); if (CH_Sell ||NATR_Sell || BB_Sell || CCIRSI_Sell || MACD_Sell || TVS_Sell) goShort(); } if (AND_Signals) { if ((CH_Buy || !Use_CH)&&(NATR_Buy || !Use_NATR)&&(BB_Buy || !Use_BB)&&(CCIRSI_Buy || !Use_CCIRSI)&&(MACD_Buy || !Use_MACD)&&(TVS_Buy || !Use_TVS) ) goLong(); if ((CH_Sell || !Use_CH)&&(NATR_Sell || !Use_NATR)&&(BB_Sell || !Use_BB)&&(TVS_Sell || !Use_TVS)&&(CCIRSI_Sell || !Use_CCIRSI)&&(MACD_Sell || !Use_MACD)) goShort(); } } } catch (Exception e) { console.getOut().println("Exception"); console.getOut().println(e.getMessage()); } }
public void onTick(Instrument instrument, ITick tick) throws JFException {
}
public void onMessage(IMessage message) throws JFException { // console.getOut().println(message); IOrder order = message.getOrder(); switch (message.getType()) { case ORDER_CLOSE_OK: if (order != null) { double profitsLoss = order.getProfitLossInPips(); printMe(order.getLabel()+ " : "+ order.getOrderCommand()+ " : "+profitsLoss); } } }
public void onStop() { }
private boolean isNoOpenPosition() throws JFException { State state; for (IOrder order : engine.getOrders(instrument)) { state = order.getState(); if (state == State.FILLED || state == State.OPENED || state == State.CREATED) { return false; } } return true; }
private boolean closeAll() throws JFException { for (IOrder order : engine.getOrders()) { engine.getOrder(order.getLabel()).close(); } return true; } private double getAmount() { double Amount = 0.01; if (fixed_Amount > 0) Amount = fixed_Amount; else if (Equity > 0) Amount = (account.getEquity() / 1000000.0) * Equity; if (Amount < 0.01) Amount = 0.01; if (Amount > 20) Amount = (account.getEquity() / 20000000.0) * Equity; Amount = Math.round(Amount * 1000.0) / 1000.0; return Amount; }
public IOrder goShort() throws JFException { closeAll(); return goShort(this.takeProfit, this.stopLoss); }
public IOrder goLong() throws JFException { closeAll(); return goLong(this.takeProfit, this.stopLoss); }
public IOrder goShort(int takeProfit, int stopLoss) throws JFException { double amount = getAmount(); if (amount < 0) return null;
double price = history.getLastTick(instrument).getBid();
double stopLossPrice = price + (instrument.getPipValue() * stopLoss);
double takeProfitPrice = price - (instrument.getPipValue() * takeProfit);
return engine.submitOrder(getLabel(), instrument, OrderCommand.SELL, amount, price, slippage, stopLossPrice, takeProfitPrice); }
public IOrder goLong(int takeProfit, int stopLoss) throws JFException { double amount = getAmount(); if (amount < 0) return null;
double price = history.getLastTick(instrument).getAsk();
double stopLossPrice = price - (instrument.getPipValue() * stopLoss); double takeProfitPrice = price + (instrument.getPipValue() * takeProfit);
return engine.submitOrder(getLabel(), instrument, OrderCommand.BUY, amount, price, slippage, stopLossPrice, takeProfitPrice); }
private String getLabel() { Run_ID = new SimpleDateFormat("yyMMddHHmmssSSS").format(new Date()); String label = StrategyID; label = label + "_" + Run_ID; label = label + "_" + instrument.name(); label = label + "_" + lcounter++; return label; }
}
|