commons

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2021 License: MIT Imports: 16 Imported by: 0

README

commons

Utilities and reusable components for cryptocurrency trading bots.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Checker = ResourceChecker{
	// contains filtered or unexported fields
}

Checker is a default global instance of ResourceChecker.

View Source
var ErrNotBool = errors.New("not a boolean")
View Source
var ErrUnrecognizedLogLevel = errors.New("unrecognized log level")
View Source
var False = AlgoContext{
	Result: false,
	Bools:  nil,
	Floats: nil,
	Ints:   nil,
}
View Source
var True = AlgoContext{
	Result: true,
	Bools:  nil,
	Floats: nil,
	Ints:   nil,
}

Functions

func Code

func Code(e *zerolog.Event, code string)

func GoInterrupt

func GoInterrupt(ctx context.Context, cancel context.CancelFunc)

func Interrupt

func Interrupt(ctx context.Context) bool

Interrupt returns when either (1) interrupt signal is received by the OS or (2) the given context is done.

func Location

func Location() string

Location returns the name of the caller function.

func Location2

func Location2() string

Location2 returns the name of the caller of the caller function.

func Location3

func Location3() string

func Msg

func Msg(e *zerolog.Event)

func Round2

func Round2(x float64) float64

func Round4

func Round4(x float64) float64

func Run added in v0.0.3

func Run(keeper *StateKeeper, algo Algo)

func SetupLogger

func SetupLogger(c LogConfig) error

func SmartCopy

func SmartCopy(dst, src interface{}) error

func TimeFromMs

func TimeFromMs(x int64) time.Time

func What

func What(e *zerolog.Event, what string)

Types

type Algo added in v0.0.3

type Algo interface {
	Run(input AlgoContext, ticker Ticker) (output AlgoContext)
}

type AlgoContainer added in v0.0.3

type AlgoContainer interface {
	Algo
	Insert(a Algo)
}

type AlgoContext added in v0.0.3

type AlgoContext struct {
	Result bool
	Bools  map[string]bool
	Floats map[string]float64
	Ints   map[string]int
}

func NewAlgoContext added in v1.0.2

func NewAlgoContext() AlgoContext

func (AlgoContext) Copy added in v1.0.1

func (c AlgoContext) Copy() AlgoContext

func (AlgoContext) Dict added in v1.0.1

func (c AlgoContext) Dict(dict *zerolog.Event) *zerolog.Event

type Book1 added in v0.0.3

type Book1 struct {
	Time        time.Time
	Symbol      string
	BidPrice    float64
	BidQuantity float64
	AskPrice    float64
	AskQuantity float64
}

Book1 is the level one order book data.

type CreateOrderResponse added in v0.0.3

type CreateOrderResponse struct {
	ExecutedQuantity string
	AvgPrice         string
}

type Exchange added in v0.0.3

type Exchange interface {

	// CreateOrder accepts arguments of the following formats:
	//
	// - side: buy or sell,
	// - orderType: market (just that for now).
	CreateOrder(symbol, side, orderType, quantityStr string, reduceOnly bool) (CreateOrderResponse, error)

	// ChangeMarginType should be invoked with marginType set to
	// "crossed" or "isolated".
	ChangeMarginType(symbol, marginType string) error

	ChangeLeverage(symbol string, leverage int) error

	// [2] Streams.
	Book1(ctx context.Context, symbol string) chan Book1
	Trade(ctx context.Context, symbol string) chan Trade
}

type FilterWriter

type FilterWriter struct {
	Level  zerolog.Level
	Writer io.Writer
}

func (*FilterWriter) Write

func (w *FilterWriter) Write(p []byte) (n int, err error)

func (*FilterWriter) WriteLevel

func (w *FilterWriter) WriteLevel(level zerolog.Level, p []byte) (n int, err error)

type LogConfig added in v0.0.3

type LogConfig struct {
	Level string

	// Enable console logging.
	ConsoleLoggingEnabled bool

	// Directory to log to to when filelogging is enabled.
	Directory string

	// Filename is the name of the logfile which will be placed inside the directory.
	Filename string

	// MaxSize the max size in MB of the logfile before it's rolled.
	MaxSize int

	// MaxBackups the max number of rolled files to keep.
	MaxBackups int

	// MaxAge the max age in days to keep a logfile.
	MaxAge int
}

https://gist.github.com/panta/2530672ca641d953ae452ecb5ef79d7d

type Parallel added in v1.0.1

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

func (*Parallel) Insert added in v1.0.1

func (p *Parallel) Insert(a Algo)

func (*Parallel) Run added in v1.0.1

func (p *Parallel) Run(input AlgoContext, ticker Ticker) (output AlgoContext)

type ResourceChecker added in v0.0.3

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

Checker is a simple tool that check if an object created/initialized is subsequently deleted/deinitialized. Can be used for goroutines, channels, etc.

func (*ResourceChecker) Assert added in v0.0.3

func (c *ResourceChecker) Assert()

CheckerAssert should be defer-called in main().

func (*ResourceChecker) Pop added in v0.0.3

func (c *ResourceChecker) Pop(xs ...string)

func (*ResourceChecker) Push added in v0.0.3

func (c *ResourceChecker) Push(xs ...string)

type SingleStack added in v0.0.3

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

func NewSingleStack added in v0.0.3

func NewSingleStack() *SingleStack

func (*SingleStack) Insert added in v0.0.3

func (s *SingleStack) Insert(a Algo)

Insert implements AlgoContainer.

func (*SingleStack) Run added in v0.0.3

func (s *SingleStack) Run(input AlgoContext, ticker Ticker) (output AlgoContext)

Run implements Algo.

type State added in v0.0.3

type State struct {
	Now     time.Time
	Tickers map[string]*Ticker
}

func (*State) ApplyBook1 added in v0.0.3

func (s *State) ApplyBook1(x Book1) Ticker

func (*State) ApplyTrade added in v0.0.3

func (s *State) ApplyTrade(x Trade) Ticker

type StateKeeper added in v0.0.3

type StateKeeper struct {
	// We yield a new State through this channel after each
	// update.
	Channel chan Ticker
	// contains filtered or unexported fields
}

func NewStateKeeper added in v0.0.3

func NewStateKeeper(numChannels int, symbols ...string) *StateKeeper

func (*StateKeeper) ConsumeBook1 added in v0.0.3

func (k *StateKeeper) ConsumeBook1(xs chan Book1)

func (*StateKeeper) ConsumeTrade added in v0.0.3

func (k *StateKeeper) ConsumeTrade(xs chan Trade)

type Ticker added in v0.0.3

type Ticker struct {
	Now    time.Time
	Symbol string

	// Trade data.
	TradePrice    float64
	TradeQuantity float64

	// Order book level 1 data.
	BidPrice    float64
	BidQuantity float64
	AskPrice    float64
	AskQuantity float64
}

Ticker holds all the relevant data for a single trading pair.

func (*Ticker) ApplyBook1 added in v0.0.3

func (s *Ticker) ApplyBook1(x Book1) Ticker

func (*Ticker) ApplyTrade added in v0.0.3

func (s *Ticker) ApplyTrade(x Trade) Ticker

type Trade added in v0.0.1

type Trade struct {
	Time     time.Time
	Symbol   string
	Price    float64
	Quantity float64
}

type VerboseAlgo added in v0.0.3

type VerboseAlgo struct{}

func (VerboseAlgo) Run added in v0.0.3

func (v VerboseAlgo) Run(ctx AlgoContext, ticker Ticker) AlgoContext

Directories

Path Synopsis
exchanges

Jump to

Keyboard shortcuts

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