forked from vevsatechnologies/dcrextdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collectors.go
144 lines (114 loc) · 2.9 KB
/
collectors.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
log "github.com/sirupsen/logrus"
)
type exchangeDataTick struct {
High float64
Low float64
Open float64
Close float64
Volume float64
Time int64
Exchange string
}
type poloniexDataTick struct {
High float64 `json:"high"`
Low float64 `json:"low"`
Open float64 `json:"open"`
Close float64 `json:"close"`
Volume float64 `json:"volume"`
Time int64 `json:"date"`
}
type poloniexAPIResponse []poloniexDataTick
type bittrexDataTick struct {
High float64 `json:"H"`
Low float64 `json:"L"`
Open float64 `json:"O"`
Close float64 `json:"C"`
Volume float64 `json:"BV"`
Time string `json:"T"`
}
type bittrexAPIResponse struct {
Result []bittrexDataTick `json:"result"`
}
var dcrlaunchtime int64 = 1454889600
func collectPoloniexData(start int64) ([]exchangeDataTick, error) {
client := &http.Client{Timeout: 300 * time.Second}
if start == 0 {
start = dcrlaunchtime
}
res, err := client.Get(fmt.Sprintf("https://poloniex.com/public?command=returnChartData¤cyPair=BTC_DCR&start=%d&end=9999999999&period=1800", start))
if err != nil {
return nil, err
}
data := new(poloniexAPIResponse)
err = json.NewDecoder(res.Body).Decode(data)
if err != nil {
return nil, err
}
res.Body.Close()
exchangeData := make([]exchangeDataTick, 0)
for _, v := range []poloniexDataTick(*data) {
eData := exchangeDataTick{
High: v.High,
Low: v.Low,
Open: v.Open,
Close: v.Close,
Time: v.Time,
Exchange: "poloniex",
}
exchangeData = append(exchangeData, eData)
}
return exchangeData, nil
}
func collectBittrexData(start int64) ([]exchangeDataTick, error) {
client := &http.Client{Timeout: 300 * time.Second}
// Bittrex "start" option doesn't work
res, err := client.Get("https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-DCR&tickInterval=thirtyMin")
if err != nil {
return nil, err
}
data := new(bittrexAPIResponse)
err = json.NewDecoder(res.Body).Decode(data)
if err != nil {
return nil, err
}
res.Body.Close()
exchangeData := make([]exchangeDataTick, 0)
for _, v := range data.Result {
t, _ := time.Parse(time.RFC3339[:19], v.Time)
if t.Unix() < start {
continue
}
eData := exchangeDataTick{
High: v.High,
Low: v.Low,
Open: v.Open,
Close: v.Close,
Time: t.Unix(),
Exchange: "bittrex",
}
exchangeData = append(exchangeData, eData)
}
return exchangeData, nil
}
func collectExchangeData(start int64) ([]exchangeDataTick, error) {
data := make([]exchangeDataTick, 0)
poloniexdata, err := collectPoloniexData(start)
if err != nil {
log.Error("Error: ", err)
return nil, err
}
bittrexdata, err := collectBittrexData(start)
if err != nil {
log.Error("Error: ", err)
return nil, err
}
data = append(data, poloniexdata...)
data = append(data, bittrexdata...)
return data, nil
}