Documentation
¶
Index ¶
- Constants
- Variables
- func MapRecordsInvesting(records ...string) (time.Time, []float64)
- func ParseFloat(value string) float64
- func Run(ctx *Context, dm *DataManager)
- func RunStrategyOnAssets(ctx *Context, dm *DataManager)
- type Accepted
- type Asset
- type Broker
- type Callback
- type Comission
- type Context
- func (ctx *Context) AddAsset(asset *Asset)
- func (ctx *Context) AddIndicator(indicator indicators.Indicator)
- func (ctx *Context) AddIndicatorOnAsset(asset *Asset, indicator indicators.Indicator)
- func (ctx *Context) AddIndicatorWithParams(indicator indicators.Indicator, param indicators.Param)
- func (ctx *Context) GetAssetByName(name string) *Asset
- func (ctx *Context) GetAssetIndicatorByName(name string) []indicators.Indicator
- func (ctx *Context) GetAssets() []Asset
- func (ctx *Context) GetDataPath() string
- func (ctx *Context) GetEndDate() time.Time
- func (ctx *Context) GetInitPeriod() int
- func (ctx *Context) GetStartDate() time.Time
- func (ctx *Context) GetStrategy() Strategy
- func (ctx *Context) SetDataPath(path string)
- func (ctx *Context) SetEndDate(endTime time.Time)
- func (ctx *Context) SetInitPeriod(period int)
- func (ctx *Context) SetStartDate(startTime time.Time)
- func (ctx *Context) SetStrategy(strategy Strategy)
- func (ctx *Context) Time() time.Time
- type DataEvent
- type DataHandler
- type DataManager
- type DataUpdate
- type Direction
- type Directon
- type Event
- type Fill
- type FixedComission
- type GenerationK
- func (k *GenerationK) AddAsset(asset *Asset)
- func (k *GenerationK) AddStrategy(strat Strategy)
- func (k *GenerationK) Assets() []string
- func (k *GenerationK) GetAsset() Asset
- func (k *GenerationK) GetAssetByName(name string) *Asset
- func (k *GenerationK) GetAssets() []Asset
- func (k *GenerationK) IsOwning(assetName string) (bool, error)
- func (k *GenerationK) Owning() bool
- func (k *GenerationK) Run() error
- func (k *GenerationK) SendOrder(direction Direction, orderType OrderType, qty int) error
- func (k *GenerationK) SendOrderFor(assetName string, direction Direction, orderType OrderType, qty int) error
- func (k *GenerationK) SetBalance(balance float64)
- func (k *GenerationK) SetComission(comission Comission)
- func (k *GenerationK) SetDataManager()
- func (k *GenerationK) SetEndDate(endDate time.Time)
- func (k *GenerationK) SetPortfolio(portfolio *Portfolio)
- func (k *GenerationK) SetStartDate(startDate time.Time)
- type Holding
- type IndicatorError
- type IndicatorNotReadyError
- type Job
- type JobStruct
- type Maprecords
- type MultiStrategy
- type OHLC
- type OhlcConst
- type Order
- type OrderStatus
- type OrderType
- type Params
- type PartialFill
- type PercentageComission
- type Portfolio
- type Quit
- type RebalanceStrategy
- type Rejected
- type Strategy
- type Submitted
- type Tick
Constants ¶
const ( //Open value will be used for the data Open = iota //High value will be used for the data High //Low value will be used for the data Low //Close value will be used for the data Close //Volume value will be used for the data Volume //Default is used when the data is something else than ohlcv Default )
Variables ¶
var AssetDoesNotExist = errors.New("Asset does not exist")
var EndOfBacktest = errors.New("End of backtest")
var EndOfData = errors.New("End of data")
var FFToStartDate = errors.New("Fast forwarding to start date")
var Initialization = errors.New("Initialization in Once() failed")
var UnknownDirection = errors.New("Unknown type of direction for order - should be Buy, Sell, Short or Cover")
var UnstablePeriod = errors.New("The stable period is not yet reached")
Functions ¶
func ParseFloat ¶
pasetFloat is used to parse the floats from the CSV files and is a better way to to handle errors
func Run ¶
func Run(ctx *Context, dm *DataManager)
func RunStrategyOnAssets ¶
func RunStrategyOnAssets(ctx *Context, dm *DataManager)
Types ¶
type Accepted ¶
type Accepted struct { }
Accepted is a status of the order to indicate that an order has been accepted by the broker.
type Asset ¶
type Asset struct {
// contains filtered or unexported fields
}
Asset data type
type Broker ¶
type Broker struct {
// contains filtered or unexported fields
}
Broker is used to send orders
func (*Broker) SendOrder ¶
func (b *Broker) SendOrder(order Order, orderstatus OrderStatus) error
SendOrder is used to place an order with the broker
func (*Broker) SetComission ¶
SetComission is used to set a comission scheme
type Callback ¶
type Callback interface { Owning() bool IsOwning(assetName string) (bool, error) SendOrder(direction Direction, orderType OrderType, qty int) error SendOrderFor(assetName string, direction Direction, orderType OrderType, qty int) error Assets() []string }
Callback is used in the strategy to give actions back to the backtest in progress
type Context ¶
type Context struct { K int // contains filtered or unexported fields }
Context holds holds the strategy, assets, indicators per asset, start date, end date, the portfolio the current date and the unstable period
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) AddIndicatorWithParams ¶
func (ctx *Context) AddIndicatorWithParams(indicator indicators.Indicator, param indicators.Param)
AddIndicator will add it to all assets
func (*Context) GetAssetByName ¶
GetAssetByName return a specific strategy
func (*Context) GetAssetIndicatorByName ¶
func (ctx *Context) GetAssetIndicatorByName(name string) []indicators.Indicator
GetAssetIndicatorByName is used to get the indicators associated with the asset
func (*Context) GetDataPath ¶
func (*Context) GetEndDate ¶
func (*Context) GetInitPeriod ¶
GetInitPeriod returns the period
func (*Context) GetStartDate ¶
func (*Context) GetStrategy ¶
SetStrategy is used to set the strategy that will be run
func (*Context) SetDataPath ¶
func (*Context) SetEndDate ¶
AddEndDate is used to set the strategy that will be run
func (*Context) SetInitPeriod ¶
SetInitPeriod is used to set the unstable period, the longest period shoul be used
func (*Context) SetStartDate ¶
AddStartDate is used to set the start date
func (*Context) SetStrategy ¶
AddStrategy is used to set the strategy that will be run
type DataHandler ¶
type DataHandler interface {
AddAsset(asset *Asset)
}
DataHandler is the interface used to recieve data from any data producing function. It can be used to feed data to generationK
type DataManager ¶
type DataManager struct { Folder string Headers bool MappingFunc Maprecords }
func NewCSVDataManager ¶
func NewCSVDataManager(folder string, headers bool, mapping Maprecords) *DataManager
func (*DataManager) ReadCSVFile ¶
func (d *DataManager) ReadCSVFile(file string) *Asset
type DataUpdate ¶
type DataUpdate interface {
Update(ohlc OHLC)
}
DataUpdate is used to update the data in the assets
type Fill ¶
Fill is used to indicate to the implementer of OrderStatus that an order has been filled
type FixedComission ¶
type FixedComission struct {
// contains filtered or unexported fields
}
FixedComission is an example of a fixded comission with 2 limits
func (FixedComission) GetComisson ¶
func (f FixedComission) GetComisson(price float64, qty int) float64
GetComission return the amount for more than or less than 500 pieces of asset
type GenerationK ¶
type GenerationK struct {
// contains filtered or unexported fields
}
func NewGenerationK ¶
func NewGenerationK() *GenerationK
NewGenerationK is used to create a new backtest
func (*GenerationK) AddAsset ¶
func (k *GenerationK) AddAsset(asset *Asset)
AddAsset is used to add a pointer to an asset
func (*GenerationK) AddStrategy ¶
func (k *GenerationK) AddStrategy(strat Strategy)
AddStrategy is used to add a strategy to the backtest
func (*GenerationK) Assets ¶
func (k *GenerationK) Assets() []string
Assets returns an array of assets
func (*GenerationK) GetAsset ¶
func (k *GenerationK) GetAsset() Asset
Returns an array of all assets
func (*GenerationK) GetAssetByName ¶
func (k *GenerationK) GetAssetByName(name string) *Asset
GetAssetByName returns a pointer to the asset by that name
func (*GenerationK) GetAssets ¶
func (k *GenerationK) GetAssets() []Asset
Returns an array of all assets
func (*GenerationK) IsOwning ¶
func (k *GenerationK) IsOwning(assetName string) (bool, error)
OwnPosition is used to find out if we have a holding in an asset and the assumption is that the strategy is using multiple assets
func (*GenerationK) Owning ¶
func (k *GenerationK) Owning() bool
Owning is used to find out if we have a holding and we are only processing 1 asset
func (*GenerationK) Run ¶
func (k *GenerationK) Run() error
func (*GenerationK) SendOrder ¶
func (k *GenerationK) SendOrder(direction Direction, orderType OrderType, qty int) error
OrderSend is used to send an order to the broker, return an error if the asset does not exist
func (*GenerationK) SendOrderFor ¶
func (k *GenerationK) SendOrderFor(assetName string, direction Direction, orderType OrderType, qty int) error
OrderSend is used to send an order to the broker, return an error if the asset does not exist
func (*GenerationK) SetBalance ¶
func (k *GenerationK) SetBalance(balance float64)
SetBalance is used to set the balance when the backtest is started
func (*GenerationK) SetComission ¶
func (k *GenerationK) SetComission(comission Comission)
SetComission is used to set the comission scheme is there is one
func (*GenerationK) SetDataManager ¶
func (k *GenerationK) SetDataManager()
AddDataManager is currently not used
func (*GenerationK) SetEndDate ¶
func (k *GenerationK) SetEndDate(endDate time.Time)
AddEndDate is used to set the end date for the backtest
func (*GenerationK) SetPortfolio ¶
func (k *GenerationK) SetPortfolio(portfolio *Portfolio)
AddPortfolio is used to add a pointer to a portfolio to the backtest
func (*GenerationK) SetStartDate ¶
func (k *GenerationK) SetStartDate(startDate time.Time)
AddStartDate is used to set the end date for the backtest
type Holding ¶
type Holding struct {
// contains filtered or unexported fields
}
This is what we are owning, a holding
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 JobStruct ¶
func (*JobStruct) GetFileName ¶
type MultiStrategy ¶
type Order ¶
type Order struct { Asset string Time time.Time Price float64 Qty int // contains filtered or unexported fields }
Order describes an order that is used to buy / sell an asset
type OrderStatus ¶
type OrderStatus interface {
OrderEvent(orderEvent Event)
}
OrderStatus is a callback interface used to recieve information about orders, it is used by the broker
type OrderType ¶
type OrderType int
const ( // A market order is an order to buy or sell a stock at the market’s // current best available price. A market order typically ensures // an execution but it does not guarantee a specified price. MarketOrder OrderType = iota //A limit order is an order to buy or sell a stock with a restriction on // the maximum price to be paid or the minimum price to be received // (the “limit price”). If the order is filled, it will only be at the // specified limit price or better. However, there is no assurance of // execution. A limit order may be appropriate when you think you can // buy at a price lower than—or sell at a price higher than—the // current quote. // Maximum price for buys, or minimum price for sells, at which the // order should be filled. LimitOrder // A stop order is an order to buy or sell a stock at the market price once // the stock has traded at or through a specified price (the “stop price”). // If the stock reaches the stop price, the order becomes a market order and // is filled at the next available market price. If the stock fails to reach // the stop price, the order is not executed. // For sells, the order will be placed if market price falls below this value. // For buys, the order will be placed if market price rises above this value. StopOrder // A stop order is an order to buy or sell a stock at the market price once // the stock has traded at or through a specified price (the “stop price”). // If the stock reaches the stop price, a limit order is placed StopLimitOrder )
type PartialFill ¶
type PartialFill struct { }
PartialFill is used to giv enotice to the strategy that a partial fill took place
func (PartialFill) String ¶
func (pf PartialFill) String() string
type PercentageComission ¶
type PercentageComission struct {
// contains filtered or unexported fields
}
PercentageComission is an example of a comission scheme with a fixed percentage
func (PercentageComission) GetComisson ¶
func (pc PercentageComission) GetComisson(price float64, qty int) float64
Returns the comission based on a percentage of the amount, qty is not used in this scheme
type Portfolio ¶
The portfolio holds assets: holdings, the portfolio holds a mutext to be able to use the same portfolio when testing many assets in parallell but updating the account on a single portfolio
func (*Portfolio) AddHolding ¶
AddHolding, its been bought
func (*Portfolio) GetBalance ¶
GetBalance returns in the balance of the account
func (*Portfolio) IsOwning ¶
IsOwning is used to find out if a position is already owned in this asset
func (*Portfolio) RemoveHolding ¶
Remove a holding, its sold
func (*Portfolio) SetBalance ¶
SetBalance is used to set the starting balance of the account
type RebalanceStrategy ¶
type Rejected ¶
type Rejected struct {
// contains filtered or unexported fields
}
Rejected type is for order that can not be executed
type Strategy ¶
type Strategy interface { GetParams() []*Params Once(ctx *Context, ohlc *OHLC) error Update(k *int) error PerBar(k int, callback Callback) error }
Strategy is the class where the logic is placed to buy and sell assets the two methods that needs to be implemented are Setup and Tick. The Setup method is used to define if any indicators will be used and what period they need to be stable. The Tick method is called for every new data which arrives and is a possibility to make checks and send orders.