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.

IHistory, trouble retrieving currently evolving IBar [getBar() / getBars()]
 Post subject: IHistory, trouble retrieving currently evolving IBar [getBar() / getBars()] Post rating: 0   New post Posted: Mon 03 Apr, 2017, 12:21 
User avatar

User rating: 0
Joined: Sat 11 Feb, 2017, 11:56
Posts: 4
Location: Switzerland,
Hello All

I'm observing a problem trying to obtain the currently evolving IBar instance from IHistory.

More specifically:

getBar(Instrument.USDCHF , Period.ONE_HOUR, OfferSide.ASK, 0)

consistently returns 'null', whereas a call with 'shift' >=1 works just fine.

I also have this problem when retrieving a range of bars.

E.g. when calling:

getBars(IFinancialInstrument financialInstrument, Period period, OfferSide side, Filter filter, long from, long to)

with 'to' set to the time of the currently evolving bar, the returned List simply leaves out the last (i.e., currently evolving) IBar.

It'd be great if someone could get back to me on this.

PS:
- I'm using the latest API version
- I posted this this AM (not sure where though) and it never appeared on the forums (?!)


 
 Post subject: Re: IHistory, trouble retrieving currently evolving IBar [getBar() / getBars()] Post rating: 0   New post Posted: Mon 03 Apr, 2017, 15:37 
User avatar

User rating: 0
Joined: Sat 11 Feb, 2017, 11:56
Posts: 4
Location: Switzerland,
Sorry guys, I managed to make an error while describing the issue.

The basic error description is correct, however period=Period.ONE_HOUR works fine.

It's when I use higher order periods, such as Period.createCustomPeriod(Unit.Minute, 120) where things fall apart for me as described.

Cheers.


 
 Post subject: Re: IHistory, trouble retrieving currently evolving IBar [getBar() / getBars()] Post rating: 0   New post Posted: Tue 04 Apr, 2017, 14:17 
JForex Master
User avatar

User rating:
Joined: Wed 16 Sep, 2009, 18:23
Posts: 1054
Location: Geneva, Switzerland
When instrument is subscribed (e.g. IContext.setSubscribedInstruments(Set<Instrument> instruments)), then we subscribe to a default set of periods:
Period.TICK
Period.TEN_SECS
Period.ONE_MIN
Period.FIVE_MINS
Period.TEN_MINS
Period.FIFTEEN_MINS
Period.THIRTY_MINS
Period.ONE_HOUR
Period.FOUR_HOURS
Period.DAILY
Period.WEEKLY
Period.MONTHLY

If one wants to use custom periods, then he needs to subscribe to these periods, example:

IContext.subscribeToBarsFeed(instrument, customPeriod, OfferSide.ASK, listener);


Try the attached sample strategy (PeriodStrategy.java), the output should be similar to this:

