exchange

package
v0.0.0-...-583d1ee Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2025 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Overview

Package exchange provides implementations for various data exchange mechanisms

Package exchange provides functionality for interacting with cryptocurrency exchanges and subscribing to data feeds for market information.

Package exchange provides functionality for interacting with cryptocurrency exchanges and managing trading pair information.

Package exchange provides functionality for interacting with cryptocurrency exchanges and simulating trading activities.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidQuantity   = errors.New("invalid quantity")
	ErrInsufficientFunds = errors.New("insufficient funds or locked")
	ErrInvalidAsset      = errors.New("invalid asset")
)

Common errors that can occur during exchange operations

View Source
var (
	// ErrInsufficientData is returned when there is not enough data to fulfill a request
	ErrInsufficientData = errors.New("insufficient data")
)

Functions

func SavePairsToFile

func SavePairsToFile(filename string) error

SavePairsToFile saves the pair map to a file

func SplitAssetQuote

func SplitAssetQuote(pair string) (asset string, quote string)

SplitAssetQuote splits a pair into its asset and quote components

func UpdateAndSavePairs

func UpdateAndSavePairs(ctx context.Context, filename string) error

UpdateAndSavePairs updates and saves the pair map to a file

func UpdatePairs

func UpdatePairs(ctx context.Context) error

UpdatePairs updates the pair map from the Binance API

Types

type AssetQuote

type AssetQuote struct {
	Quote string `json:"quote"`
	Asset string `json:"asset"`
}

AssetQuote represents a trading pair (base asset and quote currency)

func GetPair

func GetPair(pair string) (AssetQuote, bool)

GetPair returns the AssetQuote information for a pair

type AssetValue

type AssetValue struct {
	Time  time.Time
	Value float64
}

AssetValue represents the value of an asset at a specific time

type CSVFeed

type CSVFeed struct {
	Feeds               map[string]PairFeed
	CandlePairTimeFrame map[string][]core.Candle
}

CSVFeed represents a data feed from CSV files

func NewCSVFeed

func NewCSVFeed(targetTimeframe string, feeds ...PairFeed) (*CSVFeed, error)

NewCSVFeed creates a new CSV feed and resamples data to the target timeframe

func (CSVFeed) AssetsInfo

func (c CSVFeed) AssetsInfo(pair string) (core.AssetInfo, error)

AssetsInfo returns information about a trading pair's assets

func (*CSVFeed) CandlesByLimit

func (c *CSVFeed) CandlesByLimit(_ context.Context, pair, timeframe string, limit int) ([]core.Candle, error)

CandlesByLimit returns a limited number of candles and removes them from the feed

func (CSVFeed) CandlesByPeriod

func (c CSVFeed) CandlesByPeriod(_ context.Context, pair, timeframe string, start, end time.Time) ([]core.Candle, error)

CandlesByPeriod returns candles within a specific time period

func (CSVFeed) CandlesSubscription

func (c CSVFeed) CandlesSubscription(_ context.Context, pair, timeframe string) (chan core.Candle, chan error)

CandlesSubscription returns a channel to receive candles

func (CSVFeed) LastQuote

func (c CSVFeed) LastQuote(_ context.Context, _ string) (float64, error)

LastQuote returns the last quote (not implemented for CSVFeed)

func (*CSVFeed) Limit

func (c *CSVFeed) Limit(duration time.Duration) *CSVFeed

Limit limits candles to a specific time duration

type DataFeed

type DataFeed struct {
	Data chan core.Candle
	Err  chan error
}

DataFeed represents a data feed with channels for candles and errors

type DataFeedConsumer

type DataFeedConsumer func(core.Candle)

DataFeedConsumer is a function type that processes candle data

type DataFeedSubscription

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

DataFeedSubscription manages subscriptions to data feeds

func NewDataFeed

func NewDataFeed(exchange core.Exchange, log core.Logger) *DataFeedSubscription

NewDataFeed creates a new instance of DataFeedSubscription

func (*DataFeedSubscription) Connect

func (d *DataFeedSubscription) Connect()

Connect establishes connections to the exchange and initializes feeds

func (*DataFeedSubscription) Preload

func (d *DataFeedSubscription) Preload(ctx context.Context, pair, timeframe string, candles []core.Candle)

Preload loads historical candles for a specific subscription

func (*DataFeedSubscription) Start

func (d *DataFeedSubscription) Start(ctx context.Context, waitForCompletion bool)

Start begins processing all feeds

func (*DataFeedSubscription) Subscribe

func (d *DataFeedSubscription) Subscribe(pair, timeframe string, consumer DataFeedConsumer, onCandleClose bool)

Subscribe adds a new subscription for a pair and timeframe

type OrderError

type OrderError struct {
	Err      error
	Pair     string
	Quantity float64
}

OrderError encapsulates an error related to an order

func (*OrderError) Error

func (o *OrderError) Error() string

Error implements the error interface

type PairFeed

type PairFeed struct {
	Pair       string
	File       string
	Timeframe  string
	HeikinAshi bool
}

PairFeed represents data for a specific trading pair

type PairService

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

PairService manages information about trading pairs

func NewPairService

func NewPairService(pairsData []byte) (*PairService, error)

