Consider the following strategy which prints the average difference between bollinger's lower and upper bounds over the last 5 bars.
package jforex.strategies.indicators;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IIndicators.MaType;
public class BBands implements IStrategy {
private IEngine engine;
private IConsole console;
private IHistory history;
private IIndicators indicators;
private SimpleDateFormat sdf;
@Configurable("Instrument")
public Instrument instrument = Instrument.EURUSD;
@Configurable("Period")
public Period selectedPeriod = Period.ONE_MIN;
@Configurable("BBands filter")
public Filter indicatorFilter = Filter.NO_FILTER;
@Configurable("Amount")
public double amount = 0.02;
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.console = context.getConsole();
this.history = context.getHistory();
this.indicators = context.getIndicators();
String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
print("start");
}
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 {
if (!instrument.equals(this.instrument) || !period.equals(selectedPeriod))
return;
int timePeriod = 5;
MaType maType = MaType.EMA;
double nbDevDn = 2;
double nbDevUp = 2;
IBar firstBar = history.getBar(instrument, selectedPeriod, OfferSide.BID, 5);
IBar lastBar = history.getBar(instrument, selectedPeriod, OfferSide.BID, 1);
double [][] bbands = indicators.bbands(instrument, period, OfferSide.BID,
AppliedPrice.CLOSE,timePeriod, nbDevUp, nbDevDn, maType, firstBar.getTime(), lastBar.getTime());
double bbandsAvg = getAverageOverPeriod(bbands,timePeriod);
//period, average bband and the whole array
print(sdf.format(firstBar.getTime()) + " - " + sdf.format(lastBar.getTime()) + " avg: "
+ (new DecimalFormat("#.000000")).format(bbandsAvg) + " (Ups; Mids; Lows - " +arrayToString(bbands) + ")");
}
private double getAverageOverPeriod(double [][] arr, int period){
//sum averages of each time point
double avgSum = 0;
for (int c=0; c<arr[0].length; c++) {
//0=high, 2=low
avgSum += (arr[0][c] - arr[2][c]) / 2;
//to calculate median values use:
//avgSum += (arr[0][c] - arr[2][c]) + arr[2][c];
}
return avgSum/period;
}
private void print(Object message) {
console.getOut().println(message);
}
public static String arrayToString(double [][] arr){
String str = "";
for (int r=0; r<arr.length; r++) {
for (int c=0; c<arr[r].length; c++) {
str += " " + (new DecimalFormat("0.000000")).format(arr[r][c]);
}
str += "; ";
}
return str;
}
}