(Feature) OHLCV data storage and retrieval (#522)

* WIP

* end of day WIP started migration of trade history

* added kline support to hitbtc huobi lbank

* added exchangehistory to all supported exchanges started work on coinbase 300 candles/request method

* end of day WIP

* removed unused ta and misc changes to flag ready for review

* yobit cleanup

* revert coinbase changES

* general code clean up and added zb support

* poloniex support added

* renamed method to FormatExchangeKlineInterval other misc fixes

* linter fixes

* linter fixes

* removed verbose

* fixed poloniex test coverage

* revert poloniex mock data

* regenerated poloniex mock data

* a very verbose clean up

* binance mock clean up

* removed unneeded t.Log()

* setting verbose to true to debug CI issue

* first pass changes addressed

* common.ErrNotYetImplemented implemented :D

* comments added

* WIP-addressed exchange requests and reverted previous GetExchangeHistory changes

* WIP-addressed exchange requests and reverted previous GetExchangeHistory changes

* increased test coverage added kraken support

* OKGroup support completed started work on address GetExchangeHistory feedback and migrating to own PR under https://github.com/xtda/gocryptotrader/tree/exchange_history

* convert zb ratelimits

* gofmt run on okcoin

* increased delay on rate limit

* gofmt package

* fixed panic with coinbene and bithumb if conversion fails

* very broken end of day WIP

* added support for GetHistoricCandlesEx to coinbase and binance

* gofmt package

* coinbase, btcmarkets, zb ex wrapper function added

* added all exchange support for ex regenerated mock data

* update bithumb to return wrapper method

* gofmt package

* end of day started work on changes

* models created for exchange/asset/currency/currencypair, new seed system created

* reworked test coverage added okgroup support general fixes/change requests addressed

* Added OneMonth

* limit checks on supportedexchanges

* reverted getexchangehistory

* started work on currency seeding and insertion

* reworked binance tesT

* added workaround for kraken panic

* :D extremely broken WIP

* renamed command to extended removed interval check on non-implemented commands

* added wrapperconfig back

* increased test coverage for FormatExchangeKlineInterval

* WIP

* increased test coverage for FormatExchangeKlineInterval bitfinex/gateio/huobi

* linter fixes

* zb kraken lbank coinbene btcmarkets support added

* removed verbose

* OK group support for other asset types added

* swapped margin to use spot endpoint

* index support added test coverage added for asset types

* added asset type to okcoin test

* gofmt

* add asset to extended method

* removed verbose

* Very broken WIP models need to be regenerated

* add support for coinbene swap increase test coverage

* removed verbose

* small clean up of okgroup wrapper functions

* verbose to troubleshoot CI issues

* removed verbose

* added error check reverted coinbasechanges

* attempting to fix broken model generation

* readme updated

* :D i broke so much

* model regeneration fixed & complete

* candle model filled out

* removed unused start/finish started work on decoupling api requests from kline package

* restructured coinbene, bithumb methods, added bitstamp support

* kraken time fix

* BTCMarkets restructure

* typo fix

* removed test for futures due to contact changing

* removed test for futures due to contact changing

* added start/end date to extended method over range

* WIP

* added sync option to candles

* converted to assettranslator

* removed verbose

* removed verbose

* removed invalid char

* reverted incorrectly removed return

* added import

* further template updates

* macos hates my keyboard :D

* misc canges

* started work on creating kline from databases eed

* x -> i

* removed verbose

* updated fixCasing to allocate var before checks

* sqlite3 supported work started

* removed time conversion

* further work on tets

* sort all outgoing kline candles

* fixCasing fix

* after/before checks added

* added parallel to test

* logic check on BTCmarkets

* removed unused param, used correct iterator

* converted HitBTC to use time.Time

* test update

* add iszero false check to candle times

* Seed exchange & OHCLV data for test usage

* updated resultlimit to 5000

* new line added

* added comment to exported const

* move date forward

* use configured ratelimit

* fixed pair for test

* panic fixed WIP on fixCasing

* fixCasing rework, started work on readme docs

* enable rate limiter for wrapper issues tool

* docs updated

* removed unused vars from tests

* removed err from return and formatted currency

* updated Yobit supported status

* Updated HitBTC to use onehour candles due to test exeuction times

* added further details to gctcli output

* added link to docs

* added link to tempalte

* disable FTX websocket in config_example

* fix poloneix

* regenerated poloniex mock data

* removed recording flag

* format on package

* moved exchange var outside of method scope

* reworked seeding into package

* verbose output improved

* removed verbose from candles

* Added comments to exported functions

* removed verbose output

* Reworking of tests

* end of day commit

* added SQLite migration for asset, test updates for exchange, added support for withdrawal for new exchange_name_id relation

* regenerated database models

* WIP

* test rework, sqlite migration updates for withdrawal

* Reworked error returns to stop duplications, format all output to UTC, changed gctscript OHLCV output to be unix timestmap, started work on seeding tool

* gofmt

* dbseed command for seeding exchanges added, removed seed from dbmigrate, LoadCSV method added for exchange

* go mod tidy

* added import candle from csv command to dbseed

* Removed reset & duplicate migrate command from helper, renamed struct to Item/Candle over Candle/Tick, added test coverage to dbseed, improved withdraw tests

* remove broken tests due to ORM generation of Fk, removed go unneeded goroutine for inserting records

* removed t.Cleanup usage because appveyor

* added test coverage to StoreInDatabase()

* removed unneeded data from config for test

* added new line

* Added down migration support to candle/asset removal, return original error and display rollback error

* removed unneeded err assignment, break out of loop on error

* add err check to method for test

* first pass changes

* WIP

* Updated migrations for both sqlite3 & postgres to create exchanges if any withdrawal_history records are found, removed migrate command

* removed argusage as usage information is provided by flags

* added inserted records return count and test coverage

* new line added

* Database: comment config details out to disable local postgresl testing by default

* added asset support

* Database: added error return when no exchanges are found, title exchange name across queries

* Fixed test data

* Database: removed migration bool from ConnectToDatabase(), removed empty line, insert asset on test

* Database: verbose linter :D

* Database/OHLCV: removed go module from dbseed command and reverted back to main module, converted interval to int64 to match other parts of code base, provided migrations to update database, poloniex fix to skip first candle

* dbseed: add completed message to output

* Database: added migration to add asset to uniq index for candle table

* Database: database -> exchange

* Database: add asset to upsert conflict

* Poloneix: fix for invalid interval

* regenerated poloniex mock data

* Database: added down migration for candle interval update

* OHCLV/Database: WIP

* OHLCV/Datastore: added new ValidateKline() method to check that asset, pair, interval are enabled/supported by exchange and updated tests

* revert configtest changes

* OHCLV/Kline: pointer assignment to ErrorKline & format pairs on check

* goimports

* migration updates

* Database/Candle: updated tests

* revert configtest changes

* ZB: updaed defaults to use uppercase pairs

* ZB: updaed defaults to use uppercase pairs

* revert pair formatting

* Switch over to .Cotains() method from pairManagement

* Added comment & ftx back 😆

* OHLCV/Datastore: (Candles): added not null to asset, (WithdrawalHistory): added not null to exchange_name_id), reworked KlineError return

