service

package
v1.19.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2021 License: MIT Imports: 28 Imported by: 16

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrExchangeRewardServiceNotImplemented = errors.New("exchange does not implement ExchangeRewardService interface")
View Source
var ErrNotImplemented = errors.New("not implemented")
View Source
var ErrPersistenceNotExists = errors.New("persistent data does not exists")
View Source
var ErrTradeNotFound = errors.New("trade not found")

Functions

func NewDefaultTotpKey added in v1.4.0

func NewDefaultTotpKey() (*otp.Key, error)

func ReformatMysqlDSN added in v1.11.0

func ReformatMysqlDSN(dsn string) (string, error)

Types

type AggOrder added in v1.11.0

type AggOrder struct {
	types.Order
	AveragePrice *float64 `json:"averagePrice" db:"average_price"`
}

type BacktestService

type BacktestService struct {
	DB *sqlx.DB
}

func (*BacktestService) Insert

func (s *BacktestService) Insert(kline types.KLine) error

func (*BacktestService) QueryFirstKLine added in v1.19.3

func (s *BacktestService) QueryFirstKLine(ex types.ExchangeName, symbol string, interval types.Interval) (*types.KLine, error)

func (*BacktestService) QueryKLine added in v1.19.3

func (s *BacktestService) QueryKLine(ex types.ExchangeName, symbol string, interval types.Interval, orderBy string, limit int) (*types.KLine, error)

QueryKLine queries the klines from the database

func (*BacktestService) QueryKLinesBackward

func (s *BacktestService) QueryKLinesBackward(exchange types.ExchangeName, symbol string, interval types.Interval, endTime time.Time, limit int) ([]types.KLine, error)

func (*BacktestService) QueryKLinesCh

func (s *BacktestService) QueryKLinesCh(since, until time.Time, exchange types.Exchange, symbols []string, intervals []types.Interval) (chan types.KLine, chan error)

func (*BacktestService) QueryKLinesForward

func (s *BacktestService) QueryKLinesForward(exchange types.ExchangeName, symbol string, interval types.Interval, startTime time.Time, limit int) ([]types.KLine, error)

func (*BacktestService) QueryLastKLine added in v1.19.3

func (s *BacktestService) QueryLastKLine(ex types.ExchangeName, symbol string, interval types.Interval) (*types.KLine, error)

QueryLastKLine queries the last kline from the database

func (*BacktestService) Sync

func (s *BacktestService) Sync(ctx context.Context, exchange types.Exchange, symbol string, startTime time.Time) error

func (*BacktestService) SyncKLineByInterval added in v1.14.0

func (s *BacktestService) SyncKLineByInterval(ctx context.Context, exchange types.Exchange, symbol string, interval types.Interval, startTime, endTime time.Time) error

type CurrencyPositionMap added in v1.13.0

type CurrencyPositionMap map[string]fixedpoint.Value

type DatabaseService added in v1.11.0

type DatabaseService struct {
	Driver string
	DSN    string
	DB     *sqlx.DB
}

func NewDatabaseService added in v1.11.0

func NewDatabaseService(driver, dsn string) *DatabaseService

func (*DatabaseService) Close added in v1.11.0

func (s *DatabaseService) Close() error

func (*DatabaseService) Connect added in v1.11.0

func (s *DatabaseService) Connect() error

func (*DatabaseService) Upgrade added in v1.11.0

func (s *DatabaseService) Upgrade(ctx context.Context) error

type DepositService added in v1.14.0

type DepositService struct {
	DB *sqlx.DB
}

func (*DepositService) Insert added in v1.14.0

func (s *DepositService) Insert(deposit types.Deposit) error

func (*DepositService) Query added in v1.14.0

func (s *DepositService) Query(exchangeName types.ExchangeName) ([]types.Deposit, error)

func (*DepositService) QueryLast added in v1.14.0

func (s *DepositService) QueryLast(ex types.ExchangeName, limit int) ([]types.Deposit, error)

func (*DepositService) Sync added in v1.14.0

