Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

IIndicator - Instrument is always null
 Post subject: IIndicator - Instrument is always null Post rating: 0   New post Posted: Thu 16 Sep, 2010, 23:08 

User rating: 0
Joined: Wed 18 May, 2011, 11:25
Posts: 60
Location: DE
Hi,

i want to show the absolute range of the bars as indicators. it works well, if i don't want to show
the absolute values, that means if i only use the difference between the input values.

e.g. 1.3047 - 1.3037 --> 0.0010

but i want to use the absolute value --> 10 pips

this can be done with the instrument.getPipScale() ... but when i try to get the instrument, it is
always null and i don't know why.

my code:
package jforex;

import com.dukascopy.api.IConsole;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.indicators.*;
import java.awt.*;

public class Absolute_Range3 implements IIndicator {

    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private double[][][] inputs = new double[1][][];
    private double[][] outputs = new double[3][];
    private Instrument recentInstrument;
    private IConsole console;
   
    public void onStart(IIndicatorContext context) {
       recentInstrument = context.getInstrument();
       console = context.getConsole();
        indicatorInfo = new IndicatorInfo("Absolute_Range3", "Range indicator", "LAB", false, false, true, 1, 0, 3);
        inputParameterInfos = new InputParameterInfo[] {
                new InputParameterInfo("Price", InputParameterInfo.Type.PRICE)
        };
        outputParameterInfos = new OutputParameterInfo[] {
            new OutputParameterInfo("Up", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.HISTOGRAM)
                {{
                     setColor(Color.GREEN);
                }},
            new OutputParameterInfo("Down", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.HISTOGRAM)
                {{
                     setColor(Color.RED);
                }},
            new OutputParameterInfo("Side", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.HISTOGRAM)
                {{
                     setColor(Color.BLUE);
                }}               
        };
    }

    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);
        }

        int i = 0;
        Double recentDiff = null;
        for(i=startIndex+1; i <= endIndex; i++){
           recentDiff = inputs[0][0][i] - inputs[0][1][i];
           //recentDiff = recentDiff * Math.pow(10, recentInstrument.getPipScale());
           recentDiff = recentDiff * 10000;
           console.getOut().println("recentInstrument: " + recentInstrument);
           if (recentInstrument != null) {
              console.getOut().println("recentInstrument.getPipScale(): " + recentInstrument.getPipScale());
              console.getOut().println("Math.pow(10, recentInstrument.getPipScale()): " + Math.pow(10, recentInstrument.getPipScale()));
           }
           if (recentDiff < 0) {
              recentDiff = -recentDiff;
           }
            if (inputs[0][1][i] > inputs[0][0][i]) {
                outputs[0][i] = recentDiff;
            }
            else if (inputs[0][1][i] < inputs[0][0][i]) {
                outputs[1][i] = recentDiff;
            }
            else {
                outputs[2][i] = recentDiff;
            }
        }
        return new IndicatorResult(startIndex, i);
    }


    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

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

    public int getLookback() {
        return 0;
    }

    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 OptInputParameterInfo getOptInputParameterInfo(int index) {
        if (index <= optInputParameterInfos.length) {
            return optInputParameterInfos[index];
        }
        return null;
    }

    public void setOptInputParameter(int index, Object value) {

    }

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

    public int getLookforward() {
        return 0;
    }
}


 
 Post subject: Re: IIndicator - Instrument is always null Post rating: 0   New post Posted: Wed 22 Sep, 2010, 08:30 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hi,
it's because instrument is not initialize properly.
Please consider these two links:
You need to initialize your indicator in IIndicator.calculate or IIndicator.setInputParameter method.


 

Jump to:  

cron
  © 1998-2025 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