hello support,
in this post
viewtopic.php?f=65&t=47007, i reported this problem :
12:53:29 Strategy tester: com.dukascopy.api.JFException: Could not load bar for instrument [EUR/USD], period [15 Mins], side [Ask], start time [2011.04.04 09:00:00 000], current bar start time [2011.04.04 09:15:00 000] @ jforex.strategies.indicators.reversalbar1.onBar(reversalbar1.java:98).
your reply is :
There might be some problem with historical data in your local cache, consider cleaning it - it should reload from the server on the next call.
i cleaned my cache but nothing change. if a test a another strategy all works fine.
my code (it isn't finished)
package jforex.strategies.indicators;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import com.dukascopy.api.ITick;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.indicators.IIndicator;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class reversalbar2long implements IStrategy {
private IEngine engine;
private IConsole console;
private IHistory history;
private int counter = 0;
private IOrder order;
private IIndicators indicators;
// indicators
@Configurable("MA period")
public int maPeriod = 21;
@Configurable("Bar slow period")
public int barsideS = 20;
@Configurable("Bar Fast period")
public int barsideF = 10;
//price
@Configurable("Take profit pips")
public int takeProfitPips = 150;
@Configurable("stop loss pips")
public int stopLossPips = 50;
@Configurable("Pips above p")
public int pipsAbove = 0;
@Configurable("Pips below p")
public int pipsBelow = 0;
@Configurable("Order expire candle count")
public int orderExpireCandles = 2;
//instrument
@Configurable("Instrument")
public Instrument instrument = Instrument.EURUSD;
@Configurable("Period")
public Period selectedPeriod = Period.FIFTEEN_MINS;
@Configurable("Slippage")
public double slippage = 1;
@Configurable("Amount")
public double amount = 0.005;
//trailing
@Configurable("Trailing trigger pips")
public int trailingPips = 25;
//time to trade
@Configurable("Open hour")
public int openHour = 6;
@Configurable("Open min")
public int openMin = 30;
@Configurable("Close hour")
public int closeHour = 20;
@Configurable("Close min")
public int closeMin = 30;
@Override
public void onStart(IContext context) throws JFException {
this.console = context.getConsole();
this.history = context.getHistory();
this.engine = context.getEngine();
this.indicators = context.getIndicators();
}
@Override
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
if (period != this.selectedPeriod || instrument != this.instrument) {
return;
}
if (!isActive(order)) {
order = null;
}
if(order != null && order.getState().equals(IOrder.State.OPENED)) {
printTime(order.getCreationTime());
printTime(askBar.getTime());
printTime(order.getCreationTime() + (period.getInterval() * orderExpireCandles));
if(order.getCreationTime() + (period.getInterval() * orderExpireCandles) <= askBar.getTime()) {
order.close();
order = null;
}
}
boolean sellSign = false;
boolean buySign = false;
double buyPrice = 0.0, sellPrice = 0.0;
// SIGNALS
IBar currBar = history.getBar(instrument, period, OfferSide.ASK,1);
IBar prevBar = history.getBar(instrument, period, OfferSide.ASK,2);
IBar prevBar1 = history.getBar(instrument, period, OfferSide.ASK,3);
IBar prevBar2 = history.getBar(instrument, period, OfferSide.ASK,4);
IBar prevBar3 = history.getBar(instrument, period, OfferSide.ASK,5);
long lastTickTime = history.getLastTick(instrument).getTime();
long lastBarTime = history.getBarStart(period, lastTickTime);
List<IBar> bars = history.getBars(instrument, period, OfferSide.ASK, Filter.NO_FILTER, barsideF, lastBarTime, 0);
List<IBar> bars20 = history.getBars(instrument, period, OfferSide.ASK, Filter.NO_FILTER, barsideS, lastBarTime, 0);
double maxHigh10 = 0;
double maxHigh20 = 0;
double minLow10 = 0;
double minLow20 = 0;
for(IBar bar : bars){
if(maxHigh10 < bar.getHigh()){
maxHigh10 = bar.getHigh();
}
if(minLow10 < bar.getLow()){
minLow10 = bar.getLow();
}
}
for(IBar bar : bars20){
if(maxHigh20 < bar.getHigh()){
maxHigh20 = bar.getHigh();
}
if(minLow20 < bar.getLow()){
minLow20 = bar.getLow();
}
}
double ma1 = indicators.ma(instrument,period,OfferSide.ASK,IIndicators.AppliedPrice.CLOSE,maPeriod,IIndicators.MaType.SMA,0);
double ma11 = indicators.ma(instrument,period,OfferSide.ASK,IIndicators.AppliedPrice.CLOSE,maPeriod,IIndicators.MaType.SMA,1);
//for sell
if (prevBar1.getOpen() < prevBar1.getClose()
&& prevBar.getClose() > prevBar1.getOpen()
&& currBar.getClose() < prevBar1.getLow()
&& ma1 > ma11
&& maxHigh20 > maxHigh10
/*&& minLow20 > minLow10*/){
sellSign = true;
sellPrice = currBar.getLow() - getPipPrice(pipsBelow);
}
//for buy
if(prevBar1.getOpen() > prevBar1.getClose()
&& prevBar.getClose() < prevBar1.getOpen()
&& currBar.getClose() > prevBar1.getHigh()
&& ma1 < ma11
&& minLow20 > minLow10
/*&& maxHigh20 > maxHigh10*/){
buySign = true;
buyPrice = currBar.getHigh() + getPipPrice(pipsAbove);
}
// PLACE ORDER
if (buySign) {
if (order == null || !order.isLong()){
closeOrder(order);
if (isRightTime(askBar.getTime(), openHour, openMin, closeHour, closeMin)){
order = submitOrder (OrderCommand.BUYSTOP, currBar.getHigh(), buyPrice);
}
}
}
if (sellSign) {
if(order == null || order.isLong()){
closeOrder(order);
if (isRightTime(bidBar.getTime(), openHour, openMin, closeHour, closeMin)){
order = submitOrder (OrderCommand.SELLSTOP, currBar.getLow(), sellPrice);
}
}
}
}
public void onTick(Instrument instrument, ITick tick) throws JFException {
if (instrument != this.instrument) {
return;
}
updateTrailingStopLoss(tick, order, trailingPips, getRoundedPips(stopLossPips));
}
//trailing
public boolean updateTrailingStopLoss(ITick tick, IOrder pOrder, double pTriggerPips, double pStopLossPips) throws JFException {
boolean triggered = false;
if (pTriggerPips > 0 && pStopLossPips > 0
&& pOrder != null && pOrder.getState() == IOrder.State.FILLED) {
double newStop;
double openPrice = pOrder.getOpenPrice();
double currentStopLoss = pOrder.getStopLossPrice();
// (START) trailing stop loss is activated when price is higher than oper price + trailingTrigger pips
// (TRAILING STOP) if price moves further up (for BUY order), stop loss is updated to stopLossPips
if (pOrder.isLong()) { // long side order
if (tick.getBid() > currentStopLoss + getPipPrice(pStopLossPips)
&& tick.getBid() > openPrice + getPipPrice(pTriggerPips)) {
// trailing stop loss
newStop = tick.getBid() - getPipPrice(pStopLossPips);
newStop = (new BigDecimal(newStop)).setScale(instrument.getPipScale(), BigDecimal.ROUND_HALF_UP).doubleValue();
if (currentStopLoss != newStop) {
pOrder.setStopLossPrice(newStop);
triggered = true;
}
}
} else { // short side order
if (tick.getAsk() < currentStopLoss - getPipPrice(pStopLossPips)
&& tick.getAsk() < openPrice - getPipPrice(pTriggerPips)) {
// trailing stop loss
newStop = tick.getAsk() + getPipPrice(pStopLossPips);
newStop = (new BigDecimal(newStop)).setScale(instrument.getPipScale(), BigDecimal.ROUND_HALF_UP).doubleValue();
if (currentStopLoss != newStop) {
pOrder.setStopLossPrice(newStop);
triggered = true;
}
}
}
}
return triggered;
}
private IOrder submitOrder(OrderCommand orderCmd, double price, double stopLossPrice) throws JFException {
double takeProfitPrice = 0.0;
ITick tick = history.getLastTick(instrument);
// stop loss and take profit
if (orderCmd == OrderCommand.BUYSTOP) {
if (takeProfitPips > 0) {
takeProfitPrice = price + getPipPrice(takeProfitPips);
stopLossPrice = price - getPipPrice(stopLossPips);
}
} else {
if (takeProfitPips > 0) {
takeProfitPrice = price - getPipPrice(takeProfitPips);
stopLossPrice = price + getPipPrice(stopLossPips);
}
}
return engine.submitOrder(getLabel(instrument), instrument, orderCmd, amount, price, slippage, stopLossPrice, takeProfitPrice);
}
private void closeOrder(IOrder order) throws JFException {
if (order != null && isActive(order)) {
order.close();
}
}
private boolean isActive(IOrder order) throws JFException {
if (order != null && order.getState() != IOrder.State.CLOSED && order.getState() != IOrder.State.CREATED && order.getState() != IOrder.State.CANCELED) {
return true;
}
return false;
}
private double getPipPrice(double pips) {
return pips * instrument.getPipValue();
}
private String getLabel(Instrument instrument) {
String label = instrument.name();
label = label + (counter++);
label = label.toUpperCase();
return label;
}
//calendar
public boolean timeIsEqual(long time, int hour, int min){
Calendar cal = new GregorianCalendar();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.setTimeInMillis(time);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
Calendar cal2 = new GregorianCalendar();
cal2.setTimeZone(TimeZone.getTimeZone("GMT"));
cal2.setTimeInMillis(cal.getTimeInMillis());
cal2.add(Calendar.MINUTE, 5);
if(cal.getTimeInMillis() <= time
&& time <= cal2.getTimeInMillis() ) {
return true;
}
return false;
}
//time to trade
public boolean isRightTime(long time, int fromHour, int fromMin, int toHour, int toMin) {
Calendar cal = new GregorianCalendar();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.setTimeInMillis(time);
cal.set(Calendar.HOUR_OF_DAY, fromHour);
cal.set(Calendar.MINUTE, fromMin);
Calendar cal2 = new GregorianCalendar();
cal2.setTimeZone(TimeZone.getTimeZone("GMT"));
cal2.setTimeInMillis(time);
cal2.set(Calendar.HOUR_OF_DAY, toHour);
cal2.set(Calendar.MINUTE, toMin);
if (cal.getTimeInMillis() <= time
&& time <= cal2.getTimeInMillis()) {
return true;
}
return false;
}
private double getRoundedPrice(double price) {
BigDecimal bd = new BigDecimal(price);
bd = bd.setScale(instrument.getPipScale() + 1, RoundingMode.HALF_UP);
return bd.doubleValue();
}
private double getRoundedPips(double pips) {
BigDecimal bd = new BigDecimal(pips);
bd = bd.setScale(1, RoundingMode.HALF_UP);
return bd.doubleValue();
}
public void onMessage(IMessage message) throws JFException {
}
public void onAccount(IAccount account) throws JFException {
}
public void onStop() throws JFException {
}
/**************** debug print functions ***********************/
private void print(Object... o) {
for (Object ob : o) {
//console.getOut().print(ob + " ");
if (ob instanceof Double) {
print2(toStr((Double) ob));
} else if (ob instanceof double[]) {
print((double[]) ob);
} else if (ob instanceof double[]) {
print((double[][]) ob);
} else if (ob instanceof Long) {
print2(toStr((Long) ob));
} else if (ob instanceof IBar) {
print2(toStr((IBar) ob));
} else {
print2(ob);
}
print2(" ");
}
console.getOut().println();
}
private void print(Object o) {
console.getOut().println(o);
}
private void print2(Object o) {
console.getOut().print(o);
}
private void print(double d) {
print(toStr(d));
}
private void print(double[] arr) {
print(toStr(arr));
}
private void print(double[][] arr) {
print(toStr(arr));
}
private void print(IBar bar) {
print(toStr(bar));
}
private void printIndicatorInfos(IIndicator ind) {
for (int i = 0; i < ind.getIndicatorInfo().getNumberOfInputs(); i++) {
print(ind.getIndicatorInfo().getName() + " Input " + ind.getInputParameterInfo(i).getName() + " " + ind.getInputParameterInfo(i).getType());
}
for (int i = 0; i < ind.getIndicatorInfo().getNumberOfOptionalInputs(); i++) {
print(ind.getIndicatorInfo().getName() + " Opt Input " + ind.getOptInputParameterInfo(i).getName() + " " + ind.getOptInputParameterInfo(i).getType());
}
for (int i = 0; i < ind.getIndicatorInfo().getNumberOfOutputs(); i++) {
print(ind.getIndicatorInfo().getName() + " Output " + ind.getOutputParameterInfo(i).getName() + " " + ind.getOutputParameterInfo(i).getType());
}
console.getOut().println();
}
public static String toStr(double[] arr) {
String str = "";
for (int r = 0; r < arr.length; r++) {
str += "[" + r + "] " + (new DecimalFormat("#.#######")).format(arr[r]) + "; ";
}
return str;
}
public static String toStr(double[][] arr) {
String str = "";
if (arr == null) {
return "null";
}
for (int r = 0; r < arr.length; r++) {
for (int c = 0; c < arr[r].length; c++) {
str += "[" + r + "][" + c + "] " + (new DecimalFormat("#.#######")).format(arr[r][c]);
}
str += "; ";
}
return str;
}
public String toStr(double d) {
return (new DecimalFormat("#.#######")).format(d);
}
public String toStr(Long time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") {
{
setTimeZone(TimeZone.getTimeZone("GMT"));
}
};
return sdf.format(time);
}
private String toStr(IBar bar) {
return toStr(bar.getTime()) + " O:" + bar.getOpen() + " C:" + bar.getClose() + " H:" + bar.getHigh() + " L:" + bar.getLow();
}
private void printTime(Long time) {
console.getOut().println(toStr(time));
}
}