func (s *DepositService) Sync(ctx context.Context, ex types.Exchange) error

Sync syncs the withdraw records into db

type JsonPersistenceConfig added in v1.12.0

type JsonPersistenceConfig struct {
	Directory string `yaml:"directory" json:"directory"`
}

type JsonPersistenceService added in v1.12.0

type JsonPersistenceService struct {
	Directory string
}

func (*JsonPersistenceService) NewStore added in v1.12.0

func (s *JsonPersistenceService) NewStore(id string, subIDs ...string) Store

type JsonStore added in v1.12.0

type JsonStore struct {
	ID        string
	Directory string
}

func (JsonStore) Load added in v1.12.0

func (store JsonStore) Load(val interface{}) error

func (JsonStore) Reset added in v1.12.0

func (store JsonStore) Reset() error

func (JsonStore) Save added in v1.12.0

func (store JsonStore) Save(val interface{}) error

type MemoryService added in v1.12.0

type MemoryService struct {
	Slots map[string]interface{}
}

func NewMemoryService added in v1.12.0

func NewMemoryService() *MemoryService

func (*MemoryService) NewStore added in v1.12.0

func (s *MemoryService) NewStore(id string, subIDs ...string) Store

type MemoryStore added in v1.12.0

type MemoryStore struct {
	Key string
	// contains filtered or unexported fields
}

func (*MemoryStore) Load added in v1.12.0

func (store *MemoryStore) Load(val interface{}) error

func (*MemoryStore) Reset added in v1.12.0

func (store *MemoryStore) Reset() error

func (*MemoryStore) Save added in v1.12.0

func (store *MemoryStore) Save(val interface{}) error

type OrderService

type OrderService struct {
	DB *sqlx.DB
}

func (*OrderService) Insert

func (s *OrderService) Insert(order types.Order) (err error)

func (*OrderService) Query

func (s *OrderService) Query(options QueryOrdersOptions) ([]AggOrder, error)

func (*OrderService) QueryLast

func (s *OrderService) QueryLast(ex types.ExchangeName, symbol string, isMargin, isIsolated bool, limit int) ([]types.Order, error)

QueryLast queries the last order from the database

func (*OrderService) Sync added in v1.14.0

func (s *OrderService) Sync(ctx context.Context, exchange types.Exchange, symbol string, startTime time.Time) error

type PersistenceService added in v1.12.0

type PersistenceService interface {
	NewStore(id string, subIDs ...string) Store
}

type PersistenceServiceFacade added in v1.12.0

type PersistenceServiceFacade struct {
	Redis  *RedisPersistenceService
	Json   *JsonPersistenceService
	Memory *MemoryService
}

func (*PersistenceServiceFacade) Get added in v1.12.0

Get returns the preferred persistence service by fallbacks Redis will be preferred at the first position.

type QueryOrdersOptions added in v1.11.0

type QueryOrdersOptions struct {
	Exchange types.ExchangeName
	Symbol   string
	LastGID  int64
	Ordering string
}

type QueryTradesOptions added in v1.11.0

type QueryTradesOptions struct {
	Exchange types.ExchangeName
	Symbol   string
	LastGID  int64

	// ASC or DESC
	Ordering string
	Limit    int
}

type RedisPersistenceConfig added in v1.12.0

type RedisPersistenceConfig struct {
	Host     string `yaml:"host" json:"host" env:"REDIS_HOST"`
	Port     string `yaml:"port" json:"port" env:"REDIS_PORT"`
	Password string `yaml:"password,omitempty" json:"password,omitempty" env:"REDIS_PASSWORD"`
	DB       int    `yaml:"db" json:"db" env:"REDIS_DB"`
}

type RedisPersistenceService added in v1.12.0

type RedisPersistenceService struct {
	// contains filtered or unexported fields
}

func NewRedisPersistenceService added in v1.12.0

func NewRedisPersistenceService(config *RedisPersistenceConfig) *RedisPersistenceService

func (*RedisPersistenceService) NewStore added in v1.12.0

