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.

Compare data custom indicator in strategy.
 Post subject: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Fri 18 Nov, 2011, 08:03 
User avatar

User rating: 0
Joined: Thu 25 Aug, 2011, 14:04
Posts: 45
Location: Russian Federation,
Dear Support Team and All!

In my strategy I use custom indicator which implemented in strategy as a class. For create order I need compare data indicator from previous period with data indicator in current period. I know how I can do it with dukascopy.api.indicators, but I don't know how compare data from Object[] calculateIndicator.

Can anybody explain me an give some samples?

Best regards,
vs_trade


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Fri 18 Nov, 2011, 12:01 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
See if you find helpful the following example:
https://www.dukascopy.com/wiki/index.php?title=Use_custom_indicator_in_strategies#Handle_outputs_and_plot_on_chart_custom_indicator


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Mon 21 Nov, 2011, 13:50 
User avatar

User rating: 0
Joined: Thu 25 Aug, 2011, 14:04
Posts: 45
Location: Russian Federation,
Dear Support Team,

Thank you for reply. Knowlage by this link help, but I can't understand follow:

For example I have custom indicator as Object

.......
Object[] indicator = this.indicators.calculateIndicator(selectedInstrument, fixedPeriod, new OfferSide[] {OfferSide.BID}, "indicator",
new IIndicators.AppliedPrice[] {AppliedPrice.CLOSE}, new Object[]{20}, 1);

......

This indicator draw 3 types line (Up, Down and Neutral (straight)). How I can create order when line change from Neutral to Up (Buy) and from Neutral to Down (Sell).

With non custom indicator I compare data in periods use (<,>,&&), but Object no understand this operators.

Please, HELP! :roll:

Best regards,
vs_trade


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Mon 21 Nov, 2011, 15:18 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The example has been updated to include as well the case you need - custom indicator calculation by shift and using the output values.

If you need any further assistance on this, please provide precise details what values do you have and what you want to do with them.


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Wed 23 Nov, 2011, 22:53 
User avatar

User rating: 0
Joined: Thu 25 Aug, 2011, 14:04
Posts: 45
Location: Russian Federation,
Hello Support Team,

Why when I try compiling CustomIndicatorStrategy.java get error message, this sample not work?

2011-11-23 22:47:52 The method registerCustomIndicator(File) in the type IIndicators is not applicable for the arguments (Class<CustomIndicatorStrategy.Indicator>)
2011-11-23 22:47:52 ^^^^^^^^^^^^^^^^^^^^^^^
2011-11-23 22:47:52 indicators.registerCustomIndicator(Indicator.class);
2011-11-23 22:47:52 1. ERROR in /var/folders/61/61nXZzjwHfWQOPlUOCl77U+++TI/-Tmp-/jfxide/tmp/CustomIndicatorStrategy.java (at line 49)

When I change on it

indicators.registerCustomIndicator(CustomIndicatorStrategy.Indicator);

get other error message

22:51:50 CustomIndicatorStrategy.Indicator cannot be resolved
22:51:50 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22:51:50 indicators.registerCustomIndicator(CustomIndicatorStrategy.Indicator);
22:51:50 1. ERROR in /var/folders/61/61nXZzjwHfWQOPlUOCl77U+++TI/-Tmp-/jfxide/tmp/CustomIndicatorStrategy.java (at line 49)
22:51:50 ----------


Best regards,
vs_trade


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Thu 24 Nov, 2011, 10:24 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please try the updated version. And make sure you place the compiled indicator (i.e. Indicator.jfx) in ...\JForex\Strategies\files directory.


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Thu 24 Nov, 2011, 11:47 
User avatar

User rating: 0
Joined: Thu 25 Aug, 2011, 14:04
Posts: 45
Location: Russian Federation,
Hello Support Team,

I download again and compil, but get the same error message


11:43:03 ----------
11:43:03 The method registerCustomIndicator(File) in the type IIndicators is not applicable for the arguments (Class<CustomIndicatorStrategy.Indicator>)
11:43:03 ^^^^^^^^^^^^^^^^^^^^^^^
11:43:03 indicators.registerCustomIndicator(Indicator.class);
11:43:03 1. ERROR in /var/folders/61/61nXZzjwHfWQOPlUOCl77U+++TI/-Tmp-/jfxide/tmp/CustomIndicatorStrategy.java (at line 49)
11:43:03 ----------

