From 648b14eda936adf8fa7d96993ea383a5c534ec86 Mon Sep 17 00:00:00 2001 From: Seth Ladygo Date: Mon, 23 Jul 2018 01:26:42 -0700 Subject: [PATCH] improve long entry calculation --- epl/trading_system_1.epl | 45 ++++++++++++++++++++++++------- src/main/java/EsperProcessor.java | 8 ++++++ 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/epl/trading_system_1.epl b/epl/trading_system_1.epl index e7c49fb..dac2b53 100644 --- a/epl/trading_system_1.epl +++ b/epl/trading_system_1.epl @@ -123,17 +123,14 @@ insert into P2 select prev(1, low) as low, prev(1, time) as time -- Long entry -- -create schema LongEntryStream as (low double) +-- keep track of the highest OHLC high value from the most recent three +create window MaxHigh3Window#length(1) as (high double) -insert into LongEntryStream - select B1.low as low - from B1#lastevent, B2#lastevent, P1#lastevent, P2#lastevent - where B1.low < B2.low - and P1.low < P2.low - and EPLHelpers.laterThan(B1.time, P1.time) - and EPLHelpers.laterThan(B2.time, P2.time) - and EPLHelpers.laterThan(P1.time, B2.time) +insert into MaxHigh3Window + select max(high) as high from OHLCStream#length(3) +-- LE calc below is translated from: +-- -- LE = C > HHV(High,3) -- and B1 < B2 -- and P1 < P2 @@ -141,11 +138,35 @@ insert into LongEntryStream -- and BT2 > PT2 -- and PT1 > BT2 +create schema LongEntryStream as (current BigDecimal, time org.joda.time.DateTime) + +insert into LongEntryStream + select C.mid as current, C.time as time + from CurrentTickWindow as C, + MaxHigh3Window as T, + B1#lastevent, B2#lastevent, + P1#lastevent, P2#lastevent + where C.mid > T.high + and B1.low < B2.low + and P1.low < P2.low + and EPLHelpers.laterThan(B1.time, P1.time) + and EPLHelpers.laterThan(B2.time, P2.time) + and EPLHelpers.laterThan(P1.time, B2.time) + +create schema LongEntryDistinct as (current BigDecimal, time org.joda.time.DateTime) + +insert into LongEntryDistinct + select le.current as current, le.time as time + from pattern [every-distinct(le.time) le=LongEntryStream] + + -- -- Event logging -- +-- TODO: look into LogSink http://esper.espertech.com/release-7.1.0/esper-reference/html/dataflow.html#dataflow-reference-logsink + create schema LogStream as (stream string, event string) -- Enable logging specific stream events by uncommenting individual @@ -167,4 +188,8 @@ insert into LogStream select 'P1' as stream, EPLHelpers.str(*) as event from P1 insert into LogStream select 'P2' as stream, EPLHelpers.str(*) as event from P2 -insert into LogStream select 'LongEntryStream' as stream, EPLHelpers.str(*) as event from LongEntryStream +-- insert into LogStream select 'MaxHigh3Window' as stream, EPLHelpers.str(*) as event from MaxHigh3Window + +-- insert into LogStream select 'LongEntryStream' as stream, EPLHelpers.str(*) as event from LongEntryStream + +-- insert into LogStream select 'LongEntryDistinct' as stream, EPLHelpers.str(*) as event from LongEntryDistinct diff --git a/src/main/java/EsperProcessor.java b/src/main/java/EsperProcessor.java index 1ad226c..1fdfc69 100644 --- a/src/main/java/EsperProcessor.java +++ b/src/main/java/EsperProcessor.java @@ -59,6 +59,14 @@ public class EsperProcessor implements TickProcessor { // addLogStatement("LogStream"); addLogStreamHandler(); + + // respond to long entry events + addStatement("select * from LongEntryDistinct", + (newData, oldData) -> { + log.debug("Long entry triggered: {} at {}", + newData[0].get("current"), + newData[0].get("time")); + }); } /**