-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
82 lines (69 loc) · 1.67 KB
/
history.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
package fxdl
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
var millennium = time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
type period time.Duration
const (
M1 = period(1 * time.Minute)
M5 = period(5 * time.Minute)
M15 = period(15 * time.Minute)
M30 = period(30 * time.Minute)
)
func NewHistoryClient(p period) *Client {
return &Client{
p: time.Duration(p),
hc: &http.Client{},
}
}
func (c *Client) SetProxy(proxy string) {
proxyURL, _ := url.Parse(proxy)
c.hc.Transport = &http.Transport{
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
type data struct {
Time []int `json:"time"`
Open []float64 `json:"open"`
High []float64 `json:"high"`
Low []float64 `json:"low"`
Close []float64 `json:"close"`
Volume []int `json:"volume"`
}
func (c *Client) Do(symbol string) ([]*Candle, error) {
u := fmt.Sprintf("https://data.forexsb.com/data/%s%.0f.gz", symbol, c.p.Minutes())
resp, err := c.hc.Get(u)
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
d := &data{}
if err = json.Unmarshal(b, d); err != nil {
return nil, err
}
candles := make([]*Candle, 0, 200_000)
for i := 0; i < len(d.Time); i++ {
c := Candle{
Start: millennium.Add(time.Duration(d.Time[i]) * 60 * time.Second),
End: millennium.Add(time.Duration(d.Time[i])*60*time.Second + c.p - 1),
OpenPrice: d.Open[i],
ClosePrice: d.Close[i],
MaxPrice: d.High[i],
MinPrice: d.Low[i],
Volume: d.Volume[i],
}
candles = append(candles, &c)
}
return candles, nil
}