forked from sdcoffey/techan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
candle.go
79 lines (69 loc) · 1.56 KB
/
candle.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
package techan
import (
"fmt"
"strings"
"github.com/sdcoffey/big"
)
// Candle represents basic market information for a security over a given time period
type Candle struct {
Period TimePeriod
OpenPrice big.Decimal
ClosePrice big.Decimal
MaxPrice big.Decimal
MinPrice big.Decimal
Volume big.Decimal
TradeCount uint
}
// NewCandle returns a new *Candle for a given time period
func NewCandle(period TimePeriod) (c *Candle) {
return &Candle{
Period: period,
OpenPrice: big.ZERO,
ClosePrice: big.ZERO,
MaxPrice: big.ZERO,
MinPrice: big.ZERO,
Volume: big.ZERO,
}
}
// AddTrade adds a trade to this candle. It will determine if the current price is higher or lower than the min or max
// price and increment the tradecount.
func (c *Candle) AddTrade(tradeAmount, tradePrice big.Decimal) {
if c.OpenPrice.Zero() {
c.OpenPrice = tradePrice
}
c.ClosePrice = tradePrice
if c.MaxPrice.Zero() {
c.MaxPrice = tradePrice
} else if tradePrice.GT(c.MaxPrice) {
c.MaxPrice = tradePrice
}
if c.MinPrice.Zero() {
c.MinPrice = tradePrice
} else if tradePrice.LT(c.MinPrice) {
c.MinPrice = tradePrice
}
if c.Volume.Zero() {
c.Volume = tradeAmount
} else {
c.Volume = c.Volume.Add(tradeAmount)
}
c.TradeCount++
}
func (c *Candle) String() string {
return strings.TrimSpace(fmt.Sprintf(
`
Time: %s
Open: %s
Close: %s
High: %s
Low: %s
Volume: %s
`,
c.Period,
c.OpenPrice.FormattedString(2),
c.ClosePrice.FormattedString(2),
c.MaxPrice.FormattedString(2),
c.MinPrice.FormattedString(2),
c.Volume.FormattedString(2),
))
}