forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv.go
165 lines (144 loc) · 4.4 KB
/
csv.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package csv
import (
"encoding/csv"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"github.com/idoall/gocryptotrader/backtester/common"
gctkline "github.com/idoall/gocryptotrader/backtester/data/kline"
"github.com/idoall/gocryptotrader/currency"
"github.com/idoall/gocryptotrader/exchanges/asset"
"github.com/idoall/gocryptotrader/exchanges/kline"
"github.com/idoall/gocryptotrader/exchanges/order"
"github.com/idoall/gocryptotrader/exchanges/trade"
"github.com/idoall/gocryptotrader/log"
)
var errNoUSDData = errors.New("could not retrieve USD CSV candle data")
// LoadData is a basic csv reader which converts the found CSV file into a kline item
func LoadData(dataType int64, filepath, exchangeName string, interval time.Duration, fPair currency.Pair, a asset.Item, isUSDTrackingPair bool) (*gctkline.DataFromKline, error) {
resp := &gctkline.DataFromKline{}
csvFile, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer func() {
err = csvFile.Close()
if err != nil {
log.Errorln(log.BackTester, err)
}
}()
csvData := csv.NewReader(csvFile)
switch dataType {
case common.DataCandle:
candles := kline.Item{
Exchange: exchangeName,
Pair: fPair,
Asset: a,
Interval: kline.Interval(interval),
}
for {
row, errCSV := csvData.Read()
if errCSV != nil {
if errCSV == io.EOF {
break
}
return nil, fmt.Errorf("could not read csv data for %v %v %v, %v", exchangeName, a, fPair, errCSV)
}
candle := kline.Candle{}
v, errParse := strconv.ParseInt(row[0], 10, 32)
if errParse != nil {
return nil, errParse
}
candle.Time = time.Unix(v, 0).UTC()
if candle.Time.IsZero() {
err = fmt.Errorf("invalid timestamp received on row %v %v", row[0], err)
break
}
candle.Volume, err = strconv.ParseFloat(row[1], 64)
if err != nil {
err = fmt.Errorf("could not process candle volume %v %v", row[1], err)
break
}
candle.Open, err = strconv.ParseFloat(row[2], 64)
if err != nil {
err = fmt.Errorf("could not process candle volume %v %v", row[2], err)
break
}
candle.High, err = strconv.ParseFloat(row[3], 64)
if err != nil {
err = fmt.Errorf("could not process candle high %v %v", row[3], err)
break
}
candle.Low, err = strconv.ParseFloat(row[4], 64)
if err != nil {
err = fmt.Errorf("could not process candle low %v %v", row[4], err)
break
}
candle.Close, err = strconv.ParseFloat(row[5], 64)
if err != nil {
err = fmt.Errorf("could not process candle close %v %v", row[5], err)
break
}
candles.Candles = append(candles.Candles, candle)
}
if err != nil {
return nil, fmt.Errorf("could not read csv candle data for %v %v %v, %v", exchangeName, a, fPair, err)
}
resp.Item = candles
case common.DataTrade:
var trades []trade.Data
for {
row, errCSV := csvData.Read()
if errCSV != nil {
if errCSV == io.EOF {
break
}
return nil, errCSV
}
t := trade.Data{}
v, errParse := strconv.ParseInt(row[0], 10, 32)
if errParse != nil {
return nil, errParse
}
t.Timestamp = time.Unix(v, 0).UTC()
if t.Timestamp.IsZero() {
err = fmt.Errorf("invalid timestamp received on row %v", row)
break
}
t.Price, err = strconv.ParseFloat(row[1], 64)
if err != nil {
err = fmt.Errorf("could not process trade price %v, %v", row[1], err)
break
}
t.Amount, err = strconv.ParseFloat(row[2], 64)
if err != nil {
err = fmt.Errorf("could not process trade amount %v, %v", row[2], err)
break
}
t.Side, err = order.StringToOrderSide(row[3])
if err != nil {
err = fmt.Errorf("could not process trade side %v, %v", row[3], err)
break
}
trades = append(trades, t)
}
resp.Item, err = trade.ConvertTradesToCandles(kline.Interval(interval), trades...)
if err != nil {
return nil, fmt.Errorf("could not read csv trade data for %v %v %v, %v", exchangeName, a, fPair, err)
}
default:
if isUSDTrackingPair {
return nil, fmt.Errorf("%w for %v %v %v. Please add USD pair data to your CSV or set `disable-usd-tracking` to `true` in your config. %v", errNoUSDData, exchangeName, a, fPair, err)
}
return nil, fmt.Errorf("could not process csv data for %v %v %v, %w", exchangeName, a, fPair, common.ErrInvalidDataType)
}
resp.Item.Exchange = strings.ToLower(exchangeName)
resp.Item.Pair = fPair
resp.Item.Asset = a
resp.Item.Interval = kline.Interval(interval)
return resp, nil
}