Dukascopy Support Board
http://www.dukascopy.com/swiss/english/forex/jforex/forum/

Can't get bars using chart feed descriptor period if it's four hours or more
http://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=16&t=56383
Page 1 of 1

Author:  mistvak [ Fri 16 Mar, 2018, 08:31 ]
Post subject:  Can't get bars using chart feed descriptor period if it's four hours or more

The below code reproduces this problem. It prints out the current bar found in two different ways:
1. Passing the Period.FOUR_HOURS constant into getBar().
2. Calling getPeriod() on the chart's feed descriptor and passing the result into getBar().

Unexpected behavior: the result of getBar() is often null for the chart period if you set the chart period to four hours or greater (always null in the historical tester). It is never null when using Period.FOUR_HOURS.

I wonder if this has anything to do with another issue I only have when the period is four hours or greater.

There needs to be a EUR/USD chart open before starting the strategy.

package examples;

import java.util.*;
import java.text.*;

import com.dukascopy.api.*;

public class SMASmallExample1 implements IStrategy {
    private IConsole console;
    private IHistory history;
    private IContext context;

    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }
   
    private IChart getChart()
    {
        for (IChart chart : this.context.getCharts())
        {
            if(chart.isAlive() && chart.getInstrument() == Instrument.EURUSD)
            {
                return chart;
            }
        }
       
        return null;
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
        // Get the period that the chart is using.
        IChart chart = this.getChart();
        Period chartPeriod = chart.getFeedDescriptor().getPeriod();
       
        // Get the last bar using the Period.FOUR_HOURS
        IBar fourHourBarUsingConstantPeriod = history.getBar(instrument, Period.FOUR_HOURS, OfferSide.BID, 0);
       
        // Get the last bar using the period from the chart's feed descriptor.
        IBar fourHourBarUsingChartPeriod = history.getBar(instrument, chartPeriod, OfferSide.BID, 0);
       
        this.console.getOut().println("Bar looked up using constant period: " + fourHourBarUsingConstantPeriod);
        this.console.getOut().println("Bar looked up using chart feed descriptor period: " + fourHourBarUsingChartPeriod);
        this.console.getOut().println("Chart feed descriptor period: " + chartPeriod);
        this.console.getOut().println("----------------------------------------------");
    }

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

Author:  mistvak [ Thu 01 Nov, 2018, 11:26 ]
Post subject:  Re: Can't get bars using chart feed descriptor period if it's four hours or more

The code and instructions given in the OP still reproduces this.

Can someone look at it please?

Author:  API Support [ Thu 01 Nov, 2018, 12:32 ]
Post subject:  Re: Can't get bars using chart feed descriptor period if it's four hours or more

IStrategy.onTick() and IStrategy.onBar() callback methods are invoked for every subscribed instrument during strategy execution.
Strategy should filter out these calls for instruments in which it not interested.

