Hi attached I send you some code which might help you.
The strategy is as follows:
public class TestStrategy implements IStrategy
{
private IEngine engine = null;
private IIndicators indicators = null;
private IConsole console;
private IAccount account = null;
private IOrder order = null;
private Object[] optInParams = null;
private String indicatorName = "TestIndicator";
private IContext context = null;
private Signal signal = Signal.start;
private IIndicator customIndicator = null;
private Candidate lastCandidate = null;
@Configurable("FileLocation")
private String fileLocation = "D:\\Workspace\\JForex\\src\\TradeTesting\\TestIndicator.jfx";
@Configurable("PositionSize")
private double positionSize = 0.1d;
public File indicatorJfxFile = null;
@Configurable("Instrument")
public Instrument instrument = Instrument.EURUSD;
@Configurable("Period")
public Period period = Period.FIVE_MINS;
@Configurable("Printable bar count")
public int candleCount = 20;
@Configurable("Shift")
public int shift = 0;
public void onStart(IContext context)
{
this.context = context;
this.console = context.getConsole();
indicatorJfxFile = new File(fileLocation);
if(indicatorJfxFile != null)
{
console.getOut().println("File found.");
}
engine = context.getEngine();
account = context.getAccount();
console.getOut().println("Strategy started");
//Prepare the input parameters array for the custom indicator
optInParams = new Object[] { InputParameterInfo.Type.BAR };
try
{
//Get all indicators
indicators = context.getIndicators();
//Register and load custom indicator in indicators
this.indicators.registerCustomIndicator(indicatorJfxFile);
//Retrieve the custom indicator from all loaded indicators
customIndicator = indicators.getIndicator(indicatorName);
}
catch(JFException e)
{
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter( writer );
e.printStackTrace( printWriter );
printWriter.flush();
String stackTrace = writer.toString();
console.getOut().println("Error encountered " + stackTrace);
}
}
public void onStop()
{
System.out.println("Strategy stopped");
}
public void onTick(Instrument instrument, ITick tick)
{
System.out.println("Calculate Ticks");
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar)
{
if(instrument != Instrument.EURUSD || !period.name().equals("FIVE_MINS"))
{
return;
}
console.getOut().println("Processing onBar call");
long endTime = askBar.getTime();
long startTime = endTime - 300000;
console.getOut().println("endTime: " + String.valueOf(endTime) + " startTime: " + String.valueOf(startTime));
try
{
console.getOut().println("Start calculating indicator values.");
Object[] result= indicators.calculateIndicator(instrument, period, new OfferSide[] { OfferSide.ASK }, indicatorName, new AppliedPrice[]{AppliedPrice.CLOSE}, new Object[]{endTime}, startTime, endTime);
//No values are passed back from the indicator!!
}
catch(Exception e)
{
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter( writer );
e.printStackTrace( printWriter );
printWriter.flush();
String stackTrace = writer.toString();
console.getOut().println("Error encountered " + stackTrace);
}
}
protected int positionsTotal(Instrument instrument)
{
return -1;
}
public void onMessage(IMessage message) throws JFException
{
System.out.println("Message received");
}
public void onAccount(IAccount account) throws JFException
{
System.out.println("Account message");
}
}
And here comes the indicator:
public class TestIndicator implements IIndicator
{
private IndicatorInfo indicatorInfo;
private InputParameterInfo[] inputParameterInfos;
private OptInputParameterInfo[] optInputParameterInfos;
private OutputParameterInfo[] outputParameterInfos;
private IBar[][] inputs = new IBar[1][];
private int timePeriod = 30;
public Object[][] outputs = new Object[1][];
private IConsole console;
private IFeedDescriptor feedDescriptor;
private IIndicatorChartPanel chart;
private IChartObjectFactory factory;
private IIndicatorContext context;
private static final String GUID = UUID.randomUUID().toString();
public void onStart(IIndicatorContext context)
{
this.context = context;
console = context.getConsole();
console.getOut().println("Entered Indicator.");
indicatorInfo = new IndicatorInfo("TestIndicator", "TestIndicator Indicator", "TestIndicator Indicators", false, false, true, 1, 1, 1);
inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Bars", InputParameterInfo.Type.BAR)};
optInputParameterInfos = new OptInputParameterInfo[] {
new OptInputParameterInfo("Time period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(timePeriod, 1, 200, 1))
};
outputParameterInfos = new OutputParameterInfo[] {
new OutputParameterInfo("Out", OutputParameterInfo.Type.OBJECT, OutputParameterInfo.DrawingStyle.NONE)
};
feedDescriptor = context.getFeedDescriptor();
this.context = context;
console.getOut().println("OnStart finished.");
}
public IndicatorResult calculate(int startIndex, int endIndex)
{
try
{
console.getOut().println("Calculating Indicator values.");
ArrayList<Candidate> candidates = new ArrayList<Candidate>();
IndicatorResult result = null;
int count = 0;
.
.
.
Indicator calculations
.
.
.
//Adding the value to the outputs array
ExtendArrayAndCopyValues(currentCandidate);
//And after having everything calculated seting the Indicator result
return new IndicatorResult(((Candidate)outputs[0][0]).GetIndex(), outputs[0].length);
}
catch(Exception e)
{
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter( writer );
e.printStackTrace( printWriter );
printWriter.flush();
String stackTrace = writer.toString();
console.getOut().println("Error: -> " + stackTrace);
}
return null;
}
public void ExtendArrayAndCopyValues(Candidate c) throws Exception
{
if(c != null)
{
if(outputs[0] == null)
{
outputs[0] = new Object[1];
outputs[0][0] = (Object)c;
return;
}
Object[] results = (Object[])outputs[0];
outputs[0] = new Object[outputs[0].length + 1];
int count = 0;
for(Object o : results)
{
if(o != null)
{
outputs[0][count] = o;
count++;
}
else
{
throw new Exception("One candidate was set to null");
}
}
outputs[0][count] = (Object)c;
console.getOut().println("Candidates in output: " + outputs[0].length);
}
}
public IndicatorInfo getIndicatorInfo() {
return indicatorInfo;
}
public InputParameterInfo getInputParameterInfo(int index)
{
try
{
if (index <= inputParameterInfos.length) {
return inputParameterInfos[index];
}
}
catch(Exception e)
{
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter( writer );
e.printStackTrace( printWriter );
printWriter.flush();
String stackTrace = writer.toString();
console.getOut().println("Error: -> " + stackTrace);
}
return null;
}
public int getLookback() {
return timePeriod;
}
public int getLookforward() {
return 0;
}
public OptInputParameterInfo getOptInputParameterInfo(int index)
{
try
{
if (index <= optInputParameterInfos.length) {
return optInputParameterInfos[index];
}
}
catch(Exception e)
{
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter( writer );
e.printStackTrace( printWriter );
printWriter.flush();
String stackTrace = writer.toString();
console.getOut().println("Error: -> " + stackTrace);
}
return null;
}
public OutputParameterInfo getOutputParameterInfo(int index)
{
try
{
if (index <= outputParameterInfos.length) {
return outputParameterInfos[index];
}
}
catch(Exception e)
{
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter( writer );
e.printStackTrace( printWriter );
printWriter.flush();
String stackTrace = writer.toString();
console.getOut().println("Error: -> " + stackTrace);
}
return null;
}
public void setInputParameter(int index, Object array)
{
try
{
inputs[index] = (IBar[]) array;
}
catch(Exception e)
{
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter( writer );
e.printStackTrace( printWriter );
printWriter.flush();
String stackTrace = writer.toString();
console.getOut().println("Error: -> " + stackTrace);
}
}
public void setOptInputParameter(int index, Object value)
{
try
{
if(index == 0)
{
Long tmp = (Long)value;
timePeriod = 1500;//tmp.intValue();
}
}
catch(Exception e)
{
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter( writer );
e.printStackTrace( printWriter );
printWriter.flush();
String stackTrace = writer.toString();
console.getOut().println("Error: -> " + stackTrace);
}
}
public void setOutputParameter(int index, Object array)
{
try
{
outputs[0] = null;
}
catch(Exception e)
{
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter( writer );
e.printStackTrace( printWriter );
printWriter.flush();
String stackTrace = writer.toString();
console.getOut().println("Error: -> " + stackTrace);
}
}
I hope this helps determining the problem which is that the inidcator returns no values but the outputs array is filled.
Thanks for your support.
Best regards
Jenzi