func (s *RedisPersistenceService) NewStore(id string, subIDs ...string) Store

type RedisStore added in v1.12.0

type RedisStore struct {
	ID string
	// contains filtered or unexported fields
}

func (*RedisStore) Load added in v1.12.0

func (store *RedisStore) Load(val interface{}) error

func (*RedisStore) Reset added in v1.12.0

func (store *RedisStore) Reset() error

func (*RedisStore) Save added in v1.12.0

func (store *RedisStore) Save(val interface{}) error

type RewardService added in v1.13.0

type RewardService struct {
	DB *sqlx.DB
}

RewardService collects the reward records from the exchange, currently it's only available for MAX exchange. TODO: add summary query for calculating the reward amounts CREATE VIEW reward_summary_by_years AS SELECT YEAR(created_at) as year, reward_type, currency, SUM(quantity) FROM rewards WHERE reward_type != 'airdrop' GROUP BY YEAR(created_at), reward_type, currency ORDER BY year DESC;

func (*RewardService) AggregateUnspentCurrencyPosition added in v1.13.0

func (s *RewardService) AggregateUnspentCurrencyPosition(ctx context.Context, ex types.ExchangeName, since time.Time) (CurrencyPositionMap, error)

func (*RewardService) Insert added in v1.13.0

func (s *RewardService) Insert(reward types.Reward) error

func (*RewardService) MarkAsSpent added in v1.13.0

func (s *RewardService) MarkAsSpent(ctx context.Context, uuid string) error

func (*RewardService) MarkCurrencyAsSpent added in v1.13.0

func (s *RewardService) MarkCurrencyAsSpent(ctx context.Context, currency string) error

func (*RewardService) QueryLast added in v1.13.0

func (s *RewardService) QueryLast(ex types.ExchangeName, limit int) ([]types.Reward, error)

func (*RewardService) QueryUnspent added in v1.13.0

func (s *RewardService) QueryUnspent(ctx context.Context, ex types.ExchangeName, rewardTypes ...types.RewardType) ([]types.Reward, error)

func (*RewardService) QueryUnspentSince added in v1.13.0

func (s *RewardService) QueryUnspentSince(ctx context.Context, ex types.ExchangeName, since time.Time, rewardTypes ...types.RewardType) ([]types.Reward, error)

func (*RewardService) Sync added in v1.14.0

func (s *RewardService) Sync(ctx context.Context, exchange types.Exchange) error

type Store added in v1.12.0

type Store interface {
	Load(val interface{}) error
	Save(val interface{}) error
	Reset() error
}

type SyncService

type SyncService struct {
	TradeService    *TradeService
	OrderService    *OrderService
	RewardService   *RewardService
	WithdrawService *WithdrawService
	DepositService  *DepositService
}

func (*SyncService) SyncSessionSymbols added in v1.11.1

func (s *SyncService) SyncSessionSymbols(ctx context.Context, exchange types.Exchange, startTime time.Time, symbols ...string) error

SyncSessionSymbols syncs the trades from the given exchange session

type TradeService

type TradeService struct {
	DB *sqlx.DB
}

func NewTradeService

func NewTradeService(db *sqlx.DB) *TradeService

func (*TradeService) DeleteAll added in v1.17.0

func (s *TradeService) DeleteAll() error

func (*TradeService) Insert

func (s *TradeService) Insert(trade types.Trade) error

func (*TradeService) Load added in v1.11.1

func (s *TradeService) Load(ctx context.Context, id int64) (*types.Trade, error)

func (*TradeService) Mark added in v1.14.0

func (s *TradeService) Mark(ctx context.Context, id int64, strategyID string) error

func (*TradeService) Query

func (s *TradeService) Query(options QueryTradesOptions) ([]types.Trade, error)

func (*TradeService) QueryForTradingFeeCurrency

func (s *TradeService) QueryForTradingFeeCurrency(ex types.ExchangeName, symbol string, feeCurrency string) ([]types.Trade, error)

func (*TradeService) QueryLast

