persistable sclass PullbackToProfitAnalysisBase { settable double pullbackLimit = 1.0; settable double adversity = 0.2; settable boolean isShort; settable TickerSequence ticker; settable long startTime; settable L ptpList; settable double openingPrice; double priceToProfit(double price) { var profit = asPercentIncrease(price, openingPrice()); if (isShort()) profit = neg(profit); ret profit-adversity(); } run { if (notNull(ptpList())) ret; if (startTime == 0) startTime = ticker().startTime(); var idx1 = ticker().indexOfTimestamp(startTime()); openingPrice(ticker().getPrice(idx1)); var crest = 0.0; //negativeInfinity var maxPullback = 0.0; ptpList = new L; int n = ticker().size(); for (int idx = idx1; idx < n; idx++) { var price = ticker().getPrice(idx); var time = ticker().getTimestamp(idx); var profit = priceToProfit(price); crest = max(crest, profit); var pullback = crest-profit; maxPullback = max(maxPullback, pullback); if (maxPullback >= pullbackLimit()) { printConcat("Ending at pullback ", maxPullback, " (", formatMinutes(time-startTime()), ")"); break; } var result = new PullbackToProfit().pullback(maxPullback).profit(crest-maxPullback); var prev = last(ptpList()); if (prev != null) if (prev.replaces(result)) continue; else if (result.replaces(prev)) popLast(ptpList); ptpList.add(result .isShort(isShort) .timestamp(time) .startTime(startTime())); } } double maxCleanness() { run(); ret doubleMax(map(ptpList(), ptp -> ptp.cleanness()); } PullbackToProfit cleanestPTP() { run(); ret highestBy(ptpList(), ptp -> ptp.cleanness()); } }