Documentation
¶
Index ¶
- Constants
- Variables
- func Close(candles []Candle) []float64
- func DefaultViewToName(vd *view.Data, row *view.Row) string
- func GetNewContextFromCandle(c Candle) context.Context
- func High(candles []Candle) []float64
- func Low(candles []Candle) []float64
- func NoAggregation(inputCandleChan <-chan Candle) <-chan AggregatedCandle
- func Open(candles []Candle) []float64
- func RandUid() string
- func RegisterViews(views ...*view.View)
- func SameTime(a time.Time, hour, minute, seconds int) bool
- func StopAt(c Candle, hour, min, sec int) bool
- type AggregatedCandle
- type BacktestBrocker
- func (b *BacktestBrocker) AvailableCash() float64
- func (b *BacktestBrocker) ClosePosition(position Position) error
- func (b *BacktestBrocker) GetOrderByID(orderID string) (Order, error)
- func (b *BacktestBrocker) GetPosition(symbol Symbol) Position
- func (b *BacktestBrocker) GetPositions() []Position
- func (b *BacktestBrocker) ProcessOrders(candle Candle) []Order
- func (b *BacktestBrocker) Shutdown()
- func (b *BacktestBrocker) SubmitOrder(_ Candle, order Order) (string, error)
- type Broker
- type Candle
- type Cerbero
- type DataFeed
- type EvaluateCommissions
- type ExecutionResult
- type IBZippedCSV
- type MemorySignals
- type Metric
- type Order
- type OrderStatus
- type OrderType
- type Position
- type RedisExporter
- type SimplePsarStrategy
- type Strategy
- type Symbol
- type TimeAggregation
- type TimeSerie
- type ZigZagStrategy
Constants ¶
const ( OrderBuy = iota OrderSell )
const ( OrderStatusSubmitted = iota OrderStatusAccepted OrderStatusPartiallyFilled OrderStatusFullFilled OrderStatusRejected )
Variables ¶
var ( ErrOrderNotFound = errors.New("order not found") ErrInvalidSize = errors.New("order.size should be > 0") )
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) )
var ( ErrMetricNotFound = errors.New("metric not founc") MCandleOpen = NewMetricWithDefaultViews("candle_open") MCandleHigh = NewMetricWithDefaultViews("candle_high") MCandleClose = NewMetricWithDefaultViews("candle_close") MCandleLow = NewMetricWithDefaultViews("candle_low") MCandleVolume = NewMetricWithDefaultViews("candle_volume") MTradesBuy = NewMetricWithDefaultViews("trades_buy") MTradesSell = NewMetricWithDefaultViews("trades_sell") MCash = NewMetricWithDefaultViews("cash") MStartingCash = NewMetricWithDefaultViews("cash_initial") MPosition = NewMetricWithDefaultViews("position") KeySymbol, _ = tag.NewKey("symbol") )
var Nocommissions = func(order Order, price float64) float64 { return 0 }
Functions ¶
func GetNewContextFromCandle ¶
func NoAggregation ¶
func NoAggregation(inputCandleChan <-chan Candle) <-chan AggregatedCandle
func RegisterViews ¶
RegisterViews register the Opencensus views and enable the internal TimeSeries collection
Types ¶
type AggregatedCandle ¶
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 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 ExecutionResult ¶
type IBZippedCSV ¶
type IBZippedCSV struct { DataFolder string Sday time.Time Slowtime time.Duration Symbol Symbol Symbols []Symbol }
func (*IBZippedCSV) Run ¶
func (d *IBZippedCSV) Run() (chan Candle, error)
type MemorySignals ¶
type Metric ¶
type Metric struct { Name string // contains filtered or unexported fields }
func (*Metric) Get ¶
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) RecordBatch ¶
type Order ¶
type OrderStatus ¶
type OrderStatus int
type RedisExporter ¶
type RedisExporter struct { // MetricNameGenerator MUST return a string formatted as `gotrader.<symbol>.<metric>` MetricNameGenerator func(vd *view.Data, row *view.Row) string // contains filtered or unexported fields }
func NewRedisExporter ¶
func NewRedisExporter(redisHostPort string) (*RedisExporter, error)
func (RedisExporter) ExportView ¶
func (exp RedisExporter) ExportView(vd *view.Data)
func (RedisExporter) Flush ¶
func (exp RedisExporter) Flush()
func (RedisExporter) FlushBuffer ¶
func (exp RedisExporter) FlushBuffer(path string)
FlushBuffer saves the commands in a .redis file, that can be imported later
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 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 ZigZagStrategy ¶
type ZigZagStrategy struct { }