forked from sdcoffey/techan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indicator_basic.go
89 lines (70 loc) · 2.35 KB
/
indicator_basic.go
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
package techan
import "github.com/sdcoffey/big"
type volumeIndicator struct {
*TimeSeries
}
// NewVolumeIndicator returns an indicator which returns the volume of a candle for a given index
func NewVolumeIndicator(series *TimeSeries) Indicator {
return volumeIndicator{series}
}
func (vi volumeIndicator) Calculate(index int) big.Decimal {
return vi.Candles[index].Volume
}
type closePriceIndicator struct {
*TimeSeries
}
// NewClosePriceIndicator returns an Indicator which returns the close price of a candle for a given index
func NewClosePriceIndicator(series *TimeSeries) Indicator {
return closePriceIndicator{series}
}
func (cpi closePriceIndicator) Calculate(index int) big.Decimal {
return cpi.Candles[index].ClosePrice
}
type highPriceIndicator struct {
*TimeSeries
}
// NewHighPriceIndicator returns an Indicator which returns the high price of a candle for a given index
func NewHighPriceIndicator(series *TimeSeries) Indicator {
return highPriceIndicator{
series,
}
}
func (hpi highPriceIndicator) Calculate(index int) big.Decimal {
return hpi.Candles[index].MaxPrice
}
type lowPriceIndicator struct {
*TimeSeries
}
// NewLowPriceIndicator returns an Indicator which returns the low price of a candle for a given index
func NewLowPriceIndicator(series *TimeSeries) Indicator {
return lowPriceIndicator{
series,
}
}
func (lpi lowPriceIndicator) Calculate(index int) big.Decimal {
return lpi.Candles[index].MinPrice
}
type openPriceIndicator struct {
*TimeSeries
}
// NewOpenPriceIndicator returns an Indicator which returns the open price of a candle for a given index
func NewOpenPriceIndicator(series *TimeSeries) Indicator {
return openPriceIndicator{
series,
}
}
func (opi openPriceIndicator) Calculate(index int) big.Decimal {
return opi.Candles[index].OpenPrice
}
type typicalPriceIndicator struct {
*TimeSeries
}
// NewTypicalPriceIndicator returns an Indicator which returns the typical price of a candle for a given index.
// The typical price is an average of the high, low, and close prices for a given candle.
func NewTypicalPriceIndicator(series *TimeSeries) Indicator {
return typicalPriceIndicator{series}
}
func (tpi typicalPriceIndicator) Calculate(index int) big.Decimal {
numerator := tpi.Candles[index].MaxPrice.Add(tpi.Candles[index].MinPrice).Add(tpi.Candles[index].ClosePrice)
return numerator.Div(big.NewFromString("3"))
}