generationk

package module
v0.0.0-...-ec36f96 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2020 License: GPL-3.0 Imports: 16 Imported by: 0

README

generationk

The inspiration for this project took place after using a few other backtesting frameworks in Python. I was tired of waiting for results and concluded that I want the fast feeling of a compiled language and I also want to make use the multiple processor cores that often is available but rarely used.

Design choices

Going for event driven in the end came more from a point of being able to split the program up to run on multiple computers for performance reasons rather than a needs for realism (but implictly contributes to realism). Another choice was working with float arrays as arguments to all indicators to keep it as simple as possible.

The Crossing MA example looks like this

type MACrossStrategy struct {
	ma50       *indicators.SimpleMovingAverage
	close      *indicators.TimeSeries
	initPeriod int
}

func (m *MACrossStrategy) Setup(ctx *Context) error {
	m.close, e = NewTimeSeries(ctx.AssetMap["AAPL"])
	m.ma50, e = NewSimpleMovingAverage(ctx.AssetMap["AAPL"], Close, 5)
	if e != nil {
		return e
	}
	ctx.AddUpdatable(m.close, m.ma50)
}

func (m *MACrossStrategy) Tick(ctx *Context) {
	if m.ma50.ValueAtIndex(0) > m.close.ValueAtIndex(0) {
		if !ctx.Position(ctx.AssetMap["ABB"]) {
			MakeOrder(ctx, OrderType(Buy), ctx.AssetMap["ABB"], ctx.Time(), 1000)
		}
	}
}

There is also coded needed to create the strategy and run the backtest.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeOrder

func MakeOrder(ctx *Context, ordertype OrderType, asset *Asset, time time.Time, amount float64)

func Min

func Min(x, y int) int

Min returns the smaller of x or y.

func RunEventBased

func RunEventBased(ctx *Context)

Types

type Accepted

type Accepted struct {
}

Accepted is a status of the order

func (Accepted) String

func (a Accepted) String() string

type Asset

type Asset struct {
	Name string
	Ohlc []OHLC
}

Asset data type

func NewAsset

func NewAsset(name string, ohlc OHLC) *Asset

NewAsset is used to create a new asset-

func (*Asset) Close

func (a *Asset) Close() float64

Close is used to get the close value

func (*Asset) CloseArray

func (a *Asset) CloseArray() []float64

CloseArray is used to get the close series

func (*Asset) CloseAtBar

func (a *Asset) CloseAtBar(ix int) float64

CloseAtBar is used to get the close value

func (*Asset) Update

func (a *Asset) Update(ohlc OHLC)

Update interface to be able to get updated by the event queue

type Broker

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

Broker is used to send orders

func (*Broker) PlaceOrder

func (b *Broker) PlaceOrder(order Order)

PlaceOrder is used to place an order with the broker

type CSVDataManager

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

CSVDataManager type

func NewCSVDataManager

func NewCSVDataManager(ctx *Context) CSVDataManager

NewCSVDataManager creates a new data manager object

func (CSVDataManager) ReadCSVFileAsync

func (d CSVDataManager) ReadCSVFileAsync(file string)

ReadCSVFileAsync is used to start a go thread

func (CSVDataManager) ReadFolderWithCSVFilesAsync

func (d CSVDataManager) ReadFolderWithCSVFilesAsync(folder string)

ReadFolderWithCSVFilesAsync is used to read a folder of files and put them on the queue to the strategy

type Context

type Context struct {
	//Updateable        []Updateable
	Strategy          []Strategy
	Asset             []Asset
	AssetMap          map[string]*Asset
	AssetIndicatorMap map[string][]*indicators.Indicator
	StartDate         time.Time
	EndDate           time.Time
	Portfolio         Portfolio
	Broker            Broker
	K                 int
	// contains filtered or unexported fields
}

Context for this backtester

func NewContext

func NewContext() *Context

NewContext creates a new context

func (*Context) AddAsset

func (ctx *Context) AddAsset(asset *Asset)

AddAsset is used to add assets that the strategy will use

func (*Context) AddEndDate

func (ctx *Context) AddEndDate(endTime time.Time)

AddEndDate is used to set the strategy that will be run

func (*Context) AddIndicator

func (ctx *Context) AddIndicator(indicator indicators.Indicator)

AddIndicator will add it to all assets

func (*Context) AddIndicatorOnAsset

func (ctx *Context) AddIndicatorOnAsset(asset *Asset, indicator *indicators.Indicator)

AddIndicatorOnAsset will add an indicator on the asset

func (*Context) AddStartDate

func (ctx *Context) AddStartDate(startTime time.Time)

AddStartDate is used to set the strategy that will be run

func (*Context) AddStrategy

func (ctx *Context) AddStrategy(strategy *Strategy)

AddStrategy is used to set the strategy that will be run

func (*Context) AddUpdatable

func (ctx *Context) AddUpdatable(indicators ...Updateable)

AddUpdatable add an updatable interface

func (*Context) EventChannel

func (ctx *Context) EventChannel() chan Event

EventChannel return the channel for events

func (Context) GetInitPeriod

func (ctx Context) GetInitPeriod() int

