-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarket.go
94 lines (83 loc) · 1.9 KB
/
market.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
90
91
92
93
94
package marketbit
import (
"github.com/pkg/errors"
"strconv"
"strings"
"time"
)
type TickerItem struct {
High string `json:"high"`
Low string `json:"low"`
Vol string `json:"vol"`
Last string `json:"last"`
Buy string `json:"buy"`
Sell string `json:"sell"`
Date int `json:"date"`
}
type TradeItem struct {
Date int `json:"date"`
Price float64 `json:"price"`
Amount float64 `json:"amount"`
Tid int `json:"tid"`
Type string `json:"type"`
}
type OrderBookResponse struct {
Asks []Ask `json:"asks"`
Bids []Bid `json:"bids"`
}
type TickerResponse struct {
TickerItem `json:"ticker"`
}
type TradeResponse struct {
Trades []TradeItem
}
type SummaryItem struct {
Lowest float64 `json:"lowest"`
Volume float64 `json:"volume"`
Amount float64 `json:"amount"`
AvgPrice float64 `json:"avg_price"`
Opening float64 `json:"opening"`
Date string `json:"date"`
Closing float64 `json:"closing"`
Highest float64 `json:"highest"`
Quantity float64 `json:"quantity"`
}
type Ask []float64
type Bid []float64
func (c Ask) Price() float64 {
return c[0]
}
func (c Ask) Amount() float64 {
return c[1]
}
func (c Bid) Price() float64 {
return c[0]
}
func (c Bid) Amount() float64 {
return c[1]
}
func GuessDateType(date interface{}) (*time.Time, error) {
dateDefault := time.Now().AddDate(0, 0, -1)
var err error
switch date.(type) {
case string:
separator := "-"
for _, bytes := range date.(string) {
_, err := strconv.Atoi(string(bytes))
if err != nil {
separator = string(bytes)
}
}
dateDefault, err = time.Parse(strings.Replace("2006#01#02", "#", separator, 2), date.(string))
if err != nil {
return nil, errors.Wrap(err, "Parse Error, format: 2006-01-02")
}
case int, int64:
dateDefault = time.Unix(date.(int64), 64)
case time.Time:
dateDefault = date.(time.Time)
default:
return nil, errors.New("Unknown date type")
}
return &dateDefault, nil
}