Could you check line 49 may be it's not correct what written there?

Best regards,
vs_trade


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Thu 24 Nov, 2011, 12:33 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Which JForex-API version do you use?


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Thu 24 Nov, 2011, 18:06 
User avatar

User rating: 0
Joined: Thu 25 Aug, 2011, 14:04
Posts: 45
Location: Russian Federation,
JForex API ver. 2.6.33


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Fri 25 Nov, 2011, 08:32 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The functionality of registering indicator by class is available with JForex-API 2.6.38 (the current DEMO version). Consider using the first option - registering an indicator from a .jfx file.


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Fri 25 Nov, 2011, 09:10 
User avatar

User rating: 0
Joined: Thu 25 Aug, 2011, 14:04
Posts: 45
Location: Russian Federation,
Hello Support Team,

I have load JForex-API 2.6.38 to ../files
How I can run it?

Thank you.

Best regards,
vs_trade


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Fri 25 Nov, 2011, 09:41 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
vs_trade wrote:
I have load JForex-API 2.6.38 to ../files
How I can run it?
What have you loaded? An indicator? In that case you can use the first part of the example to calculate the indicator. For instance:
package jforex.strategies.indicators.custom;

import java.io.File;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.IUserInterface;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.IIndicators.AppliedPrice;
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.IntegerRangeDescription;
import com.dukascopy.api.indicators.OptInputParameterInfo;
import com.dukascopy.api.indicators.OutputParameterInfo;

public class CustomIndicatorStrategy implements IStrategy {
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   private IContext context;
   private IIndicators indicators;
   private IUserInterface userInterface;
   
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.console = context.getConsole();
      this.history = context.getHistory();
      this.context = context;
      this.indicators = context.getIndicators();
      this.userInterface = context.getUserInterface();
       
      //register custom indicator located in ...\JForex\Strategies\files folder
        indicators.registerCustomIndicator(new File(context.getFilesDir() + System.getProperty("file.separator") + "Indicator.jfx"));
       
        Object[] firstIndicatorValues =  indicators.calculateIndicator(Instrument.EURUSD, Period.ONE_HOUR, new  OfferSide[] {OfferSide.BID}, "EXAMPIND",
              new AppliedPrice[]{AppliedPrice.CLOSE}, new Object[]{4}, 0);
        console.getOut().println("first indicator value: " + ((Object[])firstIndicatorValues)[0]);
   }

   public void onAccount(IAccount account) throws JFException {
   }

   public void onMessage(IMessage message) throws JFException {
   }

   public void onStop() throws JFException {
   }

   public void onTick(Instrument instrument, ITick tick) throws JFException {
   }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
   
}


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Fri 25 Nov, 2011, 09:58 
User avatar

User rating: 0
Joined: Thu 25 Aug, 2011, 14:04
Posts: 45
Location: Russian Federation,
Hello Support Team,

I load JForex-API 2.6.38.jar file locally on my computer and I want to use indicator implemented to the strategy (not separate file). How I can do it?

Best regards,
vs_trade


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Fri 25 Nov, 2011, 18:13 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
vs_trade wrote:
I load JForex-API 2.6.38.jar file locally on my computer and I want to use indicator implemented to the strategy (not separate file). How I can do it?
You cannot use JForex-API of DEMO as a plug-in for LIVE client.


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Mon 28 Nov, 2011, 12:52 
User avatar

User rating: 0
Joined: Thu 25 Aug, 2011, 14:04
Posts: 45
Location: Russian Federation,
Hello Support Team,

When I registrate my indicator and start test strategy, I get follow error exception.

In massages:

