Skip to content

Latest commit

 

History

History
782 lines (563 loc) · 26.1 KB

README.md

File metadata and controls

782 lines (563 loc) · 26.1 KB

momentum

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

Package momentum contains the momentum indicator 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 (
    // DefaultAwesomeOscillatorShortPeriod is the default short period for the Awesome Oscillator (AO).
    DefaultAwesomeOscillatorShortPeriod = 5

    // DefaultAwesomeOscillatorLongPeriod is the default long period for the Awesome Oscillator (AO).
    DefaultAwesomeOscillatorLongPeriod = 34
)

const (
    // DefaultChaikinOscillatorShortPeriod is the default short period for the Chaikin Oscillator.
    DefaultChaikinOscillatorShortPeriod = 3

    // DefaultChaikinOscillatorLongPeriod is the default long period for the Chaikin Oscillator.
    DefaultChaikinOscillatorLongPeriod = 10
)

const (
    // DefaultIchimokuCloudConversionPeriod is the default conversion period for the Ichimoku Cloud.
    DefaultIchimokuCloudConversionPeriod = 9

    // DefaultIchimokuCloudBasePeriod is the default base period for the Ichimoku Cloud.
    DefaultIchimokuCloudBasePeriod = 26

    // DefaultIchimokuCloudLeadingPeriod is the default leading period for the Ichimoku Cloud.
    DefaultIchimokuCloudLeadingPeriod = 52

    // DefaultIchimokuCloudLaggingPeriod is the default lagging period for the Ichimoku Cloud.
    DefaultIchimokuCloudLaggingPeriod = 26
)

const (
    // DefaultPpoShortPeriod is the default short period for the Percentage Price Oscillator.
    DefaultPpoShortPeriod = 12

    // DefaultPpoLongPeriod is the default long period for the Percentage Price Oscillator.
    DefaultPpoLongPeriod = 26

    // DefaultPpoSignalPeriod is the default signal period for the Percentage Price Oscillator.
    DefaultPpoSignalPeriod = 9
)

const (
    // DefaultPvoShortPeriod is the default short period for the Percentage Volume Oscillator.
    DefaultPvoShortPeriod = 12

    // DefaultPvoLongPeriod is the default long period for the Percentage Volume Oscillator.
    DefaultPvoLongPeriod = 26

    // DefaultPvoSignalPeriod is the default signal period for the Percentage Volume Oscillator.
    DefaultPvoSignalPeriod = 9
)

const (
    // DefaultStochasticOscillatorMaxAndMinPeriod is the default max and min period for the Stochastic Oscillator.
    DefaultStochasticOscillatorMaxAndMinPeriod = 14

    // DefaultStochasticOscillatorPeriod is the default period for the Stochastic Oscillator.
    DefaultStochasticOscillatorPeriod = 3
)

const (
    // DefaultQstickPeriod is the default period for the Qstick SMA.
    DefaultQstickPeriod = 20
)

const (
    // DefaultRsiPeriod is the default period for the Relative Strength Index (RSI).
    DefaultRsiPeriod = 14
)

const (
    // DefaultStochasticRsiPeriod is the default period for the Stochastic Relative Strength Index (RSI).
    DefaultStochasticRsiPeriod = 14
)

const (
    // DefaultWilliamsRPeriod is the default period for the Williams R.
    DefaultWilliamsRPeriod = 14
)

AwesomeOscillator represents the configuration parameter for calculating the Awesome Oscillator (AO). It gauges market momentum by comparing short-term price action (5-period average) against long-term trends (34-period average). Its value around a zero line reflects bullishness above and bearishness below. Crossings of the zero line can signal potential trend reversals. Traders use the AO to confirm existing trends, identify entry/exit points, and understand momentum shifts.

Median Price = ((Low + High) / 2).
AO = 5-Period SMA - 34-Period SMA.

Example:

ao := momentum.AwesomeOscillator[float64]()
values := ao.Compute(lows, highs)
type AwesomeOscillator[T helper.Number] struct {
    // ShortSma is the SMA for the short period.
    ShortSma *trend.Sma[T]

    // LongSma is the SMA for the long period.
    LongSma *trend.Sma[T]
}

func NewAwesomeOscillator[T helper.Number]() *AwesomeOscillator[T]

NewAwesomeOscillator function initializes a new Awesome Oscillator instance.

func (*AwesomeOscillator[T]) Compute

