Calculating each exchange's fees (#188)

* Adds some constants for fee types
Adds some fee calculation in an attempt to be generic
Adds fee stuff to Bittrex
Adds fee stuff to bitstamp

* Fixes bitstamp fee calculation

* Tests
Tests all scenarios for GetFeeByType

* Adds method to wrapper
Adds err to response
Checks for err

* Adds support for Bittrex fees

* Adds maker/taker dynamic to fees
Updates tests
Adds bitmex fee support
Removes unused switch case scenarios to not waste space

* Adds bithumb support for fee calculation

* Adds Bitfinex fee support
Adds list of currencies as const strings
Sets up bitflyer

* Fixes arguments

* Greatly expands symbols
Adds Binance fee calculation support
Cleans up previous exchanges

* Fixes errors for fee calculations

* Adds ANX fee support

* Adds btcc fee support
Adds alphapoint fee wrapper support
Renames method to match "enum"
Uses symbols in tests, not inline strings

* Adds support for BTCMarkets fee calculation
Adds new method to retrieve fee amount from BTCMarkets
Adds new fee type struct: FeeBuilder
Updates ANX and BTCMarkets to use new FeeBuilder type struct
Standardises the tests to run when it comes to fee calculation

* Migrates all existing exchange fee to use new feebuilder type struct
Uses standard testing model

* Fixes unit tests

* Updates maker taker fees in test config

* Removes parallel from fee testing

* Removes more parallel from tests

* Adds coinbasepro fee support

* Adds Coinut fee support

* Adds Exmo fee support
Adds maker fee support to coinut
Introduces a type for fees and bank transfers to prevent random strings being used

* Adds partial bitflyer support
Moves bitflyer to feeBuilder struct

* Adds gateio fee support

* Adds Gemini fee support

* Adds hitbtc fee support

* Adds huobi fee support

* Adds HuobiHadax fee support

* Adds itbit fee support

* Adds partial kraken fee support with trading fees

* Finishes basic Kraken fee support

* Adds basic LakeBTC fee support

* Adds basic liqui fee support

* Adds localbitcoins fee support.......

* Adds basic okcoin fee support

* Adds simple OKEX fee support
Adds many new currency symbols
Fixes liqui's fees

* Adds poloniex fee support

* Adds fee support for Yobit

* Adds WEX fee support

* Adds ZB fee support

* Removes bad reference

* Improves accuracy of variable name

* trading fee method names are now consistent

(cherry picked from commit 21c82e8b90cae590cfd73d365d7be39e1a00e973)

* Fixes rebasing issues

* Fixes issues from rebase
Removes "IsTaker" as IsMaker bool can imply taker
Updates tests to actually work.

* Adds a zero to the test

* Fixes bitfinex api endpoints and fixes fee calculations

* Updates btcmarkets trading fee calculation

* Verifies tests with apis for all exchanges except coinbasepro, itbit and bitflyer
Removes taker fee test as taker is default

* Removes redundant all exchange wrapper error checks due to the error checks being redundant

* Addresses review comments:
- Renames variables
- Changes how functions return data
- Fixes typo
This commit is contained in:
Scott
2018-10-29 12:32:46 +11:00
committed by Adrian Gallagher
parent 83263f3245
commit e4c443b901
108 changed files with 8468 additions and 538 deletions

View File

