Add long exit (LX) code

This commit is contained in:
2018-11-14 21:14:31 -08:00
parent 7680ed81a2
commit d83b1da903
2 changed files with 51 additions and 0 deletions

View File

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

View File

@ -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"));
});
}
/**