IHistory.getBar(Instrument instrument, Period period, OfferSide side, int shift) with shift == 0 returns in-progress candle for specified period.
In-progress candles are formed only for some standard set of periods (with UTC time zone) and for custom periods which are used by charts (with time zone from "Day start time" setting for periods larger than 1H) or subscribed by strategies with IContext.subscribeToFeed(IFeedDescriptor feedDescriptor, IFeedListener feedListener) method (see https://www.dukascopy.com/wiki/en/development/strategy-api/instruments/feeds).

Attached strategy code calls IHistory.getBar() with shift == 0 for each incoming tick of different instruments.
It returns null for all instruments except EUR/USD on 4H period with custom time zone (which is used on chart), because strategy didn't subscribe for corresponding data feed.

Author:  mistvak [ Thu 01 Nov, 2018, 12:47 ]
Post subject:  Re: Can't get bars using chart feed descriptor period if it's four hours or more

API Support wrote:
IStrategy.onTick() and IStrategy.onBar() callback methods are invoked for every subscribed instrument during strategy execution.
Strategy should filter out these calls for instruments in which it not interested.

IHistory.getBar(Instrument instrument, Period period, OfferSide side, int shift) with shift == 0 returns in-progress candle for specified period.
In-progress candles are formed only for some standard set of periods (with UTC time zone) and for custom periods which are used by charts (with time zone from "Day start time" setting for periods larger than 1H) or subscribed by strategies with IContext.subscribeToFeed(IFeedDescriptor feedDescriptor, IFeedListener feedListener) method (see https://www.dukascopy.com/wiki/en/development/strategy-api/instruments/feeds).

Attached strategy code calls IHistory.getBar() with shift == 0 for each incoming tick of different instruments.
It returns null for all instruments except EUR/USD on 4H period with custom time zone (which is used on chart), because strategy didn't subscribe for corresponding data feed.


Thanks for your response. The strategy I'm actually working on that has this problem does not use the onTick function, it subscribes to the four hour data feed (among others). It still has this problem.

In the actual strategy, I have this function:

private static IBar getCurrentBarForChart(Instrument instrument, IChart chart) throws JFException
    {
        IHistory history = Phase4Point4.context.getHistory();
        Period period = chart.getFeedDescriptor().getPeriod();
       
        // HACK: Without this, bar is often null if the chart period is four hours
        if (period.interval == Period.FOUR_HOURS.interval) {
            period = Period.FOUR_HOURS;
        }
       
        IBar bar = history.getBar(instrument, period, Phase4Point4.indicatorOfferSide, 0);
        return bar;
    }


That hack fixes the problem (if the chart period is four hours, it still has the same problem if the chart period is more than four hours, as I haven't yet found a generalized equivalent).

So what I don't get is why can I use Period.FOUR_HOURS and everything works fine, but using the period I get from calling getFeedDescriptor().getPeriod() on a chart with a period of four hours breaks? Shouldn't the behavior be exactly the same?

And what you mentioned in your post doesn't actually describe what I'm seeing with the example code.

You said:

Quote:
It returns null for all instruments except EUR/USD on 4H period with custom time zone (which is used on chart), because strategy didn't subscribe for corresponding data feed.
.

This is not what I'm seeing. What I'm seeing is that there is no problem with null bars until I set the chart period to four hours or greater. If I set the chart period to anything less than four hours, the example code does not show null bars.

Author:  mistvak [ Thu 01 Nov, 2018, 12:52 ]
Post subject:  Re: Can't get bars using chart feed descriptor period if it's four hours or more

I have updated the example code to the following to ignore all instruments but EUR/USD.

package examples;
 
import java.util.*;
import java.text.*;
 
import com.dukascopy.api.*;
 
public class SMASmallExample1 implements IStrategy {
    private IConsole console;
    private IHistory history;
    private IContext context;
 
    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
    }
 
    public void onAccount(IAccount account) throws JFException {
    }
 
    public void onMessage(IMessage message) throws JFException {
    }
 
    public void onStop() throws JFException {
    }
     
    private IChart getChart()
    {
        for (IChart chart : this.context.getCharts())
        {
            if(chart.isAlive() && chart.getInstrument() == Instrument.EURUSD)
            {
                return chart;
            }
        }
         
        return null;
    }
 
    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (instrument != Instrument.EURUSD) {
            return;
        }
       
        // Get the period that the chart is using.
        IChart chart = this.getChart();
        Period chartPeriod = chart.getFeedDescriptor().getPeriod();
         
        // Get the last bar using the Period.FOUR_HOURS
        IBar fourHourBarUsingConstantPeriod = history.getBar(instrument, Period.FOUR_HOURS, OfferSide.BID, 0);
         
        // Get the last bar using the period from the chart's feed descriptor.
        IBar fourHourBarUsingChartPeriod = history.getBar(instrument, chartPeriod, OfferSide.BID, 0);
         
        this.console.getOut().println("Bar looked up using constant period: " + fourHourBarUsingConstantPeriod);
        this.console.getOut().println("Bar looked up using chart feed descriptor period: " + fourHourBarUsingChartPeriod);
        this.console.getOut().println("Chart feed descriptor period: " + chartPeriod);
        this.console.getOut().println("----------------------------------------------");
    }
 
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
}


I see the same behavior. If the chart period is set to four hours or greater, the "Bar looked up using chart feed descriptor period" is null, while the "Bar looked up using constant period" is not. If I set the chart period to anything less than four hours, neither is null. The key thing here that's weird is that history.getBar(instrument, Period.FOUR_HOURS, OfferSide.BID, 0) is non-null, while history.getBar(instrument, chart.getFeedDescriptor().getPeriod(), OfferSide.BID, 0); is null, when the chart is a four hour chart. Why is that?

Author:  API Support [ Thu 01 Nov, 2018, 16:55 ]
Post subject:  Re: Can't get bars using chart feed descriptor period if it's four hours or more

This code sample works correctly.

Image
Image
Image

Null doesn't return from history.getBar() in none of the cases. Update to latest platform version, if problem still remains - provide your console logs.

Attachments:
weekly.JPG [27.73 KiB]
Downloaded 436 times
daily.JPG [30.7 KiB]
Downloaded 445 times
4hours.JPG [30.79 KiB]
Downloaded 456 times

Author:  mistvak [ Thu 01 Nov, 2018, 17:39 ]
Post subject:  Re: Can't get bars using chart feed descriptor period if it's four hours or more

API Support wrote:
This code sample works correctly.

Image
Image
Image

Null doesn't return from history.getBar() in none of the cases. Update to latest platform version, if problem still remains - provide your console logs.


Thanks for your response again. Where do I get the console logs? I believe I am on the latest version. Here's me running the code in the historical tester. The period I get from the chart says it's 4 hours, but trying to get a bar with it behaves differently than using Period.FOUR_HOURS. I expect them to behave the same.

Image

Author:  API Support [ Fri 02 Nov, 2018, 17:13 ]
Post subject:  Re: Can't get bars using chart feed descriptor period if it's four hours or more

Period.FOUR_HOURS - is a jforex default period, it doesn't change and tied to GMT(UTC) timezone. But chart.getFeedDescriptor().getPeriod() may return a custom period depending on your chart settings.
Change chart setting to GMT(UTC) timezone and it should help.
Settings - Preferences - Chart - Day Start Time

Author:  mistvak [ Sat 03 Nov, 2018, 01:15 ]
Post subject:  Re: Can't get bars using chart feed descriptor period if it's four hours or more

API Support wrote:
Period.FOUR_HOURS - is a jforex default period, it doesn't change and tied to GMT(UTC) timezone. But chart.getFeedDescriptor().getPeriod() may return a custom period depending on your chart settings.
Change chart setting to GMT(UTC) timezone and it should help.
Settings - Preferences - Chart - Day Start Time


Thanks for your help, changing the day start time does indeed resolve the issue, so I guess this is not a bug. Marking answered.

I was noticing another thing where when the chart period was 4 hours or more, the most recent indicator value shown in the chart is slightly different from the most recent indicator value calculated in the strategy. I'm guessing the root cause is the same thing.

This page in the SDK documentation is a little out of date: https://www.dukascopy.com/wiki/en/devel ... -checklist

It seems the "Tools" menu has been renamed to "Settings".

  Page 1 of 1