Skip to content

Latest commit

 

History

History
1063 lines (760 loc) · 42.9 KB

README.md

File metadata and controls

1063 lines (760 loc) · 42.9 KB

trend

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

Package trend contains the trend 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 (
    // DefaultDemaStrategyPeriod1 is the first DEMA period.
    DefaultDemaStrategyPeriod1 = 5

    // DefaultDemaStrategyPeriod2 is the second DEMA period.
    DefaultDemaStrategyPeriod2 = 35
)

const (
    // DefaultGoldenCrossStrategyFastPeriod is the default golden cross strategy fast period.
    DefaultGoldenCrossStrategyFastPeriod = 50

    // DefaultGoldenCrossStrategySlowPeriod is the default golden cross strategy slow period.
    DefaultGoldenCrossStrategySlowPeriod = 200
)

const (
    // DefaultTrimaStrategyShortPeriod is the first TRIMA period.
    DefaultTrimaStrategyShortPeriod = 20

    // DefaultTrimaStrategyLongPeriod is the second TRIMA period.
    DefaultTrimaStrategyLongPeriod = 50
)

const (
    // DefaultTripleMovingAverageCrossoverStrategyFastPeriod is the default triple moving average crossover strategy fast period.
    DefaultTripleMovingAverageCrossoverStrategyFastPeriod = 21

    // DefaultTripleMovingAverageCrossoverStrategyMediumPeriod is the default triple moving average crossover strategy medium period.
    DefaultTripleMovingAverageCrossoverStrategyMediumPeriod = 50

    // DefaultTripleMovingAverageCrossoverStrategySlowPeriod is the default triple moving average crossover strategy slow period.
    DefaultTripleMovingAverageCrossoverStrategySlowPeriod = 200
)

const (
    // DefaultTsiStrategySignalPeriod is the default signal line period of 12.
    DefaultTsiStrategySignalPeriod = 12
)

const (
    // DefaultVwmaStrategyPeriod is the default VWMA period.
    DefaultVwmaStrategyPeriod = 20
)

func AllStrategies() []strategy.Strategy

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

ApoStrategy represents the configuration parameters for calculating the APO strategy. An APO value crossing above zero suggests a bullish trend, while crossing below zero indicates a bearish trend. Positive APO values signify an upward trend, while negative values signify a downward trend.

type ApoStrategy struct {
    // Apo represents the configuration parameters for calculating the
    // Absolute Price Oscillator (APO).
    Apo *trend.Apo[float64]
}

func NewApoStrategy() *ApoStrategy

NewApoStrategy function initializes a new APO strategy instance with the default parameters.

func (*ApoStrategy) Compute

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

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

func (*ApoStrategy) Name

func (*ApoStrategy) Name() string

Name returns the name of the strategy.

func (*ApoStrategy) Report

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

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

AroonStrategy represents the configuration parameters for calculating the Aroon strategy. Aroon is a technical analysis tool that gauges trend direction and strength in asset prices. It comprises two lines: Aroon Up and Aroon Down. Aroon Up measures uptrend strength, while Aroon Down measures downtrend strength. When Aroon Up exceeds Aroon Down, it suggests a bullish trend; when Aroon Down surpasses Aroon Up, it indicates a bearish trend.

type AroonStrategy struct {
    // Aroon represent the configuration for calculating the Aroon indicator.
    Aroon *trend.Aroon[float64]
}

func NewAroonStrategy() *AroonStrategy

NewAroonStrategy function initializes a new Aroon strategy instance with the default parameters.

func (*AroonStrategy) Compute

func (a *AroonStrategy) Compute(c <-chan *asset.Snapshot) <-chan strategy.Action

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

func (*AroonStrategy) Name

func (*AroonStrategy) Name() string

Name returns the name of the strategy.

func (*AroonStrategy) Report

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

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

BopStrategy gauges the strength of buying and selling forces using the Balance of Power (BoP) indicator. A positive BoP value suggests an upward trend, while a negative value indicates a downward trend. A BoP value of zero implies equilibrium between the two forces.

type BopStrategy struct {
    // Bop represents the configuration parameters for calculating the
    // Balance of Power (BoP).
    Bop *trend.Bop[float64]
}

func NewBopStrategy() *BopStrategy

NewBopStrategy function initializes a new BoP strategy instance with the default parameters.

func (*BopStrategy) Compute

func (b *BopStrategy) Compute(c <-chan *asset.Snapshot) <-chan strategy.Action

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

func (*BopStrategy) Name

func (*BopStrategy) Name() string

