Libraryless. Click here for Pure Java version (29438L/184K).
1 | concept RunnerTradingAlgorithm extends G22TradingStrategy { |
2 | settableWithVar int minRun = 5; |
3 | |
4 | settableWithVar int bet; |
5 | |
6 | // direction: -1 or 1 for normal short or long |
7 | // To change size of of position, use -2, 2 etc |
8 | Position openPosition(int direction) { |
9 | if (direction == 0) null; |
10 | new Position p; |
11 | p.marginToUse = marginPerPosition*abs(direction); |
12 | ret openPosition(p, direction); |
13 | } |
14 | |
15 | void start { |
16 | if (started()) fail("Already started"); |
17 | if (currentPrice() == 0) |
18 | ret with primed(true); |
19 | |
20 | // Save starting price, create price cells & digitizer |
21 | |
22 | startingPrice = currentPrice(); |
23 | startTime = currentTime(); |
24 | var priceCells = makePriceCells(startingPrice); |
25 | print("Made price cells: " + priceCells); |
26 | digitizer = new PriceDigitizer2(priceCells); |
27 | digitizer.verbose(verbose); |
28 | //digitizer.init(startingPrice); |
29 | |
30 | log("Starting RUNNER at " + startingPrice + " +/- " + cellSize + ", min run: " + minRun); |
31 | } |
32 | |
33 | void price(double price) { |
34 | if (currentPrice == price) ret; |
35 | currentPrice = price; |
36 | |
37 | if (hasClosedItself()) ret; |
38 | |
39 | if (!started()) { |
40 | if (primed) { |
41 | primed(false); |
42 | start(); |
43 | } else |
44 | ret; |
45 | } |
46 | |
47 | digitizer.digitize(price); |
48 | direction = sign(digitizedPrice()-lastDigitizedPrice()); |
49 | |
50 | // No cell move? |
51 | if (direction == 0) ret with afterStep(); |
52 | |
53 | nextStep(); |
54 | //printWithPrecedingNL("New digitized price: " + digitizedPrice()); |
55 | |
56 | if (started()) |
57 | step(); |
58 | |
59 | print(this); |
60 | } |
61 | |
62 | swappable void step { |
63 | double p1 = lastDigitizedPrice(); |
64 | double p2 = digitizedPrice(); |
65 | direction = sign(p2-p1); |
66 | if (direction == 0) ret; |
67 | |
68 | bet = makeBet(); |
69 | applyBet(); |
70 | } |
71 | |
72 | swappable void applyBet { |
73 | double drift = drift(); |
74 | if (sign(drift) != sign(bet)) { |
75 | closeAllPositions(); |
76 | openPosition(bet); |
77 | } |
78 | |
79 | log(renderVars(+stepCount, +drift, +bet, +lastDirection, +direction, +run, +minRun, +longestRunSeen)); |
80 | |
81 | afterStep(); |
82 | } |
83 | |
84 | |
85 | double currentCellNumber() { |
86 | ret toCellNumber(currentPrice()); |
87 | } |
88 | |
89 | // Cell we're in |
90 | int currentDigitizedCell() { |
91 | ret iround(toCellNumber(digitizedPrice())); |
92 | } |
93 | |
94 | int priceDirection() { |
95 | ret sign(currentCellNumber()-currentDigitizedCell()); |
96 | } |
97 | |
98 | // Cell we're currently moving to |
99 | int targetCell() { |
100 | ret currentDigitizedCell()+priceDirection(); |
101 | } |
102 | |
103 | double targetPrice() { |
104 | ret fromCellNumber(targetCell()); |
105 | } |
106 | |
107 | // Coin profit when next target is reached |
108 | double targetProfit() { |
109 | ret coinProfitAtPrice(targetPrice()); |
110 | } |
111 | |
112 | // Is profit currently going up or down |
113 | int profitDirection() { |
114 | ret sign(targetProfit()-coinProfit()); |
115 | } |
116 | |
117 | // progress to next target in percent |
118 | double progressInProfitDirection() { |
119 | ret absDiff(currentCellNumber(), currentDigitizedCell())*100; |
120 | } |
121 | |
122 | settableWithVar int lastDirection; |
123 | settableWithVar int move; |
124 | settableWithVar int run; |
125 | settableWithVar int longestRunSeen; |
126 | |
127 | S fieldsToReset() { |
128 | ret lines(ll(super.fieldsToReset(), [[lastDirection run bet longestRunSeen move]])); |
129 | } |
130 | |
131 | swappable int makeBet() { |
132 | move(direction); |
133 | if (direction == lastDirection) ++run; else run = 1; |
134 | longestRunSeen(max(longestRunSeen, run)); |
135 | if (run >= minRun) bet = direction; |
136 | if (bet != 0 && direction != bet) bet = 0; |
137 | lastDirection = direction; |
138 | ret bet; |
139 | } |
140 | } |
Began life as a copy of #1036196
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1036402 |
Snippet name: | RunnerTradingAlgorithm |
Eternal ID of this version: | #1036402/9 |
Text MD5: | dcf05195df8a81c78d5f5f541812e0b5 |
Transpilation MD5: | a133f5e629de6cb5b909249c313039e1 |
Author: | stefan |
Category: | javax / gazelle 22 |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-12-08 04:07:54 |
Source code size: | 3574 bytes / 140 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 141 / 235 |
Version history: | 8 change(s) |
Referenced in: | [show references] |