Libraryless. Click here for Pure Java version (17377L/98K).
1 | persistable sclass TickerSequences is ByteIO, IntSize, Iterable<TickerSequence> { |
2 | gettable S market; // e.g. "TRBUSDT" |
3 | |
4 | gettable new L<TickerSequence> tickers; |
5 | |
6 | *(Iterable<TickerSequence> sequences) { |
7 | main addAll(tickers, sequences); |
8 | } |
9 | |
10 | *(TickerSequence... sequences) { |
11 | main addAll(tickers, sequences); |
12 | } |
13 | |
14 | void add(TickerSequence ticker) { |
15 | tickers.add(ticker); |
16 | } |
17 | |
18 | TickerSequence get(int i) { ret tickers.get(i); } |
19 | |
20 | void sort { |
21 | sortInPlaceByCalculatedField(tickers, ->. startTime()); |
22 | } |
23 | |
24 | public int size() { ret tickers.size(); } |
25 | |
26 | toString { |
27 | ret commaCombine( |
28 | spaceCombine( |
29 | n2(tickers, " ticker sequence"), |
30 | empty(market) ? null : "for " + market), |
31 | formatDays(totalTime()), |
32 | n2(nPricePoints(), "price point") |
33 | ); |
34 | } |
35 | |
36 | long nPricePoints() { |
37 | ret longSum(map(tickers, t -> (long) t.size())); |
38 | } |
39 | |
40 | long totalTime() { |
41 | ret longSum(map(tickers, ticker -> { var tr = ticker.timeRange(); ret tr == null ? 0 : tr.length(); })); |
42 | } |
43 | |
44 | selfType dropOverlaps() { |
45 | sort(); |
46 | |
47 | for (int i = 0; i+1 < l(tickers); i++) { |
48 | var t1 = tickers.get(i); |
49 | var t2 = tickers.get(i+1); |
50 | long time2 = t2.startTime(); |
51 | while (!t1.isEmpty() && t1.endTime() >= time2) |
52 | t1.dropLastPricePoint(); |
53 | t1.trimToSize(); |
54 | } |
55 | |
56 | this; |
57 | } |
58 | |
59 | selfType merge(double maximumGapSeconds default 120) { |
60 | sort(); |
61 | |
62 | for (int i = 0; i+1 < l(tickers); i++) { |
63 | var t1 = tickers.get(i); |
64 | var t2 = tickers.get(i+1); |
65 | if (t2.startTime()-t1.endTime() <= fromSeconds(maximumGapSeconds)) { |
66 | t1.add(t2); |
67 | tickers.remove(i+1); |
68 | --i; |
69 | } |
70 | } |
71 | |
72 | this; |
73 | } |
74 | |
75 | double firstPrice() { |
76 | var ticker = first(tickers); |
77 | ret ticker == null ? Double.NaN : ticker.firstPrice(); |
78 | } |
79 | |
80 | ItIt<Double> prices() { |
81 | ret nestedIterator(tickers, ticker -> ticker.prices.iterator()); |
82 | } |
83 | |
84 | |
85 | long startTime() { ret empty(tickers) ? null : first(tickers).startTime(); } |
86 | long endTime() { ret empty(tickers) ? null : last(tickers).endTime(); } |
87 | |
88 | TickerSequence longest() { |
89 | ret highestBy(tickers(), ticker -> duration(ticker.timeRange())); |
90 | } |
91 | |
92 | TickerSequences mapTickers(IF1<TickerSequence> f) { |
93 | new TickerSequences ts; |
94 | for (t : tickers()) |
95 | ts.add(f.get(t)); |
96 | ret ts; |
97 | } |
98 | |
99 | TickerSequences filterTickers(IPred<TickerSequence> pred) { |
100 | new TickerSequences ts; |
101 | for (t : tickers()) |
102 | if (pred.get(t)) |
103 | ts.add(t); |
104 | ret ts; |
105 | } |
106 | |
107 | /*TickerSequences digitizeToPercent(double basePrice, double cellSizeInPercent) { |
108 | ret mapTickers(ts -> ts.digitizeToPercent(basePrice, cellSizeInPercent)); |
109 | }*/ |
110 | |
111 | selfType marketIfEmpty(S market) { |
112 | if (empty(this.market)) market(market); |
113 | this; |
114 | } |
115 | |
116 | selfType market(S market) { |
117 | this.market = market; |
118 | for (t : tickers) |
119 | t.market(market); |
120 | this; |
121 | } |
122 | |
123 | public void write(ByteHead head) { |
124 | for (t : tickers) |
125 | t.write(head); |
126 | } |
127 | |
128 | public void read(ByteHead head) { |
129 | TickerSequence t; |
130 | while ((t = TickerSequence.read(head)) != null) |
131 | add(t); |
132 | if (empty(market) && nempty(tickers)) |
133 | market(first(tickers).market); |
134 | } |
135 | |
136 | public void readWrite(ByteHead head) { |
137 | if (head.readMode()) read(head); |
138 | if (head.writeMode()) write(head); |
139 | } |
140 | |
141 | public Iterator<TickerSequence> iterator() { |
142 | ret tickers.iterator(); |
143 | } |
144 | } |
Began life as a copy of #1036138
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1036184 |
Snippet name: | TickerSequences - multiple ticker sequences, usually of one coin and sorted by time |
Eternal ID of this version: | #1036184/37 |
Text MD5: | 34fe4a203a2e97665ede5635e0840956 |
Transpilation MD5: | c0dc3f19f907831e4d7f568516283026 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2023-01-21 20:26:15 |
Source code size: | 3567 bytes / 144 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 227 / 464 |
Version history: | 36 change(s) |
Referenced in: | [show references] |