-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhistory.go
49 lines (44 loc) · 1.43 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
package investing
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
const historyUrl = `https://tvc4.forexpros.com/7faf7c0428fe0cdb621c45d461268e4b/%v/6/6/28/history?symbol=%v&resolution=%v&from=%v&to=%v`
func genHistoryUrl(symbol, typ string, start, end time.Time) string {
return fmt.Sprintf(historyUrl, time.Now().Unix(), symbol, typ, start.Unix(), end.Unix())
}
func GetHistory(symbol, typ string, start, end time.Time) (*Lines, error) {
lines := new(Lines)
url := genHistoryUrl(symbol, typ, start, end)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36")
req.Header.Set("Origin", "https://cn.investing.com")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return lines, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(body, lines)
if err != nil {
return lines, err
}
if lines.S == "ok" {
return lines, err
} else {
return lines, fmt.Errorf("resp body :%v", string(body))
}
}
type Lines struct {
T []int64 `json:"t"` // 时间点
C []float64 `json:"c"` // 收盘价
O []float64 `json:"o"` // 开盘价
L []float64 `json:"l"` // 最低
H []float64 `json:"h"` // 最高
V []json.Number `json:"v"` // 成交额
Vo []json.Number `json:"vo"` // 成交量
S string `json:"s"`
}