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.

MACD - Custom Indicator
 Post subject: MACD - Custom Indicator Post rating: 0   New post Posted: Tue 25 May, 2010, 19:23 

User rating: 0
Joined: Wed 05 May, 2010, 03:54
Posts: 2
We are using MACD in our application.
But somehow it gives us wrong data.
Here is our attachment which draws MACD.
Also we have comparision for JForex MACD and Our MACD in attached image file.
We used used 12,26,9 as paramter.

package jforex;

import java.awt.Color;

import com.dukascopy.api.indicators.IIndicator;
import com.dukascopy.api.indicators.IIndicatorContext;
import com.dukascopy.api.indicators.IndicatorInfo;
import com.dukascopy.api.indicators.IndicatorResult;
import com.dukascopy.api.indicators.InputParameterInfo;
import com.dukascopy.api.indicators.OptInputParameterInfo;
import com.dukascopy.api.indicators.OutputParameterInfo;

public class LeggingIndicator implements IIndicator {

   private IIndicator indMACD;

   final private double[][][] inputs = new double[1][][];
   
   final private double[][] outputs = new double[1][];

   /**
    * This Indicator Info
    */
   private IndicatorInfo indicatorInfo;

   /**
    * Input data info
    */
   private InputParameterInfo[] inputParameterInfos;

   /**
    * Output (ie. DOT) paramter info like color
    */
   private OutputParameterInfo[] outputParameterInfos;

   /**
    * Optional Input (time priod, X, etc) parameter info
    */
   private OptInputParameterInfo[] optInputParameterInfos;

   public void onStart(IIndicatorContext context) {
      indMACD = context.getIndicatorsProvider().getIndicator("MACD");

      indicatorInfo = new IndicatorInfo(getName(), getName(), getName(), false, false, true, 1, 0, 1);

      // This is data to be passed in Calculate method
      inputParameterInfos = new InputParameterInfo[] { new InputParameterInfo("Input data", InputParameterInfo.Type.PRICE) };

      // Setting Indicator input paramter
      optInputParameterInfos = new OptInputParameterInfo[] {};

      // Number of dots
      outputParameterInfos = new OutputParameterInfo[] { opi1("Color", Color.BLUE) };
   }

   final private OutputParameterInfo opi1(String title, Color c) {
      OutputParameterInfo opi = new OutputParameterInfo(title, OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE);
      opi.setDrawnByIndicator(false);
      return opi;
   }
   
   /**
    * Calculate Output parameter, Means x,y value for dot
    *
    * Start and End Index is given by JForex. We have calculate dot value for
    * each index between start and end index (Bar)
    */
   public IndicatorResult calculate(int startIndex, int endIndex) {
      if (startIndex - getLookback() < 0) {
         startIndex -= startIndex - getLookback();
      }
      if (startIndex > endIndex) { return new IndicatorResult(0, 0); }

      double[][] macdOut = calculateMacd(indMACD, inputs[0][1], 12, 26, 9, startIndex, endIndex);

      int i, j;
      for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
         // Inputs: 0 open, 1 close, 2 high, 3 low, 4 volume

         double macd = macdOut[0][j];
         outputs[0][j] = macd;
      }
      return new IndicatorResult(startIndex, j);
   }

   public double[][] calculateMacd(IIndicator indi, Object inputs, int fastPeriod, int slowPeriod, int signalPeriod, int startIndex, int endIndex) {
      indi.setOptInputParameter(0, fastPeriod);
      indi.setOptInputParameter(1, slowPeriod);
      indi.setOptInputParameter(2, signalPeriod);

      double[] macdA = new double[endIndex - startIndex + 2 + indi.getLookback()];
      double[] macdA2 = new double[endIndex - startIndex + 2 + indi.getLookback()];
      double[] macdA3 = new double[endIndex - startIndex + 2 + indi.getLookback()];

      indi.setInputParameter(0, inputs);
      indi.setOutputParameter(0, macdA);
      indi.setOutputParameter(1, macdA2);
      indi.setOutputParameter(2, macdA3);
      indi.calculate(startIndex, endIndex);
      return new double[][] { macdA, macdA2, macdA3 };
   }
   
   
   final public IndicatorInfo getIndicatorInfo() {
      return indicatorInfo;
   }

   /**
    * used to passs values[like open, close, high, low] to calculate method
    */
   final public InputParameterInfo getInputParameterInfo(int index) {
      if (index <= inputParameterInfos.length) { return inputParameterInfos[index]; }
      return null;
   }

   /**
    * Used by Indicator selector dialog to create component to accept input
    * parameter
    */
   final public OptInputParameterInfo getOptInputParameterInfo(int index) {
      if (index <= optInputParameterInfos.length) { return optInputParameterInfos[index]; }
      return null;
   }

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

   /**
    * Set current values for Input (array of OHCL data)
    */
   final public void setInputParameter(int index, Object array) {
      inputs[index] = (double[][]) array;
   }

   /**
    * Set output parameter
    */
   final public void setOutputParameter(int index, Object array) {
      outputs[index] = (double[]) array;
   }

   public String getName() {
      return "CF-ffff1";
   }

   /**
    * Used while calculating and drawing dot. How much bars needed to calculate
    * current value
    */
   public int getLookback() {
      return 26;
   }

   @Override
   public int getLookforward() {
      return 0;
   }

   /**
    * Set Input parameter
    */
   public void setOptInputParameter(int index, Object value) {
   }
}