java.lang.ArrayIndexOutOfBoundsException: 1
at com.dukascopy.api.impl.ab.b(Unknown Source)
at com.dukascopy.api.impl.ab.a(Unknown Source)
at com.dukascopy.api.impl.ab.calculateIndicator(Unknown Source)
at com.dukascopy.dds2.greed.agent.strategy.tester.al.calculateIndicator(Unknown Source)
at dukascopy.strategies.indicators.TestOboStrategy.onStart(TestOboStrategy.java:56)
at com.dukascopy.dds2.greed.agent.strategy.tester.o.run(Unknown Source)
at com.dukascopy.dds2.greed.agent.strategy.tester.o.ww(Unknown Source)
at com.dukascopy.dds2.greed.actions.StrategyTesterAction.uH(Unknown Source)
at com.dukascopy.dds2.greed.actions.StrategyTesterAction.uh(Unknown Source)
at com.dukascopy.dds2.greed.actions.c.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)

In tester:

com.dukascopy.api.JFException: java.lang.ArrayIndexOutOfBoundsException: 1
at com.dukascopy.api.impl.ab.b(Unknown Source)
at com.dukascopy.api.impl.ab.a(Unknown Source)
at com.dukascopy.api.impl.ab.calculateIndicator(Unknown Source)
at com.dukascopy.dds2.greed.agent.strategy.tester.al.calculateIndicator(Unknown Source)
at dukascopy.strategies.indicators.TestOboStrategy.onStart(TestOboStrategy.java:56)
at com.dukascopy.dds2.greed.agent.strategy.tester.o.run(Unknown Source)
at com.dukascopy.dds2.greed.agent.strategy.tester.o.ww(Unknown Source)
at com.dukascopy.dds2.greed.actions.StrategyTesterAction.uH(Unknown Source)
at com.dukascopy.dds2.greed.actions.StrategyTesterAction.uh(Unknown Source)
at com.dukascopy.dds2.greed.actions.c.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
... 13 more

55 IBar prevBar = history.getBar(selectedInstrument, fixedPeriod, OfferSide.BID, 1);
56 Object[] CustomIndicator = indicators.calculateIndicator(selectedInstrument, fixedPeriod, new OfferSide[] {OfferSide.BID},
"Myindicator", new IIndicators.AppliedPrice[] {AppliedPrice.CLOSE}, new Object[]{20}, indicatorFilter, 2, prevBar.getTime(), 1);
console.getOut().println("fourth indicator value: " + ((Object[])CustomIndicator)[0]);

Where can be problem? Separate indicator function ok, but I can't get array data.

Best regards,
vs_trade


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Mon 28 Nov, 2011, 15:44 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The exception is from indicator, without the indicator source code and the strategy that calls it, it's complicated to detect where the problem is.


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Fri 02 Dec, 2011, 11:23 
User avatar

User rating: 0
Joined: Thu 25 Aug, 2011, 14:04
Posts: 45
Location: Russian Federation,
Hello Support Team,

API Support wrote:
The exception is from indicator, without the indicator source code and the strategy that calls it, it's complicated to detect where the problem is.


Code Strategy:

...

public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.indicators = context.getIndicators();
       
       
        //register custom indicator located in ...\JForex\Strategies\files folder
        indicators.registerCustomIndicator(new File(context.getFilesDir() + System.getProperty("file.separator") + "MyInd.jfx"));
        IBar prevBar = history.getBar(selectedInstrument, fixedPeriod, OfferSide.BID, 1);
        Object[] CustomIndicator = this.indicators.calculateIndicator(selectedInstrument, fixedPeriod, new OfferSide[] {OfferSide.BID},
        "Trend", new IIndicators.AppliedPrice[] {AppliedPrice.CLOSE}, new Object[]{20}, 1);
        console.getOut().println("fourth indicator value: " + ((Object[])CustomIndicator)[0]);

...

Code Indicator:

