/
data_collector.go
117 lines (102 loc) · 3.1 KB
/
data_collector.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
// Copyright © 2017 Thomas Nguy
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package strategies
import (
"fmt"
"time"
"github.com/thomasxnguy/golang-crypto-bot/environment"
"github.com/thomasxnguy/golang-crypto-bot/exchanges"
"github.com/influxdata/influxdb/client/v2"
"github.com/thomasxnguy/golang-crypto-bot/util/influxdb"
)
var c client.Client
const (
DB = "crypto_bot"
username = "golang"
password = "golang"
precision = "s"
measurement = "binance_candle_1d"
)
// DataColletor Strategy is used to collect candlestick data from exchanger defined in configuration file.
// Candles are stored in InfluxDB as a point measurement. Modify this file to change period of the candle or market.
// By default period is 1m and market is BTCUSDT
var DataCollector Strategy = IntervalStrategy{
Model: StrategyModel{
Name: "DataCollector",
Setup: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
fmt.Println("DataCollector starting")
var err error
c, err = client.NewHTTPClient(client.HTTPConfig{
Addr: "http://localhost:8086",
Username: username,
Password: password,
})
if err != nil {
fmt.Println(err)
}
return nil
},
OnUpdate: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
lastTs, err := influxdb.GetLastTimestamp(c,DB,precision,measurement,"open")
if err != nil || lastTs == 0 {
fmt.Println("first data")
lastTs = 1483228800
}
fmt.Println(lastTs*1000)
results, err := wrappers[0].GetKlines(lastTs*1000, "BTCUSDT", "1d")
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: DB,
Precision: precision,
})
if err != nil {
fmt.Println(err)
return err
}
for _, val := range results.CandleSticks {
fields := map[string]interface{}{
"open": val.Open,
"close": val.Close,
"low": val.Low,
"high": val.High,
"trade_nb": val.TradeNb,
"volume": val.Volume,
}
pt, err := client.NewPoint(
measurement,
nil,
fields,
time.Unix(val.OpenTime/1000, 0),
)
if err != nil {
fmt.Println(err)
return err
}
bp.AddPoint(pt)
}
influxdb.WritePoints(c, bp)
fmt.Println("write points")
return nil
},
OnError: func(err error) {
fmt.Println(err)
},
TearDown: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
c.Close()
fmt.Println("DataCollector exited")
return nil
},
},
Interval: 5 * time.Second,
}