GetInitPeriod returns the period

func (*Context) OrderChannel

func (ctx *Context) OrderChannel() chan Event

OrderChannel returns the channel for orders

func (*Context) Position

func (ctx *Context) Position(asset *Asset) bool

Position is used to find out if we have a holding in an asset

func (*Context) SetInitPeriod

func (ctx *Context) SetInitPeriod(period int)

SetInitPeriod is used to set the priod for which

func (*Context) Time

func (ctx *Context) Time() time.Time

Time returns the time

type Data

type Data struct{}

Data event type

func (Data) String

func (d Data) String() string

type DataEvent

type DataEvent struct {
	Name string
	Ohlc OHLC
}

DataEvent is for sending data

func (DataEvent) String

func (d DataEvent) String() string

Handle iM not sure what it si used for

type DataSource

type DataSource interface {
	GetData(period int)
	GetLatestData()
}

DataSource interface for getting data

type DataUpdate

type DataUpdate interface {
	Update(ohlc OHLC)
}

DataUpdate is used to update the data in the assets

type Direction

type Direction int
const (
	Long Direction = iota
	Short
)

type EndOfDataError

type EndOfDataError struct {
	Description string
}

func (*EndOfDataError) Error

func (e *EndOfDataError) Error() string

type Event

type Event interface {
	String() string
}

Event type

type Fill

type Fill struct {
	Qty       int
	Price     float64
	AssetName string
	Time      time.Time
}

func (Fill) String

func (f Fill) String() string

type Holding

type Holding struct {
	Qty       int
	AssetName string
	Price     float64
	Time      time.Time
}

type IndicatorError

type IndicatorError struct {
	Err error
	// contains filtered or unexported fields
}

IndicatorError Used to signal an error with an indicator

func (IndicatorError) Error

func (e IndicatorError) Error() string

type IndicatorNotReadyError

type IndicatorNotReadyError struct {
	Msg string //description of error
	Len int    //the length needed before trying again
}

IndicatorNotReadyError is an error thrown when an indicator needs more data to be used

func (IndicatorNotReadyError) Error

func (e IndicatorNotReadyError) Error() string

type KlineExamples

type KlineExamples struct{}

KlineExamples is a struct

func (KlineExamples) Examples

func (KlineExamples) Examples()

Examples generates examples

type OHLC

type OHLC struct {
	Time                   time.Time
	Open, High, Low, Close float64
	Volume                 int
}

OHLC data type

type OhlcHeap

type OhlcHeap []OHLC

An OhlcHeap is a min-heap of ints.

func (OhlcHeap) Len

func (h OhlcHeap) Len() int

func (OhlcHeap) Less

func (h OhlcHeap) Less(i, j int) bool

func (*OhlcHeap) Pop

func (h *OhlcHeap) Pop() interface{}

Pop is used to remove items from the heap

func (*OhlcHeap) Push

func (h *OhlcHeap) Push(x interface{})

Push is used to to put items on the heap

func (OhlcHeap) Swap

func (h OhlcHeap) Swap(i, j int)

type Order

type Order struct {
	Ordertype OrderType
	Asset     *Asset
	Time      time.Time
	Amount    float64
	Qty       int
}

Order describes an order

func (Order) String

func (o Order) String() string

type OrderType

type OrderType int

OrderType is used to describe an order

const (
	//Buy order
	Buy OrderType = iota
	//Sell order
	Sell
	//SellShort order
	SellShort
	//Cover short order
	Cover
)

type PartialFill

type PartialFill struct {
}

func (PartialFill) String

func (pf PartialFill) String() string

type Portfolio

type Portfolio struct {
	Holdings []Holding
	// contains filtered or unexported fields
}

func (*Portfolio) AddHolding

func (p *Portfolio) AddHolding(position Holding)

func (*Portfolio) Fill

func (p *Portfolio) Fill(fill Fill)

func (Portfolio) GetCash

func (p Portfolio) GetCash() float64

func (Portfolio) IsOwning

func (p Portfolio) IsOwning(assetName string) bool

IsOwning is used to find out if a position is already owned in this asset

func (*Portfolio) SellHolding

func (p *Portfolio) SellHolding(position Holding)

func (*Portfolio) SetCash

func (p *Portfolio) SetCash(amount float64)

type Quit

type Quit struct{}

Quit event type

func (Quit) String

func (q Quit) String() string

type Rejected

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

func (Rejected) String

func (r Rejected) String() string

type Signal

type Signal struct{}

Signal event type

func (Signal) String

func (s Signal) String() string

type Strategy

type Strategy interface {
	Setup(ctx *Context) error
	Tick(ctx *Context)
	OrderEvent(orderEvent Event)
}

Strategy needs to implement Orders in order to generate them

type Submitted

type Submitted struct {
}

func (Submitted) String

func (s Submitted) String() string

type Tick

type Tick struct{}

Tick event type

func (Tick) String

func (t Tick) String() string

type Type

type Type struct {
	Open string
	High string
	Low  string
	// contains filtered or unexported fields
}

Type is the type for ohlc

type Updateable

type Updateable interface {
	Update([]float64)
}

Updateable takes new data into account

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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