@@ -489,36 +489,19 @@ func (b *Bitfinex) GetSymbolsDetails() ([]SymbolDetails, error) {
}
// GetAccountInfo returns information about your account incl. trading fees
func (b *Bitfinex) GetAccountInfo() (AccountInfo, error) {
func (b *Bitfinex) GetAccountInfo() ([]AccountInfo, error) {
var result AccountInfo
var response []interface{}
err := b.SendAuthenticatedHTTPRequest("POST", bitfinexAccountInfo, nil, &response)
var responses []AccountInfo
err := b.SendAuthenticatedHTTPRequest("POST", bitfinexAccountInfo, nil, &responses)
if err != nil {
return result, err
return responses, err
}
result = AccountInfo{}
result.MakerFees = response[0].(map[string]interface{})["maker_fees"].(string)
result.TakerFees = response[0].(map[string]interface{})["taker_fees"].(string)
feeslist := response[0].(map[string]interface{})["fees"].([]interface{})
for _, v := range feeslist {
item := v.(map[string]interface{})
result.Fees = append(result.Fees, AccountInfoFees{
Pairs: item["pairs"].(string),
MakerFees: item["maker_fees"].(string),
TakerFees: item["taker_fees"].(string),
})
}
return result, nil
return responses, nil
}
// GetAccountFees - NOT YET IMPLEMENTED
// GetAccountFees - Gets all fee rates for all currencies
func (b *Bitfinex) GetAccountFees() (AccountFees, error) {
response := AccountFees{}
@@ -903,7 +886,7 @@ func (b *Bitfinex) SendAuthenticatedHTTPRequest(method, path string, params map[
}
request := make(map[string]interface{})
request["request"] = fmt.Sprintf("/v%s/%s", bitfinexAPIVersion, path)
request["request"] = fmt.Sprintf("%s%s", bitfinexAPIVersion, path)
request["nonce"] = b.Nonce.String()
if params != nil {
@@ -934,3 +917,83 @@ func (b *Bitfinex) SendAuthenticatedHTTPRequest(method, path string, params map[
}
return nil
}
// GetFee returns an estimate of fee based on type of transaction
func (b *Bitfinex) GetFee(feeBuilder exchange.FeeBuilder) (float64, error) {
var fee float64
switch feeBuilder.FeeType {
case exchange.CryptocurrencyTradeFee:
accountInfos, err := b.GetAccountInfo()
if err != nil {
return 0, err
}
fee, err = b.CalculateTradingFee(accountInfos, feeBuilder.PurchasePrice, feeBuilder.Amount, feeBuilder.FirstCurrency, feeBuilder.IsMaker)
if err != nil {
return 0, err
}
case exchange.CyptocurrencyDepositFee:
//TODO: fee is charged when < $1000USD is transferred, need to infer value in some way
fee = 0
case exchange.CryptocurrencyWithdrawalFee:
accountFees, err := b.GetAccountFees()
if err != nil {
return 0, err
}
fee, err = b.GetCryptocurrencyWithdrawalFee(feeBuilder.FirstCurrency, accountFees)
if err != nil {
return 0, err
}
case exchange.InternationalBankDepositFee:
fee = getInternationalBankDepositFee(feeBuilder.Amount)
case exchange.InternationalBankWithdrawalFee:
fee = getInternationalBankWithdrawalFee(feeBuilder.Amount)
}
if fee < 0 {
fee = 0
}
return fee, nil
}
// GetCryptocurrencyWithdrawalFee returns an estimate of fee based on type of transaction
func (b *Bitfinex) GetCryptocurrencyWithdrawalFee(currency string, accountFees AccountFees) (fee float64, err error) {
switch accountFees.Withdraw[currency].(type) {
case string:
fee, err = strconv.ParseFloat(accountFees.Withdraw[currency].(string), 64)
if err != nil {
return 0, err
}
case float64:
fee = accountFees.Withdraw[currency].(float64)
}
return fee, nil
}
func getInternationalBankDepositFee(amount float64) float64 {
return 0.001 * amount
}
func getInternationalBankWithdrawalFee(amount float64) float64 {
return 0.001 * amount
}
// CalculateTradingFee returns an estimate of fee based on type of whether is maker or taker fee
func (b *Bitfinex) CalculateTradingFee(accountInfos []AccountInfo, purchasePrice, amount float64, currency string, isMaker bool) (fee float64, err error) {
for _, i := range accountInfos {
for _, j := range i.Fees {
if currency == j.Pairs {
if isMaker {
fee = j.MakerFees
} else {
fee = j.TakerFees
}
break
}
}
if fee > 0 {
break
}
}
return (fee / 100) * purchasePrice * amount, err
}

View File

@@ -8,6 +8,8 @@ import (
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/config"
"github.com/thrasher-/gocryptotrader/currency/symbol"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
)
// Please supply your own keys here to do better tests
@@ -27,7 +29,8 @@ func TestSetup(t *testing.T) {
t.Error("Test Failed - Bitfinex Setup() init error")
}
b.Setup(bfxConfig)
b.APIKey = testAPIKey
b.APISecret = testAPISecret
if !b.Enabled || b.AuthenticatedAPISupport || b.RESTPollingDelay != time.Duration(10) ||
b.Verbose || b.Websocket.IsEnabled() || len(b.BaseCurrencies) < 1 ||
len(b.AvailablePairs) < 1 || len(b.EnabledPairs) < 1 {
@@ -617,3 +620,89 @@ func TestCloseMarginFunding(t *testing.T) {
t.Error("Test Failed - CloseMarginFunding() error")
}
}
func setFeeBuilder() exchange.FeeBuilder {
return exchange.FeeBuilder{
Amount: 1,
Delimiter: "",
FeeType: exchange.CryptocurrencyTradeFee,
FirstCurrency: symbol.BTC,
SecondCurrency: symbol.LTC,
IsMaker: false,
PurchasePrice: 1,
}
}
func TestGetFee(t *testing.T) {
b.SetDefaults()
b.Verbose = true
TestSetup(t)
var feeBuilder = setFeeBuilder()
if testAPIKey != "" || testAPISecret != "" {
// CryptocurrencyTradeFee Basic
if resp, err := b.GetFee(feeBuilder); resp != float64(0.002) || err != nil {
t.Error(err)
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(0.002), resp)
}
// CryptocurrencyTradeFee High quantity
feeBuilder = setFeeBuilder()
feeBuilder.Amount = 1000
feeBuilder.PurchasePrice = 1000
if resp, err := b.GetFee(feeBuilder); resp != float64(2000) || err != nil {
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(2000), resp)
t.Error(err)
}
// CryptocurrencyTradeFee IsMaker
feeBuilder = setFeeBuilder()
feeBuilder.IsMaker = true
if resp, err := b.GetFee(feeBuilder); resp != float64(0.001) || err != nil {
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(0.001), resp)
t.Error(err)
}
// CryptocurrencyTradeFee Negative purchase price
feeBuilder = setFeeBuilder()
feeBuilder.PurchasePrice = -1000
if resp, err := b.GetFee(feeBuilder); resp != float64(0) || err != nil {
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(0), resp)
t.Error(err)
}
// CryptocurrencyWithdrawalFee Basic
feeBuilder = setFeeBuilder()
feeBuilder.FeeType = exchange.CryptocurrencyWithdrawalFee
if resp, err := b.GetFee(feeBuilder); resp != float64(0.0004) || err != nil {
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(0.0004), resp)
t.Error(err)
}
}
// CyptocurrencyDepositFee Basic
feeBuilder = setFeeBuilder()
feeBuilder.FeeType = exchange.CyptocurrencyDepositFee
if resp, err := b.GetFee(feeBuilder); resp != float64(0) || err != nil {
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(0), resp)
t.Error(err)
}
// InternationalBankDepositFee Basic
feeBuilder = setFeeBuilder()
feeBuilder.FeeType = exchange.InternationalBankDepositFee
feeBuilder.CurrencyItem = symbol.HKD
if resp, err := b.GetFee(feeBuilder); resp != float64(0.001) || err != nil {
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(0.001), resp)
t.Error(err)
}
// InternationalBankWithdrawalFee Basic
feeBuilder = setFeeBuilder()
feeBuilder.FeeType = exchange.InternationalBankWithdrawalFee
feeBuilder.CurrencyItem = symbol.HKD
if resp, err := b.GetFee(feeBuilder); resp != float64(0.001) || err != nil {
t.Errorf("Test Failed - GetFee() error. Expected: %f, Recieved: %f", float64(0.001), resp)
t.Error(err)
}
}