Attachments:
File comment: Indicator
LeggingIndicator.java [4.73 KiB]
Downloaded 500 times
File comment: Image
legging back.PNG [96.42 KiB]
Downloaded 618 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: JFOREX-1854 MACD - Custom Indicator Post rating: 0   New post Posted: Wed 30 Jun, 2010, 11:22 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please take a look at the following example:

package jforex;

import java.awt.Color;

import com.dukascopy.api.indicators.IIndicator;
import com.dukascopy.api.indicators.IIndicatorContext;
import com.dukascopy.api.indicators.IndicatorInfo;
import com.dukascopy.api.indicators.IndicatorResult;
import com.dukascopy.api.indicators.InputParameterInfo;
import com.dukascopy.api.indicators.OptInputParameterInfo;
import com.dukascopy.api.indicators.OutputParameterInfo;

public class LeggingIndicator implements IIndicator {

   private IIndicator indMACD;

   final private double[][] inputs = new double[1][];
   
   final private double[][] outputs = new double[1][];

   /**
    * This Indicator Info
    */
   private IndicatorInfo indicatorInfo;

   /**
    * Input data info
    */
   private InputParameterInfo[] inputParameterInfos;

   /**
    * Output (ie. DOT) paramter info like color
    */
   private OutputParameterInfo[] outputParameterInfos;

   /**
    * Optional Input (time priod, X, etc) parameter info
    */
   private OptInputParameterInfo[] optInputParameterInfos;

   public void onStart(IIndicatorContext context) {
      indMACD = context.getIndicatorsProvider().getIndicator("MACD");
        indMACD.setOptInputParameter(0, 12);
        indMACD.setOptInputParameter(1, 29);
        indMACD.setOptInputParameter(2, 9);

        indicatorInfo = new IndicatorInfo(getName(), getName(), getName(), false, false, false, 1, 0, 1);
      // This is data to be passed in Calculate method
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Input data", InputParameterInfo.Type.DOUBLE)};

      // Setting Indicator input paramter
      optInputParameterInfos = new OptInputParameterInfo[] {};

      // Number of dots
      outputParameterInfos = new OutputParameterInfo[] { opi1("Color", Color.BLUE) };
   }

   private OutputParameterInfo opi1(String title, Color c) {
      OutputParameterInfo opi = new OutputParameterInfo(title, OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE);
      opi.setDrawnByIndicator(false);
      return opi;
   }
   
   /**
    * Calculate Output parameter, Means x,y value for dot
    *
    * Start and End Index is given by JForex. We have calculate dot value for
    * each index between start and end index (Bar)
    */
   public IndicatorResult calculate(int startIndex, int endIndex) {
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }

        if (startIndex > endIndex) {
            return new IndicatorResult(0, 0);
        }

        indMACD.setOptInputParameter(0, 12);
        indMACD.setOptInputParameter(1, 29);
        indMACD.setOptInputParameter(2, 9);

        double[] macdA = new double[endIndex + 1 - indMACD.getLookback()];
        double[] macdA2 = new double[endIndex + 1 - indMACD.getLookback()];
        double[] macdA3 = new double[endIndex + 1 - indMACD.getLookback()];
        indMACD.setInputParameter(0, inputs[0]);
        indMACD.setOutputParameter(0, macdA);
        indMACD.setOutputParameter(1, macdA2);
        indMACD.setOutputParameter(2, macdA3);
        IndicatorResult dMACDResult = indMACD.calculate(startIndex, endIndex);

        int i, k;
        for (i = 0, k = dMACDResult.getNumberOfElements(); i < k; i++) {
            outputs[0][i] =  macdA3[i];
        }
        return new IndicatorResult(startIndex, i);
   }


   final public IndicatorInfo getIndicatorInfo() {
      return indicatorInfo;
   }

   /**
    * used to passs values[like open, close, high, low] to calculate method
    */
   final public InputParameterInfo getInputParameterInfo(int index) {
      if (index <= inputParameterInfos.length) { return inputParameterInfos[index]; }
      return null;
   }

   /**
    * Used by Indicator selector dialog to create component to accept input
    * parameter
    */
   final public OptInputParameterInfo getOptInputParameterInfo(int index) {
      if (index <= optInputParameterInfos.length) { return optInputParameterInfos[index]; }
      return null;
   }

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

   /**
    * Set current values for Input (array of OHCL data)
    */
   final public void setInputParameter(int index, Object array) {
      inputs[index] = (double[]) array;
   }

   /**
    * Set output parameter
    */
   final public void setOutputParameter(int index, Object array) {
      outputs[index] = (double[]) array;
   }

   public String getName() {
      return "CF-ffff1";
   }

   /**
    * Used while calculating and drawing dot. How much bars needed to calculate
    * current value
    */
   public int getLookback() {
        return indMACD.getLookback();
   }

   @Override
   public int getLookforward() {
      return 0;
   }

   /**
    * Set Input parameter
    */
   public void setOptInputParameter(int index, Object value) {
   }
}


 

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