Libraryless. Click here for Pure Java version (15303L/89K).
1 | persistable sclass MultiCallbackJuicer 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 callback rate. |
6 | // Callback rates are interpolated between levels. |
7 | settable double minJuiceValue; |
8 | settable double callback; |
9 | |
10 | toString { ret "Callback " + callback + "% starting at " + formatDouble2(minJuiceValue); } |
11 | } |
12 | |
13 | settableWithVar new L<Level> levels; |
14 | |
15 | // Highest juice value seen |
16 | settableWithVar double crest = -infinity(); |
17 | |
18 | settableWithVar double currentCallback; |
19 | |
20 | { |
21 | onCalculatingCloseSignals(signals -> { |
22 | if (juiceValue > crest) |
23 | crest(juiceValue); |
24 | |
25 | currentCallback(callbackForJuiceValue(crest)); |
26 | |
27 | var signal = new CloseSignal().createdBy(this).reason(formatDouble2(currentCallback) + "% Callback"); |
28 | signal.strength(doubleRatio(crest-juiceValue, currentCallback)*100); |
29 | signals.add(signal); |
30 | }); |
31 | } |
32 | |
33 | double callbackForJuiceValue(double juice) { |
34 | for (int i = l(levels)-1; i >= 0; i--) { |
35 | Level level = levels.get(i); |
36 | if (juice < level.minJuiceValue) |
37 | continue; |
38 | |
39 | Level nextLevel = get(levels, i+1); |
40 | if (nextLevel == null) |
41 | ret level.callback; |
42 | |
43 | ret blend(level.callback, nextLevel.callback, |
44 | transformToZeroToOne(juice, level.minJuiceValue, nextLevel.minJuiceValue)); |
45 | } |
46 | |
47 | ret empty(levels) ? 1000 : first(levels).callback; |
48 | } |
49 | |
50 | void addLevel(double minJuiceValue, double callback) { |
51 | levels.add(new Level().minJuiceValue(minJuiceValue).callback(callback)); |
52 | } |
53 | |
54 | toString { |
55 | ret commaCombine( |
56 | shortClassName(this), |
57 | levels, |
58 | crest == -infinity() ? null : "Highest profit seen: " + formatDouble2(crest), |
59 | "Current callback: " + formatDouble2(currentCallback), |
60 | ); |
61 | } |
62 | |
63 | void copyTransientValuesFrom(AbstractJuicer juicer) { |
64 | if (juicer cast MultiCallbackJuicer) |
65 | crest(juicer.crest); |
66 | } |
67 | } |
Began life as a copy of #1036356
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj
No comments. add comment
Snippet ID: | #1036534 |
Snippet name: | MultiCallbackJuicer - saner version of MultiPullbackJuicer |
Eternal ID of this version: | #1036534/6 |
Text MD5: | a24a630c506af79bbb848db2d812cccd |
Transpilation MD5: | 251fb15583ad1a10ff2fac0023f867db |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2023-01-24 17:57:46 |
Source code size: | 2077 bytes / 67 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 182 / 243 |
Version history: | 5 change(s) |
Referenced in: | [show references] |