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

112
LINES

< > BotCompany Repo | #1036614 // NoLoseCloudStrategy - an MA cloud + "no-lose" multi stop loss approach [dev.]

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

Libraryless. Click here for Pure Java version (31877L/204K).

1  
sclass NoLoseCloudStrategy extends G22CandleBasedStrategyWithMultiStopLossJuicer is IntegrityCheckable {
2  
  settableWithVar int fastPeriod = 10; // the "slow" MA
3  
  settableWithVar int slowPeriod = 15;   // the fast MA (for opening)
4  
  settableWithVar int slowestPeriod = 60;   // the fast MA (for opening)
5  
  settableWithVar double cloudThickness = 5;
6  
7  
  // false = only manual openLong() and openShort(),
8  
  // not based on indicators.
9  
  // true = auto-open positions indefinitely depending on
10  
  // indicators.
11  
  settableWithVar bool autoPositions = true;
12  
  
13  
  // temporal fields follow
14  
  
15  
  S fieldsToReset() {
16  
    ret lineCombine(super.fieldsToReset(), [[
17  
      ma1 ma2 ma3 cloudTop movingAverage1_cache movingAverage2_cache movingAverage3_cache
18  
      cloudTop cloudBottom
19  
      candleOverlapWithCloud
20  
      showCloud
21  
      lastShowCloud
22  
      signal
23  
    ]]);
24  
  }
25  
  
26  
  settableWithVar double ma1;
27  
  settableWithVar double ma2;
28  
  settableWithVar double ma3;
29  
  settableWithVar double cloudTop;
30  
  settableWithVar double cloudBottom;
31  
  settableWithVar bool candleOverlapWithCloud;
32  
  settableWithVar bool showCloud;
33  
  settableWithVar bool lastShowCloud;
34  
  settableWithVar int signal; // -1, 0 or 1
35  
  
36  
  bool upTrend() { ret ma1 > ma2; }
37  
  bool slowUpTrend() { ret ma1 > ma3; }
38  
  
39  
  bool trendValid() { ret upTrend() == slowUpTrend(); }
40  
  
41  
  settable transient O visualizer;
42  
  
43  
  double cloudMin() { ret min(cloudTop, cloudBottom); }
44  
  double cloudMax() { ret max(cloudTop, cloudBottom); }
45  
  
46  
  void strategyStep {
47  
    candleOverlapWithCloud(rangesOverlap(
48  
      doubleRange(cloudMin(), cloudMax()),
49  
      doubleRange(completedCandle().low(), completedCandle().high())));
50  
    showCloud(trendValid() && candleOverlapWithCloud());
51  
    
52  
    bool entrySignal = showCloud && !lastShowCloud;
53  
    signal(entrySignal ? upTrend() ? 1 : -1 : 0);
54  
    
55  
    if (signal != 0 && drift() == 0)
56  
      openPosition(signal);
57  
  }
58  
  
59  
  simplyCached MAIndicator movingAverage1() {
60  
    ret new MAIndicator(fastPeriod);
61  
  }
62  
  
63  
  simplyCached MAIndicator movingAverage2() {
64  
    ret new MAIndicator(slowPeriod);
65  
  }
66  
67  
  simplyCached MAIndicator movingAverage3() {
68  
    ret new MAIndicator(slowestPeriod);
69  
  }
70  
71  
  L<CandleBasedIndicator> indicators() {
72  
    ret ll(movingAverage1(), movingAverage2(), movingAverage3());
73  
  }
74  
75  
  {
76  
    granularity(5);
77  
    
78  
    onNewPrice(price -> strategyStep());
79  
    onCandleCompleted(candle -> {
80  
      lastShowCloud(showCloud());
81  
      ma1(movingAverage1()!);
82  
      ma2(movingAverage2()!);
83  
      ma3(movingAverage3()!);
84  
      strategyStep();
85  
    });
86  
  }
87  
  
88  
  LS status() {
89  
    ret listCombine(
90  
      "MA periods: " + fastPeriod + " " + slowPeriod + " " + slowestPeriod,
91  
      "Do: " + (!autoPositions ? "Only manual positions" : commaCombine(stringIf(doLongs(), "Longs"), stringIf(doShorts(), "Shorts"))),
92  
      "Moving averages: " + formatDouble_significant(ma1, 4) + ", " + formatDouble_significant(ma2, 4) + ", " + formatDouble_significant(ma3, 4),
93  
      super.status()
94  
    );
95  
  }
96  
  
97  
  void afterStep :: before {
98  
  }
99  
  
100  
  bool doShorts() { true; }
101  
  bool doLongs() { true; }
102  
  
103  
  int candlesNeededBeforeOperational() { ret intMax(fastPeriod, slowPeriod, slowestPeriod); }
104  
  
105  
  bool usingCells() { false; }
106  
  
107  
  public void integrityCheck {
108  
    movingAverage1().integrityCheck();
109  
    movingAverage2().integrityCheck();
110  
    movingAverage3().integrityCheck();
111  
  }
112  
}

Author comment

Began life as a copy of #1036572

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036614
Snippet name: NoLoseCloudStrategy - an MA cloud + "no-lose" multi stop loss approach [dev.]
Eternal ID of this version: #1036614/6
Text MD5: ffc87643fba17ebe2b4c470ab6c8c19d
Transpilation MD5: 682575fdc1c165996518ab0410b56a9f
Author: stefan
Category: javax / trading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-04-29 08:57:04
Source code size: 3471 bytes / 112 lines
Pitched / IR pitched: No / No
Views / Downloads: 115 / 188
Version history: 5 change(s)
Referenced in: [show references]