-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathStoch.vue
121 lines (106 loc) · 3.3 KB
/
Stoch.vue
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
<script>
import { Overlay } from 'trading-vue-js'
export default {
name: 'Stoch',
mixins: [Overlay],
methods: {
meta_info() {
return {
author: 'StdSquad', version: '1.0.0',
desc: 'Stochastic',
preset: {
name: 'Stoch $param_k $param_d $smooth',
side: 'offchart',
settings: {
lineWidth: 0.75,
kColor: '#3782f2',
dColor: '#f48709',
bandColor: '#ddd',
backColor: '#381e9c16'
}
}
}
},
draw(ctx) {
const layout = this.$props.layout
const upper = layout.$2screen(this.sett.upper || 80)
const lower = layout.$2screen(this.sett.lower || 20)
// K
ctx.lineWidth = this.line_width
ctx.strokeStyle = this.k_color
ctx.beginPath()
for (var p of this.$props.data) {
let x = layout.t2screen(p[0])
let y = layout.$2screen(p[1])
ctx.lineTo(x, y)
}
ctx.stroke()
// D
ctx.lineWidth = this.line_width
ctx.strokeStyle = this.d_color
ctx.beginPath()
for (var p of this.$props.data) {
let x = layout.t2screen(p[0])
let y = layout.$2screen(p[2])
ctx.lineTo(x, y)
}
ctx.stroke()
ctx.strokeStyle = this.band_color
ctx.setLineDash([5]) // Will be removed after draw()
ctx.beginPath()
// Fill the area between the bands
ctx.fillStyle = this.back_color
ctx.fillRect(0, upper, layout.width, lower - upper)
// Upper band
ctx.moveTo(0, upper)
ctx.lineTo(layout.width, upper)
// Lower band
ctx.moveTo(0, lower)
ctx.lineTo(layout.width, lower)
ctx.stroke()
},
use_for() { return ['Stoch'] },
data_colors() { return [this.color] },
y_range(hi, lo) {
return [
Math.max(hi, this.sett.upper || 80),
Math.min(lo, this.sett.lower || 20)
]
},
calc() {
return {
props: {
param_k: { def: 14, text: 'K' },
param_d: { def: 3, text: 'D' },
smooth: { def: 3, text: 'Smooth' },
},
update: `
let k = sma(stoch(close, high, low, param_k), smooth)
let d = sma(k, param_d)
return [k[0], d[0]]
`
}
}
},
computed: {
sett() {
return this.$props.settings
},
line_width() {
return this.sett.lineWidth || 0.75
},
k_color() {
return this.sett.kColor || '#3782f2'
},
d_color() {
return this.sett.dColor || '#f48709'
},
band_color() {
return this.sett.bandColor || '#ddd'
},
back_color() {
return this.sett.backColor || '#381e9c16'
}
}
}
</script>