-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforecast.js
341 lines (324 loc) · 11.6 KB
/
forecast.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
function getInputs() {
const inputs = document.querySelectorAll("input")
const values = []
for (let input of inputs) {
values.push(input.valueAsNumber || 0)
}
return values
}
function setInputs(values) {
const inputs = document.querySelectorAll("input")
for (let i in inputs) {
const input = inputs[i]
const value = values[i]
if (value) {
input.value = value
}
}
}
function resetInputs() {
const inputs = document.querySelectorAll("input")
for (let input of inputs) {
input.value = ''
}
}
// Levels
const BOT = -2
const DEC = -1
const NOR = 0
const INC = 1
const HIT = 2
function calcPriceProportionl(price = 100, range) {
return [ Math.floor(price * range[0]), Math.ceil(price * range[1]) ]
}
function calcPriceStaticDiff(prevPrices, range) {
return [ Math.floor(prevPrices[0] + range[0]), Math.ceil(prevPrices[1] + range[1]) ]
}
// 파도형 패턴
function createWavePatterns(initialPrice, observedPrices) {
function isValidCombination(combination) {
// 하락장은 5번 온다
if (combination.filter(increase => increase).length > 7) {
return false
}
// 5연속 하락장은 없다
let firstDecrease = null
let lastDecrease = null
for (let i in combination) {
if (firstDecrease === null && combination[i] === DEC) {
firstDecrease = i
}
if (firstDecrease !== null && combination[i] === DEC) {
lastDecrease = i
}
}
if (lastDecrease - firstDecrease < 5) {
return false
}
return true
}
function getDayType(day, combination) {
if (combination[day] === NOR) {
return 'Normal'
}
// decreasing.
if (day === 0) {
return 'FirstDrop'
}
if (combination[day - 1] === NOR) {
return 'FirstDrop'
}
return 'KeepDecreasing'
}
function getPriceRanges(initialPrice = 100, observedPrices, combination) {
const priceRanges = []
for(let d = 0; d < 12; d++){
const dayType = getDayType(d, combination)
if (dayType === 'Normal') {
priceRanges.push(calcPriceProportionl(initialPrice, [0.90, 1.40]))
}
if (dayType === 'FirstDrop') {
priceRanges.push(calcPriceProportionl(initialPrice, [0.60, 0.80]))
}
if (dayType === 'KeepDecreasing') {
const observed = observedPrices[d - 1]
const yesterdayPriceRange = observed ? [observed, observed] : priceRanges[d - 1]
priceRanges.push(calcPriceStaticDiff(yesterdayPriceRange, [-10, -4]))
}
}
return priceRanges
}
// 가격 패턴 생성
const combinations = []
for (let short = 0; short < 11; short++) {
for (let long = 0; long < 10; long++) {
let combination = [NOR,NOR,NOR,NOR,NOR,NOR,NOR,NOR,NOR,NOR,NOR,NOR]
combination[short] = DEC
combination[short+1] = DEC
combination[long] = DEC
combination[long+1] = DEC
combination[long+2] = DEC
if (isValidCombination(combination)) {
combinations.push(combination)
}
}
}
// 패턴과 예측범위 계산하여 반환
return combinations.map(combination => ({
combination,
priceRanges: getPriceRanges(initialPrice, observedPrices, combination)
}))
}
// 하락형 패턴
function createDecreasingPattern(initialPrice, observedPrices) {
const combination = [DEC,DEC,DEC,DEC,DEC,DEC,DEC,DEC,DEC,DEC,DEC,DEC]
let priceRanges = [calcPriceProportionl(initialPrice, [0.85, 0.90])]
for (let d = 1; d < 12; d++) {
const observed = observedPrices[d - 1]
const yesterdayPriceRange = observed ? [observed, observed] : priceRanges[d - 1]
priceRanges.push(calcPriceStaticDiff(yesterdayPriceRange, [-6, -2]))
}
return { combination, priceRanges }
}
// 3기 상승형 패턴
function create3UpPatterns(initialPrice, observedPrices) {
const patterns = []
for (let d = 1; d < 8; d++) {
let combination = [DEC,DEC,DEC,DEC,DEC,DEC,DEC,DEC,DEC,DEC,DEC,DEC]
combination[d] = NOR
combination[d+1] = INC
combination[d+2] = HIT
combination[d+3] = INC
combination[d+4] = NOR
for (let i = d+5; i < 12; i++) {
combination[i] = BOT
}
let priceRanges = [calcPriceProportionl(initialPrice, [0.85, 0.90])]
for (let d = 1; d < 12; d++) {
switch(combination[d]) {
case DEC:
const observed = observedPrices[d - 1]
const yesterdayPriceRange = observed ? [observed, observed] : priceRanges[d - 1]
priceRanges.push(calcPriceStaticDiff(yesterdayPriceRange, [-6, -2])); break;
case NOR:
priceRanges.push(calcPriceProportionl(initialPrice, [0.90, 1.40])); break;
case INC:
priceRanges.push(calcPriceProportionl(initialPrice, [1.40, 2.00])); break;
case HIT:
priceRanges.push(calcPriceProportionl(initialPrice, [2.00, 6.00])); break;
case BOT:
priceRanges.push(calcPriceProportionl(initialPrice, [0.40, 0.90])); break;
}
}
patterns.push({ combination, priceRanges})
}
return patterns
}
function create4UpPatterns(initialPrice, observedPrices) {
const patterns = []
for (let d = 0; d < 8; d++) {
let combination = [null,DEC,DEC,DEC,DEC,DEC,DEC,DEC,DEC,DEC,DEC,DEC]
combination[d] = NOR
combination[d+1] = NOR
combination[d+2] = INC
combination[d+3] = HIT
combination[d+4] = INC
combination[d+5] = null
let priceRanges = [calcPriceProportionl(initialPrice, [0.40, 0.90])]
if (combination[0] === NOR) {
priceRanges[0] = calcPriceProportionl(initialPrice, [0.90, 1.40]);
}
for (let d = 1; d < 12; d++) {
switch(combination[d]) {
case DEC:
const observed = observedPrices[d - 1]
const yesterdayPriceRange = observed ? [observed, observed] : priceRanges[d - 1]
priceRanges.push(calcPriceStaticDiff(yesterdayPriceRange, [-6, -2])); break;
case NOR:
priceRanges.push(calcPriceProportionl(initialPrice, [0.90, 1.40])); break;
case INC:
priceRanges.push(calcPriceProportionl(initialPrice, [1.40, 1.90])); break;
case HIT:
priceRanges.push(calcPriceProportionl(initialPrice, [1.40, 2.00])); break;
case null:
priceRanges.push(calcPriceProportionl(initialPrice, [0.40, 0.90])); break;
}
}
patterns.push({ combination, priceRanges})
}
return patterns
}
function prettyPrint(combination) {
// just for pretty format,
// display INC after HIT as (⤒)↘
combination = [...combination]
for (let i = 1; i < combination.length; i++) {
if (combination[i-1] === HIT && combination[i] === INC) {
combination[i] = DEC
}
}
return combination.map(type => {
switch(type) {
case NOR: return '―'
case INC: return '↗'
case DEC: return '↘'
case HIT: return '‾'
case BOT: return '_'
default: return '―'
}
}).join('')
}
function expect(patterns, inputs) {
const [initialValue, ...values] = inputs
const matchedPatterns = []
for (let pattern of patterns) {
const { priceRanges } = pattern
let match = true
for (let i in values) {
const [min, max] = priceRanges[i]
const value = values[i]
if (!value) {
continue
}
if (value < min || max < value) {
match = false
break
}
}
if (match) {
matchedPatterns.push(pattern)
}
}
return matchedPatterns
}
function renderResults(patterns, inputs) {
const header = renderHeader(inputs)
const rows = patterns.map(pattern => renderPattern(pattern)).join('')
const footer = renderFooter()
document.getElementById("results").innerHTML = header + rows + footer
}
function renderHeader(inputs) {
const header = `
<tr class="header">
<th class="pattern" rowspan=2>상승-하락 패턴</th>
<th colspan="2">월요일</th>
<th colspan="2">화요일</th>
<th colspan="2">수요일</th>
<th colspan="2">목요일</th>
<th colspan="2">금요일</th>
<th colspan="2">토요일</th>
</tr>
<tr class="header">
<th class="am">오전</th>
<th class="pm">오후</th>
<th class="am">오전</th>
<th class="pm">오후</th>
<th class="am">오전</th>
<th class="pm">오후</th>
<th class="am">오전</th>
<th class="pm">오후</th>
<th class="am">오전</th>
<th class="pm">오후</th>
<th class="am">오전</th>
<th class="pm">오후</th>
</tr>`
const [initial, ...observed] = inputs
const inputCells = observed.map((v, i) => `<th class="${i % 2 === 0 ? 'am' : 'pm'}">${v || '?'}</th>`)
const inputsRow = `
<tr class="user-input">
<th>관측값: ${initial}</th>
${inputCells.join('')}
</tr>
`
return header + inputsRow
}
function renderPattern(pattern) {
const {combination, priceRanges} = pattern
const maxValue = Math.max.apply(null, priceRanges.map(range => range[1]))
const ranges = priceRanges.map((range, i) => `
<td class="${i % 2 === 0 ? 'am' : 'pm'} ${maxValue === range[1] ? 'peak' : ''}">\
<span class="min">${range[0]}</span>
<span class="max ${maxValue === range[1] ? 'peak' : ''}">${range[1]}</span>
</td>
`)
const row = `
<tr class="row">
<th class="pattern">${prettyPrint(combination)}</th>
${ranges.join('')}
</tr>`
return row
}
function renderFooter() {
const footer = `<tr><td colspan=13><button onclick="closeResults();">닫기</button></td></tr>`
return footer
}
function closeResults() {
const results = document.querySelector("#results")
results.innerHTML = ""
}
(function() {
// load from save
const saved = localStorage.getItem('save')
if (saved) {
const values = saved.split(';')
setInputs(values)
}
document.getElementById("submit").addEventListener('click', (e) => {
const inputs = getInputs()
// save to localstorage
localStorage.setItem('save', inputs.join(";"))
const [initialPrice, ...observedPrices] = inputs
const wavePatterns = createWavePatterns(initialPrice, observedPrices)
const decreasingPattern = createDecreasingPattern(initialPrice, observedPrices)
const bigWavePatterns3 = create3UpPatterns(initialPrice, observedPrices)
const bigWavePatterns4 = create4UpPatterns(initialPrice, observedPrices)
const patterns = [...wavePatterns, decreasingPattern, ...bigWavePatterns3, ...bigWavePatterns4]
const matchedPatterns = expect(patterns, inputs)
renderResults(matchedPatterns, inputs)
})
document.getElementById("reset").addEventListener('click', (e) => {
resetInputs()
const zeros = getInputs()
localStorage.setItem('save', zeros.join(";"))
})
})()