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.

Trouble using PRICE input - can anyone help debug this?
 Post subject: Trouble using PRICE input - can anyone help debug this? Post rating: 0   New post Posted: Sat 19 Mar, 2011, 11:25 

User rating: 0
Joined: Wed 09 Mar, 2011, 10:40
Posts: 8
Trying to write an indicator which uses the Price inputs to draw 3 emas on the chart - ema (period) of price close, price high and price low. Fairly new to both java and the JForex inidcator framework - can't quite figure out what's wrong with the code. Can anyone help, and perhaps explain where I've gone wrong?

package com.dukascopy.indicators;

import com.dukascopy.api.indicators.*;
import com.dukascopy.api.IIndicators;
import java.awt.Color;

/**
 * @author Simon Kaufmann
 *         Date:    Mar 18, 2011
 *         Time:    9:35:00 PM
 */
public class Wave implements IIndicator {
       
    public static final Color LightSkyBlue = new Color(135, 206, 250);
    public static final Color RoyalBlue = new Color(65, 105, 225);
    public static final Color Pink = new Color(255, 192, 203);
    public static final Color MediumOrchid = new Color(186, 85, 211);
    public static final Color PaleGreen = new Color(0x80, 0xFF, 0x80);
    public static final Color IndianRed  = new Color(0xFF, 0x80, 0x80);
    public static final Color DarkYellow = new Color(0x80, 0x80, 0x00);
   
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private int Period = 34;
    private Color emaClose = LightSkyBlue;
    private Color emaHigh = RoyalBlue;
    private Color emaLow = MediumOrchid;
    private double[][] inputs = new double[5][];   // Prices for PRICE type are in the following order: open, close, high, low, volume.
    private double[][] outputs = new double[3][];  // need 3 arrays of double for 3 ema outputs
    private int Lookback = 900;
    private IIndicator ema;
   
   
    public void onStart(IIndicatorContext context)
    {
       
        IIndicatorsProvider indicatorsProvider = context.getIndicatorsProvider();
        ema = indicatorsProvider.getIndicator("EMA");
        indicatorInfo = new IndicatorInfo("WAVE", "Wave Indicator", "Overlay Studies", true, false, true, 1, 1, 3);
        inputParameterInfos = new InputParameterInfo("Input Data", InputParameterInfo.Type.PRICE);
       
/*        optInputParameterInfos = new OptInputParameterInfo[]
        { new OptInputParameterInfo("EMA Period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(34, 1, 100, 1)),
          new OptInputParameterInfo("EMA(High) Color", OptInputParameterInfo.Type.OTHER, new IntegerListDescription(AvailableColors.GREEN.ordinal(), availColorsValues, availColorsNames)),
          new OptInputParameterInfo("EMA(Close) Color", OptInputParameterInfo.Type.OTHER, new IntegerListDescription(AvailableColors.RED.ordinal(), availColorsValues, availColorsNames)),
          new OptInputParameterInfo("EMA(Low) Color", OptInputParameterInfo.Type.OTHER, new IntegerListDescription(AvailableColors.RED.ordinal(), availColorsValues, availColorsNames))             
        }; */
     
        optInputParameterInfos = new OptInputParameterInfo[]
        { new OptInputParameterInfo("EMA Period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(34, 1, 100, 1)) };   
         
        outputParameterInfos = new OutputParameterInfo[]
        { new OutputParameterInfo("EMA(close)", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) {{ setColor(emaClose); }},
          new OutputParameterInfo("EMA(high)", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) {{ setColor(emaHigh); }},
          new OutputParameterInfo("EMA(low)", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) {{ setColor(emaLow); }}
        };
    }

    public IndicatorResult calculate(int startIndex, int endIndex)
    {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
       
        // set input to ema as close prices
        ema.setInputParameter(0, inputs[1]);
 
        //calculate first ema
        ema.setOptInputParameter(0, Period);
        ema.setOutputParameter(0, outputs[0]);
        ema.calculate(startIndex, endIndex);
         
        // set input to ema as high prices
        ema.setInputParameter(0, inputs[2]);
       
        //calculate second ema
        ema.setOptInputParameter(0, Period);
        ema.setOutputParameter(0, outputs[1]);
        ema.calculate(startIndex, endIndex);
 
        // set input to ema as low prices
        ema.setInputParameter(0, inputs[3]);
       
        //calculate third ema
        ema.setOptInputParameter(0, Period);
        ema.setOutputParameter(0, outputs[2]);
       
        return ema.calculate(startIndex, endIndex);

    }

    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

    public InputParameterInfo getInputParameterInfo(int index) {
        return inputParameterInfos;
    }

    public int getLookback() {
        ema.setOptInputParameter(0, Period);
        return ema.getLookback();

    }

    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:
                Period = (Integer) value;
                ema.setOptInputParameter(0, Period);
                break;           
            default:
                throw new ArrayIndexOutOfBoundsException(index);
        }
    }

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

    public int getLookforward() {
        return 0;
    }
}


 
 Post subject: Re: Trouble using PRICE input - can anyone help debug this? Post rating: 0   New post Posted: Sat 19 Mar, 2011, 16:39 
User avatar

User rating: 2
Joined: Tue 17 May, 2011, 16:35
Posts: 10
Location: ItalyItaly
Hi Simon,
forex451 wrote:
Trying to write an indicator which uses the Price inputs to draw 3 emas on the chart - ema (period) of price close, price high and price low. Fairly new to both java and the JForex inidcator framework - can't quite figure out what's wrong with the code. Can anyone help, and perhaps explain where I've gone wrong?

private double[][] inputs = new double[5][]; // Prices for PRICE type are in the following order: open, close, high, low, volume.


First, you have to declare a three-dimensional array for store InputParameterInfo.Type.PRICE . Cause you'll have to
handle an array of array of double .
Like this:
private double[][][] inputs = new double[5][][]; // Prices for PRICE type are in the following order: open, close, high, low, volume.


Second, get the values from a bidimensional array ( index , [open, close, high, low, volume] )
Like this:
// set input to ema as close prices
ema.setInputParameter(0, inputs[0][1]);


Third, set the inputs array to a bidimensional array ,
you have to cast it in this way:

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


That should be OK then :)
Give it a try and letme know

best,
chriz


 

Jump to:  

  © 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