gotrader

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OrderBuy = iota
	OrderSell
)
View Source
const (
	OrderStatusSubmitted = iota
	OrderStatusAccepted
	OrderStatusPartiallyFilled
	OrderStatusFullFilled
	OrderStatusRejected
)

Variables

View Source
var (
	ErrOrderNotFound = errors.New("order not found")
	ErrInvalidSize   = errors.New("order.size should be > 0")
)
View Source
var (
	MetricActivePosition = NewMetricWithDefaultViews("broker_position")
	BrockerPosition      = NewMetricWithDefaultViews("broker")

	// Stdout is used to log everything that is not considered an error
	Stdout = log.New(os.Stdout, "", log.Lshortfile|log.Ltime)
	// Stderr log errors that need attention
	Stderr = log.New(os.Stderr, "[ERROR]", log.Lmsgprefix|log.Lshortfile|log.Ltime)
)
View Source
var (
	ErrMetricNotFound = errors.New("metric not founc")

	MCandleOpen  = NewMetricWithDefaultViews("candle_open")
	MCandleHigh  = NewMetricWithDefaultViews("candle_high")
	MCandleClose = NewMetricWithDefaultViews("candle_close")
	MCandleLow   = NewMetricWithDefaultViews("candle_low")
	MTradesBuy   = NewMetricWithDefaultViews("trades_buy")
	MTradesSell  = NewMetricWithDefaultViews("trades_sell")
	MCash        = NewMetricWithDefaultViews("cash")
	MPosition    = NewMetricWithDefaultViews("position")

	KeySymbol, _ = tag.NewKey("symbol")
)
View Source
var Nocommissions = func(order Order, price float64) float64 { return 0 }

Functions

func Close

func Close(candles []Candle) []float64

func GetNewContextFromCandle

func GetNewContextFromCandle(c Candle) context.Context

func High

func High(candles []Candle) []float64

func Low

func Low(candles []Candle) []float64

func NoAggregation

func NoAggregation(inputCandleChan <-chan Candle) <-chan AggregatedCandle

func Open

func Open(candles []Candle) []float64

func RandUid

func RandUid() string

func RegisterViews

func RegisterViews(views ...*view.View)

RegisterViews register the Opencensus views and enable the internal TimeSeries collection

func SignalsToGrafana

func SignalsToGrafana() []byte

Types

type AggregatedCandle

type AggregatedCandle struct {
	Original         Candle
	AggregatedCandle Candle
	IsAggregated     bool
}

type BacktestBrocker

type BacktestBrocker struct {
	BrokerAvailableCash float64
	OrderMap            map[string]*Order
	Portfolio           map[Symbol]Position
	EvalCommissions     EvaluateCommissions
}

BacktestBrocker is the default broker to back-test a strategy

func (*BacktestBrocker) AvailableCash

func (b *BacktestBrocker) AvailableCash() float64

func (*BacktestBrocker) ClosePosition

func (b *BacktestBrocker) ClosePosition(position Position) error

ClosePosition @deprecated

func (*BacktestBrocker) GetOrderByID

func (b *BacktestBrocker) GetOrderByID(orderID string) (Order, error)

func (*BacktestBrocker) GetPosition

func (b *BacktestBrocker) GetPosition(symbol Symbol) Position

func (*BacktestBrocker) GetPositions

func (b *BacktestBrocker) GetPositions() []Position

func (*BacktestBrocker) ProcessOrders

func (b *BacktestBrocker) ProcessOrders(candle Candle) []Order

func (*BacktestBrocker) Shutdown

func (b *BacktestBrocker) Shutdown()

func (*BacktestBrocker) SubmitOrder

func (b *BacktestBrocker) SubmitOrder(_ Candle, order Order) (string, error)

type Broker

type Broker interface {
	SubmitOrder(candle Candle, order Order) (string, error)
	GetOrderByID(OrderID string) (Order, error)
	ProcessOrders(candle Candle) []Order
	GetPosition(symbol Symbol) Position
	Shutdown()
	AvailableCash() float64
	ClosePosition(position Position) error
	GetPositions() []Position
}

Broker interacts with a stock broker

type Candle

