sclass G22CandleBasedStrategy extends G22TradingStrategy { settableWithVar double granularity = 1; // candle granularity in minutes settableWithVar TradingCandleMaker candleMaker; // max number of candles to keep around (only used when first // creating candle maker) settableWithVar int maxCandles = 1000; // 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); // override me L indicators() { ret new L; } // 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 { createCandleMaker(); var completed = candleMaker.completedCandle(); candleMaker.add(price, currentTime()); /*var candle = candleMaker.currentCandle(); /if (firstCandle == null) firstCandle(candle); else if (candle != firstCandle && ... */ if (completed != completedCandle()) gotCandle(completed); } finally { afterStep(); } } S fieldsToReset() { ret lineCombine(super.fieldsToReset(), [[candleMaker firstCandle completedCandle]]); } L candles() { ret candleMaker().candles(); } // How many candles we need to see before the strategy can start making positions (override me) int candlesNeededBeforeOperational() { ret 0; } void createCandleMaker { if (candleMaker == null) candleMaker(new TradingCandleMaker().candleLength(granularity)).maxCandles(maxCandles); } }