...

     public class MyInd implements IIndicator  {

    private IndicatorInfo           TrendIndicatorInfo;
    private InputParameterInfo[]    TrendInputParameterInfos;
    private OptInputParameterInfo[] TrendOptInputParameterInfos;
    private OutputParameterInfo[]   TrendoutputParameterInfos;

    private boolean OverChart       = true;
    private boolean OverVolumes     = false;   
    private boolean UnstablePeriod  = false;
   
    // Number of inputs for indicator calculations.
    private int     NumberOfInputs  = 1;
    // Number of parameters like SMA time period.
    private int     NumberOptInputs = 2;
    // Number of calculated values for every element of the input.
    private int     NumberOfOutputs = 1;

    private IConsole Console;

    // Declaration indicator ATR
    private     IIndicator TM1InATR;
    private int TM1PeriodeATR   = 5;

    // Declaration indicator  CCI
    private     IIndicator TM1InCCI;
    private int TM1PeriodeCCI   = 38;

    private double[][][] PTM1Inputs  = new double[1][][];
   
    private Object[][]   OUTGraphics1  = new Object[1][];
   
    private double TTM1Actual = 0;
    private double TTM1Before = 0;
    private int    TTM1Status = 0;

    private static class clData  {
        private  double TPoint = 0;
        private  int    TColor = 0;       
    }
 

    // -------------------------------------------------------------------------

    public void Print (String message) {
        this.Console.getOut( ).println(message);
    }

    // -------------------------------------------------------------------------


    public void onStart(IIndicatorContext context) {


        Console   = context.getConsole();


        TM1InATR = context.getIndicatorsProvider().getIndicator("ATR");
        TM1InCCI = context.getIndicatorsProvider().getIndicator("CCI");

        TM1InATR.setOptInputParameter(0, TM1PeriodeATR);
        TM1InCCI.setOptInputParameter(0, TM1PeriodeCCI);


        TrendIndicatorInfo = new IndicatorInfo("Trend", "Master Trend", "Forex",
              OverChart, OverVolumes, UnstablePeriod, NumberOfInputs, NumberOptInputs, NumberOfOutputs);

        TrendInputParameterInfos = new InputParameterInfo[] {                                       
                                        new InputParameterInfo("TM1 Flux PRICE",  InputParameterInfo.Type.PRICE),
                                    };
       

        TrendOptInputParameterInfos = new OptInputParameterInfo[] {
                                        new OptInputParameterInfo("ATR", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(TM1PeriodeATR ,  2,  20,  1)),
                                        new OptInputParameterInfo("CCI", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(TM1PeriodeCCI, 10, 100, 10))
                                     };
       
        TrendoutputParameterInfos = new OutputParameterInfo[] {           
            new OutputParameterInfo("OUTGraphics1", OutputParameterInfo.Type.OBJECT, OutputParameterInfo.DrawingStyle.LINE)
            {{
                setDrawnByIndicator(true);
            }}
        };
    }

    // -------------------------------------------------------------------------

    public IndicatorResult calculate(int startIndex, int endIndex)
    {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }

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

        double[] PTM1BufferATR = new double [endIndex + 1 - getLookback()];
        double[] PTM1BufferCCI = new double [endIndex + 1 - getLookback()];

        TM1InATR.setOutputParameter(0, PTM1BufferATR);
        TM1InCCI.setOutputParameter(0, PTM1BufferCCI);

        TM1InATR.setInputParameter(0, PTM1Inputs[0]);
        TM1InCCI.setInputParameter(0, PTM1Inputs[0]);

        IndicatorResult TM1ResultATR = TM1InATR.calculate(startIndex, endIndex);
        IndicatorResult TM1ResultCCI = TM1InCCI.calculate(startIndex, endIndex);

        int NumberOfElements = Math.min (TM1ResultATR.getNumberOfElements(), TM1ResultCCI.getNumberOfElements());

        int i, k;


        for (i = 0, k = NumberOfElements; i < k; i++) {
       

            if (PTM1BufferCCI[i] >= 0 ){
                TTM1Actual = PTM1Inputs[0][0][i+getLookback()] - PTM1BufferATR[i];                 
                if (TTM1Actual < TTM1Before) {
                    TTM1Actual = TTM1Before;                           
                } else TTM1Status = 0;
            }
           
            if (PTM1BufferCCI[i] <  0 ){               
                TTM1Actual = PTM1Inputs[0][1][i+getLookback()] + PTM1BufferATR[i];               
                 if (TTM1Actual > TTM1Before) {
                     TTM1Actual = TTM1Before;                                     
                } else TTM1Status = 1;               
            }

            clData[] vaData    = new clData[1];
            vaData[0]          = new clData();
            vaData[0].TPoint   = TTM1Actual; 
            vaData[0].TColor   = TTM1Status; 
            OUTGraphics1[0][i] = vaData;
       
            TTM1Before  = TTM1Actual;

        }

        return new IndicatorResult(startIndex, i);
       
    }

    // -------------------------------------------------------------------------

    public IndicatorInfo getIndicatorInfo()
    {
        return TrendIndicatorInfo;
    }

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

    // -------------------------------------------------------------------------

    public int getLookback()
    {
        return Math.max(TM1InATR.getLookback(),  TM1InCCI.getLookback());
    }

    // -------------------------------------------------------------------------

    public int getLookforward()
    {
        return 0;
    }

    // -------------------------------------------------------------------------

    public OptInputParameterInfo getOptInputParameterInfo(int index)
    {
        if (index <= TrendOptInputParameterInfos.length) {
            return TrendOptInputParameterInfos[index];
        }
        return null;
    }

    // -------------------------------------------------------------------------

    public OutputParameterInfo getOutputParameterInfo(int index)
    {
        if (index <= TrendoutputParameterInfos.length) {
            return TrendoutputParameterInfos[index];
        }
        return null;
    }

    // -------------------------------------------------------------------------

    public void setInputParameter(int index, Object array)
    {
        switch (index) {
            case 0 : {
                PTM1Inputs[0] = (double[][]) array ;
                break;
            }
            case 1 : { // Pour un flux supplementaire
           
                break;
            }
        }   
    }

    // -------------------------------------------------------------------------

    public void setOptInputParameter(int index, Object value)
    {
         switch (index){
            case 0: {  // ATR
                TM1PeriodeATR = (Integer) value;
                TM1InATR.setOptInputParameter(0, TM1PeriodeATR);

                break;
            }

            case 1: {  // CCI
                TM1PeriodeCCI =  (Integer) value;
                TM1InCCI.setOptInputParameter(0, TM1PeriodeCCI);
                break;
            }
        }
    }

    // -------------------------------------------------------------------------

    public void setOutputParameter(int index, Object array)
    {
        switch (index){
            case 0: {
                OUTGraphics1[0] = (Object[]) array;
                break;
            }
            case 1: { 
                // More.....
                break;
            }
        }
       
    }

