Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Submit JForex API bug reports in this forum only.
    Submit Converter issues in Converter Issues.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Can't get bars using chart feed descriptor period if it's four hours or more
 Post subject: Can't get bars using chart feed descriptor period if it's four hours or more Post rating: 0   New post Posted: Fri 16 Mar, 2018, 08:31 

User rating: 0
Joined: Fri 02 Mar, 2018, 04:23
Posts: 12
Location: Cambodia,
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 {
    }
}


 
 Post subject: Re: Can't get bars using chart feed descriptor period if it's four hours or more Post rating: 0   New post Posted: Thu 01 Nov, 2018, 11:26 

User rating: 0
Joined: Fri 02 Mar, 2018, 04:23
Posts: 12
Location: Cambodia,
The code and instructions given in the OP still reproduces this.

Can someone look at it please?


 
 Post subject: Re: Can't get bars using chart feed descriptor period if it's four hours or more Post rating: 0   New post Posted: Thu 01 Nov, 2018, 12:32 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
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.


 
 Post subject: Re: Can't get bars using chart feed descriptor period if it's four hours or more Post rating: 0   New post Posted: Thu 01 Nov, 2018, 12:47 

User rating: 0
Joined: Fri 02 Mar, 2018, 04:23
Posts: 12
Location: Cambodia,
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.


 
 Post subject: Re: Can't get bars using chart feed descriptor period if it's four hours or more Post rating: 0   New post Posted: Thu 01 Nov, 2018, 12:52 

User rating: 0
Joined: Fri 02 Mar, 2018, 04:23
Posts: 12
Location: Cambodia,
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?


 
 Post subject: Re: Can't get bars using chart feed descriptor period if it's four hours or more Post rating: 0   New post Posted: Thu 01 Nov, 2018, 16:55 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
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 417 times
daily.JPG [30.7 KiB]
Downloaded 420 times
4hours.JPG [30.79 KiB]
Downloaded 434 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.
 
 Post subject: Re: Can't get bars using chart feed descriptor period if it's four hours or more Post rating: 0   New post Posted: Thu 01 Nov, 2018, 17:39 

User rating: 0
Joined: Fri 02 Mar, 2018, 04:23
Posts: 12
Location: Cambodia,
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


 
The Best Answer  Post subject: Re: Can't get bars using chart feed descriptor period if it's four hours or more Post rating: 0   New post Posted: Fri 02 Nov, 2018, 17:13 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
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


 
 Post subject: Re: Can't get bars using chart feed descriptor period if it's four hours or more Post rating: 0   New post Posted: Sat 03 Nov, 2018, 01:15 

User rating: 0
Joined: Fri 02 Mar, 2018, 04:23
Posts: 12
Location: Cambodia,
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".


 

Jump to:  

cron
  © 1998-2024 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