strategy

package
v2.0.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: AGPL-3.0 Imports: 12 Imported by: 0

README

strategy

import "github.com/cinar/indicator/v2/strategy"

Package strategy contains the strategy functions.

This package belongs to the Indicator project. Indicator is a Golang module that supplies a variety of technical indicators, strategies, and a backtesting framework for analysis.

License
Copyright (c) 2021-2024 Onur Cinar.
The source code is provided under GNU AGPLv3 License.
https://github.com/cinar/indicator
Disclaimer

The information provided on this project is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.

Index

Constants

const (
    // DefaultBacktestWorkers is the default number of backtest workers.
    DefaultBacktestWorkers = 1

    // DefaultLastDays is the default number of days backtest should go back.
    DefaultLastDays = 30
)

func ActionSources

func ActionSources(strategies []Strategy, snapshots <-chan *asset.Snapshot) []<-chan Action

ActionSources creates a slice of action channels, one for each strategy, where each channel emits actions computed by its corresponding strategy based on snapshots from the provided snapshot channel.

func ActionsToAnnotations

func ActionsToAnnotations(ac <-chan Action) <-chan string

ActionsToAnnotations takes a channel of action recommendations and returns a new channel containing corresponding annotations for those actions.

func ComputeWithOutcome

func ComputeWithOutcome(s Strategy, c <-chan *asset.Snapshot) (<-chan Action, <-chan float64)

ComputeWithOutcome uses the given strategy to processes the provided asset snapshots and generates a stream of actionable recommendations and outcomes.

func CountActions

func CountActions(acs []<-chan Action) (int, int, int, bool)

CountActions taken a slice of Action channels, and counts them by their type.

func CountTransactions

func CountTransactions(ac <-chan Action) <-chan int

CountTransactions counts the number of recommended Buy and Sell actions.

func DenormalizeActions

func DenormalizeActions(ac <-chan Action) <-chan Action

DenormalizeActions simplifies the representation of the action sequence and facilitates subsequent processing by transforming the given channel of actions. It retains Hold actions until the first Buy or Sell action appears. Subsequently, it replaces all remaining Hold actions with the preceding Buy or Sell action, effectively merging consecutive actions.

func NormalizeActions

func NormalizeActions(ac <-chan Action) <-chan Action

NormalizeActions transforms the given channel of actions to ensure a consistent and predictable sequence. It eliminates consecutive occurrences of the same action (Buy/Sell), ensuring the order follows a pattern of Hold, Buy, Hold, Sell.

func Outcome

func Outcome[T helper.Number](values <-chan T, actions <-chan Action) <-chan float64

Outcome simulates the potential result of executing the given actions based on the provided values.

type Action

Action represents the different action categories that a strategy can recommend.

type Action int

const (
    // Hold suggests maintaining the current position and not
    // taking any actions on the asset.
    Hold Action = 0

    // Sell suggests disposing of the asset and exiting the current position.
    // This recommendation typically indicates that the strategy believes the
    // asset's price has reached its peak or is likely to decline.
    Sell Action = -1

    // Buy suggests acquiring the asset and entering a new position. This
    // recommendation usually implies that the strategy believes the
    // asset's price is undervalued.
    Buy Action = 1
)

func (Action) Annotation
func (a Action) Annotation() string

Annotation returns a single character string representing the recommended action. It returns "S" for Sell, "B" for Buy, and an empty string for Hold.

type AllStrategy

AllStrategy combines multiple strategies and emits actionable recommendations when **all** strategies in the group **reach the same actionable conclusion**. This can be a conservative approach, potentially delaying recommendations until full consensus is reached.

type AllStrategy struct {
    Strategy

    // Strategies are the group of strategies that will be consulted to make an actionable recommendation.
    Strategies []Strategy
    // contains filtered or unexported fields
}

func NewAllStrategy
func NewAllStrategy(name string) *AllStrategy

NewAllStrategy function initializes an empty all strategies group with the given name.

