Dukascopy
 
 
Wiki JStore Search Login

Java version of WATR indicator
 Post subject: Java version of WATR indicator Post rating: 0   New post Posted: Wed 15 Apr, 2015, 19:54 

User rating: 0
Joined: Thu 12 Mar, 2015, 16:04
Posts: 3
Location: UkraineUkraine
My attempt to code popular WATR mql indicator in Java
But... something is wrong. Compilation is OK. And nothing on the chart
Any help is appreciated
+++++++++++++++++++++++++++++
package jforex;

import java.util.*;
//import com.dukascopy.api.Instrument;
import com.dukascopy.api.indicators.*;
import java.awt.*;

public class WATRj implements IIndicator {
private IIndicator atr;
private int atrPeriod = 21;
private int OutSide = 0;
private double Accelerator = 4.0;
// ---------------------------
private IndicatorInfo indicatorInfo;
private InputParameterInfo[] inputParameterInfos;
private OptInputParameterInfo[] optInputParameterInfos;
private OutputParameterInfo[] outputParameterInfos;
private double[][][] inputs = new double[1][][];
private double[][] outputs = new double[2][];
private static final int OPEN = 0;
private static final int CLOSE = 1;
private static final int HLINE = 0;
private static final int LLINE = 1;

public void onStart(IIndicatorContext context) {
atrIndicator = context.getIndicatorsProvider().getIndicator("ATR");
atrIndicator.setOptInputParameter(0, atrPeriod);

indicatorInfo =
new IndicatorInfo("WATRj", "WATR Lines Indicator", "Custom Indicators", true, false, true, 1, 3, 2);
indicatorInfo.setRecalculateAll(true);
inputParameterInfos =
new InputParameterInfo[] {
new InputParameterInfo("Price", InputParameterInfo.Type.PRICE)
};
optInputParameterInfos =
new OptInputParameterInfo[] {
new OptInputParameterInfo("atrPeriod", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(atrPeriod, 1, 200, 1)),
new OptInputParameterInfo("OutSide", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(OutSide, 0, 50, 1)),
new OptInputParameterInfo("Accelerator", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(Accelerator, 2, 20, 0.1, 1)),
};
outputParameterInfos =
new OutputParameterInfo[] {
// shows DOWNtrend and have 0 index
new OutputParameterInfo("Hline 0", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) {
{
setColor(Color.BLUE);
setGapAtNaN(true);
}
},
// shows UPtrend and have 1 index
new OutputParameterInfo("Lline 1", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) {
{
setColor(Color.ORANGE);
setGapAtNaN(true);
}
}
};

// if (indicatorContext.getFeedDescriptor() != null){
// pip = indicatorContext.getFeedDescriptor().getInstrument().getPipValue();
// }

//feedDescriptor = getFeedDescriptor();
//this.context = context;
}

public IndicatorResult calculate(int startIndex, int endIndex) {
//calculating startIndex taking into account lookback value
if (startIndex - getLookback() < 0) {
startIndex -= startIndex - getLookback();
}
if (startIndex > endIndex) {
return new IndicatorResult(0, 0);
}

//double pip = Instrument.getPipValue();
double pip = 0;
atrIndicator.setInputParameter(0, inputs[0][1]);

double[] atrOutput = new double[endIndex - startIndex + 1 + atrIndicator.getLookback()];
atrIndicator.setOutputParameter(0, atrOutput);
IndicatorResult atrResult = atrIndicator.calculate(startIndex-1, endIndex);

//double[] outputs[HLINE] = new double[atrResult.getNumberOfElements()];
//double[] outputs[LLINE] = new double[atrResult.getNumberOfElements()];
// Initial values
outputs[HLINE][0] = Double.NaN;
outputs[LLINE][0] = inputs[0][CLOSE][startIndex]-Accelerator*atrOutput[0]-OutSide*pip; // initiate with lowline and UP trend assumed;

int i, j;
for (i = startIndex+1, j = 1; i <= endIndex; i++, j++) {
//Inputs: 0 open, 1 close, 2 high, 3 low, 4 volume
//check trend up or down (else)
if (inputs[0][CLOSE][i-1] > outputs[LLINE][j-1] && outputs[HLINE][j-1] == Double.NaN) { // Trend UP
//Check AntiTrend bar
if (inputs[0][CLOSE][i] < inputs[0][OPEN][i]) { // may be revers if cross
//check cross down
if (inputs[0][CLOSE][i] < outputs[LLINE][j-1]) { // this is the revers - brakes lowline from high
outputs[HLINE][j]=inputs[0][CLOSE][i]+Accelerator*atrOutput[j-1]+OutSide*pip;
outputs[LLINE][j] = Double.NaN;
}
else { // No revers - the same BOTH values
outputs[HLINE][j] = outputs[HLINE][j-1];
outputs[LLINE][j] = outputs[LLINE][j-1];
}
}
else { // follow the trend - new value for LOWLINE only
outputs[HLINE][j]=Double.NaN;
outputs[LLINE][j] = Math.max(outputs[LLINE][j-1], inputs[0][CLOSE][i]-Accelerator*atrOutput[j]-OutSide*pip);
}
}
else { // DOWN
//Check AntiTrend bar
if (inputs[0][CLOSE][i] > inputs[0][OPEN][i]) { // may be revers if cross
//check cross up
if (inputs[0][CLOSE][i] > outputs[HLINE][j-1]) { // this is the revers - brakes highline from low
outputs[HLINE][j]=Double.NaN;
outputs[LLINE][j] = inputs[0][CLOSE][i]-Accelerator*atrOutput[j-1]-OutSide*pip;
}
else { // No revers - the same BOTH values
outputs[HLINE][j] = outputs[HLINE][j-1];
outputs[LLINE][j] = outputs[LLINE][j-1];
}
}
else { // follow the trend - new value for HIGHLINE only
outputs[HLINE][j]=Math.min(outputs[HLINE][j-1], inputs[0][CLOSE][i]+Accelerator*atrOutput[j]+OutSide*pip);
outputs[LLINE][j] = Double.NaN;
}
}
}
return new IndicatorResult(startIndex, j);
}
// ----------------------------------------------
public int getLookback() {
return atrIndicator.getLookback();
}

public int getLookforward() {
return 0;
}

public IndicatorInfo getIndicatorInfo() {
return indicatorInfo;
}

public InputParameterInfo getInputParameterInfo(int index) {
if (index <= inputParameterInfos.length) {
return inputParameterInfos[index];
}
return null;
}

public OptInputParameterInfo getOptInputParameterInfo(int index) {
if (index <= optInputParameterInfos.length) {
return optInputParameterInfos[index];
}
return null;
}

public OutputParameterInfo getOutputParameterInfo(int index) {
if (index <= outputParameterInfos.length) {
return outputParameterInfos[index];
}
return null;
}

public void setInputParameter(int index, Object array) {
inputs[index] = (double[][]) array;
}

public void setOptInputParameter(int index, Object value) {
switch (index) {
case 0:
atrPeriod = (Integer) value;
atr.setOptInputParameter(0, atrPeriod);
break;
case 1:
OutSide = (Integer) value;
break;
case 2:
Accelerator = (Double) value;
break;
default:
throw new ArrayIndexOutOfBoundsException(index);
}
}

public void setOutputParameter(int index, Object array) {
outputs[index] = (double[]) array;
}
}
+++++++++++++++++++++++++++++++++++++++++++++


 

Jump to:  

  © 1998-2024 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com