Files
gocryptotrader/exchanges/itbit/itbit.go
Scott e4c443b901 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
2018-10-29 12:32:46 +11:00

395 lines
12 KiB
Go

package itbit
import (
"bytes"
"errors"
"fmt"
"log"
"net/url"
"strconv"
"time"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/config"
"github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/request"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
)
const (
itbitAPIURL = "https://api.itbit.com/v1"
itbitAPIVersion = "1"
itbitMarkets = "markets"
itbitOrderbook = "order_book"
itbitTicker = "ticker"
itbitWallets = "wallets"
itbitBalances = "balances"
itbitTrades = "trades"
itbitFundingHistory = "funding_history"
itbitOrders = "orders"
itbitCryptoDeposits = "cryptocurrency_deposits"
itbitWalletTransfer = "wallet_transfers"
itbitAuthRate = 0
itbitUnauthRate = 0
)
// ItBit is the overarching type across the ItBit package
type ItBit struct {
exchange.Base
}
// SetDefaults sets the defaults for the exchange
func (i *ItBit) SetDefaults() {
i.Name = "ITBIT"
i.Enabled = false
i.MakerFee = -0.10
i.TakerFee = 0.50
i.Verbose = false
i.RESTPollingDelay = 10
i.RequestCurrencyPairFormat.Delimiter = ""
i.RequestCurrencyPairFormat.Uppercase = true
i.ConfigCurrencyPairFormat.Delimiter = ""
i.ConfigCurrencyPairFormat.Uppercase = true
i.AssetTypes = []string{ticker.Spot}
i.SupportsAutoPairUpdating = false
i.SupportsRESTTickerBatching = false
i.Requester = request.New(i.Name,
request.NewRateLimit(time.Second, itbitAuthRate),
request.NewRateLimit(time.Second, itbitUnauthRate),
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout))
i.APIUrlDefault = itbitAPIURL
i.APIUrl = i.APIUrlDefault
i.WebsocketInit()
}
// Setup sets the exchange parameters from exchange config
func (i *ItBit) Setup(exch config.ExchangeConfig) {
if !exch.Enabled {
i.SetEnabled(false)
} else {
i.Enabled = true
i.AuthenticatedAPISupport = exch.AuthenticatedAPISupport
i.SetAPIKeys(exch.APIKey, exch.APISecret, exch.ClientID, false)
i.SetHTTPClientTimeout(exch.HTTPTimeout)
i.SetHTTPClientUserAgent(exch.HTTPUserAgent)
i.RESTPollingDelay = exch.RESTPollingDelay
i.Verbose = exch.Verbose
i.BaseCurrencies = common.SplitStrings(exch.BaseCurrencies, ",")
i.AvailablePairs = common.SplitStrings(exch.AvailablePairs, ",")
i.EnabledPairs = common.SplitStrings(exch.EnabledPairs, ",")
err := i.SetCurrencyPairFormat()
if err != nil {
log.Fatal(err)
}
err = i.SetAssetTypes()
if err != nil {
log.Fatal(err)
}
err = i.SetAutoPairDefaults()
if err != nil {
log.Fatal(err)
}
err = i.SetAPIURL(exch)
if err != nil {
log.Fatal(err)
}
err = i.SetClientProxyAddress(exch.ProxyAddress)
if err != nil {
log.Fatal(err)
}
}
}
// GetTicker returns ticker info for a specified market.
// currencyPair - example "XBTUSD" "XBTSGD" "XBTEUR"
func (i *ItBit) GetTicker(currencyPair string) (Ticker, error) {
var response Ticker
path := fmt.Sprintf("%s/%s/%s/%s", i.APIUrl, itbitMarkets, currencyPair, itbitTicker)
return response, i.SendHTTPRequest(path, &response)
}
// GetOrderbook returns full order book for the specified market.
// currencyPair - example "XBTUSD" "XBTSGD" "XBTEUR"
func (i *ItBit) GetOrderbook(currencyPair string) (OrderbookResponse, error) {
response := OrderbookResponse{}
path := fmt.Sprintf("%s/%s/%s/%s", i.APIUrl, itbitMarkets, currencyPair, itbitOrderbook)
return response, i.SendHTTPRequest(path, &response)
}
// GetTradeHistory returns recent trades for a specified market.
//
// currencyPair - example "XBTUSD" "XBTSGD" "XBTEUR"
// timestamp - matchNumber, only executions after this will be returned
func (i *ItBit) GetTradeHistory(currencyPair, timestamp string) (Trades, error) {
response := Trades{}
req := "trades?since=" + timestamp
path := fmt.Sprintf("%s/%s/%s/%s", i.APIUrl, itbitMarkets, currencyPair, req)
return response, i.SendHTTPRequest(path, &response)
}
// GetWallets returns information about all wallets associated with the account.
//
// params --
// page - [optional] page to return example 1. default 1
// perPage - [optional] items per page example 50, default 50 max 50
func (i *ItBit) GetWallets(params url.Values) ([]Wallet, error) {
resp := []Wallet{}
params.Set("userId", i.ClientID)
path := fmt.Sprintf("/%s?%s", itbitWallets, params.Encode())
return resp, i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
}
// CreateWallet creates a new wallet with a specified name.
func (i *ItBit) CreateWallet(walletName string) (Wallet, error) {
resp := Wallet{}
params := make(map[string]interface{})
params["userId"] = i.ClientID
params["name"] = walletName
err := i.SendAuthenticatedHTTPRequest("POST", "/"+itbitWallets, params, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// GetWallet returns wallet information by walletID
func (i *ItBit) GetWallet(walletID string) (Wallet, error) {
resp := Wallet{}
path := fmt.Sprintf("/%s/%s", itbitWallets, walletID)
err := i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// GetWalletBalance returns balance information for a specific currency in a
// wallet.
func (i *ItBit) GetWalletBalance(walletID, currency string) (Balance, error) {
resp := Balance{}
path := fmt.Sprintf("/%s/%s/%s/%s", itbitWallets, walletID, itbitBalances, currency)
err := i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// GetWalletTrades returns all trades for a specified wallet.
func (i *ItBit) GetWalletTrades(walletID string, params url.Values) (Records, error) {
resp := Records{}
url := fmt.Sprintf("/%s/%s/%s", itbitWallets, walletID, itbitTrades)
path := common.EncodeURLValues(url, params)
err := i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// GetFundingHistory returns all funding history for a specified wallet.
func (i *ItBit) GetFundingHistory(walletID string, params url.Values) (FundingRecords, error) {
resp := FundingRecords{}
url := fmt.Sprintf("/%s/%s/%s", itbitWallets, walletID, itbitFundingHistory)
path := common.EncodeURLValues(url, params)
err := i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// PlaceOrder places a new order
func (i *ItBit) PlaceOrder(walletID, side, orderType, currency string, amount, price float64, instrument, clientRef string) (Order, error) {
resp := Order{}
path := fmt.Sprintf("/%s/%s/%s", itbitWallets, walletID, itbitOrders)
params := make(map[string]interface{})
params["side"] = side
params["type"] = orderType
params["currency"] = currency
params["amount"] = strconv.FormatFloat(amount, 'f', -1, 64)
params["price"] = strconv.FormatFloat(price, 'f', -1, 64)
params["instrument"] = instrument
if clientRef != "" {
params["clientOrderIdentifier"] = clientRef
}
err := i.SendAuthenticatedHTTPRequest("POST", path, params, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// GetOrder returns an order by id.
func (i *ItBit) GetOrder(walletID string, params url.Values) (Order, error) {
resp := Order{}
url := fmt.Sprintf("/%s/%s/%s", itbitWallets, walletID, itbitOrders)
path := common.EncodeURLValues(url, params)
err := i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// CancelOrder cancels and open order. *This is not a guarantee that the order
// has been cancelled!*
func (i *ItBit) CancelOrder(walletID, orderID string) error {
path := fmt.Sprintf("/%s/%s/%s/%s", itbitWallets, walletID, itbitOrders, orderID)
return i.SendAuthenticatedHTTPRequest("DELETE", path, nil, nil)
}
// GetDepositAddress returns a deposit address to send cryptocurrency to.
func (i *ItBit) GetDepositAddress(walletID, currency string) (CryptoCurrencyDeposit, error) {
resp := CryptoCurrencyDeposit{}
path := fmt.Sprintf("/%s/%s/%s", itbitWallets, walletID, itbitCryptoDeposits)
params := make(map[string]interface{})
params["currency"] = currency
err := i.SendAuthenticatedHTTPRequest("POST", path, params, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// WalletTransfer transfers funds between wallets.
func (i *ItBit) WalletTransfer(walletID, sourceWallet, destWallet string, amount float64, currency string) (WalletTransfer, error) {
resp := WalletTransfer{}
path := fmt.Sprintf("/%s/%s/%s", itbitWallets, walletID, itbitWalletTransfer)
params := make(map[string]interface{})
params["sourceWalletId"] = sourceWallet
params["destinationWalletId"] = destWallet
params["amount"] = strconv.FormatFloat(amount, 'f', -1, 64)
params["currencyCode"] = currency
err := i.SendAuthenticatedHTTPRequest("POST", path, params, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// SendHTTPRequest sends an unauthenticated HTTP request
func (i *ItBit) SendHTTPRequest(path string, result interface{}) error {
return i.SendPayload("GET", path, nil, nil, result, false, i.Verbose)
}
// SendAuthenticatedHTTPRequest sends an authenticated request to itBit
func (i *ItBit) SendAuthenticatedHTTPRequest(method string, path string, params map[string]interface{}, result interface{}) error {
if !i.AuthenticatedAPISupport {
return fmt.Errorf(exchange.WarningAuthenticatedRequestWithoutCredentialsSet, i.Name)
}
request := make(map[string]interface{})
url := i.APIUrl + path
if params != nil {
for key, value := range params {
request[key] = value
}
}
PayloadJSON := []byte("")
var err error
if params != nil {
PayloadJSON, err = common.JSONEncode(request)
if err != nil {
return err
}
if i.Verbose {
log.Printf("Request JSON: %s\n", PayloadJSON)
}
}
nonce := i.Nonce.GetValue(i.Name, false).String()
timestamp := strconv.FormatInt(time.Now().UnixNano()/1000000, 10)
message, err := common.JSONEncode([]string{method, url, string(PayloadJSON), nonce, timestamp})
if err != nil {
return err
}
hash := common.GetSHA256([]byte(nonce + string(message)))
hmac := common.GetHMAC(common.HashSHA512, []byte(url+string(hash)), []byte(i.APISecret))
signature := common.Base64Encode(hmac)
headers := make(map[string]string)
headers["Authorization"] = i.ClientID + ":" + signature
headers["X-Auth-Timestamp"] = timestamp
headers["X-Auth-Nonce"] = nonce
headers["Content-Type"] = "application/json"
return i.SendPayload(method, url, headers, bytes.NewBuffer([]byte(PayloadJSON)), result, true, i.Verbose)
}
// GetFee returns an estimate of fee based on type of transaction
func (i *ItBit) GetFee(feeBuilder exchange.FeeBuilder) (float64, error) {
var fee float64
switch feeBuilder.FeeType {
case exchange.CryptocurrencyTradeFee:
fee = calculateTradingFee(feeBuilder.PurchasePrice, feeBuilder.Amount, feeBuilder.IsMaker)
}
if fee < 0 {
fee = 0
}
return fee, nil
}
func calculateTradingFee(purchasePrice, amount float64, isMaker bool) float64 {
// TODO: Itbit has volume discounts, but not API endpoint to get the exact volume numbers
// When support is added, this needs to be updated to calculate the accurate volume fee
feePercent := 0.0025
if isMaker {
feePercent = 0
}
return feePercent * purchasePrice * amount
}