* set verbose to false

* updated btc markets test

* Updated readme

* removed ability to have blank exchange name as indexing requires it

* remove old usedb and empty exchange check, return error on no data instead

* Updated kline test to match date range

* Renamed candles.exchange_id column to candles.xchange_name_id to match withdrawal table, return err on unwrap, zb fix revert

* regenerated sqlite3 models

* force UTC for sqlite.... because sqlite

* OHLCV/Datastore: upper case pairs on insert for consistency, remove unneeded rollback call on commit failure as it has no effect, move rollback outside of insertsqlite/insertpsql methods

* add error check for no candles

* nil length/cap is 0 in golang :D

* OHLCV/Datastore: updated wording on examples, removed duplicate testfile

* OHLCV/Datastore: updated README with links to dbseed

* dbtool -> dbseed
This commit is contained in:
Andrew
2020-09-03 17:13:53 +10:00
committed by GitHub
parent 4b0b0f97a1
commit 39bbd8dfc9
122 changed files with 21985 additions and 6020 deletions

View File

@@ -905,7 +905,7 @@ func TestExecutionTypeToOrderStatus(t *testing.T) {
}
func TestGetHistoricCandles(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTCUSDT")
currencyPair, err := currency.NewPairFromString("BTC-USDT")
if err != nil {
t.Fatal(err)
}
@@ -923,7 +923,7 @@ func TestGetHistoricCandles(t *testing.T) {
}
func TestGetHistoricCandlesExtended(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTCUSDT")
currencyPair, err := currency.NewPairFromString("BTC-USDT")
if err != nil {
t.Fatal(err)
}

View File

@@ -761,10 +761,8 @@ func (b *Binance) FormatExchangeKlineInterval(in kline.Interval) string {
// GetHistoricCandles returns candles between a time period for a set time interval
func (b *Binance) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !b.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := b.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
if kline.TotalCandlesPerInterval(start, end, interval) > b.Features.Enabled.Kline.ResultLimit {
@@ -812,10 +810,8 @@ func (b *Binance) GetHistoricCandles(pair currency.Pair, a asset.Item, start, en
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (b *Binance) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !b.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := b.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
ret := kline.Item{

View File

@@ -1253,7 +1253,7 @@ func TestGetHistoricCandles(t *testing.T) {
}
func TestGetHistoricCandlesExtended(t *testing.T) {
currencyPair, err := currency.NewPairFromString("TBTCUSD")
currencyPair, err := currency.NewPairFromString("BTCUSD")
if err != nil {
t.Fatal(err)
}

View File

@@ -821,10 +821,8 @@ func (b *Bitfinex) FormatExchangeKlineInterval(in kline.Interval) string {
// GetHistoricCandles returns candles between a time period for a set time interval
func (b *Bitfinex) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !b.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := b.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
if kline.TotalCandlesPerInterval(start, end, interval) > b.Features.Enabled.Kline.ResultLimit {
@@ -866,10 +864,8 @@ func (b *Bitfinex) GetHistoricCandles(pair currency.Pair, a asset.Item, start, e
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (b *Bitfinex) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !b.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := b.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
ret := kline.Item{

View File

@@ -552,7 +552,7 @@ func TestGetCandleStick(t *testing.T) {
}
func TestGetHistoricCandles(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTC_KRW")
currencyPair, err := currency.NewPairFromString("BTCKRW")
if err != nil {
t.Fatal(err)
}
@@ -564,7 +564,7 @@ func TestGetHistoricCandles(t *testing.T) {
}
func TestGetHistoricCandlesExtended(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTC_KRW")
currencyPair, err := currency.NewPairFromString("BTCKRW")
if err != nil {
t.Fatal(err)
}

View File

@@ -606,10 +606,8 @@ func (b *Bithumb) FormatExchangeKlineInterval(in kline.Interval) string {
// GetHistoricCandles returns candles between a time period for a set time interval
func (b *Bithumb) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !b.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := b.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
formattedPair, err := b.FormatExchangeCurrency(pair, a)

View File

@@ -678,7 +678,7 @@ func TestBitstamp_OHLC(t *testing.T) {
}
func TestBitstamp_GetHistoricCandles(t *testing.T) {
currencyPair, err := currency.NewPairFromString("btcusd")
currencyPair, err := currency.NewPairFromString("BTCUSD")
if err != nil {
t.Fatal(err)
}
@@ -692,7 +692,7 @@ func TestBitstamp_GetHistoricCandles(t *testing.T) {
}
func TestBitstamp_GetHistoricCandlesExtended(t *testing.T) {
currencyPair, err := currency.NewPairFromString("btcusd")
currencyPair, err := currency.NewPairFromString("BTCUSD")
if err != nil {
t.Fatal(err)
}

View File

@@ -674,10 +674,8 @@ func (b *Bitstamp) ValidateCredentials() error {
// GetHistoricCandles returns candles between a time period for a set time interval
func (b *Bitstamp) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !b.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := b.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
ret := kline.Item{
@@ -725,10 +723,8 @@ func (b *Bitstamp) GetHistoricCandles(pair currency.Pair, a asset.Item, start, e
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (b *Bitstamp) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !b.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := b.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
ret := kline.Item{

View File

@@ -1,7 +1,6 @@
package btcmarkets
import (
"errors"
"fmt"
"log"
"os"
@@ -729,7 +728,7 @@ func TestBTCMarkets_GetHistoricCandles(t *testing.T) {
}
_, err = b.GetHistoricCandles(p, asset.Spot, time.Now().Add(-time.Hour*24).UTC(), time.Now().UTC(), kline.FifteenMin)
if err != nil {
if !errors.As(err, &kline.ErrorKline{}) {
if err.Error() != "interval not supported" {
t.Fatal(err)
}
}

View File

@@ -800,10 +800,8 @@ func (b *BTCMarkets) FormatExchangeKlineInterval(in kline.Interval) string {
// GetHistoricCandles returns candles between a time period for a set time interval
func (b *BTCMarkets) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !b.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := b.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
if kline.TotalCandlesPerInterval(start, end, interval) > b.Features.Enabled.Kline.ResultLimit {
@@ -870,10 +868,8 @@ func (b *BTCMarkets) GetHistoricCandles(pair currency.Pair, a asset.Item, start,
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (b *BTCMarkets) GetHistoricCandlesExtended(p currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !b.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := b.ValidateKline(p, a, interval); err != nil {
return kline.Item{}, err
}
ret := kline.Item{

View File

@@ -81,7 +81,7 @@ func TestGetTrades(t *testing.T) {
func TestGetHistoricRatesGranularityCheck(t *testing.T) {
end := time.Now()
start := end.Add(-time.Hour * 2)
p := currency.NewPair(currency.BTC, currency.USD)
p := currency.NewPairWithDelimiter("BTC", "USD", "-")
_, err := c.GetHistoricCandles(p, asset.Spot, start, end, kline.OneHour)
if err != nil {
t.Fatal(err)
@@ -92,7 +92,7 @@ func TestCoinbasePro_GetHistoricCandlesExtended(t *testing.T) {
start := time.Unix(1546300800, 0)
end := time.Unix(1577836799, 0)
p := currency.NewPair(currency.BTC, currency.USD)
p := currency.NewPairWithDelimiter("BTC", "USD", "-")
_, err := c.GetHistoricCandlesExtended(p, asset.Spot, start, end, kline.OneDay)
if err != nil {
t.Fatal(err)

View File

@@ -762,10 +762,8 @@ func checkInterval(i time.Duration) (int64, error) {
// GetHistoricCandles returns a set of candle between two time periods for a
// designated time period
func (c *CoinbasePro) GetHistoricCandles(p currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !c.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := c.ValidateKline(p, a, interval); err != nil {
return kline.Item{}, err
}
if kline.TotalCandlesPerInterval(start, end, interval) > c.Features.Enabled.Kline.ResultLimit {
@@ -814,10 +812,8 @@ func (c *CoinbasePro) GetHistoricCandles(p currency.Pair, a asset.Item, start, e
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (c *CoinbasePro) GetHistoricCandlesExtended(p currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !c.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := c.ValidateKline(p, a, interval); err != nil {
return kline.Item{}, err
}
ret := kline.Item{

View File

@@ -708,7 +708,7 @@ func TestGetHistoricCandles(t *testing.T) {
t.Fatal(err)
}
currencyPairSwap, err := currency.NewPairFromString(swapTestPair)
currencyPairSwap, err := currency.NewPairFromString(spotTestPair)
if err != nil {
t.Fatal(err)
}

View File

@@ -776,10 +776,8 @@ func (c *Coinbene) FormatExchangeKlineInterval(in kline.Interval) string {
// GetHistoricCandles returns candles between a time period for a set time interval
func (c *Coinbene) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !c.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := c.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
formattedPair, err := c.FormatExchangeCurrency(pair, asset.PerpetualSwap)
@@ -805,6 +803,7 @@ func (c *Coinbene) GetHistoricCandles(pair currency.Pair, a asset.Item, start, e
Exchange: c.Name,
Pair: pair,
Interval: interval,
Asset: a,
}
for x := range candles.Data {

View File

@@ -1063,7 +1063,7 @@ func (e *Base) AuthenticateWebsocket() error {
}
// KlineIntervalEnabled returns if requested interval is enabled on exchange
func (e *Base) KlineIntervalEnabled(in kline.Interval) bool {
func (e *Base) klineIntervalEnabled(in kline.Interval) bool {
return e.Features.Enabled.Kline.Intervals[in.Word()]
}
@@ -1072,3 +1072,28 @@ func (e *Base) KlineIntervalEnabled(in kline.Interval) bool {
func (e *Base) FormatExchangeKlineInterval(in kline.Interval) string {
return strconv.FormatFloat(in.Duration().Seconds(), 'f', 0, 64)
}
// ValidateKline confirms that the requested pair, asset & interval are supported and/or enabled by the requested exchange
func (e *Base) ValidateKline(pair currency.Pair, a asset.Item, interval kline.Interval) error {
var errorList []string
var err kline.ErrorKline
if e.CurrencyPairs.IsAssetEnabled(a) != nil {
err.Asset = a
errorList = append(errorList, "asset not enabled")
} else if !e.CurrencyPairs.Pairs[a].Enabled.Contains(pair, true) {
err.Pair = pair
errorList = append(errorList, "pair not enabled")
}
if !e.klineIntervalEnabled(interval) {
err.Interval = interval
errorList = append(errorList, "interval not supported")
}
if len(errorList) > 0 {
err.Err = errors.New(strings.Join(errorList, ","))
return &err
}
return nil
}

View File

@@ -1872,6 +1872,54 @@ func Test_FormatExchangeKlineInterval(t *testing.T) {
}
}
func TestBase_ValidateKline(t *testing.T) {
pairs := currency.Pairs{
currency.Pair{Base: currency.BTC, Quote: currency.USDT},
}
availablePairs := currency.Pairs{
currency.Pair{Base: currency.BTC, Quote: currency.USDT},
currency.Pair{Base: currency.BTC, Quote: currency.AUD},
}
b := Base{
Name: "TESTNAME",
CurrencyPairs: currency.PairsManager{
Pairs: map[asset.Item]*currency.PairStore{
asset.Spot: {
AssetEnabled: convert.BoolPtr(true),
Enabled: pairs,
Available: availablePairs,
},
},
},
Features: Features{
Enabled: FeaturesEnabled{
Kline: kline.ExchangeCapabilitiesEnabled{
Intervals: map[string]bool{
kline.OneMin.Word(): true,
},
},
},
},
}
err := b.ValidateKline(availablePairs[0], asset.Spot, kline.OneMin)
if err != nil {
t.Fatalf("expected validation to pass received error: %v", err)
}
err = b.ValidateKline(availablePairs[1], asset.Spot, kline.OneYear)
if err == nil {
t.Fatal("expected validation to fail")
}
err = b.ValidateKline(availablePairs[1], asset.Index, kline.OneYear)
if err == nil {
t.Fatal("expected validation to fail")
}
}
func TestCheckTransientError(t *testing.T) {
b := Base{}
err := b.CheckTransientError(nil)
@@ -1993,7 +2041,7 @@ func TestAuthenticateWebsocket(t *testing.T) {
func TestKlineIntervalEnabled(t *testing.T) {
b := Base{}
if b.KlineIntervalEnabled(kline.EightHour) {
if b.klineIntervalEnabled(kline.EightHour) {
t.Fatal("unexpected value")
}
}

View File

@@ -855,7 +855,7 @@ func TestGetFundingHistory(t *testing.T) {
func TestGetHistoricCandles(t *testing.T) {
t.Parallel()
currencyPair, err := currency.NewPairFromString(spotPair)
currencyPair, err := currency.NewPairFromString("BTC/USD")
if err != nil {
t.Fatal(err)
}
@@ -869,7 +869,7 @@ func TestGetHistoricCandles(t *testing.T) {
func TestGetHistoricCandlesExtended(t *testing.T) {
t.Parallel()
currencyPair, err := currency.NewPairFromString(spotPair)
currencyPair, err := currency.NewPairFromString("BTC/USD")
if err != nil {
t.Fatal(err)
}

View File

@@ -931,10 +931,8 @@ func (f *FTX) ValidateCredentials() error {
// GetHistoricCandles returns candles between a time period for a set time interval
func (f *FTX) GetHistoricCandles(p currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !f.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := f.ValidateKline(p, a, interval); err != nil {
return kline.Item{}, err
}
formattedPair, err := f.FormatExchangeCurrency(p, a)
@@ -972,10 +970,8 @@ func (f *FTX) GetHistoricCandles(p currency.Pair, a asset.Item, start, end time.
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (f *FTX) GetHistoricCandlesExtended(p currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !f.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := f.ValidateKline(p, a, interval); err != nil {
return kline.Item{}, err
}
ret := kline.Item{

View File

@@ -733,10 +733,8 @@ func (g *Gateio) FormatExchangeKlineInterval(in kline.Interval) string {
// GetHistoricCandles returns candles between a time period for a set time interval
func (g *Gateio) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !g.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := g.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
hours := end.Sub(start).Hours()

View File

@@ -76,7 +76,7 @@ func TestGetChartCandles(t *testing.T) {
}
func TestGetHistoricCandles(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTCUSD")
currencyPair, err := currency.NewPairFromString("BTC-USD")
if err != nil {
t.Fatal(err)
}
@@ -94,7 +94,7 @@ func TestGetHistoricCandles(t *testing.T) {
}
func TestGetHistoricCandlesExtended(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTCUSD")
currencyPair, err := currency.NewPairFromString("BTC-USD")
if err != nil {
t.Fatal(err)
}

View File

@@ -700,10 +700,8 @@ func (h *HitBTC) FormatExchangeKlineInterval(in kline.Interval) string {
// GetHistoricCandles returns candles between a time period for a set time interval
func (h *HitBTC) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !h.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := h.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
formattedPair, err := h.FormatExchangeCurrency(pair, a)
@@ -742,12 +740,9 @@ func (h *HitBTC) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (h *HitBTC) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !h.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := h.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
ret := kline.Item{
Exchange: h.Name,
Pair: pair,

View File

@@ -90,7 +90,7 @@ func TestGetSpotKline(t *testing.T) {
}
func TestGetHistoricCandles(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTCUSDT")
currencyPair, err := currency.NewPairFromString("BTC-USDT")
if err != nil {
t.Fatal(err)
}
@@ -112,7 +112,7 @@ func TestGetHistoricCandles(t *testing.T) {
}
func TestGetHistoricCandlesExtended(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTCUSDT")
currencyPair, err := currency.NewPairFromString("BTC-USDT")
if err != nil {
t.Fatal(err)
}

View File

@@ -988,10 +988,8 @@ func (h *HUOBI) FormatExchangeKlineInterval(in kline.Interval) string {
// GetHistoricCandles returns candles between a time period for a set time interval
func (h *HUOBI) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !h.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := h.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
formattedPair, err := h.FormatExchangeCurrency(pair, a)

View File

@@ -104,7 +104,7 @@ func CreateKline(trades []order.TradeHistory, interval Interval, p currency.Pair
return candles, nil
}
// validatData checks for zero values on data and sorts before turning
// validateData checks for zero values on data and sorts before turning
// converting into OHLC
func validateData(trades []order.TradeHistory) error {
if len(trades) < 2 {
@@ -281,7 +281,7 @@ func CalcDateRanges(start, end time.Time, interval Interval, limit uint32) (out
}
y++
}
if allDateIntervals != nil {
if allDateIntervals != nil && lastNum+1 < len(allDateIntervals) {
out = append(out, DateRange{
Start: allDateIntervals[lastNum+1],
End: allDateIntervals[len(allDateIntervals)-1],
@@ -299,3 +299,10 @@ func (k *Item) SortCandlesByTimestamp(asc bool) {
return k.Candles[i].Time.Before(k.Candles[j].Time)
})
}
// FormatDates converts all date to UTC time
func (k *Item) FormatDates() {
for x := range k.Candles {
k.Candles[x].Time = k.Candles[x].Time.UTC()
}
}

View File

@@ -0,0 +1,155 @@
package kline
import (
"encoding/csv"
"errors"
"fmt"
"io"
"os"
"strconv"
"time"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/database/repository/candle"
"github.com/thrasher-corp/gocryptotrader/database/repository/exchange"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/log"
)
// LoadFromDatabase returns Item from database seeded data
func LoadFromDatabase(exchange string, pair currency.Pair, a asset.Item, interval Interval, start, end time.Time) (Item, error) {
retCandle, err := candle.Series(exchange,
pair.Base.String(), pair.Quote.String(),
int64(interval.Duration().Seconds()), a.String(), start, end)
if err != nil {
return Item{}, err
}
ret := Item{
Exchange: exchange,
Pair: pair,
Interval: interval,
}
for x := range retCandle.Candles {
ret.Candles = append(ret.Candles, Candle{
Time: retCandle.Candles[x].Timestamp,
Open: retCandle.Candles[x].Open,
High: retCandle.Candles[x].High,
Low: retCandle.Candles[x].Low,
Close: retCandle.Candles[x].Close,
Volume: retCandle.Candles[x].Volume,
})
}
return ret, nil
}
// StoreInDatabase returns Item from database seeded data
func StoreInDatabase(in *Item) (uint64, error) {
if in.Exchange == "" {
return 0, errors.New("name cannot be blank")
}
if (in.Pair == currency.Pair{}) {
return 0, errors.New("currency pair cannot be empty")
}
if in.Asset == "" {
return 0, errors.New("asset cannot be blank")
}
if len(in.Candles) < 1 {
return 0, errors.New("candle data is empty")
}
exchangeUUID, err := exchange.UUIDByName(in.Exchange)
if err != nil {
return 0, err
}
databaseCandles := candle.Item{
ExchangeID: exchangeUUID.String(),
Base: in.Pair.Base.Upper().String(),
Quote: in.Pair.Quote.Upper().String(),
Interval: int64(in.Interval.Duration().Seconds()),
Asset: in.Asset.String(),
}
for x := range in.Candles {
databaseCandles.Candles = append(databaseCandles.Candles, candle.Candle{
Timestamp: in.Candles[x].Time,
Open: in.Candles[x].Open,
High: in.Candles[x].High,
Low: in.Candles[x].Low,
Close: in.Candles[x].Close,
Volume: in.Candles[x].Volume,
})
}
return candle.Insert(&databaseCandles)
}
// LoadFromGCTScriptCSV loads kline data from a CSV file
func LoadFromGCTScriptCSV(file string) (out []Candle, errRet error) {
csvFile, err := os.Open(file)
if err != nil {
return out, err
}
defer func() {
err = csvFile.Close()
if err != nil {
log.Errorln(log.Global, err)
}
}()
csvData := csv.NewReader(csvFile)
for {
row, errCSV := csvData.Read()
if errCSV != nil {
if errCSV == io.EOF {
break
}
return out, errCSV
}
tempCandle := Candle{}
v, errParse := strconv.ParseInt(row[0], 10, 32)
if errParse != nil {
err = errParse
break
}
tempCandle.Time = time.Unix(v, 0).UTC()
if tempCandle.Time.IsZero() {
err = fmt.Errorf("invalid timestamp received on row %v", row)
break
}
tempCandle.Volume, err = strconv.ParseFloat(row[1], 64)
if err != nil {
break
}
tempCandle.Open, err = strconv.ParseFloat(row[2], 64)
if err != nil {
break
}
tempCandle.High, err = strconv.ParseFloat(row[3], 64)
if err != nil {
break
}
tempCandle.Low, err = strconv.ParseFloat(row[4], 64)
if err != nil {
break
}
tempCandle.Close, err = strconv.ParseFloat(row[5], 64)
if err != nil {
break
}
out = append(out, tempCandle)
}
return out, err
}

View File

@@ -1,17 +1,35 @@
package kline
import (
"errors"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/database/drivers"
"github.com/thrasher-corp/gocryptotrader/database/repository/candle"
"github.com/thrasher-corp/gocryptotrader/database/repository/exchange"
"github.com/thrasher-corp/gocryptotrader/database/testhelpers"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
)
var (
verbose = false
testExchanges = []exchange.Details{
{
Name: "one",
},
}
)
func TestValidateData(t *testing.T) {
err := validateData(nil)
if err == nil {
@@ -86,7 +104,7 @@ func TestCreateKline(t *testing.T) {
trades = append(trades, order.TradeHistory{
Timestamp: time.Now().Add((time.Duration(rand.Intn(10)) * time.Minute) +
(time.Duration(rand.Intn(10)) * time.Second)),
TID: crypto.HexEncodeToString([]byte(string(i))),
TID: crypto.HexEncodeToString([]byte(string(rune(i)))),
Amount: float64(rand.Intn(20)) + 1,
Price: 1000 + float64(rand.Intn(1000)),
})
@@ -237,14 +255,24 @@ func TestDurationToWord(t *testing.T) {
func TestKlineErrors(t *testing.T) {
v := ErrorKline{
Interval: OneYear,
Pair: currency.NewPair(currency.BTC, currency.AUD),
Err: errors.New("hello world"),
}
if v.Error() != "oneyear interval unsupported by exchange" {
t.Fatal("unexpected error returned")
if v.Interval != OneYear {
t.Fatalf("expected OneYear received %v:", v.Interval)
}
if v.Unwrap().Error() != "8760h0m0s interval unsupported by exchange" {
t.Fatal("unexpected error returned")
if v.Pair != currency.NewPair(currency.BTC, currency.AUD) {
t.Fatalf("expected OneYear received %v:", v.Pair)
}
if v.Error() != "hello world" {
t.Fatal("expected error return received empty value")
}
if v.Unwrap().Error() != "hello world" {
t.Fatal("expected error return received empty value")
}
}
@@ -381,7 +409,7 @@ func TestCalcDateRanges(t *testing.T) {
v = CalcDateRanges(time.Now(), time.Now().AddDate(0, 0, 1), OneDay, 100)
if len(v) != 1 {
t.Fatal("expected CalcDateRanges() with a Candle count lower than limit to return 1 result")
t.Fatal("expected CalcDateRanges() with a Item count lower than limit to return 1 result")
}
}
@@ -394,7 +422,7 @@ func TestItem_SortCandlesByTimestamp(t *testing.T) {
}
for x := 0; x < 100; x++ {
y := rand.Float64()
y := rand.Float64() // nolint gosec: used for generating test data no need to import crypo/rand
tempKline.Candles = append(tempKline.Candles,
Candle{
Time: time.Now().AddDate(0, 0, -x),
@@ -416,3 +444,240 @@ func TestItem_SortCandlesByTimestamp(t *testing.T) {
t.Fatal("expected kline.Candles to be in ascending order")
}
}
func setupTest(t *testing.T) {
if verbose {
testhelpers.EnableVerboseTestOutput()
}
var err error
testhelpers.MigrationDir = filepath.Join("..", "..", "database", "migrations")
testhelpers.PostgresTestDatabase = testhelpers.GetConnectionDetails()
testhelpers.TempDir, err = ioutil.TempDir("", "gct-temp")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
}
func TestStoreInDatabase(t *testing.T) {
setupTest(t)
testCases := []struct {
name string
config *database.Config
seedDB func(bool) error
runner func(t *testing.T)
closer func(dbConn *database.Instance) error
}{
{
name: "postgresql",
config: testhelpers.PostgresTestDatabase,
seedDB: seedDB,
},
{
name: "SQLite",
config: &database.Config{
Driver: database.DBSQLite3,
ConnectionDetails: drivers.ConnectionDetails{Database: "./testdb"},
},
seedDB: seedDB,
},
}
for x := range testCases {
test := testCases[x]
t.Run(test.name, func(t *testing.T) {
if !testhelpers.CheckValidConfig(&test.config.ConnectionDetails) {
t.Skip("database not configured skipping test")
}
dbConn, err := testhelpers.ConnectToDatabase(test.config)
if err != nil {
t.Fatal(err)
}
if test.seedDB != nil {
err = test.seedDB(false)
if err != nil {
t.Error(err)
}
}
_, ohlcvData, err := genOHCLVData()
if err != nil {
t.Fatal(err)
}
r, err := StoreInDatabase(&ohlcvData)
if err != nil {
t.Fatal(err)
}
if r != 365 {
t.Fatalf("unexpected number inserted: %v", r)
}
err = testhelpers.CloseDatabase(dbConn)
if err != nil {
t.Error(err)
}
})
}
err := os.RemoveAll(testhelpers.TempDir)
if err != nil {
t.Fatalf("Failed to remove temp db file: %v", err)
}
}
func TestLoadFromDatabase(t *testing.T) {
setupTest(t)
testCases := []struct {
name string
config *database.Config
seedDB func(bool) error
runner func(t *testing.T)
closer func(dbConn *database.Instance) error
}{
{
name: "postgresql",
config: testhelpers.PostgresTestDatabase,
seedDB: seedDB,
},
{
name: "SQLite",
config: &database.Config{
Driver: database.DBSQLite3,
ConnectionDetails: drivers.ConnectionDetails{Database: "./testdb"},
},
seedDB: seedDB,
},
}
for x := range testCases {
test := testCases[x]
t.Run(test.name, func(t *testing.T) {
if !testhelpers.CheckValidConfig(&test.config.ConnectionDetails) {
t.Skip("database not configured skipping test")
}
dbConn, err := testhelpers.ConnectToDatabase(test.config)
if err != nil {
t.Fatal(err)
}
if test.seedDB != nil {
err = test.seedDB(true)
if err != nil {
t.Error(err)
}
}
p, err := currency.NewPairFromString("BTCUSDT")
if err != nil {
t.Fatal(err)
}
start := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
end := start.AddDate(1, 0, 0)
ret, err := LoadFromDatabase(testExchanges[0].Name, p, asset.Spot, OneDay, start, end)
if err != nil {
t.Fatal(err)
}
if ret.Exchange != testExchanges[0].Name {
t.Fatalf("uncorrect data returned: %v", ret.Exchange)
}
err = testhelpers.CloseDatabase(dbConn)
if err != nil {
t.Error(err)
}
})
}
err := os.RemoveAll(testhelpers.TempDir)
if err != nil {
t.Fatalf("Failed to remove temp db file: %v", err)
}
}
// TODO: find a better way to handle this to remove duplication between candle test
func seedDB(includeOHLCVData bool) error {
err := exchange.InsertMany(testExchanges)
if err != nil {
return err
}
if includeOHLCVData {
data, _, err := genOHCLVData()
if err != nil {
return err
}
_, err = candle.Insert(&data)
return err
}
return nil
}
func genOHCLVData() (out candle.Item, outItem Item, err error) {
exchangeUUID, err := exchange.UUIDByName(testExchanges[0].Name)
if err != nil {
return
}
out.ExchangeID = exchangeUUID.String()
out.Base = currency.BTC.String()
out.Quote = currency.USDT.String()
out.Interval = 86400
out.Asset = "spot"
start := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
for x := 0; x < 365; x++ {
out.Candles = append(out.Candles, candle.Candle{
Timestamp: start.Add(time.Hour * 24 * time.Duration(x)),
Open: 1000,
High: 1000,
Low: 1000,
Close: 1000,
Volume: 1000,
})
}
outItem.Interval = OneDay
outItem.Asset = asset.Spot
outItem.Pair = currency.NewPair(currency.BTC, currency.USDT)
outItem.Exchange = testExchanges[0].Name
for x := 0; x < 365; x++ {
outItem.Candles = append(outItem.Candles, Candle{
Time: start.Add(time.Hour * 24 * time.Duration(x)),
Open: 1000,
High: 1000,
Low: 1000,
Close: 1000,
Volume: 1000,
})
}
return out, outItem, nil
}
func TestLoadCSV(t *testing.T) {
v, err := LoadFromGCTScriptCSV(filepath.Join("..", "..", "testdata", "binance_BTCUSDT_24h_2019_01_01_2020_01_01.csv"))
if err != nil {
t.Fatal(err)
}
if v[0].Time.UTC() != time.Unix(1546300800, 0).UTC() {
t.Fatalf("unexpected value received: %v", v[0].Time)
}
if v[269].Close != 8177.91 {
t.Fatalf("unexpected value received: %v", v[269].Close)
}
if v[364].Open != 7246 {
t.Fatalf("unexpected value received: %v", v[364].Open)
}
}

View File

@@ -1,7 +1,6 @@
package kline
import (
"fmt"
"time"
"github.com/thrasher-corp/gocryptotrader/currency"
@@ -34,8 +33,6 @@ const (
)
const (
// ErrUnsupportedInterval locale for an unsupported interval
ErrUnsupportedInterval = "%s interval unsupported by exchange"
// ErrRequestExceedsExchangeLimits locale for exceeding rate limits message
ErrRequestExceedsExchangeLimits = "requested data would exceed exchange limits please lower range or use GetHistoricCandlesEx"
)
@@ -76,17 +73,20 @@ type Interval time.Duration
// ErrorKline struct to hold kline interval errors
type ErrorKline struct {
Asset asset.Item
Pair currency.Pair
Interval Interval
Err error
}
// Error returns short interval unsupported message
func (k ErrorKline) Error() string {
return fmt.Sprintf(ErrUnsupportedInterval, k.Interval.Word())
func (k *ErrorKline) Error() string {
return k.Err.Error()
}
// Unwrap returns interval unsupported message
func (k *ErrorKline) Unwrap() error {
return fmt.Errorf(ErrUnsupportedInterval, k.Interval)
return k.Err
}
// DateRange holds a start and end date for kline usage

View File

@@ -1410,7 +1410,7 @@ func TestParseTime(t *testing.T) {
}
func TestGetHistoricCandles(t *testing.T) {
currencyPair, err := currency.NewPairFromString("XBTUSD")
currencyPair, err := currency.NewPairFromString("XBT-USD")
if err != nil {
t.Fatal(err)
}
@@ -1426,7 +1426,7 @@ func TestGetHistoricCandles(t *testing.T) {
}
func TestGetHistoricCandlesExtended(t *testing.T) {
currencyPair, err := currency.NewPairFromString("XBTUSD")
currencyPair, err := currency.NewPairFromString("XBT-USD")
if err != nil {
t.Fatal(err)
}

View File

@@ -863,10 +863,8 @@ func (k *Kraken) FormatExchangeKlineInterval(in kline.Interval) string {
// GetHistoricCandles returns candles between a time period for a set time interval
func (k *Kraken) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !k.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := k.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
ret := kline.Item{
@@ -909,12 +907,9 @@ func (k *Kraken) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (k *Kraken) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !k.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := k.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
ret := kline.Item{
Exchange: k.Name,
Pair: pair,

View File

@@ -410,7 +410,7 @@ func TestGetOrderHistory(t *testing.T) {
func TestGetHistoricCandles(t *testing.T) {
t.Parallel()
pair, err := currency.NewPairFromString(testCurrencyPair)
pair, err := currency.NewPairFromString("eth_btc")
if err != nil {
t.Fatal(err)
}
@@ -430,7 +430,7 @@ func TestGetHistoricCandlesExtended(t *testing.T) {
startTime := time.Now().Add(-time.Hour)
end := time.Now()
pair, err := currency.NewPairFromString(testCurrencyPair)
pair, err := currency.NewPairFromString("eth_btc")
if err != nil {
t.Fatal(err)
}

View File

@@ -772,10 +772,8 @@ func (l *Lbank) FormatExchangeKlineInterval(in kline.Interval) string {
// GetHistoricCandles returns candles between a time period for a set time interval
func (l *Lbank) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !l.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := l.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
formattedPair, err := l.FormatExchangeCurrency(pair, a)
@@ -815,11 +813,10 @@ func (l *Lbank) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (l *Lbank) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !l.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := l.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
ret := kline.Item{
Exchange: l.Name,
Pair: pair,

View File

@@ -1090,7 +1090,7 @@ func TestGetOrderbook(t *testing.T) {
}
func TestGetHistoricCandles(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTCUSDT")
currencyPair, err := currency.NewPairFromString("BTC-USD")
if err != nil {
t.Fatal(err)
}
@@ -1102,7 +1102,7 @@ func TestGetHistoricCandles(t *testing.T) {
}
func TestGetHistoricCandlesExtended(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTCUSDT")
currencyPair, err := currency.NewPairFromString("BTC-USD")
if err != nil {
t.Fatal(err)
}

View File

@@ -316,10 +316,8 @@ func (o *OKCoin) FetchTicker(p currency.Pair, assetType asset.Item) (tickerData
// GetHistoricCandles returns candles between a time period for a set time interval
func (o *OKCoin) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !o.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := o.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
formattedPair, err := o.FormatExchangeCurrency(pair, a)
@@ -389,10 +387,8 @@ func (o *OKCoin) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (o *OKCoin) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !o.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := o.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
ret := kline.Item{

View File

@@ -529,7 +529,7 @@ func TestGetSpotMarketData(t *testing.T) {
}
func TestGetHistoricCandles(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTCUSDT")
currencyPair, err := currency.NewPairFromString("EOS-USDT")
if err != nil {
t.Fatal(err)
}
@@ -549,7 +549,7 @@ func TestGetHistoricCandles(t *testing.T) {
t.Fatal("unexpected result")
}
swapPair, err := currency.NewPairFromString("BTC-USD_SWAP")
swapPair, err := currency.NewPairFromString("EOS-USD_SWAP")
if err != nil {
t.Fatal(err)
}
@@ -560,7 +560,7 @@ func TestGetHistoricCandles(t *testing.T) {
}
func TestGetHistoricCandlesExtended(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTCUSDT")
currencyPair, err := currency.NewPairFromString("EOS-USDT")
if err != nil {
t.Fatal(err)
}

View File

@@ -543,10 +543,8 @@ func (o *OKEX) FetchTicker(p currency.Pair, assetType asset.Item) (tickerData *t
// GetHistoricCandles returns candles between a time period for a set time interval
func (o *OKEX) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !o.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := o.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
formattedPair, err := o.FormatExchangeCurrency(pair, a)
@@ -617,10 +615,8 @@ func (o *OKEX) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end t
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (o *OKEX) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !o.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := o.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
ret := kline.Item{

View File

@@ -525,7 +525,7 @@ func TestWsHandleAccountData(t *testing.T) {
}
func TestGetHistoricCandles(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTCLTC")
currencyPair, err := currency.NewPairFromString("BTC_LTC")
if err != nil {
t.Fatal(err)
}
@@ -546,7 +546,7 @@ func TestGetHistoricCandles(t *testing.T) {
}
func TestGetHistoricCandlesExtended(t *testing.T) {
currencyPair, err := currency.NewPairFromString("BTCLTC")
currencyPair, err := currency.NewPairFromString("BTC_LTC")
if err != nil {
t.Fatal(err)
}

View File

@@ -663,10 +663,8 @@ func (p *Poloniex) ValidateCredentials() error {
// GetHistoricCandles returns candles between a time period for a set time interval
func (p *Poloniex) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !p.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := p.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
formattedPair, err := p.FormatExchangeCurrency(pair, a)
@@ -674,8 +672,11 @@ func (p *Poloniex) GetHistoricCandles(pair currency.Pair, a asset.Item, start, e
return kline.Item{}, err
}
// we use Truncate here to round star to the nearest even number that matches the requested interval
// example 10:17 with an interval of 15 minutes will go down 10:15
// this is due to poloniex returning a non-complete candle if the time does not match
candles, err := p.GetChartData(formattedPair.String(),
start, end,
start.Truncate(interval.Duration()), end,
p.FormatExchangeKlineInterval(interval))
if err != nil {
return kline.Item{}, err

View File

@@ -840,7 +840,6 @@ func TestGetHistoricCandles(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = z.GetHistoricCandles(currencyPair, asset.Spot, startTime, time.Now(), kline.Interval(time.Hour*7))
if err == nil {
t.Fatal("unexpected result")

View File

@@ -749,10 +749,8 @@ func (z *ZB) FormatExchangeKlineInterval(in kline.Interval) string {
// GetHistoricCandles returns candles between a time period for a set time interval
func (z *ZB) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
if !z.KlineIntervalEnabled(interval) {
return kline.Item{}, kline.ErrorKline{
Interval: interval,
}
if err := z.ValidateKline(pair, a, interval); err != nil {
return kline.Item{}, err
}
formattedPair, err := z.FormatExchangeCurrency(pair, a)