persistable srecord noeq PriceDigitizer(PriceCells cells) { double lastPrice = Double.NaN; gettable double lastDigitizedPrice = Double.NaN; settable double lastCellLimitTouched = Double.NaN; settable bool verbose; void init(double price) { lastDigitizedPrice = lastCellLimitTouched = lastPrice = price; } // returns new digitized price double digitize(double price) { if (verbose) printVars("digitize", +lastPrice, +price); lastDigitizedPrice = lastCellLimitTouched; if (!isNaN(lastPrice)) { if (price > lastPrice) { double limit = cells.nextCellLimit(lastPrice); if (verbose) print("PriceDigitizer limit: " + limit); if (price >= limit) { // Go up one cell lastCellLimitTouched = limit; // Go up more cells? while (price >= (limit = cells.nextCellLimit(lastCellLimitTouched))) lastCellLimitTouched = limit; } else if (price < lastPrice) { double limit = cells.previousCellLimit(lastPrice); if (verbose) print("PriceDigitizer limit: " + limit); if (price <= limit) { lastCellLimitTouched = limit; // Go down more cells? while (price <= (limit = cells.previousCellLimit(lastCellLimitTouched))) lastCellLimitTouched = limit; } } } lastPrice = price; ret lastCellLimitTouched; } // digitize price without looking at history double digitizeIndividually(double price) { double price2 = cells.nextCellLimit(price); double price1 = cells.previousCellLimit(price2); ret absdiff(price, price1) < absdiff(price, price2) ? price1 : price2; } double digitizedPrice() { ret lastCellLimitTouched; } }