View File

@@ -133,36 +133,22 @@ type AccountInfoFull struct {
// AccountInfo general account information with fees
type AccountInfo struct {
MakerFees string `json:"maker_fees"`
TakerFees string `json:"taker_fees"`
MakerFees float64 `json:"maker_fees,string"`
TakerFees float64 `json:"taker_fees,string"`
Fees []AccountInfoFees `json:"fees"`
Message string `json:"message"`
}
// AccountInfoFees general account information with fees
type AccountInfoFees struct {
Pairs string `json:"pairs"`
MakerFees string `json:"maker_fees"`
TakerFees string `json:"taker_fees"`
Pairs string `json:"pairs"`
MakerFees float64 `json:"maker_fees,string"`
TakerFees float64 `json:"taker_fees,string"`
}
// AccountFees stores withdrawal account fee data from Bitfinex
type AccountFees struct {
Withdraw struct {
BTC float64 `json:"BTC,string"`
LTC float64 `json:"LTC,string"`
ETH float64 `json:"ETH,string"`
ETC float64 `json:"ETC,string"`
ZEC float64 `json:"ZEC,string"`
XMR float64 `json:"XMR,string"`
DSH float64 `json:"DSH,string"`
XRP float64 `json:"XRP,string"`
IOT float64 `json:"IOT"`
EOS float64 `json:"EOS,string"`
SAN float64 `json:"SAN,string"`
OMG float64 `json:"OMG,string"`
BCH float64 `json:"BCH,string"`
} `json:"withdraw"`
Withdraw map[string]interface{} `json:"withdraw"`
}
// AccountSummary holds account summary data

View File

@@ -226,3 +226,8 @@ func (b *Bitfinex) WithdrawFiatExchangeFundsToInternationalBank(currency pair.Cu
func (b *Bitfinex) GetWebsocket() (*exchange.Websocket, error) {
return b.Websocket, nil
}
// GetFeeByType returns an estimate of fee based on type of transaction
func (b *Bitfinex) GetFeeByType(feeBuilder exchange.FeeBuilder) (float64, error) {
return b.GetFee(feeBuilder)
}