type Candle struct {
	Open   float64
	High   float64
	Close  float64
	Low    float64
	Volume int64
	Symbol Symbol
	Time   time.Time
}

func (Candle) String

func (candle Candle) String() string

func (Candle) TimeStr

func (candle Candle) TimeStr() string

type Cerbero

type Cerbero struct {
	Broker              Broker
	Strategy            Strategy
	DataFeed            DataFeed
	TimeAggregationFunc TimeAggregation
	// contains filtered or unexported fields
}

Cerbero is in honor to https://www.backtrader.com/ that deeply inspired this code

func (*Cerbero) Run

func (cerbero *Cerbero) Run() (ExecutionResult, error)

type DataFeed

type DataFeed interface {

	// Run starts a go routine that poll the data source, and push the candles in the returned channel.
	// The channel is expected to have a buffer larger enough to handle 1 day of data
	Run() (chan Candle, error)
}

DataFeed provides a stream of Candle.

type EvaluateCommissions

type EvaluateCommissions func(order Order, price float64) float64

type ExecutionResult

type ExecutionResult struct {
	TotalTime       time.Duration `json:"total_time"`
	TotalTimeString string        `json:"total_time_S"`
	InitialCash     float64       `json:"initial_cash"`
	PL              float64       `json:"pl"`
	FinalCash       float64       `json:"final_cash"`
}

type IBZippedCSV

type IBZippedCSV struct {
	DataFolder string
	Sday       time.Time
	Slowtime   int
	Symbol     Symbol
	Symbols    []Symbol
}

func (*IBZippedCSV) Run

func (d *IBZippedCSV) Run() (chan Candle, error)

type MemorySignals

type MemorySignals struct {
	Metrics map[string]*TimeSerie
}

func (*MemorySignals) Append

func (s *MemorySignals) Append(candle Candle, name string, value float64)

Append a metric to a given signal.

func (*MemorySignals) Get

func (s *MemorySignals) Get(candle Candle, name string, i int) (float64, error)

type Metric

type Metric struct {
	Name string
	// contains filtered or unexported fields
}

func NewMetricWithDefaultViews

func NewMetricWithDefaultViews(name string) *Metric

func (*Metric) Get

func (m *Metric) Get(ctx context.Context, step int) (float64, error)

Get the i-th metric. Metrics are saved in their chronological order. m.Get(0) returns the last value recorder by the metric m. m.Get(-3) return the value inserted 3 "step" ago. A step is defined as a full eval() cycle.

func (*Metric) Record

func (m *Metric) Record(ctx context.Context, value float64)

type Order

type Order struct {
	Id string
	// Size is always > 0
	Size   int64
	Symbol Symbol
	Type   OrderType
	Status OrderStatus
	// SizeFilled is always > 0
	SizeFilled     int64
	AvgFilledPrice float64

	// SubmittedTime When the order has been submitted (candle time)
	SubmittedTime time.Time
}

func (Order) String

func (o Order) String() string

type OrderStatus

type OrderStatus int

type OrderType

type OrderType int

type Position

type Position struct {
	// Size is negative if the position is a SHORT
	Size     int64
	AvgPrice float64
	Symbol   Symbol
}

type SimplePsarStrategy

type SimplePsarStrategy struct {
	Symbol Symbol
}

func (*SimplePsarStrategy) Eval

func (s *SimplePsarStrategy) Eval(candles []Candle)

func (*SimplePsarStrategy) Initialize

func (s *SimplePsarStrategy) Initialize(_ *Cerbero)

type Strategy

type Strategy interface {
	// Eval evaluate the strategy. candles[0] is the latest, candles[1] is the latest - 1, and so on
	Eval(candles []Candle)
	Initialize(broker *Cerbero)
}

type Symbol

type Symbol string

type TimeAggregation

type TimeAggregation func(<-chan Candle) <-chan AggregatedCandle

TimeAggregation aggregate the candles from a channel and write the output in a separate channel

func AggregateBySeconds

func AggregateBySeconds(sec int) TimeAggregation

type TimeSerie

type TimeSerie struct {
	X []time.Time `json:"x"`
	Y []float64   `json:"y"`
}

func (*TimeSerie) Append

func (ts *TimeSerie) Append(candle Candle, value float64)

Append an element to the end of this ts

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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