...


Best regards,
vs_trade


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Mon 05 Dec, 2011, 12:21 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
When we tried to plot the indicator on the chart in JForex client, we received the following messages:
2011-12-05 11:18:17 Cannot enable indicator, isDrawnByIndicator for output parameter with index [0] returns true, while indicator doesn't define drawOutput method
2011-12-05 11:18:17 Please implement IDrawingIndicator interface to define drawOutput metho


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Mon 05 Dec, 2011, 12:57 
User avatar

User rating: 0
Joined: Thu 25 Aug, 2011, 14:04
Posts: 45
Location: Russian Federation,
Hello Support Team,

Code indicator wirth drawOutput are follow:

package jforex;


import com.dukascopy.api.indicators.*;

import com.dukascopy.api.IConsole;

import java.awt.*;
import java.util.*;


public class Myind implements IIndicator , IDrawingIndicator {

   
    public static final Color UP      = new Color(0xc0, 0x00, 0x00);
    public static final Color DOWN    = new Color(0x00, 0x80, 0x00);
    public static final Color NEUTRAL = new Color(0x00, 0x00, 0x00);
   
   
    private IndicatorInfo           TrendIndicatorInfo;
    private InputParameterInfo[]    TrendInputParameterInfos;
    private OptInputParameterInfo[] TrendOptInputParameterInfos;
    private OutputParameterInfo[]   TrendoutputParameterInfos;

    private boolean OverChart       = true;
    private boolean OverVolumes     = false;   
    private boolean UnstablePeriod  = false;
   
    private int     NumberOfInputs  = 1;
    private int     NumberOptInputs = 2;
    private int     NumberOfOutputs = 1;

