diff --git a/epl/trading_system_1.epl b/epl/trading_system_1.epl index f64aa54..ec9039f 100644 --- a/epl/trading_system_1.epl +++ b/epl/trading_system_1.epl @@ -25,6 +25,9 @@ create constant variable int OHLCTicks = 100 -- Amount to be traded, measured in units. create constant variable int TradeSize = 10 +-- How large is a pip? +create constant variable BigDecimal PipSize = new BigDecimal(0.0001) + -- How many events to use for simple moving average calculation create constant variable int SMASize = 5 @@ -306,6 +309,40 @@ create schema LongEntryPreEntryBarPrevLow (low double) insert into LongEntryPreEntryBarPrevLow select prev(1, low) as low from LongEntryPreEntryBar#length(2) + +-- +-- Long exit +-- + +-- Long exit event definition +create schema LongExitStream + as (current BigDecimal, + time DateTime, + instrument String, + units int) + +-- The long exit calc below is translated from this entry in the +-- spreadsheet: +-- +-- LX = (LESBC <= 60 and C < MIN(PreEntrybar(Low,1), (LongEntryPrice - 10 pips))) +-- or +-- (LESBC>60 and C<(EntryPrice + 2 pips) + +insert into LongExitStream + select C.mid as current, + C.time as time, + C.instrument as instrument, + 0 - TradeSize as units -- negative for "sell" + from CurrentTick as C, + LongEntryPreEntryBarPrevLow#lastevent as L + where ((LongEntryStopBarCount <= 60 and C.mid < min(L.low, LongEntryPrice - (10 * PipSize))) or + (LongEntryStopBarCount > 60 and C.mid < LongEntryPrice + (2 * PipSize))) + and InLongEntry + +-- Register the long position as closed +on LongExitStream as lx set InLongEntry = false + + -- -- Event logging -- diff --git a/src/main/java/EsperProcessor.java b/src/main/java/EsperProcessor.java index c07fa6b..5a2c186 100644 --- a/src/main/java/EsperProcessor.java +++ b/src/main/java/EsperProcessor.java @@ -61,6 +61,20 @@ public class EsperProcessor implements TickProcessor { newData[0].get("current"), newData[0].get("time")); }); + + // respond to long exit events + addStatement("select * from LongExitStream", + (newData, oldData) -> { + String instrument = (String)newData[0].get("instrument"); + Integer units = (Integer)newData[0].get("units"); + trader.placeOrder(new MarketOrderRequest(instrument, units)); + + log.debug("Long exit triggered: {} of {} at price {} at time {}", + units, + instrument, + newData[0].get("current"), + newData[0].get("time")); + }); } /**