func (*AllStrategy) Compute
func (a *AllStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan Action

Compute processes the provided asset snapshots and generates a stream of actionable recommendations.

func (*AllStrategy) Name
func (a *AllStrategy) Name() string

Name returns the name of the strategy.

func (*AllStrategy) Report
func (a *AllStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

Report processes the provided asset snapshots and generates a report annotated with the recommended actions.

type Backtest

Backtest function rigorously evaluates the potential performance of the specified strategies applied to a defined set of assets. It generates comprehensive visual representations for each strategy-asset pairing.

type Backtest struct {

    // Names is the names of the assets to backtest.
    Names []string

    // Strategies is the list of strategies to apply.
    Strategies []Strategy

    // Workers is the number of concurrent workers.
    Workers int

    // LastDays is the number of days backtest should go back.
    LastDays int
    // contains filtered or unexported fields
}

func NewBacktest
func NewBacktest(repository asset.Repository, outputDir string) *Backtest

NewBacktest function initializes a new backtest instance.

func (*Backtest) Run
func (b *Backtest) Run() error

Run executes a comprehensive performance evaluation of the designated strategies, applied to a specified collection of assets. In the absence of explicitly defined assets, encompasses all assets within the repository. Likewise, in the absence of explicitly defined strategies, encompasses all the registered strategies.

type BuyAndHoldStrategy

BuyAndHoldStrategy defines an investment approach of acquiring and indefinitely retaining an asset. This strategy primarily serves as a benchmark for evaluating the performance of alternative strategies against a baseline of passive asset ownership.

type BuyAndHoldStrategy struct {
    Strategy
}

func NewBuyAndHoldStrategy
func NewBuyAndHoldStrategy() *BuyAndHoldStrategy

NewBuyAndHoldStrategy function initializes a new buy and hold strategy instance.

func (*BuyAndHoldStrategy) Compute
func (*BuyAndHoldStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan Action

Compute processes the provided asset snapshots and generates a stream of actionable recommendations.

func (*BuyAndHoldStrategy) Name
func (*BuyAndHoldStrategy) Name() string

Name returns the name of the strategy.

func (*BuyAndHoldStrategy) Report
func (b *BuyAndHoldStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

Report processes the provided asset snapshots and generates a report annotated with the recommended actions.

type MajorityStrategy

MajorityStrategy emits actionable recommendations aligned with what the strategies in the group recommends.

type MajorityStrategy struct {
    Strategy

    // Strategies are the group of strategies that will be consulted to make an actionable recommendation.
    Strategies []Strategy
    // contains filtered or unexported fields
}

func NewMajorityStrategy
func NewMajorityStrategy(name string) *MajorityStrategy

NewMajorityStrategy function initializes an empty majority strategies group with the given name.

func NewMajorityStrategyWith
func NewMajorityStrategyWith(name string, strategies []Strategy) *MajorityStrategy

NewMajorityStrategyWith function initializes a majority strategies group with the given name and strategies.

func (*MajorityStrategy) Compute
func (a *MajorityStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan Action

Compute processes the provided asset snapshots and generates a stream of actionable recommendations.

func (*MajorityStrategy) Name
func (a *MajorityStrategy) Name() string

Name returns the name of the strategy.

func (*MajorityStrategy) Report
func (a *MajorityStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

Report processes the provided asset snapshots and generates a report annotated with the recommended actions.

type OrStrategy

OrStrategy emits actionable recommendations when **at least one** strategy in the group recommends an action **without any conflicting recommendations** from other strategies.

type OrStrategy struct {
    Strategy

    // Strategies are the group of strategies that will be consulted to make an actionable recommendation.
    Strategies []Strategy
    // contains filtered or unexported fields
}

func NewOrStrategy
func NewOrStrategy(name string) *OrStrategy

NewOrStrategy function initializes an empty or strategies group with the given name.

func (*OrStrategy) Compute
func (a *OrStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan Action

Compute processes the provided asset snapshots and generates a stream of actionable recommendations.

func (*OrStrategy) Name
func (a *OrStrategy) Name() string

Name returns the name of the strategy.

func (*OrStrategy) Report
func (a *OrStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

Report processes the provided asset snapshots and generates a report annotated with the recommended actions.

type Result

Result is only used inside the test cases to facilitate the comparison between the actual and expected strategy results.

type Result struct {
    Action Action
}

type SplitStrategy

SplitStrategy leverages two separate strategies. It utilizes the first strategy to identify potential Buy opportunities, and the second strategy to identify potential Sell opportunities. When there is a conflicting recommendation, returns Hold.

type SplitStrategy struct {
    // BuyStrategy is used to identify potential Buy opportunities.
    BuyStrategy Strategy

    // SellStrategy is used to identify potential Sell opportunities.
    SellStrategy Strategy
}

func NewSplitStrategy
func NewSplitStrategy(buyStrategy, sellStrategy Strategy) *SplitStrategy

NewSplitStrategy function initializes a new split strategy with the given parameters.

func (*SplitStrategy) Compute
func (s *SplitStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan Action

Compute processes the provided asset snapshots and generates a stream of actionable recommendations.

func (*SplitStrategy) Name
func (s *SplitStrategy) Name() string

Name returns the name of the strategy.

func (*SplitStrategy) Report
func (s *SplitStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

Report processes the provided asset snapshots and generates a report annotated with the recommended actions.

type Strategy

Strategy defines a shared interface for trading strategies.

type Strategy interface {
    // Name returns the name of the strategy.
    Name() string

    // Compute processes the provided asset snapshots and generates a
    // stream of actionable recommendations.
    Compute(snapshots <-chan *asset.Snapshot) <-chan Action

    // Report processes the provided asset snapshots and generates a
    // report annotated with the recommended actions.
    Report(snapshots <-chan *asset.Snapshot) *helper.Report
}

func AllSplitStrategies
func AllSplitStrategies(strategies []Strategy) []Strategy

AllSplitStrategies performs a cartesian product operation on the given strategies, resulting in a collection containing all split strategies formed by combining individual buy and sell strategies.

func AllStrategies
func AllStrategies() []Strategy

AllStrategies returns a slice containing references to all available base strategies.

Generated by gomarkdoc

Documentation

Overview

Package strategy contains the strategy functions.

This package belongs to the Indicator project. Indicator is a Golang module that supplies a variety of technical indicators, strategies, and a backtesting framework for analysis.

License

Copyright (c) 2021-2024 Onur Cinar.
The source code is provided under GNU AGPLv3 License.
https://github.com/cinar/indicator

Disclaimer

The information provided on this project is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.

Index

Constants

View Source
const (
	// DefaultBacktestWorkers is the default number of backtest workers.
	DefaultBacktestWorkers = 1

	// DefaultLastDays is the default number of days backtest should go back.
	DefaultLastDays = 30
)

Variables

This section is empty.

Functions

func ActionSources

func ActionSources(strategies []Strategy, snapshots <-chan *asset.Snapshot) []<-chan Action

ActionSources creates a slice of action channels, one for each strategy, where each channel emits actions computed by its corresponding strategy based on snapshots from the provided snapshot channel.

func ActionsToAnnotations

func ActionsToAnnotations(ac <-chan Action) <-chan string

ActionsToAnnotations takes a channel of action recommendations and returns a new channel containing corresponding annotations for those actions.

func ComputeWithOutcome

func ComputeWithOutcome(s Strategy, c <-chan *asset.Snapshot) (<-chan Action, <-chan float64)

ComputeWithOutcome uses the given strategy to processes the provided asset snapshots and generates a stream of actionable recommendations and outcomes.

func CountActions

func CountActions(acs []<-chan Action) (int, int, int, bool)

CountActions taken a slice of Action channels, and counts them by their type.

func CountTransactions

func CountTransactions(ac <-chan Action) <-chan int

CountTransactions counts the number of recommended Buy and Sell actions.

func DenormalizeActions

func DenormalizeActions(ac <-chan Action) <-chan Action

DenormalizeActions simplifies the representation of the action sequence and facilitates subsequent processing by transforming the given channel of actions. It retains Hold actions until the first Buy or Sell action appears. Subsequently, it replaces all remaining Hold actions with the preceding Buy or Sell action, effectively merging consecutive actions.

func NormalizeActions

func NormalizeActions(ac <-chan Action) <-chan Action

NormalizeActions transforms the given channel of actions to ensure a consistent and predictable sequence. It eliminates consecutive occurrences of the same action (Buy/Sell), ensuring the order follows a pattern of Hold, Buy, Hold, Sell.

func Outcome

func Outcome[T helper.Number](values <-chan T, actions <-chan Action) <-chan float64

Outcome simulates the potential result of executing the given actions based on the provided values.

Types

type Action

type Action int

Action represents the different action categories that a strategy can recommend.

const (
	// Hold suggests maintaining the current position and not
	// taking any actions on the asset.
	Hold Action = 0

	// Sell suggests disposing of the asset and exiting the current position.
	// This recommendation typically indicates that the strategy believes the
	// asset's price has reached its peak or is likely to decline.
	Sell Action = -1

	// Buy suggests acquiring the asset and entering a new position. This
	// recommendation usually implies that the strategy believes the
	// asset's price is undervalued.
	Buy Action = 1
)

func (Action) Annotation

func (a Action) Annotation() string

Annotation returns a single character string representing the recommended action. It returns "S" for Sell, "B" for Buy, and an empty string for Hold.

type AllStrategy

type AllStrategy struct {
	Strategy

	// Strategies are the group of strategies that will be consulted to make an actionable recommendation.
	Strategies []Strategy
	// contains filtered or unexported fields
}

AllStrategy combines multiple strategies and emits actionable recommendations when **all** strategies in the group **reach the same actionable conclusion**. This can be a conservative approach, potentially delaying recommendations until full consensus is reached.

func NewAllStrategy

func NewAllStrategy(name string) *AllStrategy

NewAllStrategy function initializes an empty all strategies group with the given name.

func (*AllStrategy) Compute

func (a *AllStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan Action

Compute processes the provided asset snapshots and generates a stream of actionable recommendations.

func (*AllStrategy) Name

func (a *AllStrategy) Name() string

Name returns the name of the strategy.

func (*AllStrategy) Report

func (a *AllStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

Report processes the provided asset snapshots and generates a report annotated with the recommended actions.

type Backtest

type Backtest struct {

	// Names is the names of the assets to backtest.
	Names []string

	// Strategies is the list of strategies to apply.
	Strategies []Strategy

	// Workers is the number of concurrent workers.
	Workers int

	// LastDays is the number of days backtest should go back.
	LastDays int
	// contains filtered or unexported fields
}

Backtest function rigorously evaluates the potential performance of the specified strategies applied to a defined set of assets. It generates comprehensive visual representations for each strategy-asset pairing.

func NewBacktest

func NewBacktest(repository asset.Repository, outputDir string) *Backtest

NewBacktest function initializes a new backtest instance.

func (*Backtest) Run

func (b *Backtest) Run() error

Run executes a comprehensive performance evaluation of the designated strategies, applied to a specified collection of assets. In the absence of explicitly defined assets, encompasses all assets within the repository. Likewise, in the absence of explicitly defined strategies, encompasses all the registered strategies.

type BuyAndHoldStrategy

type BuyAndHoldStrategy struct {
	Strategy
}

BuyAndHoldStrategy defines an investment approach of acquiring and indefinitely retaining an asset. This strategy primarily serves as a benchmark for evaluating the performance of alternative strategies against a baseline of passive asset ownership.

func NewBuyAndHoldStrategy

func NewBuyAndHoldStrategy() *BuyAndHoldStrategy

NewBuyAndHoldStrategy function initializes a new buy and hold strategy instance.

func (*BuyAndHoldStrategy) Compute

func (*BuyAndHoldStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan Action

Compute processes the provided asset snapshots and generates a stream of actionable recommendations.

func (*BuyAndHoldStrategy) Name

func (*BuyAndHoldStrategy) Name() string

Name returns the name of the strategy.

func (*BuyAndHoldStrategy) Report

func (b *BuyAndHoldStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

Report processes the provided asset snapshots and generates a report annotated with the recommended actions.

type MajorityStrategy

type MajorityStrategy struct {
	Strategy

	// Strategies are the group of strategies that will be consulted to make an actionable recommendation.
	Strategies []Strategy
	// contains filtered or unexported fields
}

MajorityStrategy emits actionable recommendations aligned with what the strategies in the group recommends.

func NewMajorityStrategy

func NewMajorityStrategy(name string) *MajorityStrategy

NewMajorityStrategy function initializes an empty majority strategies group with the given name.

func NewMajorityStrategyWith

func NewMajorityStrategyWith(name string, strategies []Strategy) *MajorityStrategy

NewMajorityStrategyWith function initializes a majority strategies group with the given name and strategies.

func (*MajorityStrategy) Compute

func (a *MajorityStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan Action

Compute processes the provided asset snapshots and generates a stream of actionable recommendations.

func (*MajorityStrategy) Name

func (a *MajorityStrategy) Name() string

Name returns the name of the strategy.

func (*MajorityStrategy) Report

func (a *MajorityStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

Report processes the provided asset snapshots and generates a report annotated with the recommended actions.

type OrStrategy

type OrStrategy struct {
	Strategy

	// Strategies are the group of strategies that will be consulted to make an actionable recommendation.
	Strategies []Strategy
	// contains filtered or unexported fields
}

OrStrategy emits actionable recommendations when **at least one** strategy in the group recommends an action **without any conflicting recommendations** from other strategies.

func NewOrStrategy

func NewOrStrategy(name string) *OrStrategy

NewOrStrategy function initializes an empty or strategies group with the given name.

func (*OrStrategy) Compute

func (a *OrStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan Action

Compute processes the provided asset snapshots and generates a stream of actionable recommendations.

func (*OrStrategy) Name

func (a *OrStrategy) Name() string

Name returns the name of the strategy.

func (*OrStrategy) Report

func (a *OrStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

Report processes the provided asset snapshots and generates a report annotated with the recommended actions.

type Result

type Result struct {
	Action Action
}

Result is only used inside the test cases to facilitate the comparison between the actual and expected strategy results.

type SplitStrategy

type SplitStrategy struct {
	// BuyStrategy is used to identify potential Buy opportunities.
	BuyStrategy Strategy

	// SellStrategy is used to identify potential Sell opportunities.
	SellStrategy Strategy
}

SplitStrategy leverages two separate strategies. It utilizes the first strategy to identify potential Buy opportunities, and the second strategy to identify potential Sell opportunities. When there is a conflicting recommendation, returns Hold.

func NewSplitStrategy

func NewSplitStrategy(buyStrategy, sellStrategy Strategy) *SplitStrategy

NewSplitStrategy function initializes a new split strategy with the given parameters.

func (*SplitStrategy) Compute

func (s *SplitStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan Action

Compute processes the provided asset snapshots and generates a stream of actionable recommendations.

func (*SplitStrategy) Name

func (s *SplitStrategy) Name() string

Name returns the name of the strategy.

func (*SplitStrategy) Report

func (s *SplitStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

Report processes the provided asset snapshots and generates a report annotated with the recommended actions.

type Strategy

type Strategy interface {
	// Name returns the name of the strategy.
	Name() string

	// Compute processes the provided asset snapshots and generates a
	// stream of actionable recommendations.
	Compute(snapshots <-chan *asset.Snapshot) <-chan Action

	// Report processes the provided asset snapshots and generates a
	// report annotated with the recommended actions.
	Report(snapshots <-chan *asset.Snapshot) *helper.Report
}

Strategy defines a shared interface for trading strategies.

func AllSplitStrategies

func AllSplitStrategies(strategies []Strategy) []Strategy

AllSplitStrategies performs a cartesian product operation on the given strategies, resulting in a collection containing all split strategies formed by combining individual buy and sell strategies.

func AllStrategies

func AllStrategies() []Strategy

AllStrategies returns a slice containing references to all available base strategies.

Directories

Path Synopsis
Package compound contains the compound strategy functions.
Package compound contains the compound strategy functions.
Package momentum contains the momentum strategy functions.
Package momentum contains the momentum strategy functions.
Package trend contains the trend strategy functions.
Package trend contains the trend strategy functions.
Package volatility contains the volatility strategy functions.
Package volatility contains the volatility strategy functions.

Jump to

Keyboard shortcuts

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