Files
gocryptotrader/exchanges/ftx/ftx_wrapper.go
Scott 80bc8c7e9e Trade history, recent trades, live trade processing and storage (#558)
* End of day commit moving packages and setting foundation into how trade processing will go

* Conformity

* tdd candle generation based on received trade data, renames orderbookbuffer back to buffer for now...

* Formalises test functions and designs the trade processor

* Theoretical amending old candles to allow any trades that were part of an old processed candle to be more accurate. Saving of candles will only occur on previous cycles, extending memory usage a bit longer

* Figures out sqlboiler for sqlite. Updates websocket entries to process trade data

* One more trade data

* Adds more exchange support

* Adds PSQL stuff

* Begins creating sql implementation

* End of day commit. Helper functions and understanding sql usage in GCT

* Adds delete and cleans up table design

* Finishes trades conceptually. Awaits candle data update in order to translate trades to candles

* Initial handling of trades in coinbene

* Proto

* Fixing of some bugs, attempting to address coinbene asset type ws issues

* Fixes up coinbene websocket implementation for the most part

* finalises coinbene websocket implementation. Adds new ability to parse currencies without a delimiter

* Implements rpc commands and adds testing

* updates the following to be compatible with trade data update: Theoretical amending old candles to allow any trades that were part of an old processed candle to be more accurate. Saving of candles will only occur on previous cycles, extending memory usage a bit longer

* Changes trade to be its own entity rather than attached to a websocket.

* Adds coverage to trades. Changes signature of `AddTradesToBuffer` to return error. Now automatically shuts down without need for channel listening. Will automatically start up again if it gets data

* Implements trade fetching at the wrapper level for a bunch of exchanges. Adds trade id to script updoot. Probably breaking change

* Implements trade fetching for all wrappers hurray hurrah. Updates all the tests

* Adds new interface func to get recent trades. Ensures GetExchangeHistory continues until conditions are met

* Adds new readme, tests all new wrapper endpoints, updates exchange_wrapper_issues to test new endpoints. Updates exchange_wrapper_coverage with new coverage... Fixes lame bug causing wrapper tests to fail from being poorly setup. Adds loopy loop to ensure that all data is captured when requesting exchange history

* Bugfix on psql migrations. Rebases latest changes, updates table design to use base and quote, updates trades to use exchange_name_id

* Adds new config field for saving trades to the database per exchange. Now exits trade processing when trade saving is not enabled. Similarly for wrapper, does not save if not enabled

* Minor bitfinex trade fixes. continues on buffer processing errors, now saves transactionid to the db

* Adds support for generating candles from candlesextended. May extend it further, idk

* Updates trade candles to be able to fill missing data with trades. Adds more tests. Also does a thing where you can forcefully override a candle based on internal trade data instead of API data

* Fixes bug where force deletions did not follow up with insertions. Adds force to candle commands

* Fixes specific exchange based issues. Extends recent trades to 24 hours where possible

* Fixes issue with saved tests. Fixes tests for trades. Adds parallel to tests. Pre-fixes people's nits

* Adds new GRPC functions to find out what data is missing from trades and candles. Fixes some assumptions from missing period code.

* Adds unique constraint. Fixes up niggling issues for wrappers and websockets

* Fixes issues with using unix times in the database trying to retrieve data via the CLI. Reduces save time to 15 seconds

* Updates trades to use timestamps instead of int64 unix

* Adds missing FTX wrapper implementation. Regens docs

* Linting the linters. Updating readme

* Adds new command to set whether an exchange can process trades

* Doc update

* Adds recent trades and historic trade endpoints to grpc

* formats pair_test.go to appease linter gods

* Addresses data race. Removes logging of missing intervals on unrelated function (now that it has its own rpc command). The buffer time isnt customisable, but I don't feel it needs to be at a config level at all really.

* Fixes a few niterinos regarding spacing, type conversion, a weird Bitmex 0 trade value error, unsubscriptions and cli command references

* Reduces map lookups. Adds base func and moves wrappers to use it

* Uses better currency formatter. Adds time based validation to trade history. Reverts configtest.json

* Reverts config and updates test names. Also WAYYYYY LESS SPAMMY

* oopsie doopsie missed a whoopsie

* mint flavoured lint

* Fixes issues caused by rebase

* Fixes issue with timestamps not converting properly from command to RPCServer. Adds new error type. Adds shorthand entries to some commands. Removes os.Exit from tests. Makes Gemini test rolling. Adds enabled exchange check to RPC function. Escapes timestamp on bitstamp. Renames var

* fixes whoopsie oopsie doopsie I forgot to remove code shoopsie

* missed a line

* 🎉 🎉 :tada:Breaks everything in an end of day commit 🎉 🎉 🎉

* Modifies function 'createlocaloffset' to return a string instead. Uses strings for all time based start and end commands. Uses UTC times in RPC server and updates SQLITE to use formatted time based queries

* Adds concurrency-safe way of changing SaveTradeData and checking it. Fixes embarrassing typo

* End of day fix, adds bitfinex update to loop until either the return trades shows no new dates, or meets specifications. Fixes egregious typo

* Improves testing and handling of historical trades function

* Fixes tests after latest changes

* Fix potential fatal err now that db is enabled in test config now

* Fixes up some database settings to use a local engine instead of global var

* DELICIOUS LINT CHOCOLATE FIXES

* Fixes data race by slashing competitor's tyres

* Adds mock test fixes to allow for live and stored data test

* Removes verbosity in engine level tests. Adds new timezone format to highlight the timezone for RPC functions. Removes reference to Preix index fund

* Oopsie doopsie, fixed a whoopsie

* Loggers can no longer do data drag races on my lawn 👴

* Removes bad lock

* Addresses command nits. End of day conceptual commit, trying to calculate spans of time in the context of missing periods. Tests will fail

* Adds new stream response for retrieving trade history as it can take time to do. Unsuccessfully attempts to simplify time range calculation for missing trades response

* Adds new timeperiods package to calculate time periods, time ranges and whether data is in those ranges. Removes kline basic implementation of same concept

* Fixes lint issues. Fixes test. Moves trade cli commands to their own trade subcommands

* Updates lakebtc to no longer have gethistorictrades as it is unsupported. Adds more validation to rpc functions

* Removes requirement to have trades when testing trade wrapper functions. Doesn't really prove it works if there are no trades for a given currency in a time period.

* Addresses nits, runs linting fix and ensures a test is consistent

* Fix merge issues

* Moves sort to timeperiods. Adds test coverage. Fixes typo

* Removes log package in CLI

* Fixes `GetTrades` url

* Reorders all instances of validation occuring after settingup RPC connection

* Fixes test to ensure that it is setup before testing that it is setup

* Fixed issue with bool retrieval. Removes double append

* Fixes Binance times, fixes bitfinex sell sides, fixes huobi times, sorts all responses

* Fixes poloniex trade id consistency. Makes recent trade for poloniex consistent with others (15 minutes). Fixes coinbene. Fixes localbitcoins to use quote currency. Fixes coinut times. Updates huobi trade id, saves okgroup trades. Fixes bid and ask to buy and sell

* Removes websocket trades for lakebtc as it did not meet our requirements for processing. Adds new constraints to the database to ensure we have uniqueness on trades where ID doesn't exist and doesn't trigger errors for trades where the tid does

* Fixes migration for postgres to downscale properly

* Really really fixes the psql index changes

* Fixes broken tests

* Now with working tests and no pocket lint

* Makes the side column nullable with no more constraint for it. adds migrations and runs generation. comments lakebtc

* Lint & Sprüngli

* Updates zb to use more appropriate side

* Fixes oopsie

* Attempts to address a data race from globals

* Fixes build

* Fixes missed regen rpc files

* Updates readme to point to trade readme. Fixes exchange_wrapper_coverage wrapper count and untested panics, tests bitfinex funding pair test for `fUSD`, adds shiny new param `tradeprocessinginterval`

* mint flavoured lint

* Uses the real default to set the default value by default

* Fixes some extra tests surrounding email sending and number incompatibility

* Reverts test config

* re-adds gom2/usdt currency

* Fixes typo, don't look!

* Fixes minor codelingo pickups

* Adds more precision to handling of trade data from Kraken. Expands test

* interface christmas tree

* lint
2020-10-29 13:00:02 +11:00

1067 lines
30 KiB
Go

package ftx
import (
"fmt"
"sort"
"strconv"
"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"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
"github.com/thrasher-corp/gocryptotrader/log"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
)
// GetDefaultConfig returns a default exchange config
func (f *FTX) GetDefaultConfig() (*config.ExchangeConfig, error) {
f.SetDefaults()
exchCfg := new(config.ExchangeConfig)
exchCfg.Name = f.Name
exchCfg.HTTPTimeout = exchange.DefaultHTTPTimeout
exchCfg.BaseCurrencies = f.BaseCurrencies
err := f.SetupDefaults(exchCfg)
if err != nil {
return nil, err
}
if f.Features.Supports.RESTCapabilities.AutoPairUpdates {
err = f.UpdateTradablePairs(true)
if err != nil {
return nil, err
}
}
return exchCfg, nil
}
// SetDefaults sets the basic defaults for FTX
func (f *FTX) SetDefaults() {
f.Name = "FTX"
f.Enabled = true
f.Verbose = true
f.API.CredentialsValidator.RequiresKey = true
f.API.CredentialsValidator.RequiresSecret = true
spot := currency.PairStore{
RequestFormat: &currency.PairFormat{
Uppercase: true,
Delimiter: "/",
},
ConfigFormat: &currency.PairFormat{
Uppercase: true,
Delimiter: "/",
},
}
futures := currency.PairStore{
RequestFormat: &currency.PairFormat{
Uppercase: true,
Delimiter: "-",
},
ConfigFormat: &currency.PairFormat{
Uppercase: true,
Delimiter: "-",
},
}
err := f.StoreAssetPairFormat(asset.Spot, spot)
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
err = f.StoreAssetPairFormat(asset.Futures, futures)
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
f.Features = exchange.Features{
Supports: exchange.FeaturesSupported{
REST: true,
Websocket: true,
RESTCapabilities: protocol.Features{
TickerFetching: true,
KlineFetching: true,
TradeFetching: true,
OrderbookFetching: true,
AutoPairUpdates: true,
AccountInfo: true,
GetOrder: true,
GetOrders: true,
CancelOrders: true,
CancelOrder: true,
SubmitOrder: true,
TradeFee: true,
FiatDepositFee: true,
FiatWithdrawalFee: true,
CryptoWithdrawalFee: true,
},
WebsocketCapabilities: protocol.Features{
OrderbookFetching: true,
TradeFetching: true,
Subscribe: true,
Unsubscribe: true,
GetOrders: true,
GetOrder: true,
},
WithdrawPermissions: exchange.NoAPIWithdrawalMethods,
Kline: kline.ExchangeCapabilitiesSupported{
DateRanges: true,
Intervals: true,
},
},
Enabled: exchange.FeaturesEnabled{
AutoPairUpdates: true,
Kline: kline.ExchangeCapabilitiesEnabled{
Intervals: map[string]bool{
kline.FifteenSecond.Word(): true,
kline.OneMin.Word(): true,
kline.FiveMin.Word(): true,
kline.FifteenMin.Word(): true,
kline.OneHour.Word(): true,
kline.FourHour.Word(): true,
kline.OneDay.Word(): true,
},
ResultLimit: 5000,
},
},
}
f.Requester = request.New(f.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(request.NewBasicRateLimit(ratePeriod, rateLimit)))
f.API.Endpoints.URLDefault = ftxAPIURL
f.API.Endpoints.URL = f.API.Endpoints.URLDefault
f.Websocket = stream.New()
f.WebsocketResponseMaxLimit = exchange.DefaultWebsocketResponseMaxLimit
f.WebsocketResponseCheckTimeout = exchange.DefaultWebsocketResponseCheckTimeout
f.WebsocketOrderbookBufferLimit = exchange.DefaultWebsocketOrderbookBufferLimit
}
// Setup takes in the supplied exchange configuration details and sets params
func (f *FTX) Setup(exch *config.ExchangeConfig) error {
if !exch.Enabled {
f.SetEnabled(false)
return nil
}
err := f.SetupDefaults(exch)
if err != nil {
return err
}
err = f.Websocket.Setup(&stream.WebsocketSetup{
Enabled: exch.Features.Enabled.Websocket,
Verbose: exch.Verbose,
AuthenticatedWebsocketAPISupport: exch.API.AuthenticatedWebsocketSupport,
WebsocketTimeout: exch.WebsocketTrafficTimeout,
DefaultURL: ftxWSURL,
ExchangeName: exch.Name,
RunningURL: exch.API.Endpoints.WebsocketURL,
Connector: f.WsConnect,
Subscriber: f.Subscribe,
UnSubscriber: f.Unsubscribe,
GenerateSubscriptions: f.GenerateDefaultSubscriptions,
Features: &f.Features.Supports.WebsocketCapabilities,
OrderbookBufferLimit: exch.WebsocketOrderbookBufferLimit,
})
if err != nil {
return err
}
return f.Websocket.SetupNewConnection(stream.ConnectionSetup{
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
})
}
// Start starts the FTX go routine
func (f *FTX) Start(wg *sync.WaitGroup) {
wg.Add(1)
go func() {
f.Run()
wg.Done()
}()
}
// Run implements the FTX wrapper
func (f *FTX) Run() {
if f.Verbose {
log.Debugf(log.ExchangeSys,
"%s Websocket: %s.",
f.Name,
common.IsEnabled(f.Websocket.IsEnabled()))
f.PrintEnabledPairs()
}
if !f.GetEnabledFeatures().AutoPairUpdates {
return
}
err := f.UpdateTradablePairs(false)
if err != nil {
log.Errorf(log.ExchangeSys,
"%s failed to update tradable pairs. Err: %s",
f.Name,
err)
}
}
// FetchTradablePairs returns a list of the exchanges tradable pairs
func (f *FTX) FetchTradablePairs(a asset.Item) ([]string, error) {
if !f.SupportsAsset(a) {
return nil, fmt.Errorf("asset type of %s is not supported by %s", a, f.Name)
}
markets, err := f.GetMarkets()
if err != nil {
return nil, err
}
var pairs []string
switch a {
case asset.Spot:
for x := range markets {
if markets[x].MarketType == spotString {
pairs = append(pairs, markets[x].Name)
}
}
case asset.Futures:
for x := range markets {
if markets[x].MarketType == futuresString {
pairs = append(pairs, markets[x].Name)
}
}
}
return pairs, nil
}
// UpdateTradablePairs updates the exchanges available pairs and stores
// them in the exchanges config
func (f *FTX) UpdateTradablePairs(forceUpdate bool) error {
assets := f.GetAssetTypes()
for x := range assets {
pairs, err := f.FetchTradablePairs(assets[x])
if err != nil {
return err
}
p, err := currency.NewPairsFromStrings(pairs)
if err != nil {
return err
}
err = f.UpdatePairs(p, assets[x], false, forceUpdate)
if err != nil {
return err
}
}
return nil
}
// UpdateTicker updates and returns the ticker for a currency pair
func (f *FTX) UpdateTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
allPairs, err := f.GetEnabledPairs(assetType)
if err != nil {
return nil, err
}
if !allPairs.Contains(p, true) {
allPairs = append(allPairs, p)
}
markets, err := f.GetMarkets()
if err != nil {
return nil, err
}
for a := range allPairs {
formattedPair, err := f.FormatExchangeCurrency(allPairs[a], assetType)
if err != nil {
return nil, err
}
for x := range markets {
if markets[x].Name != formattedPair.String() {
continue
}
var resp ticker.Price
resp.Pair, err = currency.NewPairFromString(markets[x].Name)
if err != nil {
return nil, err
}
resp.Last = markets[x].Last
resp.Bid = markets[x].Bid
resp.Ask = markets[x].Ask
resp.LastUpdated = time.Now()
resp.AssetType = assetType
resp.ExchangeName = f.Name
err = ticker.ProcessTicker(&resp)
if err != nil {
return nil, err
}
}
}
return ticker.GetTicker(f.Name, p, assetType)
}
// FetchTicker returns the ticker for a currency pair
func (f *FTX) FetchTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
tickerNew, err := ticker.GetTicker(f.Name, p, assetType)
if err != nil {
return f.UpdateTicker(p, assetType)
}
return tickerNew, nil
}
// FetchOrderbook returns orderbook base on the currency pair
func (f *FTX) FetchOrderbook(currency currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
ob, err := orderbook.Get(f.Name, currency, assetType)
if err != nil {
return f.UpdateOrderbook(currency, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (f *FTX) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
orderBook := new(orderbook.Base)
formattedPair, err := f.FormatExchangeCurrency(p, assetType)
if err != nil {
return nil, err
}
tempResp, err := f.GetOrderbook(formattedPair.String(), 0)
if err != nil {
return orderBook, err
}
for x := range tempResp.Bids {
orderBook.Bids = append(orderBook.Bids, orderbook.Item{
Amount: tempResp.Bids[x].Size,
Price: tempResp.Bids[x].Price})
}
for y := range tempResp.Asks {
orderBook.Asks = append(orderBook.Asks, orderbook.Item{
Amount: tempResp.Asks[y].Size,
Price: tempResp.Asks[y].Price})
}
orderBook.Pair = p
orderBook.ExchangeName = f.Name
orderBook.AssetType = assetType
err = orderBook.Process()
if err != nil {
return orderBook, err
}
return orderbook.Get(f.Name, p, assetType)
}
// UpdateAccountInfo retrieves balances for all enabled currencies
func (f *FTX) UpdateAccountInfo() (account.Holdings, error) {
var resp account.Holdings
data, err := f.GetBalances()
if err != nil {
return resp, err
}
var acc account.SubAccount
for i := range data {
c := currency.NewCode(data[i].Coin)
hold := data[i].Total - data[i].Free
total := data[i].Total
acc.Currencies = append(acc.Currencies,
account.Balance{CurrencyName: c,
TotalValue: total,
Hold: hold})
}
resp.Accounts = append(resp.Accounts, acc)
resp.Exchange = f.Name
err = account.Process(&resp)
if err != nil {
return account.Holdings{}, err
}
return resp, nil
}
// FetchAccountInfo retrieves balances for all enabled currencies
func (f *FTX) FetchAccountInfo() (account.Holdings, error) {
acc, err := account.GetHoldings(f.Name)
if err != nil {
return f.UpdateAccountInfo()
}
return acc, nil
}
// GetFundingHistory returns funding history, deposits and
// withdrawals
func (f *FTX) GetFundingHistory() ([]exchange.FundHistory, error) {
var resp []exchange.FundHistory
depositData, err := f.FetchDepositHistory()
if err != nil {
return resp, err
}
for x := range depositData {
var tempData exchange.FundHistory
tempData.Fee = depositData[x].Fee
tempData.Timestamp = depositData[x].Time
tempData.ExchangeName = f.Name
tempData.CryptoTxID = depositData[x].TxID
tempData.Status = depositData[x].Status
tempData.Amount = depositData[x].Size
tempData.Currency = depositData[x].Coin
tempData.TransferID = strconv.FormatInt(depositData[x].ID, 10)
resp = append(resp, tempData)
}
withdrawalData, err := f.FetchWithdrawalHistory()
if err != nil {
return resp, err
}
for y := range withdrawalData {
var tempData exchange.FundHistory
tempData.Fee = depositData[y].Fee
tempData.Timestamp = depositData[y].Time
tempData.ExchangeName = f.Name
tempData.CryptoTxID = depositData[y].TxID
tempData.Status = depositData[y].Status
tempData.Amount = depositData[y].Size
tempData.Currency = depositData[y].Coin
tempData.TransferID = strconv.FormatInt(depositData[y].ID, 10)
resp = append(resp, tempData)
}
return resp, nil
}
// GetRecentTrades returns the most recent trades for a currency and asset
func (f *FTX) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
return f.GetHistoricTrades(p, assetType, time.Now().Add(-time.Hour), time.Now())
}
// GetHistoricTrades returns historic trade data within the timeframe provided
func (f *FTX) GetHistoricTrades(p currency.Pair, assetType asset.Item, timestampStart, timestampEnd time.Time) ([]trade.Data, error) {
if timestampStart.Equal(timestampEnd) ||
timestampEnd.After(time.Now()) ||
timestampEnd.Before(timestampStart) ||
(timestampStart.IsZero() && !timestampEnd.IsZero()) {
return nil, fmt.Errorf("invalid time range supplied. Start: %v End %v",
timestampStart,
timestampEnd)
}
var err error
p, err = f.FormatExchangeCurrency(p, assetType)
if err != nil {
return nil, err
}
ts := timestampStart
var resp []trade.Data
limit := 100
allTrades:
for {
var trades []TradeData
trades, err = f.GetTrades(p.String(),
ts.Unix(),
timestampEnd.Unix(),
100)
if err != nil {
return nil, err
}
for i := 0; i < len(trades); i++ {
if trades[i].Time.Before(timestampStart) || trades[i].Time.After(timestampEnd) {
break allTrades
}
var side order.Side
side, err = order.StringToOrderSide(trades[i].Side)
if err != nil {
return nil, err
}
resp = append(resp, trade.Data{
TID: strconv.FormatInt(trades[i].ID, 10),
Exchange: f.Name,
CurrencyPair: p,
AssetType: assetType,
Side: side,
Price: trades[i].Price,
Amount: trades[i].Size,
Timestamp: trades[i].Time,
})
if i == len(trades)-1 {
if ts.Equal(trades[i].Time) {
// reached end of trades to crawl
break allTrades
}
ts = trades[i].Time
}
}
if len(trades) != limit {
break allTrades
}
}
err = f.AddTradesToBuffer(resp...)
if err != nil {
return nil, err
}
sort.Sort(trade.ByDate(resp))
return trade.FilterTradesByTime(resp, timestampStart, timestampEnd), nil
}
// SubmitOrder submits a new order
func (f *FTX) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
var resp order.SubmitResponse
if err := s.Validate(); err != nil {
return resp, err
}
if s.Side == order.Ask {
s.Side = order.Sell
}
if s.Side == order.Bid {
s.Side = order.Buy
}
fPair, err := f.FormatExchangeCurrency(s.Pair, s.AssetType)
if err != nil {
return resp, err
}
tempResp, err := f.Order(fPair.String(),
s.Side.Lower(),
s.Type.Lower(),
"",
"",
"",
s.ClientOrderID,
s.Price,
s.Amount)
if err != nil {
return resp, err
}
resp.IsOrderPlaced = true
resp.OrderID = strconv.FormatInt(tempResp.ID, 10)
return resp, nil
}
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (f *FTX) ModifyOrder(action *order.Modify) (string, error) {
if err := action.Validate(); err != nil {
return "", err
}
if action.TriggerPrice != 0 {
a, err := f.ModifyTriggerOrder(action.ID,
action.Type.String(),
action.Amount,
action.TriggerPrice,
action.Price,
0)
if err != nil {
return "", err
}
return strconv.FormatInt(a.ID, 10), err
}
var o OrderData
var err error
switch action.ID {
case "":
o, err = f.ModifyOrderByClientID(action.ClientOrderID, action.ClientOrderID, action.Price, action.Amount)
if err != nil {
return "", err
}
default:
o, err = f.ModifyPlacedOrder(action.ID, action.ClientOrderID, action.Price, action.Amount)
if err != nil {
return "", err
}
}
return strconv.FormatInt(o.ID, 10), err
}
// CancelOrder cancels an order by its corresponding ID number
func (f *FTX) CancelOrder(o *order.Cancel) error {
if err := o.Validate(o.StandardCancel()); err != nil {
return err
}
if o.ClientOrderID != "" {
_, err := f.DeleteOrderByClientID(o.ClientOrderID)
return err
}
_, err := f.DeleteOrder(o.ID)
return err
}
// CancelAllOrders cancels all orders associated with a currency pair
func (f *FTX) CancelAllOrders(orderCancellation *order.Cancel) (order.CancelAllResponse, error) {
if err := orderCancellation.Validate(); err != nil {
return order.CancelAllResponse{}, err
}
var resp order.CancelAllResponse
formattedPair, err := f.FormatExchangeCurrency(orderCancellation.Pair, orderCancellation.AssetType)
if err != nil {
return resp, err
}
orders, err := f.GetOpenOrders(formattedPair.String())
if err != nil {
return resp, err
}
tempMap := make(map[string]string)
for x := range orders {
_, err := f.DeleteOrder(strconv.FormatInt(orders[x].ID, 10))
if err != nil {
tempMap[strconv.FormatInt(orders[x].ID, 10)] = "Cancellation Failed"
continue
}
tempMap[strconv.FormatInt(orders[x].ID, 10)] = "Success"
}
resp.Status = tempMap
return resp, nil
}
// GetCompatible gets compatible variables for order vars
func (s *OrderData) GetCompatible(f *FTX) (OrderVars, error) {
var resp OrderVars
switch s.Side {
case order.Buy.Lower():
resp.Side = order.Buy
case order.Sell.Lower():
resp.Side = order.Sell
}
switch s.Status {
case strings.ToLower(order.New.String()):
resp.Status = order.New
case strings.ToLower(order.Open.String()):
resp.Status = order.Open
case closedStatus:
if s.FilledSize != 0 && s.FilledSize != s.Size {
resp.Status = order.PartiallyCancelled
}
if s.FilledSize == 0 {
resp.Status = order.Cancelled
}
if s.FilledSize == s.Size {
resp.Status = order.Filled
}
}
var feeBuilder exchange.FeeBuilder
feeBuilder.PurchasePrice = s.AvgFillPrice
feeBuilder.Amount = s.Size
resp.OrderType = order.Market
if strings.EqualFold(s.OrderType, order.Limit.String()) {
resp.OrderType = order.Limit
feeBuilder.IsMaker = true
}
fee, err := f.GetFee(&feeBuilder)
if err != nil {
return resp, err
}
resp.Fee = fee
return resp, nil
}
// GetOrderInfo returns order information based on order ID
func (f *FTX) GetOrderInfo(orderID string, pair currency.Pair, assetType asset.Item) (order.Detail, error) {
var resp order.Detail
orderData, err := f.GetOrderStatus(orderID)
if err != nil {
return resp, err
}
p, err := currency.NewPairFromString(orderData.Market)
if err != nil {
return resp, err
}
orderAssetType, err := f.GetPairAssetType(p)
if err != nil {
return resp, err
}
resp.ID = strconv.FormatInt(orderData.ID, 10)
resp.Amount = orderData.Size
resp.ClientOrderID = orderData.ClientID
resp.Date = orderData.CreatedAt
resp.Exchange = f.Name
resp.ExecutedAmount = orderData.Size - orderData.RemainingSize
resp.Pair = p
resp.AssetType = orderAssetType
resp.Price = orderData.Price
resp.RemainingAmount = orderData.RemainingSize
orderVars, err := orderData.GetCompatible(f)
if err != nil {
return resp, err
}
resp.Status = orderVars.Status
resp.Side = orderVars.Side
resp.Type = orderVars.OrderType
resp.Fee = orderVars.Fee
return resp, nil
}
// GetDepositAddress returns a deposit address for a specified currency
func (f *FTX) GetDepositAddress(cryptocurrency currency.Code, _ string) (string, error) {
a, err := f.FetchDepositAddress(cryptocurrency.String())
if err != nil {
return "", err
}
return a.Address, nil
}
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
// submitted
func (f *FTX) WithdrawCryptocurrencyFunds(withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
if err := withdrawRequest.Validate(); err != nil {
return nil, err
}
resp, err := f.Withdraw(withdrawRequest.Currency.String(),
withdrawRequest.Crypto.Address,
withdrawRequest.Crypto.AddressTag,
withdrawRequest.TradePassword,
strconv.FormatInt(withdrawRequest.OneTimePassword, 10),
withdrawRequest.Amount)
if err != nil {
return nil, err
}
return &withdraw.ExchangeResponse{
ID: strconv.FormatInt(resp.ID, 10),
Status: resp.Status,
}, nil
}
// WithdrawFiatFunds returns a withdrawal ID when a withdrawal is
// submitted
func (f *FTX) WithdrawFiatFunds(_ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
return nil, common.ErrFunctionNotSupported
}
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a
// withdrawal is submitted
func (f *FTX) WithdrawFiatFundsToInternationalBank(_ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
return nil, common.ErrFunctionNotSupported
}
// GetWebsocket returns a pointer to the exchange websocket
func (f *FTX) GetWebsocket() (*stream.Websocket, error) {
return f.Websocket, nil
}
// GetActiveOrders retrieves any orders that are active/open
func (f *FTX) GetActiveOrders(getOrdersRequest *order.GetOrdersRequest) ([]order.Detail, error) {
if err := getOrdersRequest.Validate(); err != nil {
return nil, err
}
var resp []order.Detail
for x := range getOrdersRequest.Pairs {
assetType, err := f.GetPairAssetType(getOrdersRequest.Pairs[x])
if err != nil {
return resp, err
}
formattedPair, err := f.FormatExchangeCurrency(getOrdersRequest.Pairs[x], assetType)
if err != nil {
return nil, err
}
var tempResp order.Detail
orderData, err := f.GetOpenOrders(formattedPair.String())
if err != nil {
return resp, err
}
for y := range orderData {
var p currency.Pair
p, err = currency.NewPairFromString(orderData[y].Market)
if err != nil {
return nil, err
}
tempResp.ID = strconv.FormatInt(orderData[y].ID, 10)
tempResp.Amount = orderData[y].Size
tempResp.AssetType = assetType
tempResp.ClientOrderID = orderData[y].ClientID
tempResp.Date = orderData[y].CreatedAt
tempResp.Exchange = f.Name
tempResp.ExecutedAmount = orderData[y].Size - orderData[y].RemainingSize
tempResp.Pair = p
tempResp.Price = orderData[y].Price
tempResp.RemainingAmount = orderData[y].RemainingSize
var orderVars OrderVars
orderVars, err = f.compatibleOrderVars(orderData[y].Side,
orderData[y].Status,
orderData[y].OrderType,
orderData[y].FilledSize,
orderData[y].Size,
orderData[y].AvgFillPrice)
if err != nil {
return resp, err
}
tempResp.Status = orderVars.Status
tempResp.Side = orderVars.Side
tempResp.Type = orderVars.OrderType
tempResp.Fee = orderVars.Fee
resp = append(resp, tempResp)
}
triggerOrderData, err := f.GetOpenTriggerOrders(formattedPair.String(),
getOrdersRequest.Type.String())
if err != nil {
return resp, err
}
for z := range triggerOrderData {
var p currency.Pair
p, err = currency.NewPairFromString(triggerOrderData[z].Market)
if err != nil {
return nil, err
}
tempResp.ID = strconv.FormatInt(triggerOrderData[z].ID, 10)
tempResp.Amount = triggerOrderData[z].Size
tempResp.AssetType = assetType
tempResp.Date = triggerOrderData[z].CreatedAt
tempResp.Exchange = f.Name
tempResp.ExecutedAmount = triggerOrderData[z].FilledSize
tempResp.Pair = p
tempResp.Price = triggerOrderData[z].AvgFillPrice
tempResp.RemainingAmount = triggerOrderData[z].Size - triggerOrderData[z].FilledSize
tempResp.TriggerPrice = triggerOrderData[z].TriggerPrice
orderVars, err := f.compatibleOrderVars(triggerOrderData[z].Side,
triggerOrderData[z].Status,
triggerOrderData[z].OrderType,
triggerOrderData[z].FilledSize,
triggerOrderData[z].Size,
triggerOrderData[z].AvgFillPrice)
if err != nil {
return resp, err
}
tempResp.Status = orderVars.Status
tempResp.Side = orderVars.Side
tempResp.Type = orderVars.OrderType
tempResp.Fee = orderVars.Fee
resp = append(resp, tempResp)
}
}
return resp, nil
}
// GetOrderHistory retrieves account order information
// Can Limit response to specific order status
func (f *FTX) GetOrderHistory(getOrdersRequest *order.GetOrdersRequest) ([]order.Detail, error) {
if err := getOrdersRequest.Validate(); err != nil {
return nil, err
}
var resp []order.Detail
for x := range getOrdersRequest.Pairs {
var tempResp order.Detail
assetType, err := f.GetPairAssetType(getOrdersRequest.Pairs[x])
if err != nil {
return resp, err
}
formattedPair, err := f.FormatExchangeCurrency(getOrdersRequest.Pairs[x],
assetType)
if err != nil {
return nil, err
}
orderData, err := f.FetchOrderHistory(formattedPair.String(),
getOrdersRequest.StartTicks, getOrdersRequest.EndTicks, "")
if err != nil {
return resp, err
}
for y := range orderData {
var p currency.Pair
p, err = currency.NewPairFromString(orderData[y].Market)
if err != nil {
return nil, err
}
tempResp.ID = strconv.FormatInt(orderData[y].ID, 10)
tempResp.Amount = orderData[y].Size
tempResp.AssetType = assetType
tempResp.ClientOrderID = orderData[y].ClientID
tempResp.Date = orderData[y].CreatedAt
tempResp.Exchange = f.Name
tempResp.ExecutedAmount = orderData[y].Size - orderData[y].RemainingSize
tempResp.Pair = p
tempResp.Price = orderData[y].Price
tempResp.RemainingAmount = orderData[y].RemainingSize
var orderVars OrderVars
orderVars, err = f.compatibleOrderVars(orderData[y].Side,
orderData[y].Status,
orderData[y].OrderType,
orderData[y].FilledSize,
orderData[y].Size,
orderData[y].AvgFillPrice)
if err != nil {
return resp, err
}
tempResp.Status = orderVars.Status
tempResp.Side = orderVars.Side
tempResp.Type = orderVars.OrderType
tempResp.Fee = orderVars.Fee
resp = append(resp, tempResp)
}
triggerOrderData, err := f.GetTriggerOrderHistory(formattedPair.String(),
getOrdersRequest.StartTicks,
getOrdersRequest.EndTicks,
strings.ToLower(getOrdersRequest.Side.String()),
strings.ToLower(getOrdersRequest.Type.String()),
"")
if err != nil {
return resp, err
}
for z := range triggerOrderData {
var p currency.Pair
p, err = currency.NewPairFromString(triggerOrderData[z].Market)
if err != nil {
return nil, err
}
tempResp.ID = strconv.FormatInt(triggerOrderData[z].ID, 10)
tempResp.Amount = triggerOrderData[z].Size
tempResp.AssetType = assetType
tempResp.Date = triggerOrderData[z].CreatedAt
tempResp.Exchange = f.Name
tempResp.ExecutedAmount = triggerOrderData[z].FilledSize
tempResp.Pair = p
tempResp.Price = triggerOrderData[z].AvgFillPrice
tempResp.RemainingAmount = triggerOrderData[z].Size - triggerOrderData[z].FilledSize
tempResp.TriggerPrice = triggerOrderData[z].TriggerPrice
orderVars, err := f.compatibleOrderVars(triggerOrderData[z].Side,
triggerOrderData[z].Status,
triggerOrderData[z].OrderType,
triggerOrderData[z].FilledSize,
triggerOrderData[z].Size,
triggerOrderData[z].AvgFillPrice)
if err != nil {
return resp, err
}
tempResp.Status = orderVars.Status
tempResp.Side = orderVars.Side
tempResp.Type = orderVars.OrderType
tempResp.Fee = orderVars.Fee
resp = append(resp, tempResp)
}
}
return resp, nil
}
// GetFeeByType returns an estimate of fee based on the type of transaction
func (f *FTX) GetFeeByType(feeBuilder *exchange.FeeBuilder) (float64, error) {
return f.GetFee(feeBuilder)
}
// SubscribeToWebsocketChannels appends to ChannelsToSubscribe
// which lets websocket.manageSubscriptions handle subscribing
func (f *FTX) SubscribeToWebsocketChannels(channels []stream.ChannelSubscription) error {
return f.Websocket.SubscribeToChannels(channels)
}
// UnsubscribeToWebsocketChannels removes from ChannelsToSubscribe
// which lets websocket.manageSubscriptions handle unsubscribing
func (f *FTX) UnsubscribeToWebsocketChannels(channels []stream.ChannelSubscription) error {
return f.Websocket.UnsubscribeChannels(channels)
}
// AuthenticateWebsocket sends an authentication message to the websocket
func (f *FTX) AuthenticateWebsocket() error {
return f.WsAuth()
}
// ValidateCredentials validates current credentials used for wrapper
// functionality
func (f *FTX) ValidateCredentials() error {
_, err := f.UpdateAccountInfo()
return f.CheckTransientError(err)
}
// 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 err := f.ValidateKline(p, a, interval); err != nil {
return kline.Item{}, err
}
formattedPair, err := f.FormatExchangeCurrency(p, a)
if err != nil {
return kline.Item{}, err
}
ohlcData, err := f.GetHistoricalData(formattedPair.String(),
f.FormatExchangeKlineInterval(interval),
strconv.FormatInt(int64(f.Features.Enabled.Kline.ResultLimit), 10),
start, end)
if err != nil {
return kline.Item{}, err
}
ret := kline.Item{
Exchange: f.Name,
Pair: p,
Asset: a,
Interval: interval,
}
for x := range ohlcData {
ret.Candles = append(ret.Candles, kline.Candle{
Time: ohlcData[x].StartTime,
Open: ohlcData[x].Open,
High: ohlcData[x].High,
Low: ohlcData[x].Low,
Close: ohlcData[x].Close,
Volume: ohlcData[x].Volume,
})
}
return ret, nil
}
// 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 err := f.ValidateKline(p, a, interval); err != nil {
return kline.Item{}, err
}
ret := kline.Item{
Exchange: f.Name,
Pair: p,
Asset: a,
Interval: interval,
}
dates := kline.CalcDateRanges(start, end, interval, f.Features.Enabled.Kline.ResultLimit)
formattedPair, err := f.FormatExchangeCurrency(p, a)
if err != nil {
return kline.Item{}, err
}
for x := range dates {
ohlcData, err := f.GetHistoricalData(formattedPair.String(),
f.FormatExchangeKlineInterval(interval),
strconv.FormatInt(int64(f.Features.Enabled.Kline.ResultLimit), 10),
dates[x].Start, dates[x].End)
if err != nil {
return kline.Item{}, err
}
for i := range ohlcData {
ret.Candles = append(ret.Candles, kline.Candle{
Time: ohlcData[i].StartTime,
Open: ohlcData[i].Open,
High: ohlcData[i].High,
Low: ohlcData[i].Low,
Close: ohlcData[i].Close,
Volume: ohlcData[i].Volume,
})
}
}
return ret, nil
}