func (s *TradeService) QueryLast(ex types.ExchangeName, symbol string, isMargin, isIsolated bool, limit int) ([]types.Trade, error)

QueryLast queries the last trade from the database

func (*TradeService) QueryTradingVolume added in v1.11.0

func (s *TradeService) QueryTradingVolume(startTime time.Time, options TradingVolumeQueryOptions) ([]TradingVolume, error)

func (*TradeService) Sync added in v1.14.0

func (s *TradeService) Sync(ctx context.Context, exchange types.Exchange, symbol string) error

func (*TradeService) UpdatePnL added in v1.11.1

func (s *TradeService) UpdatePnL(ctx context.Context, id int64, pnl float64) error

type TradingVolume added in v1.11.0

type TradingVolume struct {
	Year        int       `db:"year" json:"year"`
	Month       int       `db:"month" json:"month,omitempty"`
	Day         int       `db:"day" json:"day,omitempty"`
	Time        time.Time `json:"time,omitempty"`
	Exchange    string    `db:"exchange" json:"exchange,omitempty"`
	Symbol      string    `db:"symbol" json:"symbol,omitempty"`
	QuoteVolume float64   `db:"quote_volume" json:"quoteVolume"`
}

type TradingVolumeQueryOptions added in v1.11.0

type TradingVolumeQueryOptions struct {
	GroupByPeriod string
	SegmentBy     string
}

type WebsocketClientBase added in v1.13.0

type WebsocketClientBase struct {
	// contains filtered or unexported fields
}

func NewWebsocketClientBase added in v1.13.0

func NewWebsocketClientBase(baseURL string, reconnectDuration time.Duration) *WebsocketClientBase

func (*WebsocketClientBase) Conn added in v1.13.0

func (s *WebsocketClientBase) Conn() *websocket.Conn

func (*WebsocketClientBase) Connect added in v1.13.0

func (s *WebsocketClientBase) Connect(ctx context.Context) error

func (*WebsocketClientBase) EmitConnected added in v1.13.0

func (s *WebsocketClientBase) EmitConnected(conn *websocket.Conn)

func (*WebsocketClientBase) EmitDisconnected added in v1.13.0

func (s *WebsocketClientBase) EmitDisconnected(conn *websocket.Conn)

func (*WebsocketClientBase) EmitError added in v1.13.0

func (s *WebsocketClientBase) EmitError(err error)

func (*WebsocketClientBase) EmitMessage added in v1.13.0

func (s *WebsocketClientBase) EmitMessage(message []byte)

func (*WebsocketClientBase) Listen added in v1.13.0

func (s *WebsocketClientBase) Listen(ctx context.Context)

func (*WebsocketClientBase) OnConnected added in v1.13.0

func (s *WebsocketClientBase) OnConnected(cb func(conn *websocket.Conn))

func (*WebsocketClientBase) OnDisconnected added in v1.13.0

func (s *WebsocketClientBase) OnDisconnected(cb func(conn *websocket.Conn))

func (*WebsocketClientBase) OnError added in v1.13.0

func (s *WebsocketClientBase) OnError(cb func(err error))

func (*WebsocketClientBase) OnMessage added in v1.13.0

func (s *WebsocketClientBase) OnMessage(cb func(message []byte))

func (*WebsocketClientBase) Reconnect added in v1.13.0

func (s *WebsocketClientBase) Reconnect()

type WithdrawService added in v1.14.0

type WithdrawService struct {
	DB *sqlx.DB
}

func (*WithdrawService) Insert added in v1.14.0

func (s *WithdrawService) Insert(withdrawal types.Withdraw) error

func (*WithdrawService) Query added in v1.14.0

func (s *WithdrawService) Query(exchangeName types.ExchangeName) ([]types.Withdraw, error)

func (*WithdrawService) QueryLast added in v1.14.0

func (s *WithdrawService) QueryLast(ex types.ExchangeName, limit int) ([]types.Withdraw, error)

func (*WithdrawService) Sync added in v1.14.0

Sync syncs the withdraw records into db

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL