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.

BollingerBands Width Indicator
 Post subject: BollingerBands Width Indicator Post rating: 0   New post Posted: Mon 02 Feb, 2015, 18:13 

User rating: 0
Joined: Fri 04 Apr, 2014, 16:35
Posts: 50
Location: Monaco, Monte-Carlo
Hello,

I am trying to write my own Bollinger Bands indicator that visually shows the band width since it does not seem possible to get this from the supplied BBANDS indicator.

I have looked on the forum and came across this topic feed: (but the link provided by the API support team is no longer available)
https://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=65&t=47617&p=65300&hilit=Bollinger#p65300

I have tried writing my own but am having problems, (I'm finding the indicator creation harder than the strategy stuff!) (e.g Any idea how to set the "optInputParameterInfos" as an MaType???")

I have provided the entire code below, does anyone have any suggestions as to how I can get it to work?

Many thanks

J
package jforex;

import com.dukascopy.api.indicators.*;

public class Indicator implements IIndicator {
    private IIndicator BBANDSWIDTH;
   
   
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private double[][] inputs = new double[1][];
    private int timePeriod = 20;
    private double[][] outputs = new double[1][];
   
    public void onStart(IIndicatorContext context) {
     IIndicatorsProvider indicatorsProvider = context.getIndicatorsProvider();
     BBANDSWIDTH = indicatorsProvider.getIndicator("BBANDS");       
 
     indicatorInfo = new IndicatorInfo("BBANDSWIDTH", "Bollinger Bands Width", "My Indicators",false, false, false, 1, 1, 4);
     inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Price", InputParameterInfo.Type.PRICE)};
     optInputParameterInfos = new OptInputParameterInfo[] {new InputParameterInfo("Time Period", InputParameterInfo.Type.DOUBLE),new InputParameterInfo("Nb Dev Up", InputParameterInfo.Type.DOUBLE),new InputParameterInfo("Nb Dev Down", InputParameterInfo.Type.DOUBLE),new OptInputParameterInfo("MA Type", OptInputParameterInfo.Type.OTHER)};
     outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE,OutputParameterInfo.DrawingStyle.LINE)};
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
        int i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
            /*double value = 0;
            for (int k = timePeriod; k > 0; k--) {
                value += inputs[0][i - k];
            }
            outputs[0][j] = value;*/
        //Bollinger Bands with is taken as: (Upper bol band - lower bol band)/(moving average)
        BBANDSWIDTH.calculate(0);
        double UpperBand = BBANDSWIDTH[0];
        double BBandsMA = BBANDSWIDTH[1];
        double LowerBand = BBANDSWIDTH[2];
        double Bandswidth = (UpperBand - LowerBand)/BBandsMA;
        }
       

        return new IndicatorResult(startIndex, Bandswidth);     
    }

    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

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

    public int getLookback() {
        return timePeriod;
    }

    public int getLookforward() {
        return 0;
    }

    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) {
        timePeriod = (Integer) value;
    }

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


 
 Post subject: Re: BollingerBands Width Indicator Post rating: 0   New post Posted: Tue 03 Feb, 2015, 23:17 

User rating: 0
Joined: Fri 04 Apr, 2014, 16:35
Posts: 50
Location: Monaco, Monte-Carlo
I have further tried to correct the code I originally provided and have got as far as the code supplied below.

Any help/guidance would be very much appreciated.

package jforex;

import com.dukascopy.api.indicators.*;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IIndicators.MaType;
import com.dukascopy.api.indicators.IIndicator;

public class MyIndicator implements IIndicator {
    private IIndicators indicators;
    private IIndicator BBANDSWIDTH;
   
   
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] BBANDSoutputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private double[][] inputs = new double[1][];
    private int timePeriod = 20;
    private double[][] outputs = new double[1][];
   
    public void onStart(IIndicatorContext context) {
     IIndicatorsProvider indicatorsProvider = context.getIndicatorsProvider();
     BBANDSWIDTH = indicatorsProvider.getIndicator("BBANDS");       
 
     indicatorInfo = new IndicatorInfo("BBANDSWIDTH", "Bollinger Bands Width", "My Indicators",false, false, false, 1, 1, 4);
     inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Price", InputParameterInfo.Type.PRICE)};
     optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("Time Period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(2, 2, 100, 1) ),new OptInputParameterInfo("Nb Dev Up", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(2.0, 0.1, 5.0, 0.01,2)),new OptInputParameterInfo("Nb Dev Down", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(2.0, 0.1, 5.0, 0.01,2)),new OptInputParameterInfo("MA Type", OptInputParameterInfo.Type.OTHER, new StringOptInputDescription(IIndicators.MAType[].value()))};
     BBANDSoutputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("Upper BBAND", OutputParameterInfo.Type.DOUBLE),new OutputParameterInfo("BBAND MA", OutputParameterInfo.Type.DOUBLE),new OutputParameterInfo("Lower BBAND", OutputParameterInfo.Type.DOUBLE)};
     outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE,OutputParameterInfo.DrawingStyle.LINE)};
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
        }
           
        int i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
            /*double value = 0;
            for (int k = timePeriod; k > 0; k--) {
                value += inputs[0][i - k];
            }
            outputs[0][j] = value;*/
        //Bollinger Bands with is taken as: (Upper bol band - lower bol band)/(moving average)
        BBANDSWIDTH.setInputParameter(0, inputParameterInfos);
        BBANDSWIDTH.setOptInputParameter(0, optInputParameterInfos);
        BBANDSWIDTH.setOutputParameter(0, BBANDSoutputParameterInfos);
        BBANDSWIDTH.calculate(0,0);
        double dUpperBand = BBANDSWIDTH[0];
        double dBBandsMA = BBANDSWIDTH[1];
        double dLowerBand = BBANDSWIDTH[2];
        double dBandswidth = (dUpperBand - dLowerBand)/dBBandsMA;
        }
       

        return new IndicatorResult(startIndex, dBandswidth);     
   
    }
    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

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

    public int getLookback() {
        return timePeriod;
    }

    public int getLookforward() {
        return 0;
    }

    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) {
        timePeriod = (Integer) value;
    }

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


 
 Post subject: Re: BollingerBands Width Indicator Post rating: 0   New post Posted: Wed 04 Feb, 2015, 16:48 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Hi James,

You can find the platform's indicator sources here:
https://www.dukascopy.com/wiki/#Platform ... urce_files

Hope this helps!


 
 Post subject: Re: BollingerBands Width Indicator Post rating: 0   New post Posted: Wed 04 Feb, 2015, 22:34 

User rating: 0
Joined: Fri 04 Apr, 2014, 16:35
Posts: 50
Location: Monaco, Monte-Carlo
Thanks Tcsabina!

That made it very easy to solve!


 

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