sclass G22CandleBasedStrategy extends G22TradingStrategy { settableWithVar double granularity = 1; // candle granularity in minutes settableWithVar TradingCandleMaker candleMaker; // first (usually partial) candle that will be ignored settableWithVar TradingCandle firstCandle; // Last completed candle settableWithVar TradingCandle completedCandle; // A candle has been completed event candleCompleted(TradingCandle candle); event newPrice(double price); // The attached indicators new L indicators; // For testing - feed candles directly void feed(Iterable candles) { fOr (candle : candles) try { currentTime(candle.endTime.toLong()); gotCandle(candle); } finally { afterStep(); } } void gotCandle(TradingCandle candle) { completedCandle(candle); for (indicator : indicators) indicator.feed(candle); candleCompleted(candle); } // Normal operation from ticker void price(double price) { currentPrice = price; if (hasClosedItself()) ret; newPrice(price); //printVars("G22CandleBasedStrategy.price", +price); try { if (candleMaker == null) candleMaker(new TradingCandleMaker().candleLength(granularity)); var completed = candleMaker.completedCandle(); candleMaker.add(price, currentTime()); var candle = candleMaker.currentCandle(); if (firstCandle == null) firstCandle(candle); else if (candle != firstCandle && completed != completedCandle()) gotCandle(completed); } finally { afterStep(); } } void reset :: after { resetFields(this, "candleMaker firstCandle completedCandle"); } /*selfType emptyClone() { var clone = (selfType) super.emptyClone(); resetFields(clone, "indicators"); clone.addIndicators(); ret clone; }*/ L candles() { ret candleMaker().candles(); } void add aka addIndicator(CandleBasedIndicator indicator) { indicators.add(indicator); change(); } // override me void addIndicators() {} }