func (a *AwesomeOscillator[T]) Compute(highs, lows <-chan T) <-chan T

Compute function takes a channel of numbers and computes the AwesomeOscillator.

func (*AwesomeOscillator[T]) IdlePeriod

func (a *AwesomeOscillator[T]) IdlePeriod() int

IdlePeriod is the initial period that Awesome Oscillator won't yield any results.

ChaikinOscillator represents the configuration parameter for calculating the Chaikin Oscillator. It measures the momentum of the Accumulation/Distribution (A/D) using the Moving Average Convergence Divergence (MACD) formula. It takes the difference between fast and slow periods EMA of the A/D. Cross above the A/D line indicates bullish.

CO = Ema(fastPeriod, AD) - Ema(slowPeriod, AD)

Example:

co := momentum.ChaikinOscillator[float64]()
values := co.Compute(lows, highs)
type ChaikinOscillator[T helper.Number] struct {
    // Ad is the Accumulation/Distribution (A/D) instance.
    Ad  *volume.Ad[T]

    // ShortEma is the SMA for the short period.
    ShortEma *trend.Ema[T]

    // LongEma is the SMA for the long period.
    LongEma *trend.Ema[T]
}

func NewChaikinOscillator[T helper.Number]() *ChaikinOscillator[T]

NewChaikinOscillator function initializes a new Chaikin Oscillator instance.

func (*ChaikinOscillator[T]) Compute

func (c *ChaikinOscillator[T]) Compute(highs, lows, closings, volumes <-chan T) (<-chan T, <-chan T)

Compute function takes a channel of numbers and computes the Chaikin Oscillator.

func (*ChaikinOscillator[T]) IdlePeriod

func (c *ChaikinOscillator[T]) IdlePeriod() int

IdlePeriod is the initial period that Chaikin Oscillator won't yield any results.

IchimokuCloud represents the configuration parameter for calculating the Ichimoku Cloud. It is also known as the Ichimoku Kinko Hyo, is a versatile indicator that defines support and resistance, identifies trend direction, gauges momentum, and provides trading signals.

Tenkan-sen (Conversion Line) = (9-Period High + 9-Period Low) / 2
Kijun-sen (Base Line) = (26-Period High + 26-Period Low) / 2
Senkou Span A (Leading Span A) = (Conversion Line + Base Line) / 2
Senkou Span B (Leading Span B) = (52-Period High + 52-Period Low) / 2
Chikou Span (Lagging Span) = Closing plotted 26 days in the past.

Example:

ic := momentum.IchimokuCloud[float64]()
conversionLine, baseLine, leadingSpanA, leasingSpanB, laggingSpan := ic.Compute(highs, lows, closings)
type IchimokuCloud[T helper.Number] struct {
    // ConversionMax is the conversion Moving Max instance.
    ConversionMax *trend.MovingMax[T]

    // ConversionMin is the conversion Moving Min instance.
    ConversionMin *trend.MovingMin[T]

    // BaseMax is the base Moving Max instance.
    BaseMax *trend.MovingMax[T]

    // BaseMin is the base Moving Min instance.
    BaseMin *trend.MovingMin[T]

    // LeadingMax is the leading Moving Max instance.
    LeadingMax *trend.MovingMax[T]

    // LeadingMin is the leading Moving Min instance.
    LeadingMin *trend.MovingMin[T]

    // LaggingPeriod is the lagging period.
    LaggingPeriod int
}

func NewIchimokuCloud[T helper.Number]() *IchimokuCloud[T]

NewIchimokuCloud function initializes a new Ichimoku Cloud instance.

func (*IchimokuCloud[T]) Compute

func (i *IchimokuCloud[T]) Compute(highs, lows, closings <-chan T) (<-chan T, <-chan T, <-chan T, <-chan T, <-chan T)

Compute function takes a channel of numbers and computes the Ichimoku Cloud. Returns conversionLine, baseLine, leadingSpanA, leadingSpanB, laggingSpan

func (*IchimokuCloud[T]) IdlePeriod

func (i *IchimokuCloud[T]) IdlePeriod() int

IdlePeriod is the initial period that Ichimoku Cloud won't yield any results.

type Ppo

Ppo represents the configuration parameter for calculating the Percentage Price Oscillator (PPO). It is a momentum oscillator for the price. It is used to indicate the ups and downs based on the price. A breakout is confirmed when PPO is positive.

