persistable sclass TrailingStopJuicer extends StopLossJuicer { // juice points to start trailing at settableWithVar double trailStart = 1; // pullback juice points from crest to close position settableWithVar double callbackRate = 0.3; // Highest juice value seen - we start at 0 settableWithVar double crest = 0; *(double *trailStart, double *callbackRate) {} // just for cosmetics - which signal strength // we assign when trailStart is hit double firstPartOfJourney() { ret 90; } { onCalculatingCloseSignals(signals -> { if (juiceValue > crest) crest(juiceValue); var signal = new CloseSignal().createdBy(this); double strength; S reason; if (crest < trailStart) { reason = formatDouble1(trailStart) + "% trailing stop"; strength = max(crest, 0)/trailStart*firstPartOfJourney(); } else { reason = formatDouble1(callbackRate) + "% callback"; strength = callbackRate <= 0 ? 100 : blend(firstPartOfJourney(), 100, doubleRatio(crest-juiceValue, callbackRate)); } if (strength >= 100) reason = "Trailing stop"; signal.reason(reason); signal.strength(strength); signals.add(signal); }); } toString { ret commaCombine( shortClassName(this), "Stop loss: " + formatDouble2(stopLossLimit()), "Trail start: " + formatDouble2(trailStart()), "Callback rate: " + formatDouble2(callbackRate()), "Highest profit seen: " + formatDouble2(crest), ); } void copyTransientValuesFrom(AbstractJuicer juicer) { if (juicer cast selfType) crest(juicer.crest); } }