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.

How to get the average value form custom indicator?
 Post subject: How to get the average value form custom indicator? Post rating: 0   Post Posted: Fri 09 Sep, 2011, 17:17 
User avatar

User rating: 0
Joined: Wed 07 Sep, 2011, 16:30
Posts: 7
Location: China,
Hi, Support,

Through the calculation to get outputs[0][resIndex] = AB[i],the AB[i] is not a standard indicator. How to get the average value of the AB[i] (That is outputs[1][resIndex] )?

Why now the outputs[1][resIndex] are all zero?

Can you help me?

Thank you.

package jforex;

import com.dukascopy.api.indicators.*;

//+===================================================================================+

public class BarFour2 implements IIndicator
{
    public static final int OPEN  = 0;
    public static final int CLOSE = 1;
    public static final int MIN   = 2;
    public static final int MAX   = 3;
    public static final int AVE   = 4;

    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private double[][][] inputs = new double[1][][];
    private double[][] outputs = new double[2][];
   
   
  //+---------------------------------------------------------------------------------+
    public void onStart(IIndicatorContext context)
    {
       
        indicatorInfo = new IndicatorInfo("ABsum", "ABsum", "Custom", false, true, false, 1, 0, 2);
        inputParameterInfos = new InputParameterInfo[]
        {
            new InputParameterInfo("Price", InputParameterInfo.Type.PRICE)
        };

        outputParameterInfos = new OutputParameterInfo[]
        {
          new OutputParameterInfo("Line1", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE),
          new OutputParameterInfo("Line2", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE)
        };
    }
  //+--------------------------------------------------------------------------------+
    public IndicatorResult calculate(int startIndex, int endIndex)
    {
        if (startIndex - getLookback() < 0)
        {
            startIndex -= startIndex - getLookback();
        }
        if (startIndex > endIndex)
        {
            return new IndicatorResult(0, 0);
        }

        double[] AB = new double[endIndex - startIndex + 1];
        int resIndex = 0;
        for (int i = startIndex; i <= endIndex; i++, resIndex++)
        {
            double[] candle = new double[4];
           
            candle[OPEN]  = inputs[0][0][i];
            candle[CLOSE] = inputs[0][1][i] ;
            candle[MAX]   = inputs[0][2][i];
            candle[MIN]   = inputs[0][3][i];
           
            if(i >=1 )
            {               
              AB[i] = inputs[0][2][i] + inputs[0][2][i-1] + inputs[0][0][i] + inputs[0][0][i-1] + inputs[0][3][i] + inputs[0][3][i-1] + inputs[0][2][i] + inputs[0][2][i-1];
            }
            else
            {
                AB[i] = 0;
            }           
            outputs[0][resIndex] = AB[i];
 
            //???????????????????????????????????????????????????????????+           
            double x3 = 0;         
            for(int j =i; j<=i-2; j--)
            {
                x3 += outputs[0][j];
            }         
            outputs[1][resIndex] = x3/3;
            //???????????????????????????????????????????????????????????+

        }
        return new IndicatorResult(startIndex, resIndex);
    }
  //+--------------------------------------------------------------------------------+
    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[0] = (double[][]) array;
    }

  //+--------------------------------------------------------------------------------+
    public OptInputParameterInfo getOptInputParameterInfo(int 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: How to get the average value form custom indicator? Post rating: 0   Post Posted: Mon 12 Sep, 2011, 08:53 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please find comments in the source code:
package jforex.indicators;

import java.text.DecimalFormat;

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

//+===================================================================================+

public class BarFour2 implements IIndicator {
   public static final int OPEN = 0;
   public static final int CLOSE = 1;
   public static final int MIN = 2;
   public static final int MAX = 3;
   public static final int AVE = 4;

   private IndicatorInfo indicatorInfo;
   private InputParameterInfo[] inputParameterInfos;
   private OutputParameterInfo[] outputParameterInfos;
   private double[][][] inputs = new double[1][][];
   private double[][] outputs = new double[2][];

   private IConsole console;

   // +---------------------------------------------------------------------------------+
   public void onStart(IIndicatorContext context) {

      indicatorInfo = new IndicatorInfo("ABsum", "ABsum", "Custom", false, true, false, 1, 0, 2);
      inputParameterInfos = new InputParameterInfo[] { new InputParameterInfo("Price", InputParameterInfo.Type.PRICE) };

      outputParameterInfos = new OutputParameterInfo[] {
            new OutputParameterInfo("Line1", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE),
            new OutputParameterInfo("Line2", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) };
      console = context.getConsole();
   }

   // +--------------------------------------------------------------------------------+
   public IndicatorResult calculate(int startIndex, int endIndex) {

      if (startIndex - getLookback() < 0) {
         startIndex -= startIndex - getLookback();
      }
      if (startIndex > endIndex) {
         return new IndicatorResult(0, 0);
      }
      
      // Support: log some values if there are less than 200 inputs (to avoid this affecting the performance)
      if (inputs[0].length < 200) {
         print("startIndex=" + startIndex + " endIndex=" + endIndex + " inputs[0] = " + arrayToString(inputs[0]) + " outputs[0] = "
            + arrayToString(outputs[0]));
      }

      double[] AB = new double[endIndex - startIndex + 1];
      int resIndex = 0;
      for (int i = startIndex; i <= endIndex; i++, resIndex++) {
         double[] candle = new double[4];

         candle[OPEN] = inputs[0][0][i];
         candle[CLOSE] = inputs[0][1][i];
         candle[MAX] = inputs[0][2][i];
         candle[MIN] = inputs[0][3][i];

         //Support: AB[i] changed to AB[resIndex], also see getLookback method
         if (i >= 1) {
            AB[resIndex] = inputs[0][2][i] + inputs[0][2][i - 1] + inputs[0][0][i] + inputs[0][0][i - 1] + inputs[0][3][i]
                  + inputs[0][3][i - 1] + inputs[0][2][i] + inputs[0][2][i - 1];
         } else {
            AB[resIndex] = 0;
         }
         outputs[0][resIndex] = AB[resIndex];

         // ???????????????????????????????????????????????????????????+
         double x3 = 0;

         // Support: execution never steps in here, consider i=4 => j=4 => j<=4-2 = false
         for (int j = i; j <= i - 2; j--) {
            x3 += outputs[0][j];
         }
         //Support: added double cast to 3
         outputs[1][resIndex] = x3 / 3d;

         // Support: log some values if there are less than 200 outputs (to avoid this affecting the performance)
         if (outputs[1].length < 200) {
            print("i=" + i + " resIndex=" + resIndex + " AB=" + arrayToString(AB) + " x3=" + x3 + " outputs[0]: "
                  + arrayToString(outputs[0]) + " outputs[1]: " + arrayToString(outputs[1]));
         }
         // ???????????????????????????????????????????????????????????+

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

   // Support: some helper functions
   public static String arrayToString(double[] arr) {
      String str = "";
      for (int r = 0; r < arr.length; r++) {
         str += "[" + r + "] " + (new DecimalFormat("0.00000")).format(arr[r]) + "; ";
      }
      return str;
   }

   public static String arrayToString(double[][] arr) {
      String str = "";
      for (int r = 0; r < arr.length; r++) {
         for (int c = 0; c < arr[r].length; c++) {
            str += " " + "[" + r + "][" + c + "] " + (new DecimalFormat("0.00000")).format(arr[r][c]);
         }
         str += "; ";
      }
      return str;
   }

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

   // +--------------------------------------------------------------------------------+
   //Support: presumably your indicator needs at least one value back, so that is how you ensure that
   public int getLookback() {
      return 1;
   }

   // +--------------------------------------------------------------------------------+
   public OutputParameterInfo getOutputParameterInfo(int index) {
      if (index <= outputParameterInfos.length) {
         return outputParameterInfos[index];
      }
      return null;
   }

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

   // +--------------------------------------------------------------------------------+
   public OptInputParameterInfo getOptInputParameterInfo(int 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;
   }
}
// +////////////////////////////////////////////////////////////////////////////+
See more on loockback here:
https://www.dukascopy.com/wiki/index.php ... etLookback


 

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