Libraryless. Click here for Pure Java version (30795L/194K).
1 | sclass G22CandleBasedStrategy extends G22TradingStrategy { |
2 | settableWithVar double granularity = 1; // candle granularity in minutes |
3 | settableWithVar TradingCandleMaker candleMaker; |
4 | |
5 | // max number of candles to keep around (only used when first |
6 | // creating candle maker) |
7 | settableWithVar int maxCandles = 1000; |
8 | |
9 | // first (usually partial) candle that will be ignored |
10 | //settableWithVar TradingCandle firstCandle; |
11 | |
12 | // Last completed candle |
13 | settableWithVar TradingCandle completedCandle; |
14 | |
15 | // A candle has been completed |
16 | event candleCompleted(TradingCandle candle); |
17 | event newPrice(double price); |
18 | |
19 | // override me |
20 | L<CandleBasedIndicator> indicators() { ret new L; } |
21 | |
22 | // For testing - feed candles directly |
23 | void feed(Iterable<TradingCandle> candles) { |
24 | fOr (candle : candles) try { |
25 | currentTime(candle.endTime.toLong()); |
26 | gotCandle(candle); |
27 | } finally { |
28 | afterStep(); |
29 | } |
30 | } |
31 | |
32 | void gotCandle(TradingCandle candle) { |
33 | completedCandle(candle); |
34 | fOr (indicator : indicators()) |
35 | indicator?.feed(candle); |
36 | candleCompleted(candle); |
37 | } |
38 | |
39 | // Normal operation from ticker |
40 | void price(double price) { |
41 | currentPrice = price; |
42 | if (hasClosedItself()) ret; |
43 | |
44 | newPrice(price); |
45 | |
46 | //printVars("G22CandleBasedStrategy.price", +price); |
47 | try { |
48 | createCandleMaker(); |
49 | |
50 | var completed = candleMaker.completedCandle(); |
51 | candleMaker.add(price, currentTime()); |
52 | /*var candle = candleMaker.currentCandle(); |
53 | /if (firstCandle == null) |
54 | firstCandle(candle); |
55 | else if (candle != firstCandle && ... */ |
56 | if (completed != completedCandle()) |
57 | gotCandle(completed); |
58 | } finally { |
59 | afterStep(); |
60 | } |
61 | } |
62 | |
63 | S fieldsToReset() { |
64 | ret lineCombine(super.fieldsToReset(), [[candleMaker firstCandle completedCandle]]); |
65 | } |
66 | |
67 | L<TradingCandle> candles() { ret candleMaker().candles(); } |
68 | |
69 | // How many candles we need to see before the strategy can start making positions (override me) |
70 | int candlesNeededBeforeOperational() { ret 0; } |
71 | |
72 | void createCandleMaker { |
73 | if (candleMaker == null) |
74 | candleMaker(new TradingCandleMaker().candleLength(granularity)).maxCandles(maxCandles); |
75 | } |
76 | } |
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1036449 |
Snippet name: | G22CandleBasedStrategy |
Eternal ID of this version: | #1036449/29 |
Text MD5: | 6499173596bbc1853b91c490b94d0cbd |
Transpilation MD5: | a6154b2258b467cdf8a7404e53c303e3 |
Author: | stefan |
Category: | javax / trading |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2023-03-28 19:12:38 |
Source code size: | 2303 bytes / 76 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 277 / 453 |
Version history: | 28 change(s) |
Referenced in: | [show references] |