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

75
LINES

< > BotCompany Repo | #1036356 // MultiPullbackJuicer - juicer with a different relative pullback for each profit level

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

Libraryless. Click here for Pure Java version (15351L/90K).

1  
persistable sclass MultiPullbackJuicer extends StopLossJuicer {
2  
  sclass Level {
3  
    // Starting at juice value minJuiceValue, keep keepPercentage of the juice.
4  
    // Keep percentages between levels are interpolated linearly.
5  
    // No keeping at all below first minJuiceValue.
6  
    settable double minJuiceValue;
7  
    settable double keepPercentage;
8  
    
9  
    toString { ret "Keep " + keepPercentage + "% starting at " + formatDouble2(minJuiceValue); }
10  
  }
11  
  
12  
  settableWithVar new L<Level> levels;
13  
  
14  
  // Highest juice value seen
15  
  settableWithVar double crest = -infinity();
16  
  
17  
  settableWithVar double currentMaxPullback;
18  
  
19  
  {
20  
    onCalculatingCloseSignals(signals -> {
21  
      if (juiceValue > crest)
22  
        crest(juiceValue);
23  
        
24  
      if (crest < 0) ret;
25  
      
26  
      double keepPercentage = getKeepPercentageForJuiceValue(crest);
27  
      if (keepPercentage > 100) ret;
28  
      
29  
      double toKeep = crest*(keepPercentage/100);
30  
      currentMaxPullback(crest-toKeep);
31  
32  
      var signal = new CloseSignal().createdBy(this).reason("Profit");
33  
      if (currentMaxPullback == 0)
34  
        signal.strength(100);
35  
      else
36  
        signal.strength(doubleRatio(crest-juiceValue, currentMaxPullback)*100);
37  
      signals.add(signal);
38  
    });
39  
  }
40  
  
41  
  double getKeepPercentageForJuiceValue(double juice) {
42  
    for (int i = l(levels)-1; i >= 0; i--) {
43  
      Level level = levels.get(i);
44  
      if (juice < level.minJuiceValue)
45  
        continue;
46  
        
47  
      Level nextLevel = get(levels, i+1);
48  
      if (nextLevel == null)
49  
        ret level.keepPercentage;
50  
      
51  
      ret blend(level.keepPercentage, nextLevel.keepPercentage,
52  
        transformToZeroToOne(juice, level.minJuiceValue, nextLevel.minJuiceValue));
53  
    }
54  
    ret 101;
55  
  }
56  
  
57  
  void addLevel(double minJuiceValue, double keepPercentage) {
58  
    levels.add(new Level().minJuiceValue(minJuiceValue).keepPercentage(keepPercentage));
59  
  }
60  
  
61  
  toString {
62  
    ret commaCombine(
63  
      shortClassName(this),
64  
      !stopLossEnabled ? null : "Stop loss: " + formatDouble2(stopLossLimit()),
65  
      levels,
66  
      crest == -infinity() ? null : "Highest profit seen: " + formatDouble2(crest),
67  
      currentMaxPullback == 0 ? null : "Current max pullback: " + formatDouble2(currentMaxPullback),
68  
    );
69  
  }
70  
  
71  
  void copyTransientValuesFrom(AbstractJuicer juicer) {
72  
    if (juicer cast MultiPullbackJuicer)
73  
      crest(juicer.crest);
74  
  }
75  
}

Author comment

Began life as a copy of #1036355

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036356
Snippet name: MultiPullbackJuicer - juicer with a different relative pullback for each profit level
Eternal ID of this version: #1036356/17
Text MD5: 0130f3191d507a21cd34f85eaa3f740d
Transpilation MD5: 57469b1fdcdb7dcdaabe9cb49d650b37
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-12-16 05:05:58
Source code size: 2455 bytes / 75 lines
Pitched / IR pitched: No / No
Views / Downloads: 164 / 293
Version history: 16 change(s)
Referenced in: [show references]