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 PullbackToProfit ptp; 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()); } 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); if (ptp == null || !ptp.replaces(result)) ptp(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 ptp == null ? "Unstarted PTP analysis" : str(ptp); } }