I tried to calculate indicator values on custom data as described in the Wiki. But the values do not match with the ones on the chart(same filter). Can you figure what have I done wrong? Jforex API version is v2.13.25
code output:
Quote:
12:36:10 resultArr JforexFeed: [0] 0.00246; [1] 0.00255; [2] 0.00250; [3] 0.00248; [4] 0.00241; [5] 0.00244;
12:36:10 resultArr CustomData: [0] 0.00239; [1] 0.00248; [2] 0.00243; [3] 0.00242; [4] 0.00235; [5] 0.00238;
12:36:10 look back: 14, look forward: 0
code:
package jforex;
import com.dukascopy.api.*;
import com.dukascopy.api.indicators.IIndicator;
import java.text.DecimalFormat;
import java.util.*;
public class indicatorTest implements IStrategy {
private IEngine engine;
private IConsole console;
private IHistory history;
private IContext context;
private IIndicators indicators;
private IUserInterface userInterface;
public static DecimalFormat df = new DecimalFormat("0.00000");
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.console = context.getConsole();
this.history = context.getHistory();
this.context = context;
this.indicators = context.getIndicators();
this.userInterface = context.getUserInterface();
atrIndicatorTest();
context.stop();
}
void atrIndicatorTest()throws JFException{
IIndicator atrIndicator = indicators.getIndicator("ATR");
atrIndicator.setOptInputParameter(0, 14);
int barsToLoad = 20;
Instrument instrument = Instrument.EURUSD;
Period period = Period.ONE_HOUR;
long currentBarTime = history.getBarStart(period,history.getLastTick(instrument).getTime());
List<IBar> bars = history.getBars(instrument,period,OfferSide.BID,Filter.WEEKENDS,barsToLoad,currentBarTime,0);
//console.getOut().println(bars.toString());
double [][] arr = new double[5][barsToLoad];
for (int i = 0; i < barsToLoad; i++) {
//open=0, close=1, high=2, low=3, volume=4
IBar bar = bars.get(i);
arr[0][i] = bar.getOpen();
arr[1][i] = bar.getClose();
arr[2][i] = bar.getHigh();
arr[3][i] = bar.getLow();
arr[4][i] = bar.getVolume();
}
atrIndicator.setInputParameter(0, arr);
//set outputs
double [] resultArr = new double [barsToLoad-atrIndicator.getLookback()];
atrIndicator.setOutputParameter(0, resultArr);
console.getOut().format("look back: %d, look forward: %d", atrIndicator.getLookback(), atrIndicator.getLookforward()).println();
//calculate indicator values on custon data
atrIndicator.calculate(atrIndicator.getLookback(), barsToLoad - 1);
//print results
console.getOut().println("resultArr CustomData: " + arrayToString(resultArr));
//print indicator values calculate on jforex feed
console.getOut().println("resultArr JforexFeed: " + arrayToString(indicators.atr(instrument, period, OfferSide.BID, 14, Filter.WEEKENDS, barsToLoad-atrIndicator.getLookback(), currentBarTime, 0)));
}
public static String arrayToString(double[] arr) {
String str = "";
for (int r = 0; r < arr.length; r++) {
str += "[" + r + "] " + df.format(arr[r]) + "; ";
}
return str;
}
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 {
}
}