forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
currency.go
207 lines (179 loc) · 5.56 KB
/
currency.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
package main
import (
"errors"
"fmt"
"log"
"net/url"
"strings"
"time"
)
type Rate struct {
Id string `json:"id"`
Name string `json:"Name"`
Rate float64 `json:",string"`
Date string `json:"Date"`
Time string `json:"Time"`
Ask float64 `json:",string"`
Bid float64 `json:",string"`
}
type YahooJSONResponseInfo struct {
Count int `json:"count"`
Created time.Time `json:"created"`
Lang string `json:"lang"`
}
type YahooJSONResponse struct {
Query struct {
YahooJSONResponseInfo
Results struct {
Rate []Rate `json:"rate"`
}
}
}
const (
MAX_CURRENCY_PAIRS_PER_REQUEST = 350
YAHOO_YQL_URL = "http://query.yahooapis.com/v1/public/yql"
YAHOO_DATABASE = "store://datatables.org/alltableswithkeys"
DEFAULT_CURRENCIES = "USD,AUD,EUR,CNY"
)
var (
CurrencyStore map[string]Rate
BaseCurrencies string
ErrCurrencyDataNotFetched = errors.New("Yahoo currency data has not been fetched yet.")
ErrCurrencyNotFound = errors.New("Unable to find specified currency.")
ErrQueryingYahoo = errors.New("Unable to query Yahoo currency values.")
ErrQueryingYahooZeroCount = errors.New("Yahoo returned zero currency data.")
)
func IsDefaultCurrency(currency string) bool {
return StringContains(DEFAULT_CURRENCIES, StringToUpper(currency))
}
func IsFiatCurrency(currency string) bool {
return StringContains(BaseCurrencies, StringToUpper(currency))
}
func IsCryptocurrency(currency string) bool {
return StringContains(bot.config.Cryptocurrencies, StringToUpper(currency))
}
func RetrieveConfigCurrencyPairs(config Config) error {
var fiatCurrencies, cryptoCurrencies []string
for _, exchange := range config.Exchanges {
if exchange.Enabled {
enabledPairs := SplitStrings(exchange.EnabledPairs, ",")
baseCurrencies := SplitStrings(exchange.BaseCurrencies, ",")
for _, x := range baseCurrencies {
for _, y := range enabledPairs {
if StringContains(y, "_") {
pairs := SplitStrings(y, "_")
for _, z := range pairs {
if z != x && !IsCryptocurrency(z) {
cryptoCurrencies = append(cryptoCurrencies, z)
} else if z == x && !IsDefaultCurrency(x) {
fiatCurrencies = append(fiatCurrencies, x)
}
}
} else {
if StringContains(y, x) {
if !IsDefaultCurrency(x) {
fiatCurrencies = append(fiatCurrencies, x)
}
currency := TrimString(y, x)
if !IsCryptocurrency(currency) {
cryptoCurrencies = append(cryptoCurrencies, currency)
}
} else {
if !IsCryptocurrency(y[0:3]) {
cryptoCurrencies = append(cryptoCurrencies, y[0:3])
}
if !IsCryptocurrency(y[3:]) {
cryptoCurrencies = append(cryptoCurrencies, y[3:])
}
}
}
}
}
}
}
bot.config.Cryptocurrencies = JoinStrings(StringSliceDifference(SplitStrings(bot.config.Cryptocurrencies, ","), cryptoCurrencies), ",")
BaseCurrencies = JoinStrings(StringSliceDifference(SplitStrings(DEFAULT_CURRENCIES, ","), fiatCurrencies), ",")
err := QueryYahooCurrencyValues(BaseCurrencies)
if err != nil {
return ErrQueryingYahoo
}
log.Println("Fetched currency value data.")
return nil
}
func MakecurrencyPairs(supportedCurrencies string) string {
currencies := SplitStrings(supportedCurrencies, ",")
pairs := []string{}
count := len(currencies)
for i := 0; i < count; i++ {
currency := currencies[i]
for j := 0; j < count; j++ {
if currency != currencies[j] {
pairs = append(pairs, currency+currencies[j])
}
}
}
return JoinStrings(pairs, ",")
}
func ConvertCurrency(amount float64, from, to string) (float64, error) {
currency := StringToUpper(from + to)
for x, y := range CurrencyStore {
if x == currency {
return amount * y.Rate, nil
}
}
return 0, ErrCurrencyNotFound
}
func FetchYahooCurrencyData(currencyPairs []string) error {
values := url.Values{}
values.Set("q", fmt.Sprintf("SELECT * from yahoo.finance.xchange WHERE pair in (\"%s\")", JoinStrings(currencyPairs, ",")))
values.Set("format", "json")
values.Set("env", YAHOO_DATABASE)
headers := make(map[string]string)
headers["Content-Type"] = "application/x-www-form-urlencoded"
resp, err := SendHTTPRequest("POST", YAHOO_YQL_URL, headers, strings.NewReader(values.Encode()))
if err != nil {
return err
}
yahooResp := YahooJSONResponse{}
err = JSONDecode([]byte(resp), &yahooResp)
if err != nil {
return err
}
if yahooResp.Query.Count == 0 {
return ErrQueryingYahooZeroCount
}
for i := 0; i < yahooResp.Query.YahooJSONResponseInfo.Count; i++ {
CurrencyStore[yahooResp.Query.Results.Rate[i].Id] = yahooResp.Query.Results.Rate[i]
}
return nil
}
func QueryYahooCurrencyValues(currencies string) error {
CurrencyStore = make(map[string]Rate)
currencyPairs := SplitStrings(MakecurrencyPairs(currencies), ",")
log.Printf("%d fiat currency pairs generated. Fetching Yahoo currency data (this may take a minute)..\n", len(currencyPairs))
var err error
var pairs []string
index := 0
if len(currencyPairs) > MAX_CURRENCY_PAIRS_PER_REQUEST {
for index < len(currencyPairs) {
if len(currencyPairs)-index > MAX_CURRENCY_PAIRS_PER_REQUEST {
pairs = currencyPairs[index : index+MAX_CURRENCY_PAIRS_PER_REQUEST]
index += MAX_CURRENCY_PAIRS_PER_REQUEST
} else {
pairs = currencyPairs[index:len(currencyPairs)]
index += (len(currencyPairs) - index)
}
err = FetchYahooCurrencyData(pairs)
if err != nil {
return err
}
}
} else {
pairs = currencyPairs[index:len(currencyPairs)]
err = FetchYahooCurrencyData(pairs)
if err != nil {
return err
}
}
return nil
}