Hi,
When I create an IBarFeedListener to listen for 2HR bars on GBP/USD, I do not get the same indicator values in the strategy that I see on the GBP/USD 2HR chart.
Below is the source code for the strategy that outlines this issue:
//package strategies;
package singlejartest;
import java.util.*;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import com.dukascopy.api.*;
import com.dukascopy.api.feed.*;
public class TestStrategy implements IStrategy
{
/////////////////////////////
//constants used for console logging
/////////////////////////////
static final int INFO = 0;
static final int WARNING = 1;
static final int ERROR = 2;
/////////////////////////////
//Configurable input parameters
/////////////////////////////
@Configurable("Instrument") public Instrument _instrument = Instrument.GBPUSD;
@Configurable("Larger EMA Period") public int _largerEmaPeriod = 30;
@Configurable("Smaller EMA Period") public int _smallerEmaPeriod = 24;
@Configurable("Tiny EMA Period") public int _tinyEmaPeriod = 3;
@Configurable("MACD Fast Period") public int _macdFastPeriod = 12;
@Configurable("MACD Slow Period") public int _macdSlowPeriod = 33;
@Configurable("MACD Signal Period") public int _macdSignalPeriod = 9;
@Configurable("Debug Printing") public boolean _debugPrintFlag = true;
/////////////////////////////
//JForex interfaces for future use
/////////////////////////////
private IConsole _console;
private IContext _context;
private IIndicators _indicators;
/////////////////////////////
//misc
/////////////////////////////
Period _period = Period.createCustomPeriod(Unit.Hour, 2);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd 'at' HH:mm:ss z");
private void debugPrint (int level, String strToPrint)
{
if (_debugPrintFlag)
{
switch (level)
{
case ERROR:
_console.getErr().println(strToPrint);
break;
case WARNING:
_console.getWarn().println(strToPrint);
break;
case INFO:
default:
_console.getOut().println(strToPrint);
}
}
}
/////////////////////////////
// IStrategy interface methods
/////////////////////////////
public void onStart(IContext context) throws JFException
{
_console = context.getConsole();
_context = context;
_indicators = context.getIndicators();
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
//subscribe to instrument
Set<Instrument> instSet = new HashSet<Instrument>();
instSet.add(_instrument);
context.setSubscribedInstruments(instSet);
debugPrint (INFO, "Subscribing to: " + instSet.toString() + " " + _period);
//subscribing to custom Period
this._context.subscribeToBarsFeed
(_instrument,
_period,
OfferSide.BID,
new IBarFeedListener()
{
public void onBar(Instrument instrument, Period period, OfferSide offerSide, IBar bar)
{
debugPrint (INFO, "Received new " + period + " bar for " + instrument + ", open = " + bar.getOpen() + ", high = " + bar.getHigh() + ", low = " + bar.getLow() + ", close = " + bar.getClose());
try
{
double emaSmallerArray[] = _indicators.ema(_instrument, _period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, _smallerEmaPeriod, bar.getTime(), bar.getTime());
double emaLargerArray[] = _indicators.ema(_instrument, _period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, _largerEmaPeriod, bar.getTime(), bar.getTime());
double emaShadowArray[] = _indicators.ema(_instrument, _period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, _tinyEmaPeriod, bar.getTime(), bar.getTime());
double[][] macdArray = _indicators.macd(_instrument, _period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, _macdFastPeriod, _macdSlowPeriod, _macdSignalPeriod, bar.getTime(), bar.getTime());
double emaSmaller = emaSmallerArray[0];
double emaLarger = emaLargerArray[0];
double emaShadow = emaShadowArray[0];
double macdHistogram = macdArray[2][0];
double bid = bar.getClose();
debugPrint (INFO, "Bar at " + sdf.format(new Date(bar.getTime())) +
", ema values 24 = " + emaSmaller +
", 30 = " + emaLarger +
", 3 = " + emaShadow +
", macd histogram = " + macdHistogram );
}
catch (JFException e)
{
debugPrint (ERROR, "Exception: " + e.toString());
e.printStackTrace();
}
}
});
}
public void onAccount(IAccount account) throws JFException {
}
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 {
}
}
The below is an example of the output that I get in the console when backtesting this strategy:
Quote:
2012-07-26 18:57:12 Bar at 2012.07.16 at 08:00:00 GMT, ema values 24 = 1.5561952528316139, 30 = 1.555868660615521, 3 = 1.5544033593742514, macd histogram = -8.2355000061654E-4
2012-07-26 18:57:12 Received new 2 Hours bar for GBP/USD, open = 1.55473, high = 1.55503, low = 1.55165, close = 1.55284
2012-07-26 18:57:12 Bar at 2012.07.16 at 06:00:00 GMT, ema values 24 = 1.5564870255569827, 30 = 1.5560775707026042, 3 = 1.5559667187485031, macd histogram = -5.806067886842967E-4
2012-07-26 18:57:12 Received new 2 Hours bar for GBP/USD, open = 1.5572, high = 1.55746, low = 1.55365, close = 1.55473
2012-07-26 18:57:11 Bar at 2012.07.16 at 04:00:00 GMT, ema values 24 = 1.5566398307600358, 30 = 1.5561705175313343, 3 = 1.5572034374970065, macd histogram = -3.995685508460017E-4
2012-07-26 18:57:11 Received new 2 Hours bar for GBP/USD, open = 1.55708, high = 1.5574, low = 1.55643, close = 1.5572
2012-07-26 18:57:11 Bar at 2012.07.16 at 02:00:00 GMT, ema values 24 = 1.5565911299814863, 30 = 1.5560995557267558, 3 = 1.5572068749940131, macd histogram = -4.0018133959733517E-4
2012-07-26 18:57:11 Received new 2 Hours bar for GBP/USD, open = 1.55683, high = 1.55777, low = 1.55678, close = 1.55709
2012-07-26 18:57:11 Bar at 2012.07.16 at 00:00:00 GMT, ema values 24 = 1.5565477582656206, 30 = 1.5560313375948525, 3 = 1.5573237499880261, macd histogram = -3.791592339691223E-4
2012-07-26 18:57:10 Received new 2 Hours bar for GBP/USD, open = 1.55797, high = 1.558, low = 1.55639, close = 1.5568
2012-07-26 18:57:10 Subscribing to: [GBP/USD] 2 Hours
When comparing the values against the chart, the open, high, low and close values are correct.... but none of the indicator values obtained by the strategy are correct. I'm not getting errors or exceptions - the indicator values are just incorrect.
What am I doing wrong?
thanks in advance,
C