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.

KAMA-indicator
 Post subject: KAMA-indicator Post rating: 0   New post Posted: Sun 11 Sep, 2011, 13:44 

User rating: 0
Joined: Wed 07 Sep, 2011, 12:10
Posts: 4
Location: DE
Dear Support,

I thank you for your support.

I have additionally programmed another KAMA-indicator for which I can change the 'fastestPeriod' and 'slowestPeriod' (at Dukascopys KAMA I can only modify the 'time period'). Now I wanted to ask if maybe the code can be optimized:

/**
 * KAMA - INDICATOR
 */
package jforex.indicators;

import com.dukascopy.api.indicators.*;
import com.dukascopy.api.IConsole;

public class myKama implements IIndicator {
  private IndicatorInfo indicatorInfo;
   private InputParameterInfo[] inputParameterInfos;
   private OptInputParameterInfo[] optInputParameterInfos;
   private OutputParameterInfo[] outputParameterInfos;
   private double[][] inputs = new double[1][];
   private int timePeriod = 40;
   private int fastestPeriod = 2;
   private int slowestPeriod = 4;
   private double[][] outputs = new double[1][];
   private IIndicatorContext context;
   private IConsole console;
   
   public void onStart(IIndicatorContext context) {
            this.console = context.getConsole();
        indicatorInfo = new IndicatorInfo("myKama", "myKama Indicator", "My indicators", true, false, false, 1, 3, 1);
        inputParameterInfos = new InputParameterInfo[] { new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE) };
        optInputParameterInfos = new OptInputParameterInfo[] {
            new OptInputParameterInfo("Time Period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(40, 1, 100, 1)),
            new OptInputParameterInfo("fastest Period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(2, 1, 20, 1)),
            new OptInputParameterInfo("slowest Period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(4, 1, 40, 1))
        }; 
        outputParameterInfos = new OutputParameterInfo[] { new OutputParameterInfo("myKama line", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) };
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        if (startIndex - getLookback() < 0)
            startIndex -= startIndex - getLookback();

        /* FORMULA:
         * er(t) = abs(close(t) - close(t-n)) / sum(n)(abs(close(t - n) - close(t - n - 1)))
         * sc(t) = (er(t) * (2 / (fp + 1) - 2 / (sp + 1)) + 2 / (sp + 1))^2
         * kama(t) = sc(t) * (close(t) - kama(t-1)) + kama(t-1)
         *
         * Note: First KAMA values equals a price.
         */
        double direction, volatility = 0;
        //direction is just present over previous price
        direction = Math.abs(inputs[0][endIndex] - inputs[0][endIndex - timePeriod]);
     
        for (int i = 0; i < timePeriod; i++)
            //volatility is the sum of all differences in the past period
            volatility += Math.abs(inputs[0][endIndex - i] - inputs[0][endIndex - i - 1]);

        //elastic ratio
        double er = 1;
        if (volatility != 0)
            er = direction/volatility;

        double fastest = 2 / (fastestPeriod + 1.0);
        double slowest = 2 / (slowestPeriod + 1.0);
       
        //smoothing constant
        double sc = Math.pow(er * (fastest - slowest) + slowest, 2);

        //first KAMA values equals the input-price
        int i, j;
        for (i = startIndex, j = 0; j < timePeriod; i++, j++)
            outputs[0][j] = inputs[0][i];

        for (; i < endIndex + 1; i++, j++)
            outputs[0][j] = sc * (inputs[0][i] - outputs[0][j - 1]) + outputs[0][j - 1];

        return new IndicatorResult(startIndex, outputs[0].length);
    }

      private void print(Object o){    this.console.getOut().println(o);   }

    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) {
        //set Time Periods
        switch (index) {
        case 0:
            timePeriod = (Integer) value;
            break;
        case 1:
            fastestPeriod = (Integer) value;
            break;
        case 2:
            slowestPeriod = (Integer) value;
            break;
        }
    }
 
    public void setOutputParameter(int index, Object array) {
        outputs[index] = (double[]) array;
    }
}


 
 Post subject: Re: KAMA-indicator Post rating: 0   New post Posted: Mon 12 Sep, 2011, 10:40 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The for statement at line 66 is not range safe, please consider changing it, e.g. (note, check if this complies your indicator logic):
for (i = startIndex, j = 0; i<inputs[0].length && j < outputs[0].length; i++, j++)
    outputs[0][j] = inputs[0][i];


 

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