    private IConsole Console;

    private     IIndicator TM1InATR;
    private int TM1PeriodeATR   = 5;

    private     IIndicator TM1InCCI;
    private int TM1PeriodeCCI   = 38;

    private double[][][] PTM1Inputs  = new double[1][][];
    private Object[][]   OUTGraphics1  = new Object[1][];
   
    private double TTM1Actual = 0;
    private double TTM1Before = 0;
    private int    TTM1Status = 0;

    private static class clData  {
        private  double TPoint = 0;
        private  int    TColor = 0;       
    }
   
    public void Print (String message) {
        this.Console.getOut( ).println(message);
    }

    public void onStart(IIndicatorContext context) {


        Console   = context.getConsole();

        TM1InATR = context.getIndicatorsProvider().getIndicator("ATR");
        TM1InCCI = context.getIndicatorsProvider().getIndicator("CCI");

        TM1InATR.setOptInputParameter(0, TM1PeriodeATR);
        TM1InCCI.setOptInputParameter(0, TM1PeriodeCCI);


        TrendIndicatorInfo = new IndicatorInfo("Trend", "Master Trend", "Forex",
              OverChart, OverVolumes, UnstablePeriod, NumberOfInputs, NumberOptInputs, NumberOfOutputs);

        TrendInputParameterInfos = new InputParameterInfo[] {                                       
                                        new InputParameterInfo("TM1 Flux PRICE",  InputParameterInfo.Type.PRICE),
                                    };
       
        TrendOptInputParameterInfos = new OptInputParameterInfo[] {
                                        new OptInputParameterInfo("ATR", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(TM1PeriodeATR ,  2,  20,  1)),
                                        new OptInputParameterInfo("CCI", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(TM1PeriodeCCI, 10, 100, 10))
                                     };
       
        TrendoutputParameterInfos = new OutputParameterInfo[] {           
            new OutputParameterInfo("OUTGraphics1", OutputParameterInfo.Type.OBJECT, OutputParameterInfo.DrawingStyle.LINE)
            {{
                setDrawnByIndicator(true);
            }}
        };
    }

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

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

        double[] PTM1BufferATR = new double [endIndex + 1 - getLookback()];
        double[] PTM1BufferCCI = new double [endIndex + 1 - getLookback()];

        TM1InATR.setOutputParameter(0, PTM1BufferATR);
        TM1InCCI.setOutputParameter(0, PTM1BufferCCI);

        TM1InATR.setInputParameter(0, PTM1Inputs[0]);
        TM1InCCI.setInputParameter(0, PTM1Inputs[0]);

        IndicatorResult TM1ResultATR = TM1InATR.calculate(startIndex, endIndex);
        IndicatorResult TM1ResultCCI = TM1InCCI.calculate(startIndex, endIndex);

        int NumberOfElements = Math.min (TM1ResultATR.getNumberOfElements(), TM1ResultCCI.getNumberOfElements());

        int i, k;


        for (i = 0, k = NumberOfElements; i < k; i++) {
       

            if (PTM1BufferCCI[i] >= 0 ){
                TTM1Actual = PTM1Inputs[0][0][i+getLookback()] - PTM1BufferATR[i];                 
                if (TTM1Actual < TTM1Before) {
                    TTM1Actual = TTM1Before;                           
                } else TTM1Status = 0;
            }
           
            if (PTM1BufferCCI[i] <  0 ){               
                TTM1Actual = PTM1Inputs[0][1][i+getLookback()] + PTM1BufferATR[i];               
                 if (TTM1Actual > TTM1Before) {
                     TTM1Actual = TTM1Before;                                     
                } else TTM1Status = 1;               
            }

            clData[] vaData    = new clData[1];
            vaData[0]          = new clData();
            vaData[0].TPoint   = TTM1Actual; 
            vaData[0].TColor   = TTM1Status; 
            OUTGraphics1[0][i] = vaData;
       
            TTM1Before  = TTM1Actual;

        }

