forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exchangerate.go
265 lines (221 loc) · 6.78 KB
/
exchangerate.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package exchangeratehost
import (
"context"
"errors"
"net/http"
"net/url"
"strconv"
"time"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/currency/forexprovider/base"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
)
// A client for the exchangerate.host API. NOTE: The format and callback
// parameters aren't supported as they're not needed for this implementation.
// Furthermore, the source is set to "ECB" as default
const (
timeLayout = "2006-01-02"
exchangeRateHostURL = "https://api.exchangerate.host"
)
var (
// DefaultSource uses the ecb for forex rates
DefaultSource = "ecb"
)
// Setup sets up the ExchangeRateHost config
func (e *ExchangeRateHost) Setup(config base.Settings) error {
e.Name = config.Name
e.Enabled = config.Enabled
e.RESTPollingDelay = config.RESTPollingDelay
e.Verbose = config.Verbose
e.PrimaryProvider = config.PrimaryProvider
e.Requester = request.New(e.Name,
common.NewHTTPClientWithTimeout(base.DefaultTimeOut))
return nil
}
// GetLatestRates returns a list of forex rates based on the supplied params
func (e *ExchangeRateHost) GetLatestRates(baseCurrency, symbols string, amount float64, places int64, source string) (*LatestRates, error) {
v := url.Values{}
if baseCurrency != "" {
v.Set("base", baseCurrency)
}
if symbols != "" {
v.Set("symbols", symbols)
}
if amount != 0 {
v.Set("amount", strconv.FormatFloat(amount, 'f', -1, 64))
}
if places != 0 {
v.Set("places", strconv.FormatInt(places, 10))
}
targetSource := DefaultSource
if source != "" {
targetSource = source
}
v.Set("source", targetSource)
var l LatestRates
return &l, e.SendHTTPRequest("latest", v, &l)
}
// ConvertCurrency converts a currency based on the supplied params
func (e *ExchangeRateHost) ConvertCurrency(from, to, baseCurrency, symbols, source string, date time.Time, amount float64, places int64) (*ConvertCurrency, error) {
v := url.Values{}
if from != "" {
v.Set("from", from)
}
if to != "" {
v.Set("to", to)
}
if !date.IsZero() {
v.Set("date", date.UTC().Format(timeLayout))
}
if baseCurrency != "" {
v.Set("base", baseCurrency)
}
if symbols != "" {
v.Set("symbols", symbols)
}
if amount != 0 {
v.Set("amount", strconv.FormatFloat(amount, 'f', -1, 64))
}
if places != 0 {
v.Set("places", strconv.FormatInt(places, 10))
}
targetSource := DefaultSource
if source != "" {
targetSource = source
}
v.Set("source", targetSource)
var c ConvertCurrency
return &c, e.SendHTTPRequest("convert", v, &c)
}
// GetHistoricalRates returns a list of historical rates based on the supplied params
func (e *ExchangeRateHost) GetHistoricalRates(date time.Time, baseCurrency, symbols string, amount float64, places int64, source string) (*HistoricRates, error) {
v := url.Values{}
if date.IsZero() {
date = time.Now()
}
fmtDate := date.UTC().Format(timeLayout)
v.Set("date", fmtDate)
if baseCurrency != "" {
v.Set("base", baseCurrency)
}
if symbols != "" {
v.Set("symbols", symbols)
}
if amount != 0 {
v.Set("amount", strconv.FormatFloat(amount, 'f', -1, 64))
}
if places != 0 {
v.Set("places", strconv.FormatInt(places, 10))
}
targetSource := DefaultSource
if source != "" {
targetSource = source
}
v.Set("source", targetSource)
var h HistoricRates
return &h, e.SendHTTPRequest(fmtDate, v, &h)
}
// GetTimeSeries returns time series forex data based on the supplied params
func (e *ExchangeRateHost) GetTimeSeries(startDate, endDate time.Time, baseCurrency, symbols string, amount float64, places int64, source string) (*TimeSeries, error) {
if startDate.IsZero() || endDate.IsZero() {
return nil, errors.New("startDate and endDate must be set")
}
if startDate.After(endDate) || startDate.Equal(endDate) {
return nil, errors.New("startDate and endDate must be set correctly")
}
v := url.Values{}
v.Set("start_date", startDate.UTC().Format(timeLayout))
v.Set("end_date", endDate.UTC().Format(timeLayout))
if baseCurrency != "" {
v.Set("base", baseCurrency)
}
if symbols != "" {
v.Set("symbols", symbols)
}
if amount != 0 {
v.Set("amount", strconv.FormatFloat(amount, 'f', -1, 64))
}
if places != 0 {
v.Set("places", strconv.FormatInt(places, 10))
}
targetSource := DefaultSource
if source != "" {
targetSource = source
}
v.Set("source", targetSource)
var t TimeSeries
return &t, e.SendHTTPRequest("timeseries", v, &t)
}
// GetFluctuations returns a list of forex price fluctuations based on the supplied params
func (e *ExchangeRateHost) GetFluctuations(startDate, endDate time.Time, baseCurrency, symbols string, amount float64, places int64, source string) (*Fluctuations, error) {
if startDate.IsZero() || endDate.IsZero() {
return nil, errors.New("startDate and endDate must be set")
}
if startDate.After(endDate) || startDate.Equal(endDate) {
return nil, errors.New("startDate and endDate must be set correctly")
}
v := url.Values{}
v.Set("start_date", startDate.UTC().Format(timeLayout))
v.Set("end_date", endDate.UTC().Format(timeLayout))
if baseCurrency != "" {
v.Set("base", baseCurrency)
}
if symbols != "" {
v.Set("symbols", symbols)
}
if amount != 0 {
v.Set("amount", strconv.FormatFloat(amount, 'f', -1, 64))
}
if places != 0 {
v.Set("places", strconv.FormatInt(places, 10))
}
targetSource := DefaultSource
if source != "" {
targetSource = source
}
v.Set("source", targetSource)
var f Fluctuations
return &f, e.SendHTTPRequest("fluctuation", v, &f)
}
// GetSupportedSymbols returns a list of supported symbols
func (e *ExchangeRateHost) GetSupportedSymbols() (*SupportedSymbols, error) {
var s SupportedSymbols
return &s, e.SendHTTPRequest("symbols", url.Values{}, &s)
}
// GetSupportedCurrencies returns a list of supported currencies
func (e *ExchangeRateHost) GetSupportedCurrencies() ([]string, error) {
s, err := e.GetSupportedSymbols()
if err != nil {
return nil, err
}
var symbols []string
for x := range s.Symbols {
symbols = append(symbols, x)
}
return symbols, nil
}
// GetRates returns the forex rates based on the supplied base currency and symbols
func (e *ExchangeRateHost) GetRates(baseCurrency, symbols string) (map[string]float64, error) {
l, err := e.GetLatestRates(baseCurrency, symbols, 0, 0, "")
if err != nil {
return nil, err
}
rates := make(map[string]float64)
for k, v := range l.Rates {
rates[baseCurrency+k] = v
}
return rates, nil
}
// SendHTTPRequest sends a typical get request
func (e *ExchangeRateHost) SendHTTPRequest(endpoint string, v url.Values, result interface{}) error {
path := common.EncodeURLValues(exchangeRateHostURL+"/"+endpoint, v)
item := &request.Item{
Method: http.MethodGet,
Path: path,
Result: &result,
Verbose: e.Verbose,
}
return e.Requester.SendPayload(context.TODO(), request.Unset, func() (*request.Item, error) {
return item, nil
})
}