Customize trading hours

Assume that you trade in certain market hours and therefore in onBar you want to know if the time is within time frame of 10:00 - 18:00:

public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    if (period != this.period || instrument != this.instrument){
         return;
    }
    print ( "Is valid time? " + isValidTime(10, 0, 18, 0) );
}

There are two possible implementations of the isValidTime method:

In the following sections it is shown how to use both of the approaches. Also it is shown how to trade on particular weekdays.

Use string operations

private boolean isValidTime(int fromHour, int fromMin, int toHour, int toMin) throws JFException {                      

    boolean result = false;
    long lastTickTime = history.getLastTick(instrument).getTime();
    //you want to work with the date of the last tick - in a case you are back-testing
    String fromStr = gmtSdf.format(lastTickTime).substring(0, 11) + String.valueOf(fromHour)+":"+String.valueOf(fromMin) + ":00";
    String toStr = gmtSdf.format(lastTickTime).substring(0, 11) + String.valueOf(toHour)+":"+String.valueOf(toMin) + ":00";
    try {
        long from = gmtSdf.parse(fromStr).getTime();
        long to = gmtSdf.parse(toStr).getTime();

        print(String.format("calendar: %s - %s last tick: %s", gmtSdf.format(from), gmtSdf.format(to), gmtSdf.format(lastTickTime)));
        result = lastTickTime > from  && lastTickTime < to;

    } catch (ParseException e) {
        e.printStackTrace();
    }

    return result;
}

CheckMarketHoursSdf.java

Use Calendar

//use of calendar
private boolean isValidTime(int fromHour, int fromMin, int toHour, int toMin) throws JFException {

    long lastTickTime = history.getLastTick(instrument).getTime();
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
    //you want to work with the date of the last tick - in a case you are back-testing
    calendar.setTimeInMillis(lastTickTime);
    calendar.set(Calendar.HOUR_OF_DAY, fromHour);
    calendar.set(Calendar.MINUTE, fromMin);
    calendar.set(Calendar.SECOND, 0);
    long from = calendar.getTimeInMillis();

    calendar.setTimeZone(TimeZone.getTimeZone("GMT"));          
    calendar.setTimeInMillis(lastTickTime);
    calendar.set(Calendar.HOUR_OF_DAY, toHour);
    calendar.set(Calendar.MINUTE, toMin);
    calendar.set(Calendar.SECOND, 0);
    long to = calendar.getTimeInMillis();

    print(String.format("calendar: %s - %s last tick: %s", gmtSdf.format(from), gmtSdf.format(to), gmtSdf.format(lastTickTime)));

    boolean result = lastTickTime > from  && lastTickTime < to;
    return result;
}

CheckMarketHoursCalendar.java

Use Calendar weekdays

Consider now that you want to trade only on Tuesdays and Thursdays, i.e. you want to call the method in the following manner:

boolean isValid = isValidTime(10, 0, 18, 0, Calendar.TUESDAY, Calendar.THURSDAY);

Consider the implementation of the isValidTime method (the beginning skipped since it matches the previous example):

//use of calendar
private boolean isValidTime(int fromHour, int fromMin, int toHour, int toMin, Integer... days) throws JFException {

    //(..)

    boolean isDayOk = (Arrays.asList(days)).contains(calendar.get(Calendar.DAY_OF_WEEK));
    boolean result = lastTickTime > from  && lastTickTime < to && isDayOk;
    return result;
}

CheckMarketHoursCalendarDay.java

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