Created
February 9, 2018 14:47
-
-
Save Tachyon5/cbf759ecfe34c10eb06a273174d0a8ff to your computer and use it in GitHub Desktop.
EMA example: it's only called LSTM for the moment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ch.algotrader.strategy; | |
import java.math.BigDecimal; | |
import org.springframework.stereotype.Component; | |
import org.apache.log4j.LogManager; | |
import org.joda.time.DateTime; | |
import org.joda.time.Period; | |
import ch.algotrader.entity.marketData.BarVO; | |
//import ch.algotrader.entity.marketData.TickVO; | |
import ch.algotrader.entity.trade.MarketOrderVO; | |
import ch.algotrader.entity.trade.MarketOrderVOBuilder; | |
import ch.algotrader.enumeration.Side; | |
import ch.algotrader.service.StrategyService; | |
import ch.algotrader.vo.LifecycleEventVO; | |
import eu.verdelhan.ta4j.TimeSeries; | |
import eu.verdelhan.ta4j.Decimal; | |
import eu.verdelhan.ta4j.Tick; | |
import eu.verdelhan.ta4j.indicators.simple.ClosePriceIndicator; | |
import eu.verdelhan.ta4j.indicators.simple.DifferenceIndicator; | |
import eu.verdelhan.ta4j.indicators.trackers.EMAIndicator; | |
//import java.math.BigDecimal; | |
/** | |
* The main service class of the LSTM Strategy | |
*/ | |
@Component | |
public class LSTMService extends StrategyService { | |
private static final org.apache.log4j.Logger log = LogManager.getLogger(LSTMService.class); | |
private final long accountId = 1; | |
private final long securityId = 247; | |
private final double orderQuantity = 0.011d; | |
private final int emaPeriodShort = 10; | |
private final int emaPeriodLong = 20; | |
private TimeSeries series; | |
private DifferenceIndicator emaDifference; | |
private DateTime lastSeriesEndTime; | |
public LSTMService() { | |
setStrategyName("SERVER"); | |
this.series = new TimeSeries(Period.minutes(1)); | |
this.series.setMaximumTickCount(this.emaPeriodLong); | |
ClosePriceIndicator closePriceIndicator = new ClosePriceIndicator(this.series); | |
EMAIndicator emaShort = new EMAIndicator(closePriceIndicator, this.emaPeriodShort); | |
EMAIndicator emaLong = new EMAIndicator(closePriceIndicator, this.emaPeriodLong); | |
this.emaDifference = new DifferenceIndicator(emaShort, emaLong); | |
} | |
@Override | |
public void onStart(final LifecycleEventVO event) { | |
log.info("EMA Service is initialized"); | |
getSubscriptionService().subscribeMarketDataEvent(getStrategyName(), this.securityId); | |
} | |
@Override | |
public void onBar(BarVO bar) { | |
log.info("Bar looks like " + bar); | |
addBar(bar); | |
int i = this.series.getEnd(); | |
log.info("calculating EMA on series"); | |
Decimal currentValue = this.emaDifference.getValue(i); | |
Decimal previousValue = this.emaDifference.getValue(i - 1); | |
log.info("current Value is " + currentValue); | |
log.info("previous Value was " + previousValue); | |
if (currentValue.isPositive() && previousValue.isNegativeOrZero()) { | |
sendOrder(Side.BUY); | |
log.info("sending buy order"); | |
} else if (currentValue.isNegative() && previousValue.isPositiveOrZero()) { | |
sendOrder(Side.SELL); | |
log.info("sending sell order"); | |
} | |
} | |
private void addBar(BarVO bar) { | |
Tick tick = new Tick(new DateTime(bar.getDateTime().getTime()), | |
bar.getOpen().doubleValue(), | |
bar.getHigh().doubleValue(), | |
bar.getLow().doubleValue(), | |
bar.getClose().doubleValue(), | |
bar.getVol().doubleValue()); | |
// if (tick.getEndTime().isAfter(lastSeriesEndTime)) { | |
this.series.addTick(tick); | |
lastSeriesEndTime = tick.getEndTime(); | |
log.info("ok it worked that time"); | |
// } else { | |
log.info("this isn't working"); | |
lastSeriesEndTime = tick.getEndTime(); | |
// } | |
} | |
private void sendOrder(Side side) { | |
MarketOrderVO order = MarketOrderVOBuilder.create() | |
.setStrategyId(getStrategy().getId()) | |
.setAccountId(this.accountId) | |
.setSecurityId(this.securityId) | |
.setQuantity(new BigDecimal(this.orderQuantity)) | |
.setSide(side) | |
.build(); | |
getOrderService().sendOrder(order); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment