Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

56
LINES

< > BotCompany Repo | #1036560 // TrailingStopJuicer - juicer with trailing stop + separate stop loss

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (15672L/92K).

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);
  }
}

Author comment

Began life as a copy of #1036517

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036560
Snippet name: TrailingStopJuicer - juicer with trailing stop + separate stop loss
Eternal ID of this version: #1036560/6
Text MD5: 733685c8aefee51db6ef9f246c7cd217
Transpilation MD5: 94d3f3a64611d3d3b3106b7ad8fccd3f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-03-22 19:34:00
Source code size: 1749 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 152 / 236
Version history: 5 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)