PPO = ((EMA(shortPeriod, prices) - EMA(longPeriod, prices)) / EMA(longPeriod, prices)) * 100
Signal = EMA(9, PPO)
Histogram = PPO - Signal

Example:

ppo := momentum.Ppo[float64]()
p, s, h := ppo.Compute(closings)
type Ppo[T helper.Number] struct {
    // ShortEma is the short EMA instance.
    ShortEma *trend.Ema[T]

    // LongEma is the long EMA instance.
    LongEma *trend.Ema[T]

    // SignalEma is the signal EMA instance.
    SignalEma *trend.Ema[T]
}

func NewPpo

func NewPpo[T helper.Number]() *Ppo[T]

NewPpo function initializes a new Percentage Price Oscillator instance.

func (*Ppo[T]) Compute

func (p *Ppo[T]) Compute(closings <-chan T) (<-chan T, <-chan T, <-chan T)

Compute function takes a channel of numbers and computes the Percentage Price Oscillator. Returns ppo, signal, histogram.

func (*Ppo[T]) IdlePeriod

func (p *Ppo[T]) IdlePeriod() int

IdlePeriod is the initial period that Percentage Price Oscillator won't yield any results.

type Pvo

Pvo represents the configuration parameter for calculating the Percentage Volume Oscillator (PVO). It is a momentum oscillator for the price. It is used to indicate the ups and downs based on the price. A breakout is confirmed when PVO is positive.

PVO = ((EMA(shortPeriod, prices) - EMA(longPeriod, prices)) / EMA(longPeriod, prices)) * 100
Signal = EMA(9, PVO)
Histogram = PVO - Signal

Example:

pvo := momentum.Pvo[float64]()
p, s, h := pvo.Compute(volumes)
type Pvo[T helper.Number] struct {
    // ShortEma is the short EMA instance.
    ShortEma *trend.Ema[T]

    // LongEma is the long EMA instance.
    LongEma *trend.Ema[T]

    // SignalEma is the signal EMA instance.
    SignalEma *trend.Ema[T]
}

func NewPvo

func NewPvo[T helper.Number]() *Pvo[T]

NewPvo function initializes a new Percentage Volume Oscillator instance.

func (*Pvo[T]) Compute

func (p *Pvo[T]) Compute(volumes <-chan T) (<-chan T, <-chan T, <-chan T)

Compute function takes a channel of numbers and computes the Percentage Volume Oscillator. Returns pvo, signal, histogram.

func (*Pvo[T]) IdlePeriod

func (p *Pvo[T]) IdlePeriod() int

IdlePeriod is the initial period that Percentage Volume Oscillator won't yield any results.

type Qstick

Qstick represents the configuration parameter for calculating the Qstick indicator. 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.

QS = SMA(Closings - Openings)

Example:

qstick := momentum.Qstick[float64]()
qstick.Sma.Period = 50

values := qstick.Compute(openings, closings)
type Qstick[T helper.Number] struct {
    Sma *trend.Sma[T]
}

func NewQstick[T helper.Number]() *Qstick[T]

NewQstick function initializes a new QStick instance.

func (*Qstick[T]) Compute

func (q *Qstick[T]) Compute(openings, closings <-chan T) <-chan T

Compute function takes a channel of numbers and computes the Qstick.

func (*Qstick[T]) IdlePeriod

func (q *Qstick[T]) IdlePeriod() int

IdlePeriod is the initial period that Qstick won't yield any results.

type Rsi

Rsi represents the configuration parameter for calculating the Relative Strength Index (RSI). It is a momentum indicator that measures the magnitude of recent price changes to evaluate overbought and oversold conditions.

RS = Average Gain / Average Loss
RSI = 100 - (100 / (1 + RS))

Example:

rsi := momentum.NewRsi[float64]()
result := rsi.Compute(closings)
type Rsi[T helper.Number] struct {
    // Rma is the RMA instance.
    Rma *trend.Rma[T]
}

func NewRsi

func NewRsi[T helper.Number]() *Rsi[T]

NewRsi function initializes a new Relative Strength Index instance with the default parameters.

func NewRsiWithPeriod[T helper.Number](period int) *Rsi[T]

NewRsiWithPeriod function initializes a new Relative Strength Index instance with the given period.

func (*Rsi[T]) Compute

func (r *Rsi[T]) Compute(closings <-chan T) <-chan T

