Skip to content

Commit

Permalink
Initial kline trade converter && restructure wrapper functions (thras…
Browse files Browse the repository at this point in the history
…her-corp#454)

* Initial kline trade converter && restructure wrapper function

* Addr nits

* fix linter issues

* fix requested

* fix after merge interface issue with fakepassingexchange

* consistentizations

* Addr glorious nits

* Added in explicit interval strings for gctcli client (ease of use)

* rm value stutter

* Addr nits

* update protobuf and push regen

* go mod tidy

* change description of usage for granularity
  • Loading branch information
shazbert authored Mar 15, 2020
1 parent 99e8d2f commit 2c7e531
Show file tree
Hide file tree
Showing 67 changed files with 976 additions and 657 deletions.
39 changes: 30 additions & 9 deletions cmd/gctcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3964,7 +3964,7 @@ var candleRangeSize, candleGranularity int64
var getHistoricCandlesCommand = cli.Command{
Name: "gethistoriccandles",
Usage: "gets historical candles for the specified granularity up to range size time from now.",
ArgsUsage: "<exchange> <pair> <rangesize> <granularity>",
ArgsUsage: "<exchange> <pair> <asset> <rangesize> <granularity>",
Action: getHistoricCandles,
Flags: []cli.Flag{
cli.StringFlag{
Expand All @@ -3975,6 +3975,10 @@ var getHistoricCandlesCommand = cli.Command{
Name: "pair",
Usage: "the currency pair to get the candles for",
},
cli.StringFlag{
Name: "asset",
Usage: "the asset type of the currency pair",
},
cli.Int64Flag{
Name: "rangesize, r",
Usage: "the amount of time to go back from now to fetch candles in the given granularity",
Expand All @@ -3983,7 +3987,7 @@ var getHistoricCandlesCommand = cli.Command{
},
cli.Int64Flag{
Name: "granularity, g",
Usage: "value is in seconds and can be one of the following {60, 300, 900, 3600, 21600, 86400}",
Usage: "example values are in seconds and can be one of the following {60 (1 Minute), 300 (5 Minute), 900 (15 Minute), 3600 (1 Hour), 21600 (6 Hour), 86400 (1 Day)}",
Value: 86400,
Destination: &candleGranularity,
},
Expand Down Expand Up @@ -4017,21 +4021,32 @@ func getHistoricCandles(c *cli.Context) error {
}
p := currency.NewPairDelimiter(currencyPair, pairDelimiter)

var assetType string
if c.IsSet("asset") {
assetType = c.String("asset")
} else {
assetType = c.Args().Get(2)
}

if !validAsset(assetType) {
return errInvalidAsset
}

if c.IsSet("rangesize") {
candleRangeSize = c.Int64("rangesize")
} else if c.Args().Get(2) != "" {
} else if c.Args().Get(3) != "" {
var err error
candleRangeSize, err = strconv.ParseInt(c.Args().Get(2), 10, 64)
candleRangeSize, err = strconv.ParseInt(c.Args().Get(3), 10, 64)
if err != nil {
return err
}
}

if c.IsSet("granularity") {
candleGranularity = c.Int64("granularity")
} else if c.Args().Get(3) != "" {
} else if c.Args().Get(4) != "" {
var err error
candleGranularity, err = strconv.ParseInt(c.Args().Get(3), 10, 64)
candleGranularity, err = strconv.ParseInt(c.Args().Get(4), 10, 64)
if err != nil {
return err
}
Expand All @@ -4043,6 +4058,11 @@ func getHistoricCandles(c *cli.Context) error {
}
defer conn.Close()

candleInterval := time.Duration(candleGranularity) * time.Second

end := time.Now().UTC().Truncate(candleInterval)
start := end.Add(-candleInterval * time.Duration(candleRangeSize))

client := gctrpc.NewGoCryptoTraderClient(conn)
result, err := client.GetHistoricCandles(context.Background(),
&gctrpc.GetHistoricCandlesRequest{
Expand All @@ -4052,10 +4072,11 @@ func getHistoricCandles(c *cli.Context) error {
Base: p.Base.String(),
Quote: p.Quote.String(),
},
Rangesize: candleRangeSize,
Granularity: candleGranularity,
AssetType: assetType,
Start: start.Unix(),
End: end.Unix(),
TimeInterval: int64(candleInterval),
})

if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions engine/fake_exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
Expand Down Expand Up @@ -176,8 +177,8 @@ func (h *FakePassingExchange) GetSubscriptions() ([]wshandler.WebsocketChannelSu
func (h *FakePassingExchange) GetDefaultConfig() (*config.ExchangeConfig, error) { return nil, nil }
func (h *FakePassingExchange) GetBase() *exchange.Base { return nil }
func (h *FakePassingExchange) SupportsAsset(_ asset.Item) bool { return true }
func (h *FakePassingExchange) GetHistoricCandles(_ currency.Pair, _, _ int64) ([]exchange.Candle, error) {
return []exchange.Candle{}, nil
func (h *FakePassingExchange) GetHistoricCandles(_ currency.Pair, _ asset.Item, _, _ time.Time, _ time.Duration) (kline.Item, error) {
return kline.Item{}, nil
}
func (h *FakePassingExchange) DisableRateLimiter() error { return nil }
func (h *FakePassingExchange) EnableRateLimiter() error { return nil }
Expand Down
25 changes: 14 additions & 11 deletions engine/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1530,21 +1530,24 @@ func (s *RPCServer) GetHistoricCandles(ctx context.Context, req *gctrpc.GetHisto
Delimiter: req.Pair.Delimiter,
Base: currency.NewCode(req.Pair.Base),
Quote: currency.NewCode(req.Pair.Quote),
}, req.Rangesize, req.Granularity)
},
asset.Item(req.AssetType),
time.Unix(req.Start, 0),
time.Unix(req.End, 0),
time.Duration(req.TimeInterval))
if err != nil {
return nil, err
}
resp := gctrpc.GetHistoricCandlesResponse{}
for _, candle := range candles {
tempCandle := &gctrpc.Candle{
Time: candle.Time,
Low: candle.Low,
High: candle.High,
Open: candle.Open,
Close: candle.Close,
Volume: candle.Volume,
}
resp.Candle = append(resp.Candle, tempCandle)
for i := range candles.Candles {
resp.Candle = append(resp.Candle, &gctrpc.Candle{
Time: candles.Candles[i].Time.Unix(),
Low: candles.Candles[i].Low,
High: candles.Candles[i].High,
Open: candles.Candles[i].Open,
Close: candles.Candles[i].Close,
Volume: candles.Candles[i].Volume,
})
}
return &resp, nil
}
Expand Down
5 changes: 0 additions & 5 deletions exchanges/binance/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ type Binance struct {
validIntervals []TimeInterval
}

// GetHistoricCandles returns rangesize number of candles for the given granularity and pair starting from the latest available
func (b *Binance) GetHistoricCandles(pair currency.Pair, rangesize, granularity int64) ([]exchange.Candle, error) {
return nil, common.ErrNotYetImplemented
}

// GetExchangeInfo returns exchange information. Check binance_types for more
// information
func (b *Binance) GetExchangeInfo() (ExchangeInfo, error) {
Expand Down
6 changes: 6 additions & 0 deletions exchanges/binance/binance_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
Expand Down Expand Up @@ -670,3 +671,8 @@ func (b *Binance) ValidateCredentials() error {
_, err := b.UpdateAccountInfo()
return b.CheckTransientError(err)
}

// 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 time.Duration) (kline.Item, error) {
return kline.Item{}, common.ErrNotYetImplemented
}
5 changes: 0 additions & 5 deletions exchanges/bitfinex/bitfinex.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ type Bitfinex struct {
WebsocketSubdChannels map[int]WebsocketChanInfo
}

// GetHistoricCandles returns rangesize number of candles for the given granularity and pair starting from the latest available
func (b *Bitfinex) GetHistoricCandles(pair currency.Pair, rangesize, granularity int64) ([]exchange.Candle, error) {
return nil, common.ErrNotYetImplemented
}

// GetPlatformStatus returns the Bifinex platform status
func (b *Bitfinex) GetPlatformStatus() (int, error) {
var response []int
Expand Down
6 changes: 6 additions & 0 deletions exchanges/bitfinex/bitfinex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
Expand Down Expand Up @@ -771,3 +772,8 @@ func (b *Bitfinex) ValidateCredentials() error {
_, err := b.UpdateAccountInfo()
return b.CheckTransientError(err)
}

// 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 time.Duration) (kline.Item, error) {
return kline.Item{}, common.ErrNotYetImplemented
}
6 changes: 0 additions & 6 deletions exchanges/bitflyer/bitflyer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/url"
"strconv"

"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
Expand Down Expand Up @@ -72,11 +71,6 @@ type Bitflyer struct {
exchange.Base
}

// GetHistoricCandles returns rangesize number of candles for the given granularity and pair starting from the latest available
func (b *Bitflyer) GetHistoricCandles(pair currency.Pair, rangesize, granularity int64) ([]exchange.Candle, error) {
return nil, common.ErrNotYetImplemented
}

// GetLatestBlockCA returns the latest block information from bitflyer chain
// analysis system
func (b *Bitflyer) GetLatestBlockCA() (ChainAnalysisBlock, error) {
Expand Down
7 changes: 7 additions & 0 deletions exchanges/bitflyer/bitflyer_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package bitflyer
import (
"strings"
"sync"
"time"

"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
Expand Down Expand Up @@ -388,3 +390,8 @@ func (b *Bitflyer) ValidateCredentials() error {
_, err := b.UpdateAccountInfo()
return b.CheckTransientError(err)
}

// GetHistoricCandles returns candles between a time period for a set time interval
func (b *Bitflyer) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval time.Duration) (kline.Item, error) {
return kline.Item{}, common.ErrNotYetImplemented
}
6 changes: 0 additions & 6 deletions exchanges/bithumb/bithumb.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strconv"
"strings"

"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
Expand Down Expand Up @@ -48,11 +47,6 @@ type Bithumb struct {
exchange.Base
}

// GetHistoricCandles returns rangesize number of candles for the given granularity and pair starting from the latest available
func (b *Bithumb) GetHistoricCandles(pair currency.Pair, rangesize, granularity int64) ([]exchange.Candle, error) {
return nil, common.ErrNotYetImplemented
}

// GetTradablePairs returns a list of tradable currencies
func (b *Bithumb) GetTradablePairs() ([]string, error) {
result, err := b.GetAllTickers()
Expand Down
6 changes: 6 additions & 0 deletions exchanges/bithumb/bithumb_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
Expand Down Expand Up @@ -592,3 +593,8 @@ func (b *Bithumb) ValidateCredentials() error {
_, err := b.UpdateAccountInfo()
return b.CheckTransientError(err)
}

// 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 time.Duration) (kline.Item, error) {
return kline.Item{}, common.ErrNotYetImplemented
}
6 changes: 0 additions & 6 deletions exchanges/bitmex/bitmex.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"time"

"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
Expand Down Expand Up @@ -903,8 +902,3 @@ func calculateTradingFee(purchasePrice, amount float64, isMaker bool) float64 {

return fee * purchasePrice * amount
}

// GetHistoricCandles returns rangesize number of candles for the given granularity and pair starting from the latest available
func (b *Bitmex) GetHistoricCandles(pair currency.Pair, rangesize, granularity int64) ([]exchange.Candle, error) {
return nil, common.ErrNotYetImplemented
}
7 changes: 7 additions & 0 deletions exchanges/bitmex/bitmex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"math"
"strings"
"sync"
"time"

"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
Expand Down Expand Up @@ -686,3 +688,8 @@ func (b *Bitmex) ValidateCredentials() error {
_, err := b.UpdateAccountInfo()
return b.CheckTransientError(err)
}

// GetHistoricCandles returns candles between a time period for a set time interval
func (b *Bitmex) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval time.Duration) (kline.Item, error) {
return kline.Item{}, common.ErrNotYetImplemented
}
5 changes: 0 additions & 5 deletions exchanges/bitstamp/bitstamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ type Bitstamp struct {
WebsocketConn *wshandler.WebsocketConnection
}

// GetHistoricCandles returns rangesize number of candles for the given granularity and pair starting from the latest available
func (b *Bitstamp) GetHistoricCandles(pair currency.Pair, rangesize, granularity int64) ([]exchange.Candle, error) {
return nil, common.ErrNotYetImplemented
}

// GetFee returns an estimate of fee based on type of transaction
func (b *Bitstamp) GetFee(feeBuilder *exchange.FeeBuilder) (float64, error) {
var fee float64
Expand Down
6 changes: 6 additions & 0 deletions exchanges/bitstamp/bitstamp_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
Expand Down Expand Up @@ -674,3 +675,8 @@ func (b *Bitstamp) ValidateCredentials() error {
_, err := b.UpdateAccountInfo()
return b.CheckTransientError(err)
}

// 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 time.Duration) (kline.Item, error) {
return kline.Item{}, common.ErrNotYetImplemented
}
6 changes: 0 additions & 6 deletions exchanges/bittrex/bittrex.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"time"

"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
Expand Down Expand Up @@ -64,11 +63,6 @@ type Bittrex struct {
exchange.Base
}

// GetHistoricCandles returns rangesize number of candles for the given granularity and pair starting from the latest available
func (b *Bittrex) GetHistoricCandles(pair currency.Pair, rangesize, granularity int64) ([]exchange.Candle, error) {
return nil, common.ErrNotYetImplemented
}

// GetMarkets is used to get the open and available trading markets at Bittrex
// along with other meta data.
func (b *Bittrex) GetMarkets() (Market, error) {
Expand Down
Loading

0 comments on commit 2c7e531

Please sign in to comment.