        return new IndicatorResult(startIndex, i);
       
    }

        public IndicatorInfo getIndicatorInfo()
    {
        return TrendIndicatorInfo;
    }

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

   
    public int getLookback()
    {
        return Math.max(TM1InATR.getLookback(),  TM1InCCI.getLookback());
    }

   
    public int getLookforward()
    {
        return 0;
    }

   
    public OptInputParameterInfo getOptInputParameterInfo(int index)
    {
        if (index <= TrendOptInputParameterInfos.length) {
            return TrendOptInputParameterInfos[index];
        }
        return null;
    }

   
    public OutputParameterInfo getOutputParameterInfo(int index)
    {
        if (index <= TrendoutputParameterInfos.length) {
            return TrendoutputParameterInfos[index];
        }
        return null;
    }

   
    public void setInputParameter(int index, Object array)
    {
        switch (index) {
            case 0 : {
                PTM1Inputs[0] = (double[][]) array ;
                break;
            }
            case 1 : {
           
                break;
            }
        }   
    }

   
    public void setOptInputParameter(int index, Object value)
    {
         switch (index){
            case 0: { 
                TM1PeriodeATR = (Integer) value;
                TM1InATR.setOptInputParameter(0, TM1PeriodeATR);

                break;
            }

            case 1: { 
                TM1PeriodeCCI =  (Integer) value;
                TM1InCCI.setOptInputParameter(0, TM1PeriodeCCI);
                break;
            }
        }
    }

   

    public void setOutputParameter(int index, Object array)
    {
        switch (index){
            case 0: {
                OUTGraphics1[0] = (Object[]) array;
                break;
            }
            case 1: { 
               
                break;
            }
        }
       
    }

   

      public Point drawOutput(Graphics g, int outputIdx, Object values2, Color color, Stroke stroke,
                           IIndicatorDrawingSupport indicatorDrawingSupport, java.util.List<Shape> shapes,
                           Map<Color, java.util.List<Point>> handles)
      {
         
          Object[] values = (Object[]) values2;

          if (values2 != null) {
             
              for (int j = indicatorDrawingSupport.getIndexOfFirstCandleOnScreen(), k =
                        j + indicatorDrawingSupport.getNumberOfCandlesOnScreen() ; j < k; j++) {
               
                  if ((values[j] != null) &&  ( j > 0)) {         
                                         
                      try {                             
                          clData[] Actual   = (clData[]) values[j];
                          clData[] Preced   = (clData[]) values[j-1];
                         
                          int[] xPoints = new int[2];
                          int[] yPoints = new int[2];   
                         
                          xPoints[0] = (int) indicatorDrawingSupport.getMiddleOfCandle(j - 1);
                          xPoints[1] = (int) indicatorDrawingSupport.getMiddleOfCandle(j);
                         
                          yPoints[0] = (int) indicatorDrawingSupport.getYForValue(Preced[0].TPoint);
                          yPoints[1] = (int) indicatorDrawingSupport.getYForValue(Actual[0].TPoint);
                             
                          switch (Actual[0].TColor){
                              case 0: {
                                  g.setColor (DOWN);
                                  break;
                              }
                              case 1: { 
                                  g.setColor (UP);
                                  break;
                              }
                              case 2: { 
                                  g.setColor (NEUTRAL);
                                  break;
                              }
                              default : {                                 
                                  g.setColor (Color.YELLOW);
                                  break;
                              }
                          }
                         
                          g.drawLine(xPoints[0] , yPoints[0] , xPoints[1], yPoints[1]) ;
                          g.drawLine(xPoints[0] , yPoints[0]-1 , xPoints[1], yPoints[1]-1) ;
                       
                                       
                      }
                      catch (Exception e) {
                          Print ("Error : "+e.toString());
                      }
                  }         
              }
           }
           return null;
       } 
   }


Best regards,
vs_trade


 
 Post subject: Re: Compare data custom indicator in strategy. Post rating: 0   New post Posted: Mon 05 Dec, 2011, 17:01 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please find the modified indicator and strategy files in attachments. See comments in source code.


Attachments:
Myind2.java [10.75 KiB]
Downloaded 360 times
MyIndStrategy.java [2.87 KiB]
Downloaded 358 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.
 

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