Compute function takes a channel of closings numbers and computes the Relative Strength Index.

func (*Rsi[T]) IdlePeriod

func (r *Rsi[T]) IdlePeriod() int

IdlePeriod is the initial period that Relative Strength Index won't yield any results.

StochasticOscillator represents the configuration parameter for calculating the Stochastic Oscillator. It is a momentum indicator that shows the location of the closing relative to high-low range over a set number of periods.

K = (Closing - Lowest Low) / (Highest High - Lowest Low) * 100
D = 3-Period SMA of K

Example:

so := momentum.StochasticOscillator[float64]()
k, d := wr.Compute(highs, lows, closings)
type StochasticOscillator[T helper.Number] struct {
    // Max is the Moving Max instance.
    Max *trend.MovingMax[T]

    // Min is the Moving Min instance.
    Min *trend.MovingMin[T]

    // Sma is the SMA instance.
    Sma *trend.Sma[T]
}

func NewStochasticOscillator[T helper.Number]() *StochasticOscillator[T]

NewStochasticOscillator function initializes a new Stochastic Oscillator instance.

func (*StochasticOscillator[T]) Compute

func (s *StochasticOscillator[T]) Compute(highs, lows, closings <-chan T) (<-chan T, <-chan T)

Compute function takes a channel of numbers and computes the Stochastic Oscillator. Returns k and d.

func (*StochasticOscillator[T]) IdlePeriod

func (s *StochasticOscillator[T]) IdlePeriod() int

IdlePeriod is the initial period that Stochastic Oscillator won't yield any results.

StochasticRsi represents the configuration parameter for calculating the Stochastic Relative Strength Index (RSI). It is a momentum indicator that focuses on the historical performance to evaluate overbought and oversold conditions.

RSI - Min(RSI)
Stochastic RSI = -------------------------
                   Max(RSI) - Min(RSI)

Example:

stochasticRsi := momentum.NewStochasticRsi[float64]()
result := stochasticRsi.Compute(closings)
type StochasticRsi[T helper.Number] struct {
    // Rsi is that RSI instance.
    Rsi *Rsi[T]

    // Min is the Moving Min instance.
    Min *trend.MovingMin[T]

    // Max is the Moving Max instance.
    Max *trend.MovingMax[T]
}

func NewStochasticRsi[T helper.Number]() *StochasticRsi[T]

NewStochasticRsi function initializes a new Storchastic RSI instance with the default parameters.

func NewStochasticRsiWithPeriod[T helper.Number](period int) *StochasticRsi[T]

NewStochasticRsiWithPeriod function initializes a new Stochastic RSI instance with the given period.

func (*StochasticRsi[T]) Compute

func (s *StochasticRsi[T]) Compute(closings <-chan T) <-chan T

Compute function takes a channel of closings numbers and computes the Stochastic RSI.

func (*StochasticRsi[T]) IdlePeriod

func (s *StochasticRsi[T]) IdlePeriod() int

IdlePeriod is the initial period that Stochasic RSI won't yield any results.

WilliamsR represents the configuration parameter for calculating the Williams %R, or just %R. It is a technical analysis oscillator showing the current closing price in relation to the high and low of the past N days (for a given N). It was developed by a publisher and promoter of trading materials, Larry Williams. Its purpose is to tell whether a stock or commodity market is trading near the high or the low, or somewhere in between, of its recent trading range. Buy when -80 and below. Sell when -20 and above.

WR = (Highest High - Closing) / (Highest High - Lowest Low) * -100.

Example:

wr := momentum.WilliamsR[float64]()
values := wr.Compute(highs, lows, closings)
type WilliamsR[T helper.Number] struct {
    // Max is the Moving Max instance.
    Max *trend.MovingMax[T]

    // Min is the Moving Min instance.
    Min *trend.MovingMin[T]
}

func NewWilliamsR[T helper.Number]() *WilliamsR[T]

NewWilliamsR function initializes a new Williams R instance.

func (*WilliamsR[T]) Compute

func (w *WilliamsR[T]) Compute(highs, lows, closings <-chan T) <-chan T

Compute function takes a channel of numbers and computes the Williams R.

func (*WilliamsR[T]) IdlePeriod

func (w *WilliamsR[T]) IdlePeriod() int

IdlePeriod is the initial period that Williams R won't yield any results.

Generated by gomarkdoc