Market Hours

The IDataService interface provides information about market offline time periods - both in the past and the upcoming ones, which are represented by the ITimeDomain interface. Checking if market is open is essential for trading operations, because they are prohibited while the market is closed. See Customize trading hours if you wish to carry out more elaborate time checking operations.

Get market offline times

Consider retrieving offline periods for the last 3 weeks and the following week:

private IDataService dataService;
private IHistory history;

@Override
public void onStart(IContext context) throws JFException {
    dataService = context.getDataService();
    history = context.getHistory();
    long time = history.getLastTick(Instrument.EURUSD).getTime();
    Set<ITimeDomain> prevOfflines = dataService.getOfflineTimeDomains(
            time - Period.WEEKLY.getInterval() * 3, 
            time
        );
    Set<ITimeDomain> nextOffline = dataService.getOfflineTimeDomains(
            time, 
            time + Period.WEEKLY.getInterval() 
        );

    context.getConsole().getOut().println("Offlines last 3 weeks " +
        prevOfflines + "\n Offlines in the following week: " + nextOffline);
}

MarketOfflineHours.java

Check if market is offline

Consider checking if the market is offline over the previous 7 days and the next 7 days:

private IDataService dataService;
private IHistory history;
private IConsole console;

public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");    

@Override
public void onStart(IContext context) throws JFException {
    dataService = context.getDataService();
    history = context.getHistory();
    console = context.getConsole();
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

    long lastTickTime = history.getLastTick(Instrument.EURUSD).getTime();
    //check from 7 days ago till 7 days in future
    for(int i = -7; i < 7; i++){
        long time = lastTickTime + Period.DAILY.getInterval() * i;
        console.getOut().println(sdf.format(time) + " offline=" + isOffline(time));
    }
}

private boolean isOffline(long time) throws JFException{
    Set<ITimeDomain> offlines = 
        dataService.getOfflineTimeDomains(time - Period.WEEKLY.getInterval(), time + Period.WEEKLY.getInterval());
    for(ITimeDomain offline : offlines){
        if( time > offline.getStart() &&  time < offline.getEnd()){
            return true;
        }
    }
    return false;
}

MarketIsOffline.java

The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.