Improve trading window - can specify times as "hh:mm"
This commit is contained in:
@ -9,11 +9,11 @@
|
|||||||
|
|
||||||
-- The time the trading logic will begin to enter trades.
|
-- The time the trading logic will begin to enter trades.
|
||||||
-- Exiting trades is 24/7.
|
-- 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.
|
-- 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.
|
-- The time frame for OHLC calculation.
|
||||||
-- Example values: '5s', '1m', '1h 30m', '2h', '12h', '1d'.
|
-- 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
|
-- InTradingHours will be set to true when the current time is between
|
||||||
-- StartTime and EndTime.
|
-- TradingStartTime and TradingEndTime.
|
||||||
create variable bool InTradingHours = false
|
create variable bool InTradingHours = false
|
||||||
|
|
||||||
-- Update on each tick
|
-- Update on each tick
|
||||||
-- NOTE: see "timer:within" pattern for possible alternate formulation
|
on TickEvent as t
|
||||||
on TickEvent as t set InTradingHours =
|
set InTradingHours = EPLHelpers.inTimeRange(t.time, TradingStartTime, TradingEndTime)
|
||||||
(EPLHelpers.getHour(t.time) >= StartTimeHour and
|
|
||||||
EPLHelpers.getHour(t.time) < EndTimeHour)
|
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
|
|||||||
@ -1,27 +1,43 @@
|
|||||||
import org.joda.time.DateTime;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.joda.time.DateTimeComparator;
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EPLHelpers contains small helper routines used within epl files.
|
||||||
|
*/
|
||||||
public class EPLHelpers {
|
public class EPLHelpers {
|
||||||
final static Logger log = LoggerFactory.getLogger(EPLHelpers.class);
|
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 a DateTime object for the time. Should be specified in
|
||||||
return date.getHourOfDay();
|
* 24-hour "hh:mm" format.
|
||||||
|
*/
|
||||||
|
public static DateTime parseTime(String time) {
|
||||||
|
return timeFormatter.parseDateTime(time);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A simple toString() wrapper for use in epl. */
|
/** A simple toString() wrapper for use in epl. */
|
||||||
public static String str(Object o) { return o.toString(); }
|
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
|
* Compare two times and return true if the first is earlier than
|
||||||
* the second.
|
* the second.
|
||||||
*/
|
*/
|
||||||
public static boolean earlierThan(DateTime a, DateTime b) {
|
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.
|
* the second.
|
||||||
*/
|
*/
|
||||||
public static boolean laterThan(DateTime a, DateTime b) {
|
public static boolean laterThan(DateTime a, DateTime b) {
|
||||||
return DateTimeComparator.getInstance().compare(a, b) > 0;
|
return timeComparator.compare(a, b) > 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user