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.

Filters and API
 Post subject: Filters and API Post rating: 0   New post Posted: Sun 01 Apr, 2012, 13:36 
User avatar

User rating: 0
Joined: Tue 17 Jan, 2012, 15:15
Posts: 20
Location: Russian FederationRussian Federation
Hello,

I've made a strategy using api calls without filter and I want to check this strategy with filters now. I've read wiki and found that I need to use Filter.NO_FILTER and Filter.ALL_FLATS for tests. The problem is values that I see on graph (Historical tester) and on log (double[] output) are very different. I spent a lot of time on testing and found that only Ichimoku indicator works as needed:

double[][] ichimokuFlat = indicators.ichimoku(instrument, activePeriod, OfferSide.BID, 9, 26, 52, indFilter,0,askbar.getTime(),ichiLength);

Is ichimoku call correct? I believe that parameters of candles before/after are switched. Please explain if its correct or wrong.

I couldn't get correct values of macd and dmi at all:
                double macdNoFlat[][] = indicators.macd(instrument,activePeriod,OfferSide.BID,AppliedPrice.CLOSE,12,26,9,indFilter,macdLength-1,askbar.getTime(),1);
                double adxNoFlat[][] = indicators.dmi(instrument,activePeriod,OfferSide.BID,14,indFilter,macdLength-1,askbar.getTime(),1);

Please write sample for correct dmi/macd call.


 
 Post subject: Re: Filters and API Post rating: 0   New post Posted: Tue 03 Apr, 2012, 09:26 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please compare the printed the values and the values on the chart's OHLC. Please make sure that the filter property matches the value of your platform preferences.
package jforex.strategies.indicators;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.drawings.IOhlcChartObject;
import com.dukascopy.api.indicators.IIndicator;
import com.dukascopy.api.indicators.OutputParameterInfo;

public class AddToChartIchiMacdAdx2 implements IStrategy {
    private IIndicators indicators;
    private IChart chart;
    private IHistory history;
    private IConsole console;
    private IOhlcChartObject ohlc = null;
   
    @Configurable("")
    public Filter filter = Filter.NO_FILTER;
    @Configurable("")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("")
    public Period period = Period.TEN_MINS;
    @Configurable("")
    public OfferSide side = OfferSide.BID;
    @Configurable("")
    public int candleCount = 10;

    public void onStart(IContext context) throws JFException {
        this.indicators = context.getIndicators();
        this.chart = context.getChart(Instrument.EURUSD);
        this.history = context.getHistory();
        this.console = context.getConsole();
       
        chart = context.getChart(instrument);
        IBar prevBar = history.getBar(instrument, period, side, 1);

        @SuppressWarnings("serial")
        Map<IIndicator, Object[]> indOptInputMap = new HashMap<IIndicator, Object[]>(){{
            put(indicators.getIndicator("ICHIMOKU"), new Object[]{9, 26, 52});
            put(indicators.getIndicator("MACD"), new Object[]{12, 26, 9});
            put(indicators.getIndicator("ADX"), new Object[]{14});
        }};
       
        for(Map.Entry<IIndicator, Object[]> entry : indOptInputMap.entrySet()){
            IIndicator indicator = entry.getKey();
            Object[] optInputs = entry.getValue();
           
            //0. add to chart
            addToChart(chart, indicator, optInputs);
           
            //1. input related params - for all indicators use close prices and BID side, where applicable
            AppliedPrice [] appliedPrices = new AppliedPrice[indicator.getIndicatorInfo().getNumberOfInputs()];
            Arrays.fill(appliedPrices, AppliedPrice.CLOSE);
            OfferSide [] offerSides = new OfferSide[indicator.getIndicatorInfo().getNumberOfInputs()];
            Arrays.fill(offerSides, OfferSide.BID);
           
            //2. calculation
            Object[] indResult = indicators.calculateIndicator(instrument, period, offerSides, indicator.getIndicatorInfo().getName(),
                    appliedPrices, optInputs, filter, candleCount, prevBar.getTime(), 0);
           
            //3. process outputs
            print("indicator outputs: ");
            for(int i = 0; i < indicator.getIndicatorInfo().getNumberOfOutputs(); i++){
                OutputParameterInfo outputInfo = indicator.getOutputParameterInfo(i);
                String outputName = outputInfo.getName();
                String shiftInfo = outputInfo.getShift() == 0 ? "" : " (output shifted by " + outputInfo.getShift()+")";
                //note that the last element is the latest value, the 0th - the oldest one
                if(outputInfo.getType() == OutputParameterInfo.Type.DOUBLE){                   
                    print("%s%s=%s", outputName, shiftInfo, arrayToString((double[])indResult[i])); 
                } else if(outputInfo.getType() == OutputParameterInfo.Type.INT){
                    print("%s%s=%s", outputName, shiftInfo, Arrays.asList((int[])indResult[i])); 
                } else {
                    print("%s type is Object - %s, which means that it is custom drawing output or it needs customized processing.", outputName, indResult[i].getClass());
                }
            }
        }

    }
   
    private void addToChart(IChart chart, IIndicator indicator, Object[] optInputs){
        if(chart == null){
            print("chart not opened!");   
            return;
        }
        if (chart.getSelectedOfferSide() != this.side) {
            print("chart offer side is not " + this.side);
            return;
        } 
        if (chart.getSelectedPeriod() != this.period) {
            print("chart period is not " + this.period);
            return;
        }         

        chart.addIndicator(indicator, optInputs);
       
        for (IChartObject obj : chart.getAll()) {
            if (obj instanceof IOhlcChartObject) {
                ohlc = (IOhlcChartObject) obj;
            }
        }
        if (ohlc == null) {
            ohlc = chart.getChartObjectFactory().createOhlcInformer();
            chart.addToMainChart(ohlc);
        }
        ohlc.setShowIndicatorInfo(true);
    }
   
    private void print(String format, Object...args){
        print(String.format(format, args));
    }

    private void print(Object message) {
        console.getOut().println(message);
    }
   
    public static String arrayToString(double[] arr) {
        String str = "";
        for (int r = 0; r < arr.length; r++) {
            str += String.format("[%s] %.5f; ", r, arr[r]);
        }
        return str;
    }

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



Attachments:
AddToChartIchiMacdAdx2.java [5.42 KiB]
Downloaded 272 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