-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathinstrument.coffee
78 lines (68 loc) · 1.59 KB
/
instrument.coffee
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
_ = require 'underscore'
talib = require('./talib_sync')
class Instrument
constructor: (@platform,@id)->
@open = []
@low = []
@high = []
@close = []
@volumes = []
@bars = []
@pair = @id.split('_')
asset: ->
@pair[0]
curr: ->
@pair[1]
update: (data)->
if @open.length == 1000 # Hold up to 1000 values
@open.shift()
@low.shift()
@high.shift()
@close.shift()
@volumes.shift()
@bars.shift()
@open.push data.open
@low.push data.low
@high.push data.high
@close.push data.close
@volumes.push data.volume
@price = data.close
@volume = data.volume
@bars.push data
vwap: (period)->
if period < @bars.length
idx = @bars.length - period
else
idx = 0
flux = 0
volume = 0
while idx < @bars.length
flux += @volumes[idx] * @close[idx]
volume += @volumes[idx]
idx++
if volume
return flux / volume
ema: (period)->
output = talib.EMA
name: 'EMA'
startIdx: 0
endIdx: @close.length-1
inReal: @close
optInTimePeriod: period
_.last(output)
macd: (fastPeriod, slowPeriod, signalPeriod)->
results = talib.MACD
name: 'MACD'
startIdx: 0
endIdx: @close.length-1
inReal: @close
optInFastPeriod: fastPeriod
optInSlowPeriod: slowPeriod
optInSignalPeriod: signalPeriod
if results.outMACD.length
output =
macd: _.last results.outMACD
signal: _.last results.outMACDSignal
histogram: _.last results.outMACDHist
output
module.exports = Instrument