Files
gocryptotrader/gctscript/wrappers/gct/exchange/exchange.go
Andrew 4a736fb335 (Exchange) Add GetHistoricCandles() & GetHistoricCandlesEx() support to exchanges (#479)
* implemented binance and bitfinex GetHistoricCandles wrapper methods)

* coinbene supported added

* after and before clean up

* gateio wrapper completed

* merged upstream/master

* Added bsaic KlineIntervalSupported() method

* Converted binance fixed test

* WIP

* new KlineConvertToExchangeStandardString method added

* end of day WIP

* 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

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

* Added OneMonth

* limit checks on supportedexchanges

* reverted getexchangehistory

* reworked binance tesT

* added workaround for kraken panic

* 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

* 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

* readme updated

* 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

* added start/end date to extended method over range

* converted to assettranslator

* removed verbose

* removed invalid char

* reverted incorrectly removed return

* added import

* further template updates

* macos hates my keyboard :D

* misc canges

* x -> i

* removed verbose

* updated fixCasing to allocate var before checks

* removed time conversion

* 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

* add iszero false check to candle times

* updated resultlimit to 5000

* new line added

* added comment to exported const

* 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 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
2020-07-08 10:51:54 +10:00

231 lines
6.5 KiB
Go

package exchange
import (
"errors"
"fmt"
"sort"
"strconv"
"time"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/engine"
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"
"github.com/thrasher-corp/gocryptotrader/portfolio/banking"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
)
// Exchange implements all required methods for Wrapper
type Exchange struct{}
// Exchanges returns slice of all current exchanges
func (e Exchange) Exchanges(enabledOnly bool) []string {
return engine.GetExchangeNames(enabledOnly)
}
// GetExchange returns IBotExchange for exchange or error if exchange is not found
func (e Exchange) GetExchange(exch string) (exchange.IBotExchange, error) {
ex := engine.GetExchangeByName(exch)
if ex == nil {
return nil, fmt.Errorf("%v exchange not found", exch)
}
return ex, nil
}
// IsEnabled returns if requested exchange is enabled or disabled
func (e Exchange) IsEnabled(exch string) bool {
ex, err := e.GetExchange(exch)
if err != nil {
return false
}
return ex.IsEnabled()
}
// Orderbook returns current orderbook requested exchange, pair and asset
func (e Exchange) Orderbook(exch string, pair currency.Pair, item asset.Item) (*orderbook.Base, error) {
return engine.GetSpecificOrderbook(pair, exch, item)
}
// Ticker returns ticker for provided currency pair & asset type
func (e Exchange) Ticker(exch string, pair currency.Pair, item asset.Item) (*ticker.Price, error) {
ex, err := e.GetExchange(exch)
if err != nil {
return nil, err
}
return ex.FetchTicker(pair, item)
}
// Pairs returns either all or enabled currency pairs
func (e Exchange) Pairs(exch string, enabledOnly bool, item asset.Item) (*currency.Pairs, error) {
x, err := engine.Bot.Config.GetExchangeConfig(exch)
if err != nil {
return nil, err
}
if enabledOnly {
return &x.CurrencyPairs.Get(item).Enabled, nil
}
return &x.CurrencyPairs.Get(item).Available, nil
}
// QueryOrder returns details of a valid exchange order
func (e Exchange) QueryOrder(exch, orderID string) (*order.Detail, error) {
ex, err := e.GetExchange(exch)
if err != nil {
return nil, err
}
r, err := ex.GetOrderInfo(orderID)
if err != nil {
return nil, err
}
return &r, nil
}
// SubmitOrder submit new order on exchange
func (e Exchange) SubmitOrder(submit *order.Submit) (*order.SubmitResponse, error) {
r, err := engine.Bot.OrderManager.Submit(submit)
if err != nil {
return nil, err
}
return &r.SubmitResponse, nil
}
// CancelOrder wrapper to cancel order on exchange
func (e Exchange) CancelOrder(exch, orderID string) (bool, error) {
orderDetails, err := e.QueryOrder(exch, orderID)
if err != nil {
return false, err
}
cancel := &order.Cancel{
AccountID: orderDetails.AccountID,
ID: orderDetails.ID,
Pair: orderDetails.Pair,
Side: orderDetails.Side,
}
err = engine.Bot.OrderManager.Cancel(cancel)
if err != nil {
return false, err
}
return true, nil
}
// AccountInformation returns account information (balance etc) for requested exchange
func (e Exchange) AccountInformation(exch string) (account.Holdings, error) {
ex, err := e.GetExchange(exch)
if err != nil {
return account.Holdings{}, err
}
accountInfo, err := ex.FetchAccountInfo()
if err != nil {
return account.Holdings{}, err
}
return accountInfo, nil
}
// DepositAddress gets the address required to deposit funds for currency type
func (e Exchange) DepositAddress(exch string, currencyCode currency.Code) (out string, err error) {
if currencyCode.IsEmpty() {
err = errors.New("currency code is empty")
return
}
return engine.Bot.DepositAddressManager.GetDepositAddressByExchange(exch, currencyCode)
}
// WithdrawalFiatFunds withdraw funds from exchange to requested fiat source
func (e Exchange) WithdrawalFiatFunds(exch, bankAccountID string, request *withdraw.Request) (string, error) {
ex, err := e.GetExchange(exch)
if err != nil {
return "", err
}
var v *banking.Account
v, err = banking.GetBankAccountByID(bankAccountID)
if err != nil {
v, err = ex.GetBase().GetExchangeBankAccounts(bankAccountID, request.Currency.String())
if err != nil {
return "", err
}
}
otp, err := engine.GetExchangeoOTPByName(exch)
if err == nil {
otpValue, errParse := strconv.ParseInt(otp, 10, 64)
if errParse != nil {
return "", errors.New("failed to generate OTP unable to continue")
}
request.OneTimePassword = otpValue
}
request.Exchange = exch
request.Fiat.Bank.AccountName = v.AccountName
request.Fiat.Bank.AccountNumber = v.AccountNumber
request.Fiat.Bank.BankName = v.BankName
request.Fiat.Bank.BankAddress = v.BankAddress
request.Fiat.Bank.BankPostalCity = v.BankPostalCity
request.Fiat.Bank.BankCountry = v.BankCountry
request.Fiat.Bank.BankPostalCode = v.BankPostalCode
request.Fiat.Bank.BSBNumber = v.BSBNumber
request.Fiat.Bank.SWIFTCode = v.SWIFTCode
request.Fiat.Bank.IBAN = v.IBAN
resp, err := engine.SubmitWithdrawal(ex.GetName(), request)
if err != nil {
return "", err
}
return resp.Exchange.ID, nil
}
// WithdrawalCryptoFunds withdraw funds from exchange to requested Crypto source
func (e Exchange) WithdrawalCryptoFunds(exch string, request *withdraw.Request) (out string, err error) {
ex, err := e.GetExchange(exch)
if err != nil {
return "", err
}
otp, err := engine.GetExchangeoOTPByName(exch)
if err == nil {
v, errParse := strconv.ParseInt(otp, 10, 64)
if errParse != nil {
return "", errors.New("failed to generate OTP unable to continue")
}
request.OneTimePassword = v
}
resp, err := engine.SubmitWithdrawal(ex.GetName(), request)
if err != nil {
return "", err
}
return resp.Exchange.ID, nil
}
// OHLCV returns open high low close volume candles for requested exchange/pair/asset/start & end time
func (e Exchange) OHLCV(exch string, pair currency.Pair, item asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
ex, err := e.GetExchange(exch)
if err != nil {
return kline.Item{}, err
}
ret, err := ex.GetHistoricCandles(pair, item, start, end, interval)
if err != nil {
return kline.Item{}, err
}
sort.Slice(ret.Candles, func(i, j int) bool {
return ret.Candles[i].Time.Before(ret.Candles[j].Time)
})
return ret, nil
}