-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfe5173
commit aa3c84c
Showing
8 changed files
with
157 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,6 @@ | |
|
||
# Prject configuration by Goland | ||
.idea/ | ||
.vscode/ | ||
|
||
vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package exchange | ||
|
||
import ( | ||
"fmt" | ||
"github.com/preichenberger/go-coinbasepro/v2" | ||
"github.com/sirupsen/logrus" | ||
"math" | ||
"net/http" | ||
"sort" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type coinbaseClient struct { | ||
coinbasepro *coinbasepro.Client | ||
} | ||
|
||
//type coinbaseClient *coinbasepro.Client | ||
|
||
func NewCoinBaseClient(httpClient *http.Client) *coinbaseClient { | ||
client := coinbasepro.NewClient() | ||
client.HTTPClient = httpClient | ||
return &coinbaseClient{coinbasepro: client} | ||
} | ||
|
||
func (client *coinbaseClient) GetName() string { | ||
return "Coinbase" | ||
} | ||
|
||
func (client *coinbaseClient) GetPriceRightAfter(candles []coinbasepro.HistoricRate, after time.Time) (float64, error) { | ||
for _, candle := range candles { | ||
if after.Equal(candle.Time) || after.After(candle.Time) { | ||
// Assume candles are sorted in desc order, so the first less than or equal to is the candle looking for | ||
logrus.Debugf("%s - Kline for %v uses open price at %v", client.GetName(), after.Local(), candle.Time.Local()) | ||
return candle.Open, nil | ||
} | ||
} | ||
return 0, fmt.Errorf("no time found right after %v", after) | ||
} | ||
|
||
func (client *coinbaseClient) GetSymbolPrice(symbol string) (*SymbolPrice, error) { | ||
ticker, err := client.coinbasepro.GetTicker(symbol) | ||
if err != nil { | ||
return nil, err | ||
} | ||
currentPrice, err := strconv.ParseFloat(ticker.Price, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var percentChange1h, percentChange24h = math.MaxFloat64, math.MaxFloat64 | ||
candles, err := client.coinbasepro.GetHistoricRates(symbol, coinbasepro.GetHistoricRatesParams{ | ||
Granularity: 300, | ||
}) | ||
if err != nil { | ||
logrus.Warnf("%s - Failed to get kline ticks, error: %v", client.GetName(), err) | ||
} else { | ||
now := time.Now() | ||
sort.Slice(candles, func(i, j int) bool { return candles[i].Time.After(candles[j].Time) }) | ||
|
||
lastHour := now.Add(-1 * time.Hour) | ||
price1hAgo, err := client.GetPriceRightAfter(candles, lastHour) | ||
if err != nil { | ||
logrus.Warnf("%s - Failed to get price 1 hour ago, error: %v\n", client.GetName(), err) | ||
} else if price1hAgo != 0 { | ||
percentChange1h = (currentPrice - price1hAgo) / price1hAgo * 100 | ||
} | ||
|
||
last24Hour := now.Add(-24 * time.Hour) | ||
price24hAgo, err := client.GetPriceRightAfter(candles, last24Hour) | ||
if err != nil { | ||
logrus.Warnf("%s - Failed to get price 24 hours ago, error: %v\n", client.GetName(), err) | ||
} else if price24hAgo != 0 { | ||
percentChange24h = (currentPrice - price24hAgo) / price24hAgo * 100 | ||
} | ||
} | ||
|
||
return &SymbolPrice{ | ||
Symbol: symbol, | ||
Price: ticker.Price, | ||
UpdateAt: time.Time(ticker.Time), | ||
Source: client.GetName(), | ||
PercentChange1h: percentChange1h, | ||
PercentChange24h: percentChange24h, | ||
}, nil | ||
} | ||
|
||
func init() { | ||
register((&coinbaseClient{}).GetName(), func(client *http.Client) ExchangeClient { | ||
// Limited by type system in Go, I hate wrapper/adapter | ||
return NewCoinBaseClient(client) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package exchange | ||
|
||
import ( | ||
"math" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestCoinbaseClient(t *testing.T) { | ||
|
||
var client = NewCoinBaseClient(http.DefaultClient) | ||
|
||
t.Run("GetSymbolPrice", func(t *testing.T) { | ||
sp, err := client.GetSymbolPrice("BTC-USd") | ||
|
||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
if sp.Price == "" { | ||
t.Fatalf("Get an empty price?") | ||
} | ||
if sp.PercentChange1h == math.MaxFloat64 { | ||
t.Logf("WARNING - PercentChange1h unset?") | ||
} | ||
if sp.PercentChange24h == math.MaxFloat64 { | ||
t.Logf("WARNING - PercentChange24h unset?") | ||
} | ||
}) | ||
|
||
t.Run("GetUnexistSymbolPrice", func(t *testing.T) { | ||
_, err := client.GetSymbolPrice("ABC123") | ||
|
||
if err == nil { | ||
t.Fatalf("Should throws on invalid symbol") | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,4 +78,8 @@ exchanges: | |
|
||
- name: Kraken | ||
tokens: | ||
- EOSETH | ||
- EOSETH | ||
|
||
- name: Coinbase | ||
tokens: | ||
- BTC-USD |