Improve trading window - can specify times as "hh:mm"

This commit is contained in:
2018-11-14 18:28:18 -08:00
parent ed88eca0c7
commit 22a4a76561
2 changed files with 30 additions and 15 deletions

View File

@ -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)
--

View File

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