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.

Getting indicator values from subscribed IFeedDescriptors
 Post subject: Getting indicator values from subscribed IFeedDescriptors Post rating: 0   New post Posted: Wed 10 Oct, 2012, 15:49 
User avatar

User rating: 0
Joined: Thu 16 Jun, 2011, 21:37
Posts: 98
Location: SwitzerlandSwitzerland
Hi everyone,

is it possible to get the current value of an uncompleted IRangeBar or ITickBar through the API? This would be similar to getting the uncompleted bar value of a specified Period through the onTick method. As far as I know the overriden onFeedData method in IFeedListener only returns completed bars of custom feeds. If I try to get the indicator value for a RangeBarFeedDescriptor or TickBarFeedDescriptor in onTick with IIndicators.calculateIndicator I get the following error:

Quote:
14:28:52 problem caught: Passed Time [2012-10-10 14:28:52:209] has to be in interval [2007-03-30 16:01:15:688; 2012-10-10 14:28:51:378]


man thanks,
mm


 
 Post subject: Re: Getting indicator values from subscribed IFeedDescriptors Post rating: 0   New post Posted: Fri 12 Oct, 2012, 12:04 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You can either calculate the values by shift, or if you want to retrieve multiple values, consider calculating them by passing the candler interval and the start time of the current ITimedData feed element. Consider the example strategy (you can switch feeds from strategy parameters, also for chart-strategy value comparison you can check the open chart parameter):
package jforex.feed.test;

import java.util.Arrays;
import java.util.HashSet;

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.drawings.IOhlcChartObject;
import com.dukascopy.api.feed.*;
import com.dukascopy.api.feed.util.*;

public class FeedSmaOnTickTickBarRangeBar implements IStrategy {

    private IConsole console;
    private IIndicators indicators;
    private IHistory history;

    @Configurable("Feed data count for indicator calculation")
    public int dataCount = 5;
    @Configurable("")
    public int smaPeriod = 10;

    @Configurable("feed type")
    public MyFeeds feed = MyFeeds.RANGE_BARS_2;
    @Configurable("open chart")
    public boolean openChart = false;   


    enum MyFeeds {
        RANGE_BARS_2(new RangeBarFeedDescriptor(Instrument.EURUSD, PriceRange.TWO_PIPS, OfferSide.ASK)),
        TICK_BARS_20(new TickBarFeedDescriptor(Instrument.EURUSD, TickBarSize.valueOf(20), OfferSide.ASK));

        private final IFeedDescriptor feedDescriptor;

        MyFeeds(IFeedDescriptor feedDescriptor) {
            this.feedDescriptor = feedDescriptor;
        }

        public IFeedDescriptor getFeedDescriptor() {
            return feedDescriptor;
        }
    }   
   
    private IChart chart;

    @Override
    public void onStart(IContext context) throws JFException {

        console = context.getConsole();
        indicators = context.getIndicators();
        history = context.getHistory();

        IFeedDescriptor feedDescriptor = feed.getFeedDescriptor();
        context.setSubscribedInstruments(new HashSet<Instrument>(Arrays.asList(new Instrument[] { feedDescriptor.getInstrument() })), true);

        if (openChart) {
            chart = context.openChart(feedDescriptor);
            chart.add(indicators.getIndicator("SMA"), new Object[] { smaPeriod });
            IOhlcChartObject ohlc = null;
            for (IChartObject obj : chart.getAll()) {
                if (obj instanceof IOhlcChartObject) {
                    ohlc = (IOhlcChartObject) obj;
                }
            }
            if(ohlc == null){
                ohlc = chart.getChartObjectFactory().createOhlcInformer();
                chart.add(ohlc);
            }
           
            ohlc.setShowIndicatorInfo(true);
                       
        }
       
        print("subscribe to feed=" + feedDescriptor);
        context.subscribeToFeed(feedDescriptor, new IFeedListener() {
            @Override
            public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData) {
                console.getInfo().println("completed " + feedData + " of feed: " + feedDescriptor);
            }
        });

    }
   
    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {       
        if(instrument != feed.getFeedDescriptor().getInstrument()){
            return;
        }
       
        ITimedData feedData = history.getFeedData(feed.getFeedDescriptor(), 0);
        if(feed.getFeedDescriptor().getDataType() == DataType.TICK_BAR && feedData == null){
            //There are 0 ticks in the current tick bar, take the previously finished bar
            feedData = history.getFeedData(feed.getFeedDescriptor(), 1);
        }
        // calculate SMA for the last 3 feed data
        Object[] macdFeed = indicators.calculateIndicator(feed.getFeedDescriptor(), new OfferSide[] { feed.getFeedDescriptor().getOfferSide() }, "SMA",
                new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { smaPeriod },
                dataCount, feedData.getTime(), 0);
        double[] sma =(double[]) macdFeed[0];
        print("sma calculation on feed" + arrayToString(sma));
        print("last bar value: %.5f", sma[dataCount-1]);

    }
   
    private void print(String format, Object... args) {
        print(String.format(format, args));
    }

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

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

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }

    @Override
    public void onMessage(IMessage message) throws JFException {
    }

    @Override
    public void onAccount(IAccount account) throws JFException {
    }

    @Override
    public void onStop() throws JFException {}

}


Attachments:
FeedSmaOnTickTickBarRangeBar.java [4.47 KiB]
Downloaded 282 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:  

  © 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