Name returns the name of the strategy.

func (*BopStrategy) Report

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

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

CciStrategy represents the configuration parameters for calculating the CCI strategy. A CCI value crossing above the 100+ suggests a bullish trend, while crossing below the 100- indicates a bearish trend.

type CciStrategy struct {
    // Cci represents the configuration parameters for calculating the CCI.
    Cci *trend.Cci[float64]
}

func NewCciStrategy() *CciStrategy

NewCciStrategy function initializes a new CCI strategy instance.

func (*CciStrategy) Compute

func (t *CciStrategy) Compute(c <-chan *asset.Snapshot) <-chan strategy.Action

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

func (*CciStrategy) Name

func (*CciStrategy) Name() string

Name returns the name of the strategy.

func (*CciStrategy) Report

func (t *CciStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

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

DemaStrategy represents the configuration parameters for calculating the DEMA strategy. A bullish cross occurs when DEMA with 5 days period moves above DEMA with 35 days period. A bearish cross occurs when DEMA with 35 days period moves above DEMA With 5 days period.

type DemaStrategy struct {
    // Dema1 represents the configuration parameters for
    // calculating the first DEMA.
    Dema1 *trend.Dema[float64]

    // Dema2 represents the configuration parameters for
    // calculating the second DEMA.
    Dema2 *trend.Dema[float64]
}

func NewDemaStrategy() *DemaStrategy

NewDemaStrategy function initializes a new DEMA strategy instance with the default parameters.

func (*DemaStrategy) Compute

func (d *DemaStrategy) Compute(c <-chan *asset.Snapshot) <-chan strategy.Action

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

func (*DemaStrategy) Name

func (*DemaStrategy) Name() string

Name returns the name of the strategy.

func (*DemaStrategy) Report

func (d *DemaStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

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

EnvelopeStrategy represents the configuration parameters for calculating the Envelope strategy. When the closing is above the upper band suggests a Sell recommendation, and when the closing is below the lower band suggests a buy recommendation.

type EnvelopeStrategy struct {
    // Envelope is the envelope indicator instance.
    Envelope *trend.Envelope[float64]
}

func NewEnvelopeStrategy() *EnvelopeStrategy

NewEnvelopeStrategy function initializes a new Envelope strategy with the default parameters.

func NewEnvelopeStrategyWith(envelope *trend.Envelope[float64]) *EnvelopeStrategy

NewEnvelopeStrategyWith function initializes a new Envelope strategy with the given Envelope instance.

func (*EnvelopeStrategy) Compute

func (e *EnvelopeStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan strategy.Action

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

func (*EnvelopeStrategy) Name

func (e *EnvelopeStrategy) Name() string

Name returns the name of the strategy.

func (*EnvelopeStrategy) Report

func (e *EnvelopeStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

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

GoldenCrossStrategy defines the parameters used to calculate the Golden Cross trading strategy. This strategy uses two Exponential Moving Averages (EMAs) with different lengths to identify potential buy and sell signals. - A buy signal is generated when the **fastest** EMA crosses above the **slowest** EMAs. - A sell signal is generated when the fastest EMA crosses below the slowest EMAs. - Otherwise, the strategy recommends holding the asset.

type GoldenCrossStrategy struct {
    // FastEma is the fastest EMA.
    FastEma *trend.Ema[float64]

    // SlowEma is the slowest EMA.
    SlowEma *trend.Ema[float64]
}

func NewGoldenCrossStrategy() *GoldenCrossStrategy

NewGoldenCrossStrategy function initializes a new Golden Cross strategy instance with the default parameters.

func NewGoldenCrossStrategyWith(fastPeriod, slowPeriod int) *GoldenCrossStrategy

NewGoldenCrossStrategyWith function initializes a new Golden Cross strategy instance with the given periods.

func (*GoldenCrossStrategy) Compute

func (t *GoldenCrossStrategy) Compute(c <-chan *asset.Snapshot) <-chan strategy.Action

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

func (*GoldenCrossStrategy) Name

func (*GoldenCrossStrategy) Name() string

Name returns the name of the strategy.

func (*GoldenCrossStrategy) Report

func (t *GoldenCrossStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

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

KamaStrategy represents the configuration parameters for calculating the KAMA strategy. A closing price crossing above the KAMA suggests a bullish trend, while crossing below the KAMA indicates a bearish trend.

type KamaStrategy struct {
    // Kama represents the configuration parameters for calculating the Kaufman's Adaptive Moving Average (KAMA).
    Kama *trend.Kama[float64]
}

func NewKamaStrategy() *KamaStrategy

NewKamaStrategy function initializes a new KAMA strategy instance.

func NewKamaStrategyWith(erPeriod, fastScPeriod, slowScPeriod int) *KamaStrategy

NewKamaStrategyWith function initializes a new KAMA strategy instance with the given parameters.

func (*KamaStrategy) Compute

func (k *KamaStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan strategy.Action

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

func (*KamaStrategy) Name

func (k *KamaStrategy) Name() string

Name returns the name of the strategy.

func (*KamaStrategy) Report

func (k *KamaStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

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

KdjStrategy represents the configuration parameters for calculating the KDJ strategy. Generates BUY action when j value crosses above both k and d values. Generates SELL action when j value crosses below both k and d values.

type KdjStrategy struct {
    // Kdj represents the configuration parameters for calculating the KDJ.
    Kdj *trend.Kdj[float64]
}

func NewKdjStrategy() *KdjStrategy

NewKdjStrategy function initializes a new KDJ strategy instance.

func (*KdjStrategy) Compute

func (kdj *KdjStrategy) Compute(c <-chan *asset.Snapshot) <-chan strategy.Action

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

func (*KdjStrategy) Name

func (*KdjStrategy) Name() string

Name returns the name of the strategy.

func (*KdjStrategy) Report

func (kdj *KdjStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

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

MacdStrategy represents the configuration parameters for calculating the MACD strategy. A MACD value crossing above the signal line suggests a bullish trend, while crossing below the signal line indicates a bearish trend.

type MacdStrategy struct {
    // Macd represents the configuration parameters for calculating the
    // Moving Average Convergence Divergence (MACD).
    Macd *trend.Macd[float64]
}

func NewMacdStrategy() *MacdStrategy

NewMacdStrategy function initializes a new MACD strategy instance.

func NewMacdStrategyWith(period1, period2, period3 int) *MacdStrategy

NewMacdStrategyWith function initializes a new MACD strategy instance with the given parameters.

func (*MacdStrategy) Compute

func (m *MacdStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan strategy.Action

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

func (*MacdStrategy) Name

func (m *MacdStrategy) Name() string

Name returns the name of the strategy.

func (*MacdStrategy) Report

func (m *MacdStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

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

QstickStrategy represents the configuration parameters for calculating the Qstick strategy. Qstick is a momentum indicator used to identify an asset's trend by looking at the SMA of the difference between its closing and opening.

A Qstick above zero indicates increasing buying pressure, while a Qstick below zero indicates increasing selling pressure.

type QstickStrategy struct {
    // Qstick represents the configuration parameters for calculating the Qstick.
    Qstick *momentum.Qstick[float64]
}

func NewQstickStrategy() *QstickStrategy

NewQstickStrategy function initializes a new Qstick strategy instance.

func (*QstickStrategy) Compute

func (q *QstickStrategy) Compute(c <-chan *asset.Snapshot) <-chan strategy.Action

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

func (*QstickStrategy) Name

func (*QstickStrategy) Name() string

Name returns the name of the strategy.

func (*QstickStrategy) Report

func (q *QstickStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

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

TrimaStrategy represents the configuration parameters for calculating the TRIMA strategy. A bullish cross occurs when the short TRIMA moves above the long TRIMA. A bearish cross occurs when the short TRIMA moves below the long TRIME.

type TrimaStrategy struct {
    // Trima1 represents the configuration parameters for calculating the short TRIMA.
    Short *trend.Trima[float64]

    // Trima2 represents the configuration parameters for calculating the long TRIMA.
    Long *trend.Trima[float64]
}

func NewTrimaStrategy() *TrimaStrategy

NewTrimaStrategy function initializes a new TRIMA strategy instance with the default parameters.

func (*TrimaStrategy) Compute

func (t *TrimaStrategy) Compute(c <-chan *asset.Snapshot) <-chan strategy.Action

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

func (*TrimaStrategy) Name

func (*TrimaStrategy) Name() string

Name returns the name of the strategy.

func (*TrimaStrategy) Report

func (t *TrimaStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

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

TripleMovingAverageCrossoverStrategy defines the parameters used to calculate the Triple Moving Average Crossover trading strategy. This strategy uses three Exponential Moving Averages (EMAs) with different lengths to identify potential buy and sell signals. - A buy signal is generated when the **fastest** EMA crosses above both the **medium** and **slowest** EMAs. - A sell signal is generated when the fastest EMA crosses below both the medium and slowest EMAs. - Otherwise, the strategy recommends holding the asset.

type TripleMovingAverageCrossoverStrategy struct {
    // FastEma is the fastest EMA.
    FastEma *trend.Ema[float64]

    // MediumEma is the meium EMA.
    MediumEma *trend.Ema[float64]

    // SlowEma is the slowest EMA.
    SlowEma *trend.Ema[float64]
}

func NewTripleMovingAverageCrossoverStrategy() *TripleMovingAverageCrossoverStrategy

NewTripleMovingAverageCrossoverStrategy function initializes a new Triple Moving Average Crossover strategy instance with the default parameters.

func NewTripleMovingAverageCrossoverStrategyWith(fastPeriod, mediumPeriod, slowPeriod int) *TripleMovingAverageCrossoverStrategy

NewTripleMovingAverageCrossoverStrategyWith function initializes a new Triple Moving Average Crossover strategy instance with the given periods.

func (*TripleMovingAverageCrossoverStrategy) Compute

func (t *TripleMovingAverageCrossoverStrategy) Compute(c <-chan *asset.Snapshot) <-chan strategy.Action

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

func (*TripleMovingAverageCrossoverStrategy) Name

func (*TripleMovingAverageCrossoverStrategy) Name() string

Name returns the name of the strategy.

func (*TripleMovingAverageCrossoverStrategy) Report

func (t *TripleMovingAverageCrossoverStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

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

TrixStrategy represents the configuration parameters for calculating the TRIX strategy. A TRIX value crossing above the zero line suggests a bullish trend, while crossing below the zero line indicates a bearish trend.

type TrixStrategy struct {
    // Trix represents the configuration parameters for calculating the TRIX.
    Trix *trend.Trix[float64]
}

func NewTrixStrategy() *TrixStrategy

NewTrixStrategy function initializes a new TRIX strategy instance.

func (*TrixStrategy) Compute

func (t *TrixStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan strategy.Action

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

func (*TrixStrategy) Name

func (*TrixStrategy) Name() string

Name returns the name of the strategy.

func (*TrixStrategy) Report

func (t *TrixStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

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

TsiStrategy represents the configuration parameters for calculating the TSI strategy. When the TSI is above zero and crossing above the signal line suggests a bullish trend, while TSI being below zero and crossing below the signal line indicates a bearish trend.

Signal Line = Ema(12, TSI)
When TSI > 0, TSI > Signal Line, Buy.
When TSI < 0, TSI < Signal Line, Sell.const
type TsiStrategy struct {
    // Tsi represents the configuration parameters for calculating the True Strength Index (TSI).
    Tsi *trend.Tsi[float64]

    // Signal line is the moving average of the TSI.
    Signal trend.Ma[float64]
}

func NewTsiStrategy() *TsiStrategy

NewTsiStrategy function initializes a new TSI strategy instance.

func NewTsiStrategyWith(firstSmoothingPeriod, secondSmoothingPeriod, signalPeriod int) *TsiStrategy

NewTsiStrategyWith function initializes a new TSI strategy instance with the given parameters.

func (*TsiStrategy) Compute

func (t *TsiStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan strategy.Action

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

func (*TsiStrategy) IdlePeriod

func (t *TsiStrategy) IdlePeriod() int

IdlePeriod is the initial period that TSI strategy yield any results.

func (*TsiStrategy) Name

func (t *TsiStrategy) Name() string

Name returns the name of the strategy.

func (*TsiStrategy) Report

func (t *TsiStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

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

VwmaStrategy represents the configuration parameters for calculating the VWMA strategy. The VwmaStrategy function uses SMA and VWMA indicators to provide a BUY action when VWMA is above SMA, and a SELL signal when VWMA is below SMA, a HOLD otherwse.

type VwmaStrategy struct {
    // VWMA indicator.
    Vwma *trend.Vwma[float64]

    // SMA indicator.
    Sma *trend.Sma[float64]
}

func NewVwmaStrategy() *VwmaStrategy

NewVwmaStrategy function initializes a new VWMA strategy instance with the default parameters.

func (*VwmaStrategy) Compute

func (v *VwmaStrategy) Compute(c <-chan *asset.Snapshot) <-chan strategy.Action

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

func (*VwmaStrategy) Name

func (*VwmaStrategy) Name() string

Name returns the name of the strategy.

func (*VwmaStrategy) Report

func (v *VwmaStrategy) Report(c <-chan *asset.Snapshot) *helper.Report

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

Generated by gomarkdoc