persistable sclass LivePullbackToProfitAnalysis { settable double pullbackLimit = 1.0; settable double adversity = 0.2; settable boolean isShort; settable long startTime; settable double openingPrice; settable double crest = 0; settable double maxPullback; settable L ptpList; double priceToProfit(double price) { var profit = asPercentIncrease(price, openingPrice()); if (isShort()) profit = neg(profit); ret profit-adversity(); } void add aka feed(PricePoint pp) { if (startTime == 0) { startTime(pp.time()); openingPrice(pp.price()); ptpList(new L); } var price = pp.price(); var time = pp.time(); var profit = priceToProfit(price); crest = max(crest, profit); var pullback = crest-profit; maxPullback = max(maxPullback, pullback); if (done()) ret; var result = new PullbackToProfit().pullback(maxPullback).profit(crest-maxPullback); var prev = last(ptpList); if (prev != null) if (prev.replaces(result)) ret; else if (result.replaces(prev)) popLast(ptpList); ptpList.add(result .isShort(isShort) .timestamp(time) .startTime(startTime())); } /*double cleanness() { ret ptp == null ? 0 : ptp.cleanness(); } double profit() { ret ptp == null ? negativeInfinity() : ptp.profit(); }*/ bool done() { ret maxPullback >= pullbackLimit(); } toString { ret startTime == 0 ? "Unstarted PTP analysis" : joinWithComma(ptpList); } double maxCleanness() { ret doubleMax(map(ptpList(), ptp -> ptp.cleanness()); } PullbackToProfit cleanestPTP() { ret highestBy(ptpList(), ptp -> ptp.cleanness()); } }