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).

1  
persistable sclass TrailingStopJuicer extends StopLossJuicer {
2  
  // juice points to start trailing at
3  
  settableWithVar double trailStart = 1;
4  
  
5  
  // pullback juice points from crest to close position
6  
  settableWithVar double callbackRate = 0.3;
7  
  
8  
  // Highest juice value seen - we start at 0
9  
  settableWithVar double crest = 0;
10  
  
11  
  *(double *trailStart, double *callbackRate) {}
12  
  
13  
  // just for cosmetics - which signal strength
14  
  // we assign when trailStart is hit
15  
  double firstPartOfJourney() { ret 90; }
16  
  
17  
  {
18  
    onCalculatingCloseSignals(signals -> {
19  
      if (juiceValue > crest)
20  
        crest(juiceValue);
21  
        
22  
      var signal = new CloseSignal().createdBy(this);
23  
      double strength;
24  
      S reason;
25  
      if (crest < trailStart) {
26  
        reason = formatDouble1(trailStart) + "% trailing stop";
27  
        strength = max(crest, 0)/trailStart*firstPartOfJourney();
28  
      } else {
29  
        reason = formatDouble1(callbackRate) + "% callback";
30  
        strength =
31  
            callbackRate <= 0 ? 100
32  
          : blend(firstPartOfJourney(), 100, doubleRatio(crest-juiceValue, callbackRate));
33  
      }
34  
      if (strength >= 100)
35  
        reason = "Trailing stop";
36  
      signal.reason(reason);
37  
      signal.strength(strength);
38  
      signals.add(signal);
39  
    });
40  
  }
41  
  
42  
  toString {
43  
    ret commaCombine(
44  
      shortClassName(this),
45  
      "Stop loss: " + formatDouble2(stopLossLimit()),
46  
      "Trail start: " + formatDouble2(trailStart()),
47  
      "Callback rate: " + formatDouble2(callbackRate()),
48  
      "Highest profit seen: " + formatDouble2(crest),
49  
    );
50  
  }
51  
  
52  
  void copyTransientValuesFrom(AbstractJuicer juicer) {
53  
    if (juicer cast selfType)
54  
      crest(juicer.crest);
55  
  }
56  
}

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: 151 / 236
Version history: 5 change(s)
Referenced in: [show references]