NewPairService creates a new instance of the pair service

type PaperWallet

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

PaperWallet implements a simulated wallet for backtesting

func NewPaperWallet

func NewPaperWallet(ctx context.Context, baseCoin string, log core.Logger, options ...PaperWalletOption) *PaperWallet

NewPaperWallet creates a new simulated wallet

func (*PaperWallet) Account

func (p *PaperWallet) Account(_ context.Context) (core.Account, error)

Account returns account information

func (*PaperWallet) AssetValues

func (p *PaperWallet) AssetValues(pair string) []AssetValue

AssetValues returns the value history of an asset

func (*PaperWallet) AssetsInfo

func (p *PaperWallet) AssetsInfo(pair string) (core.AssetInfo, error)

AssetsInfo returns information about the assets of a pair

func (*PaperWallet) Cancel

func (p *PaperWallet) Cancel(_ context.Context, order core.Order) error

Cancel cancels an order

func (*PaperWallet) CandlesByLimit

func (p *PaperWallet) CandlesByLimit(ctx context.Context, pair, period string, limit int) ([]core.Candle, error)

CandlesByLimit returns a limited number of candles

func (*PaperWallet) CandlesByPeriod

func (p *PaperWallet) CandlesByPeriod(ctx context.Context, pair, period string,
	start, end time.Time) ([]core.Candle, error)

CandlesByPeriod returns candles within a period

func (*PaperWallet) CandlesSubscription

func (p *PaperWallet) CandlesSubscription(ctx context.Context, pair, timeframe string) (chan core.Candle, chan error)

CandlesSubscription returns a channel to receive candles

func (*PaperWallet) CreateOrderLimit

func (p *PaperWallet) CreateOrderLimit(_ context.Context, side core.SideType, pair string,
	size float64, limit float64) (core.Order, error)

CreateOrderLimit creates a limit order

func (*PaperWallet) CreateOrderMarket

func (p *PaperWallet) CreateOrderMarket(ctx context.Context, side core.SideType, pair string, size float64) (core.Order, error)

CreateOrderMarket creates a market order

func (*PaperWallet) CreateOrderMarketQuote

func (p *PaperWallet) CreateOrderMarketQuote(
	ctx context.Context,
	side core.SideType,
	pair string,
	quoteQuantity float64,
) (core.Order, error)

CreateOrderMarketQuote creates a market order with a quantity in quote currency

func (*PaperWallet) CreateOrderOCO

func (p *PaperWallet) CreateOrderOCO(_ context.Context, side core.SideType, pair string,
	size, price, stop, stopLimit float64) ([]core.Order, error)

CreateOrderOCO creates an OCO (One-Cancels-the-Other) order

func (*PaperWallet) CreateOrderStop

func (p *PaperWallet) CreateOrderStop(_ context.Context, pair string, size float64, limit float64) (core.Order, error)

CreateOrderStop creates a stop order

func (*PaperWallet) EquityValues

func (p *PaperWallet) EquityValues() []AssetValue

EquityValues returns the wallet's value history

func (*PaperWallet) ID

func (p *PaperWallet) ID() int64

ID generates a unique ID for orders

func (*PaperWallet) LastQuote

func (p *PaperWallet) LastQuote(ctx context.Context, pair string) (float64, error)

LastQuote returns the last quote of a pair

func (*PaperWallet) MaxDrawdown

func (p *PaperWallet) MaxDrawdown() (float64, time.Time, time.Time)

MaxDrawdown calculates the maximum drawdown of the wallet

func (*PaperWallet) OnCandle

func (p *PaperWallet) OnCandle(candle core.Candle)

OnCandle processes a new candle

func (*PaperWallet) Order

func (p *PaperWallet) Order(_ context.Context, _ string, id int64) (core.Order, error)

Order returns a specific order

func (*PaperWallet) Pairs

func (p *PaperWallet) Pairs() []string

Pairs returns the list of available pairs in the wallet

func (*PaperWallet) Position

func (p *PaperWallet) Position(ctx context.Context, pair string) (asset, quote float64, err error)

Position returns the position of a pair

func (*PaperWallet) Summary

func (p *PaperWallet) Summary()

Summary prints a summary of the wallet

type PaperWalletOption

type PaperWalletOption func(*PaperWallet)

PaperWalletOption defines an option function to configure PaperWallet

func WithDataFeed

func WithDataFeed(feeder core.Feeder) PaperWalletOption

WithDataFeed configures the data provider

func WithPaperAsset

func WithPaperAsset(pair string, amount float64) PaperWalletOption

WithPaperAsset adds an initial asset to the wallet

func WithPaperFee

func WithPaperFee(maker, taker float64) PaperWalletOption

WithPaperFee configures the wallet fees

type PeriodBoundaryCheck

type PeriodBoundaryCheck func(t time.Time, fromTimeframe, targetTimeframe string) (bool, error)

PeriodBoundaryCheck defines an interface for checking period boundaries

type Subscription

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

Subscription represents a consumer subscription to a data feed

Directories

Path Synopsis
Package binance provides interfaces to interact with Binance exchange
Package binance provides interfaces to interact with Binance exchange

Jump to

Keyboard shortcuts

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