Files
gocryptotrader/exchanges/alphapoint/alphapoint_wrapper.go
Scott 5ea5245afb Improvement: Subsystem separation (#664)
* Initial codes for a trade tracker

* Moving everything in a broken fashion

* Removes tradetracker. Removes some errors for subsystems

* Cleans up some subsystems, renames stuttering types. Removes some global Bot usage

* More basic subsystem renaming and file moving

* Removes engine dependency from events,ntpserver,ordermanager,comms manager

* Exports eventManager, fixes rpcserver. puts rpcserver back for now

* Removes redundant error message, further removes engine dependencies

* experimental end of day interface usage

* adds ability to build the application

* Withdraw and event manager handling

* cleans up apiserver and communications manager

* Cleans up some start/setup processes. Though should separate

* More consistency with Setup Start Stop IsRunning funcs

* Final consistency pass before testing phase

* Fixes engine tests. Fixes stop nil issue

* api server tests

* Communications manager testing

* Connection manager tests and nilsubsystem error

* End of day currencypairsyncer tests

* Adds databaseconnection/databaseconnection_test.go

* Adds withdrawal manager tests

* Deposit address testing. Moved orderbook sync first as its more important

* Adds test for event manager

* More full eventmanager testing

* Adds testfile. Enables skipped test.

* ntp manager tests

* Adds ordermanager tests, Extracts a whole new subsystem from engine and fanangles import cycles

* Adds websocket routine manager tests

* Basic portfolio manager testing

* Fixes issue with currency pair sync startup

* Fixes issue with event manager startup

* Starts the order manager before backtester starts

* Fixes fee tests. Expands testing. Doesnt fix races

* Fixes most test races

* Resolves data races

* Fixes subsystem test issues

* currency pair syncer coverage tests

* Refactors portfolio. Fixes tests. Withdraw validation

Portfolio didn't need to exist with a portfolio manager. Now the porfolio manager
is in charge how the portfolio is handled and all portfolio functions are attached
to the base instead of just exported at the package level

Withdrawal validation occurred at the exchange level when it can just be run at the
withdrawal manager level. All withdrawal requests go through that endpoint

* lint -fix

* golang lint fixes

* lints and comments everything

* Updates GCT logo, adds documentation for some subsystems

* More documentation and more logo updates

* Fixes backtesting and apiserver errors encountered

* Fixes errors and typos from reviewing

* More minor fixes

* Changes %h verb to %w

* reverbs to %s

* Humbly begins reverting to more flat engine package

The main reasoning for this is that the subsystem split doesn't make sense
in a golang environment. The subsystems are only meant to be used with engine
and so by placing them in a non-engine area, it does not work and is
inconsistent with the rest of the application's package layout.

This will begin salvaging the changes made by reverting to a flat
engine package, but maintaining the consistent designs introduced.
Further, I will look to remove any TestMains and decrease the scope
of testing to be more local and decrease the issues that have been
caused from our style of testing.

* Manages to re-flatten things. Everything is within its own file

* mini fixes

* Fixes tests and data races and lints

* Updates docs tool for engine to create filename readmes

* os -> ioutil

* remove err

* Appveyor version increase test

* Removes tCleanup as its unsupported on appveyor

* Adds stuff that I thought was in previous merge master commit

* Removes cancel from test

* Fixes really fun test-exclusive data race

* minor nit fixes

* niterinos

* docs gen

* rm;rf test

* Remove typoline. expands startstop helper. Splits apiserver

* Removes accidental folder

* Uses update instead of replace for order upsert

* addresses nits. Renames files. Regenerates documentation.

* lint and removal of comments

* Add new test for default scenario

* Fixes typo

* regen docs
2021-05-31 10:17:12 +10:00

447 lines
14 KiB
Go

package alphapoint
import (
"errors"
"strconv"
"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/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/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 for Alphapoint
func (a *Alphapoint) GetDefaultConfig() (*config.ExchangeConfig, error) {
return nil, common.ErrFunctionNotSupported
}
// SetDefaults sets current default settings
func (a *Alphapoint) SetDefaults() {
a.Name = "Alphapoint"
a.Enabled = true
a.Verbose = true
a.API.Endpoints = a.NewEndpoints()
err := a.API.Endpoints.SetDefaultEndpoints(map[exchange.URL]string{
exchange.RestSpot: alphapointDefaultAPIURL,
exchange.WebsocketSpot: alphapointDefaultWebsocketURL,
})
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
a.API.CredentialsValidator.RequiresKey = true
a.API.CredentialsValidator.RequiresSecret = true
a.Features = exchange.Features{
Supports: exchange.FeaturesSupported{
REST: true,
Websocket: true,
RESTCapabilities: protocol.Features{
AccountInfo: true,
TickerFetching: true,
TradeFetching: true,
OrderbookFetching: true,
GetOrders: true,
CancelOrder: true,
CancelOrders: true,
SubmitOrder: true,
ModifyOrder: true,
UserTradeHistory: true,
CryptoDeposit: true,
CryptoWithdrawal: true,
TradeFee: true,
},
WebsocketCapabilities: protocol.Features{
AccountInfo: true,
},
WithdrawPermissions: exchange.WithdrawCryptoWith2FA |
exchange.AutoWithdrawCryptoWithAPIPermission |
exchange.NoFiatWithdrawals,
},
}
a.Requester = request.New(a.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout))
}
// FetchTradablePairs returns a list of the exchanges tradable pairs
func (a *Alphapoint) FetchTradablePairs(asset asset.Item) ([]string, error) {
return nil, common.ErrFunctionNotSupported
}
// UpdateTradablePairs updates the exchanges available pairs and stores
// them in the exchanges config
func (a *Alphapoint) UpdateTradablePairs(forceUpdate bool) error {
return common.ErrFunctionNotSupported
}
// UpdateAccountInfo retrieves balances for all enabled currencies on the
// Alphapoint exchange
func (a *Alphapoint) UpdateAccountInfo(assetType asset.Item) (account.Holdings, error) {
var response account.Holdings
response.Exchange = a.Name
acc, err := a.GetAccountInformation()
if err != nil {
return response, err
}
var balances []account.Balance
for i := range acc.Currencies {
var balance account.Balance
balance.CurrencyName = currency.NewCode(acc.Currencies[i].Name)
balance.TotalValue = float64(acc.Currencies[i].Balance)
balance.Hold = float64(acc.Currencies[i].Hold)
balances = append(balances, balance)
}
response.Accounts = append(response.Accounts, account.SubAccount{
Currencies: balances,
})
err = account.Process(&response)
if err != nil {
return account.Holdings{}, err
}
return response, nil
}
// FetchAccountInfo retrieves balances for all enabled currencies on the
// Alphapoint exchange
func (a *Alphapoint) FetchAccountInfo(assetType asset.Item) (account.Holdings, error) {
acc, err := account.GetHoldings(a.Name, assetType)
if err != nil {
return a.UpdateAccountInfo(assetType)
}
return acc, nil
}
// UpdateTicker updates and returns the ticker for a currency pair
func (a *Alphapoint) UpdateTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
tick, err := a.GetTicker(p.String())
if err != nil {
return nil, err
}
err = ticker.ProcessTicker(&ticker.Price{
Pair: p,
Ask: tick.Ask,
Bid: tick.Bid,
Low: tick.Low,
High: tick.High,
Volume: tick.Volume,
Last: tick.Last,
ExchangeName: a.Name,
AssetType: assetType,
})
if err != nil {
return nil, err
}
return ticker.GetTicker(a.Name, p, assetType)
}
// FetchTicker returns the ticker for a currency pair
func (a *Alphapoint) FetchTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
tick, err := ticker.GetTicker(a.Name, p, assetType)
if err != nil {
return a.UpdateTicker(p, assetType)
}
return tick, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (a *Alphapoint) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
orderBook := new(orderbook.Base)
orderbookNew, err := a.GetOrderbook(p.String())
if err != nil {
return orderBook, err
}
for x := range orderbookNew.Bids {
orderBook.Bids = append(orderBook.Bids, orderbook.Item{
Amount: orderbookNew.Bids[x].Quantity,
Price: orderbookNew.Bids[x].Price,
})
}
for x := range orderbookNew.Asks {
orderBook.Asks = append(orderBook.Asks, orderbook.Item{
Amount: orderbookNew.Asks[x].Quantity,
Price: orderbookNew.Asks[x].Price,
})
}
orderBook.Pair = p
orderBook.Exchange = a.Name
orderBook.Asset = assetType
err = orderBook.Process()
if err != nil {
return orderBook, err
}
return orderbook.Get(a.Name, p, assetType)
}
// FetchOrderbook returns the orderbook for a currency pair
func (a *Alphapoint) FetchOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
ob, err := orderbook.Get(a.Name, p, assetType)
if err != nil {
return a.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// GetFundingHistory returns funding history, deposits and
// withdrawals
func (a *Alphapoint) GetFundingHistory() ([]exchange.FundHistory, error) {
// https://alphapoint.github.io/slate/#generatetreasuryactivityreport
return nil, common.ErrNotYetImplemented
}
// GetWithdrawalsHistory returns previous withdrawals data
func (a *Alphapoint) GetWithdrawalsHistory(c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
}
// GetRecentTrades returns the most recent trades for a currency and asset
func (a *Alphapoint) GetRecentTrades(_ currency.Pair, _ asset.Item) ([]trade.Data, error) {
return nil, common.ErrNotYetImplemented
}
// GetHistoricTrades returns historic trade data within the timeframe provided
func (a *Alphapoint) GetHistoricTrades(_ currency.Pair, _ asset.Item, _, _ time.Time) ([]trade.Data, error) {
return nil, common.ErrNotYetImplemented
}
// SubmitOrder submits a new order and returns a true value when
// successfully submitted
func (a *Alphapoint) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
var submitOrderResponse order.SubmitResponse
if err := s.Validate(); err != nil {
return submitOrderResponse, err
}
fPair, err := a.FormatExchangeCurrency(s.Pair, s.AssetType)
if err != nil {
return submitOrderResponse, err
}
response, err := a.CreateOrder(fPair.String(),
s.Side.String(),
s.Type.String(),
s.Amount,
s.Price)
if err != nil {
return submitOrderResponse, err
}
if response > 0 {
submitOrderResponse.OrderID = strconv.FormatInt(response, 10)
}
if s.Type == order.Market {
submitOrderResponse.FullyMatched = true
}
submitOrderResponse.IsOrderPlaced = true
return submitOrderResponse, nil
}
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (a *Alphapoint) ModifyOrder(_ *order.Modify) (string, error) {
return "", common.ErrNotYetImplemented
}
// CancelOrder cancels an order by its corresponding ID number
func (a *Alphapoint) CancelOrder(o *order.Cancel) error {
if err := o.Validate(o.StandardCancel()); err != nil {
return err
}
orderIDInt, err := strconv.ParseInt(o.ID, 10, 64)
if err != nil {
return err
}
_, err = a.CancelExistingOrder(orderIDInt, o.AccountID)
return err
}
// CancelBatchOrders cancels an orders by their corresponding ID numbers
func (a *Alphapoint) CancelBatchOrders(o []order.Cancel) (order.CancelBatchResponse, error) {
return order.CancelBatchResponse{}, common.ErrNotYetImplemented
}
// CancelAllOrders cancels all orders for a given account
func (a *Alphapoint) CancelAllOrders(orderCancellation *order.Cancel) (order.CancelAllResponse, error) {
if err := orderCancellation.Validate(); err != nil {
return order.CancelAllResponse{}, err
}
return order.CancelAllResponse{},
a.CancelAllExistingOrders(orderCancellation.AccountID)
}
// GetOrderInfo returns order information based on order ID
func (a *Alphapoint) GetOrderInfo(orderID string, pair currency.Pair, assetType asset.Item) (float64, error) {
orders, err := a.GetOrders()
if err != nil {
return 0, err
}
for x := range orders {
for y := range orders[x].OpenOrders {
if strconv.Itoa(orders[x].OpenOrders[y].ServerOrderID) == orderID {
return orders[x].OpenOrders[y].QtyRemaining, nil
}
}
}
return 0, errors.New("order not found")
}
// GetDepositAddress returns a deposit address for a specified currency
func (a *Alphapoint) GetDepositAddress(cryptocurrency currency.Code, _ string) (string, error) {
addreses, err := a.GetDepositAddresses()
if err != nil {
return "", err
}
for x := range addreses {
if addreses[x].Name == cryptocurrency.String() {
return addreses[x].DepositAddress, nil
}
}
return "", errors.New("associated currency address not found")
}
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
// submitted
func (a *Alphapoint) WithdrawCryptocurrencyFunds(_ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
return nil, common.ErrNotYetImplemented
}
// WithdrawFiatFunds returns a withdrawal ID when a withdrawal is submitted
func (a *Alphapoint) WithdrawFiatFunds(_ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
return nil, common.ErrNotYetImplemented
}
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a withdrawal is
// submitted
func (a *Alphapoint) WithdrawFiatFundsToInternationalBank(_ *withdraw.Request) (string, error) {
return "", common.ErrNotYetImplemented
}
// GetFeeByType returns an estimate of fee based on type of transaction
func (a *Alphapoint) GetFeeByType(_ *exchange.FeeBuilder) (float64, error) {
return 0, common.ErrFunctionNotSupported
}
// GetActiveOrders retrieves any orders that are active/open
// This function is not concurrency safe due to orderSide/orderType maps
func (a *Alphapoint) GetActiveOrders(req *order.GetOrdersRequest) ([]order.Detail, error) {
if err := req.Validate(); err != nil {
return nil, err
}
resp, err := a.GetOrders()
if err != nil {
return nil, err
}
var orders []order.Detail
for x := range resp {
for y := range resp[x].OpenOrders {
if resp[x].OpenOrders[y].State != 1 {
continue
}
orderDetail := order.Detail{
Amount: resp[x].OpenOrders[y].QtyTotal,
Exchange: a.Name,
AccountID: strconv.FormatInt(int64(resp[x].OpenOrders[y].AccountID), 10),
ID: strconv.FormatInt(int64(resp[x].OpenOrders[y].ServerOrderID), 10),
Price: resp[x].OpenOrders[y].Price,
RemainingAmount: resp[x].OpenOrders[y].QtyRemaining,
}
orderDetail.Side = orderSideMap[resp[x].OpenOrders[y].Side]
orderDetail.Date = time.Unix(resp[x].OpenOrders[y].ReceiveTime, 0)
orderDetail.Type = orderTypeMap[resp[x].OpenOrders[y].OrderType]
if orderDetail.Type == "" {
orderDetail.Type = order.UnknownType
}
orders = append(orders, orderDetail)
}
}
order.FilterOrdersByType(&orders, req.Type)
order.FilterOrdersBySide(&orders, req.Side)
order.FilterOrdersByTimeRange(&orders, req.StartTime, req.EndTime)
return orders, nil
}
// GetOrderHistory retrieves account order information
// Can Limit response to specific order status
// This function is not concurrency safe due to orderSide/orderType maps
func (a *Alphapoint) GetOrderHistory(req *order.GetOrdersRequest) ([]order.Detail, error) {
if err := req.Validate(); err != nil {
return nil, err
}
resp, err := a.GetOrders()
if err != nil {
return nil, err
}
var orders []order.Detail
for x := range resp {
for y := range resp[x].OpenOrders {
if resp[x].OpenOrders[y].State == 1 {
continue
}
orderDetail := order.Detail{
Amount: resp[x].OpenOrders[y].QtyTotal,
AccountID: strconv.FormatInt(int64(resp[x].OpenOrders[y].AccountID), 10),
Exchange: a.Name,
ID: strconv.FormatInt(int64(resp[x].OpenOrders[y].ServerOrderID), 10),
Price: resp[x].OpenOrders[y].Price,
RemainingAmount: resp[x].OpenOrders[y].QtyRemaining,
}
orderDetail.Side = orderSideMap[resp[x].OpenOrders[y].Side]
orderDetail.Date = time.Unix(resp[x].OpenOrders[y].ReceiveTime, 0)
orderDetail.Type = orderTypeMap[resp[x].OpenOrders[y].OrderType]
if orderDetail.Type == "" {
orderDetail.Type = order.UnknownType
}
orders = append(orders, orderDetail)
}
}
order.FilterOrdersByType(&orders, req.Type)
order.FilterOrdersBySide(&orders, req.Side)
order.FilterOrdersByTimeRange(&orders, req.StartTime, req.EndTime)
return orders, nil
}
// ValidateCredentials validates current credentials used for wrapper
// functionality
func (a *Alphapoint) ValidateCredentials(assetType asset.Item) error {
_, err := a.UpdateAccountInfo(assetType)
return a.CheckTransientError(err)
}