Permalink
Browse files

now the ticker service actually does something

  • Loading branch information...
1 parent 9db4732 commit 87570f192a3af40103e2cd8fbd7b8a10e0b67e97 @mikedanese mikedanese committed Dec 30, 2013
Showing with 19 additions and 3 deletions.
  1. +19 −3 src/main/java/com/raskol/tradebot/TickerService.java
View
22 src/main/java/com/raskol/tradebot/TickerService.java
@@ -5,33 +5,49 @@
import com.xeiam.xchange.ExchangeFactory;
import com.xeiam.xchange.bitstamp.BitstampExchange;
import com.xeiam.xchange.currency.Currencies;
+import com.xeiam.xchange.dto.marketdata.Trade;
import com.xeiam.xchange.dto.marketdata.Trades;
import com.xeiam.xchange.service.polling.PollingMarketDataService;
+import java.util.Arrays;
+import java.util.List;
import java.util.concurrent.TimeUnit;
import static java.lang.Math.exp;
public class TickerService extends AbstractScheduledService {
private static final int INTERVAL = 5;
+ public static final int SAMPLE_SIZE = 1024;
private final PollingMarketDataService marketDataService = ExchangeFactory.INSTANCE
.createExchange(BitstampExchange.class.getName()).getPollingMarketDataService();
- private final ExponentiallyDecayingReservoir oneMinute = new ExponentiallyDecayingReservoir(1024, getAlpha(1l, TimeUnit.MINUTES));
- private final ExponentiallyDecayingReservoir oneDay = new ExponentiallyDecayingReservoir(1024, getAlpha(1l, TimeUnit.DAYS));
+ private final ExponentiallyDecayingReservoir oneMinute = new ExponentiallyDecayingReservoir(SAMPLE_SIZE, getAlpha(1, TimeUnit.MINUTES));
+ private final ExponentiallyDecayingReservoir oneHour = new ExponentiallyDecayingReservoir(SAMPLE_SIZE, getAlpha(1, TimeUnit.HOURS));
+ private final ExponentiallyDecayingReservoir oneDay = new ExponentiallyDecayingReservoir(SAMPLE_SIZE, getAlpha(1, TimeUnit.DAYS));
+ private final ExponentiallyDecayingReservoir fiveDays = new ExponentiallyDecayingReservoir(SAMPLE_SIZE, getAlpha(5, TimeUnit.DAYS));
+ private final ExponentiallyDecayingReservoir oneMonth = new ExponentiallyDecayingReservoir(SAMPLE_SIZE, getAlpha(30, TimeUnit.DAYS));
+ private final List<ExponentiallyDecayingReservoir> reservoirs = Arrays.asList(oneMinute, oneHour, oneDay, fiveDays, oneMonth);
@Override
protected void runOneIteration() throws Exception {
Trades trades = marketDataService.getTrades(Currencies.BTC, Currencies.USD);
+
+ for (Trade trade : trades.getTrades()) {
+ final long time = trade.getTimestamp().getTime();
+ final long price = trade.getPrice().getAmountMajorLong();
+ for (ExponentiallyDecayingReservoir reservoir : reservoirs) {
+ reservoir.update(price, time);
+ }
+ }
}
@Override
protected Scheduler scheduler() {
return Scheduler.newFixedRateSchedule(0, 1, TimeUnit.HOURS);
}
- private double getAlpha(Long duration, TimeUnit timeUnit) {
+ private double getAlpha(Integer duration, TimeUnit timeUnit) {
return 1 - exp(-INTERVAL / timeUnit.toSeconds(duration));
}
}

0 comments on commit 87570f1

Please sign in to comment.