2017-04-04 14:07:25   (Subscribed) 120 Secs 1491314760000[2017-04-04 14:06:00.000+0000] O: 1.01588 C: 1.01572 H: 1.01588 L: 1.01566 V: 188.68
2017-04-04 14:07:23   (Not subscribed by default) 120 Secs null
2017-04-04 14:07:23   Monthly  1491004800000[2017-04-01 00:00:00.000+0000] O: 1.01714 C: 1.01572 H: 1.02063 L: 1.01255 V: 173002.78
2017-04-04 14:07:23   Weekly   1491177600000[2017-04-03 00:00:00.000+0000] O: 1.01691 C: 1.01572 H: 1.02063 L: 1.01255 V: 168506.83
2017-04-04 14:07:23   Daily    1491264000000[2017-04-04 00:00:00.000+0000] O: 1.01808 C: 1.01572 H: 1.01916 L: 1.01352 V: 79433.42
2017-04-04 14:07:23   4 Hours  1491307200000[2017-04-04 12:00:00.000+0000] O: 1.01532 C: 1.01572 H: 1.01664 L: 1.01458 V: 15679.43
2017-04-04 14:07:23   Hourly   1491314400000[2017-04-04 14:00:00.000+0000] O: 1.01544 C: 1.01572 H: 1.01592 L: 1.01528 V: 1064.52
2017-04-04 14:07:23   30 Mins  1491314400000[2017-04-04 14:00:00.000+0000] O: 1.01544 C: 1.01572 H: 1.01592 L: 1.01528 V: 1064.52
2017-04-04 14:07:23   15 Mins  1491314400000[2017-04-04 14:00:00.000+0000] O: 1.01544 C: 1.01572 H: 1.01592 L: 1.01528 V: 1064.52
2017-04-04 14:07:23   10 Mins  1491314400000[2017-04-04 14:00:00.000+0000] O: 1.01544 C: 1.01572 H: 1.01592 L: 1.01528 V: 1064.52
2017-04-04 14:07:23   5 Mins   1491314700000[2017-04-04 14:05:00.000+0000] O: 1.0158 C: 1.01572 H: 1.01589 L: 1.01566 V: 280.64
2017-04-04 14:07:23   1 Min    1491314820000[2017-04-04 14:07:00.000+0000] O: 1.01574 C: 1.01572 H: 1.01575 L: 1.01566 V: 54.73
2017-04-04 14:07:23   10 Secs  1491314840000[2017-04-04 14:07:20.000+0000] O: 1.01574 C: 1.01572 H: 1.01574 L: 1.01572 V: 8.75
2017-04-04 14:07:23   Ticks    1491314842669[2017-04-04 14:07:22.669+0000] / 1.01572 / 1.01553


package apiclient2220;

import java.util.HashSet;
import java.util.Set;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.Unit;
import com.dukascopy.api.feed.IBarFeedListener;

public class PeriodStategy implements IStrategy, IBarFeedListener{

   private Instrument instrument = Instrument.AUDCAD;
   
   @Override
   public void onStart(IContext context) throws JFException {
      
      Set<Instrument> instruments = new HashSet<>();
      instruments.add(instrument);
      context.setSubscribedInstruments(instruments, true);
      
      Period[] periods = new Period[]{
            Period.TEN_SECS,
            Period.ONE_MIN,
            Period.FIVE_MINS,
            Period.TEN_MINS,
            Period.FIFTEEN_MINS,
            Period.THIRTY_MINS,
            Period.ONE_HOUR,
            Period.FOUR_HOURS,
            Period.DAILY,
            Period.WEEKLY,
            Period.MONTHLY
      };
      
      //these will be available by default:
      context.getConsole().getInfo().println(String.format("%1$-8s %2$s", Period.TICK, context.getHistory().getTick(instrument, 0)));
      for (Period p : periods){
         context.getConsole().getInfo().println(String.format("%1$-8s %2$s", p, context.getHistory().getBar(instrument, p, OfferSide.ASK, 0)));
      }
      
      //try to load load non-subscribed custom period:
      Period customPeriod = Period.createCustomPeriod(Unit.Second, 120);
      context.getConsole().getInfo().println(String.format("(Not subscribed by default) %1$-8s %2$s", customPeriod, context.getHistory().getBar(instrument, customPeriod, OfferSide.ASK, 0)));
      
      //subscribe custom period:
      context.subscribeToBarsFeed(instrument, customPeriod, OfferSide.ASK, this);
      
      //and print custom in-progress candle:
      context.getConsole().getInfo().println(String.format("(Subscribed) %1$-8s %2$s", customPeriod, context.getHistory().getBar(instrument, customPeriod, OfferSide.ASK, 0)));
   }

   @Override public void onTick(Instrument instrument, ITick tick) throws JFException { }

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

   @Override public void onBar(Instrument instrument, Period period, OfferSide offerSide, IBar bar) { }

}


 
 Post subject: Re: IHistory, trouble retrieving currently evolving IBar [getBar() / getBars()] Post rating: 0   New post Posted: Fri 07 Apr, 2017, 07:21 
User avatar

User rating: 0
Joined: Sat 11 Feb, 2017, 11:56
Posts: 4
Location: Switzerland,
Okay, I got that to work.

Thanks.


 

Jump to:  

  © 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