diff --git a/epl/trading_system_1.epl b/epl/trading_system_1.epl index 01ff8b8..743d7c8 100644 --- a/epl/trading_system_1.epl +++ b/epl/trading_system_1.epl @@ -9,11 +9,11 @@ -- The time the trading logic will begin to enter trades. -- Exiting trades is 24/7. -create constant variable int StartTimeHour = 9 +create constant variable DateTime TradingStartTime = EPLHelpers.parseTime("00:00") --- The time the trading logic will begin to enter trades. +-- The time the trading logic will no longer enter trades. -- Exiting trades is 24/7. -create constant variable int EndTimeHour = 17 +create constant variable DateTime TradingEndTime = EPLHelpers.parseTime("23:59") -- The time frame for OHLC calculation. -- Example values: '5s', '1m', '1h 30m', '2h', '12h', '1d'. @@ -47,14 +47,13 @@ insert into CurrentTickWindow select * from TickEvent -- -- InTradingHours will be set to true when the current time is between --- StartTime and EndTime. +-- TradingStartTime and TradingEndTime. create variable bool InTradingHours = false -- Update on each tick --- NOTE: see "timer:within" pattern for possible alternate formulation -on TickEvent as t set InTradingHours = - (EPLHelpers.getHour(t.time) >= StartTimeHour and - EPLHelpers.getHour(t.time) < EndTimeHour) +on TickEvent as t + set InTradingHours = EPLHelpers.inTimeRange(t.time, TradingStartTime, TradingEndTime) + -- diff --git a/src/main/java/EPLHelpers.java b/src/main/java/EPLHelpers.java index ac40a71..f40af94 100644 --- a/src/main/java/EPLHelpers.java +++ b/src/main/java/EPLHelpers.java @@ -1,27 +1,43 @@ import org.joda.time.DateTime; +import org.joda.time.DateTimeComparator; +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.joda.time.DateTimeComparator; - +/** + * EPLHelpers contains small helper routines used within epl files. + */ public class EPLHelpers { final static Logger log = LoggerFactory.getLogger(EPLHelpers.class); + private static DateTimeFormatter timeFormatter = DateTimeFormat.forPattern("HH:mm"); + private static DateTimeComparator timeComparator = DateTimeComparator.getTimeOnlyInstance(); - /** Return the hour of the day for the given date. */ - public static int getHour(DateTime date) { - return date.getHourOfDay(); + /** + * Return a DateTime object for the time. Should be specified in + * 24-hour "hh:mm" format. + */ + public static DateTime parseTime(String time) { + return timeFormatter.parseDateTime(time); } /** A simple toString() wrapper for use in epl. */ public static String str(Object o) { return o.toString(); } + /** + * Return true if the time portion of 'now' is between the time + * portion of 'start' and 'end'. + */ + public static boolean inTimeRange(DateTime now, DateTime start, DateTime end) { + return laterThan(now, start) && earlierThan(now, end); + } /** * Compare two times and return true if the first is earlier than * the second. */ public static boolean earlierThan(DateTime a, DateTime b) { - return DateTimeComparator.getInstance().compare(a, b) < 0; + return timeComparator.compare(a, b) < 0; } /** @@ -29,6 +45,6 @@ public class EPLHelpers { * the second. */ public static boolean laterThan(DateTime a, DateTime b) { - return DateTimeComparator.getInstance().compare(a, b) > 0; + return timeComparator.compare(a, b) > 0; } }