Hi,
Indicators are designed as simple functions that take some input prices and calculate some results based on those prices. They are not designed to do main business functionality. Instead you should use strategy, where you are able to access your created indicators. In your situation, where you are using different instruments and making calculations, you should consider to use strategy where you can write main logic and make some indicator which will do the math calculations. In strategy you are able to access indicator passing them any instrument. When you are creating indicators, there is several things that you should take into account - for indicator you should initialize three main parts: IndicatorInfo, InputParameterInfo, OutputParameterInfo. As well you could initialize
OptInputParameterInfo if there is need for that. Use method calculate to make necessary math calculations on incoming data.
Use of IndicatorInfo:
name - gives a name by which you will bee able to call it from strategy (should be simple);
title - gives a title to this indicator - more detailed name which are used in JForex "add indicator" dialog window;
groupName - specifies directory where it would bee accessible thru JForex in "add indicator" dialog window;
overChart - true if indicator should be drawn over candles/ticks;
overVolumes - true if indicator should be drawn over volume information;
unstablePeriod - true if indicator has unstable period (like EMA or SAR). This will add more candles in every call to stabilize function;
candlesticks - true if indicator should be shown over bars;
numberOfInputs - number of inputs that user should provide;
numberOfOptionalInputs - number of optional inputs;
numberOfOutputs - number of outputs, that function returns;
For example:
new IndicatorInfo("ENTROPY", "Calculates entropy number", "My indicators",false, false, false, 1, 1, 1);
Use InputParameterInfo to describe your input data:
1)name - name of the input;
2)type - type of the input (InputParameterInfo.Type);
For example:
new InputParameterInfo("Input data", InputParameterInfo.Type.PRICE);
Use OutputParameterInfo to describe type of output data and drawing style:
name - name of the output
type - type of the output
drawingStyle - specifies how to draw this output (OutputParameterInfo.DrawingStyle)
For example:
new OutputParameterInfo("Output data", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE);
Use OptInputParameterInfo to describe optional input parameters:
name - name of the optional input
type - type of the optional input (OptInputParameterInfo.Type)
description - object that describes possible values of the optional input (OptInputDescription)
For example:
new OptInputParameterInfo("Time period", OptInputParameterInfo.Type.OTHER,new IntegerRangeDescription(2, 2, 100, 1));
More detail description
https://www.dukascopy.com/swiss/docs/api/index.htm To get access to data from newly created indicator, should use one of calculateIndicator methods.
For example:
indicators.calculateIndicator(Instrument.EURUSD,Period.TICK,new OfferSide[] {OfferSide.BID},"ENTROPY",
new IIndicators.AppliedPrice[] {IIndicators.AppliedPrice.CLOSE}, new Object[]{1},1);
We added strategy as sample that shows how to call and get data from your own indicator.
package jforex;
import com.dukascopy.api.*;
public class TestMyIndicatorStrategy implements IStrategy {
private IConsole console;
private IIndicators indicators;
public void onStart(IContext context) throws JFException {
this.console = context.getConsole();
this.indicators = context.getIndicators();
}
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 {
//making calculations on current instrument and period with manually created indicator ENTROPY
Object[] outputs = indicators.calculateIndicator(instrument,
period,
new OfferSide[] {OfferSide.BID},
"ENTROPY",
new IIndicators.AppliedPrice[] {IIndicators.AppliedPrice.CLOSE},
new Object[]{1},
1);
//getting output on console
for (int i=0;i<outputs.length;i++){
this.console.getOut().print("output["+i+"]: "+(Double)outputs[i]);
}
this.console.getOut().println();
}
}