Libraryless. Click here for Pure Java version (15695L/93K).
1 | persistable sclass MultiStopLossJuicer extends AbstractJuicer { |
2 | // Must have at least one level, otherwise no closing at all |
3 | |
4 | sclass Level { |
5 | // At juice value minJuiceValue, define the stopLoss. |
6 | settable double minJuiceValue; |
7 | settable double stopLoss; |
8 | |
9 | toString { ret "Stop loss " + stopLoss + "% 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 | // for current level, set by MultiStopLossJuicer itself |
18 | settableWithVar StopLossJuicer stopLossJuicer; |
19 | |
20 | settableWithVar int levelsTriggered; |
21 | |
22 | Level nextLevel() { ret _get(levels, levelsTriggered); } |
23 | |
24 | { |
25 | onCalculatingCloseSignals(signals -> { |
26 | if (juiceValue > crest) |
27 | crest(juiceValue); |
28 | |
29 | Level level; |
30 | while ((level = nextLevel()) != null && crest >= level.minJuiceValue) { |
31 | levelsTriggered(levelsTriggered+1); |
32 | var slj = new StopLossJuicer(level.stopLoss); |
33 | slj.juiceable(juiceable()); |
34 | stopLossJuicer(slj); |
35 | } |
36 | |
37 | if (stopLossJuicer != null) |
38 | addAll(signals, stopLossJuicer.calculateCloseSignals()); |
39 | }); |
40 | } |
41 | |
42 | void addLevel(double minJuiceValue, double stopLoss) { |
43 | levels.add(new Level().+minJuiceValue.+stopLoss); |
44 | } |
45 | |
46 | toString { |
47 | ret commaCombine( |
48 | shortClassName(this), |
49 | levels, |
50 | crest == -infinity() ? null : "Highest profit seen: " + formatDouble2(crest), |
51 | "Levels triggered: " + levelsTriggered, |
52 | "Current stop loss: " + stopLossJuicer, |
53 | ); |
54 | } |
55 | |
56 | // Hmm. Is this correct? |
57 | void copyTransientValuesFrom(AbstractJuicer juicer) { |
58 | if (juicer cast MultiStopLossJuicer) |
59 | crest(juicer.crest); |
60 | } |
61 | } |
Began life as a copy of #1036534
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj
No comments. add comment
Snippet ID: | #1036611 |
Snippet name: | MultiStopLossJuicer - even simpler version of MultiCallbackJuicer with fixed thresholds & stop loss levels |
Eternal ID of this version: | #1036611/4 |
Text MD5: | 4399043d5be4800f48d0eb617f702031 |
Transpilation MD5: | 5505ccce76d0fc47311c7f271a1cd387 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2023-04-29 00:13:22 |
Source code size: | 1812 bytes / 61 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 178 / 237 |
Version history: | 3 change(s) |
Referenced in: | [show references] |