ma

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2023 License: MIT Imports: 1 Imported by: 1

README

Go Reference Go Report Card

go-ma

The Simple Moving Average (SMA), Exponential Moving (EMA), and Moving Average Convergence Divergence (MACD) technical analysis algorithms implemented in Golang.

If there are any other moving average algorithms you'd like implemented, please feel free to open an issue or a PR. Example: Weighted Moving Average (WMA)

import "github.com/MicahParks/go-ma"

Usage

Please see the examples directory for full customizable examples.

Create an MACD and signal EMA data structure

This will produce points on the MACD and signal EMA lines, as well as buy/sell signals.

// Create a logger.
logger := log.New(os.Stdout, "", 0)

// Gather some data.
//
// For production systems, it'd be best to gather data asynchronously.
prices := testData()

// Create the MACD and signal EMA pair.
signal := ma.DefaultMACDSignal(prices[:ma.RequiredSamplesForDefaultMACDSignal])

// Iterate through the rest of the data and print the results.
var results ma.MACDSignalResults
for i, p := range prices[ma.RequiredSamplesForDefaultMACDSignal:] {
	results = signal.Calculate(p)

	// Interpret the buy signal.
	var buySignal string
	if results.BuySignal != nil {
		if *results.BuySignal {
			buySignal = "Buy, buy, buy!"
		} else {
			buySignal = "Sell, sell, sell!"
		}
	} else {
		buySignal = "Do nothing."
	}

	logger.Printf("Price index: %d\n  MACD: %.5f\n  Signal EMA: %.5f\n  Buy signal: %s", i+ma.RequiredSamplesForDefaultMACDSignal, results.MACD.Result, results.SignalEMA, buySignal)
}

Testing

There is 100% test coverage and benchmarks for this project. Here is an example benchmark result:

$ go test -bench .
goos: linux
goarch: amd64
pkg: github.com/MicahParks/go-ma
cpu: Intel(R) Core(TM) i5-9600K CPU @ 3.70GHz
BenchmarkEMABig_Calculate-6             1000000000               0.0000272 ns/op
BenchmarkEMA_Calculate-6           1000000000               0.0000009 ns/op
BenchmarkMACDSignalBig_Calculate-6      1000000000               0.0004847 ns/op
BenchmarkMACDSignal_Calculate-6    1000000000               0.0000064 ns/op
BenchmarkMACDBig_Calculate-6            1000000000               0.0000389 ns/op
BenchmarkMACD_Calculate-6          1000000000               0.0000019 ns/op
BenchmarkSMABig_Calculate-6             1000000000               0.0000476 ns/op
BenchmarkSMA_Calculate-6           1000000000               0.0000009 ns/op
PASS
ok      github.com/MicahParks/go-ma     0.014s

Other Technical Algorithms

Looking for some other technical analysis algorithms? Here are some other ones I've implemented:

  • Accumulation/Distribution (A/D): go-ad
  • Chaikin: go-chaikin
  • Moving Average Convergence Divergence (MACD), Exponential Moving Average (EMA), Simple Moving Average (SMA): go-ma
  • Relative Strength Index (RSI): go-rsi

Resources

I built and tested this package based on the resources here:

Documentation

Index

Constants

View Source
const (

	// DefaultLongMACDPeriod is a common MACD period for the long input EMA algorithm.
	DefaultLongMACDPeriod = 26

	// DefaultShortMACDPeriod is a common MACD period for the short input EMA algorithm.
	DefaultShortMACDPeriod = 12

	// DefaultSignalEMAPeriod is a common MACD period for the signal EMA that will crossover the MACD line.
	DefaultSignalEMAPeriod = 9
)
View Source
const (

	// DefaultEMASmoothing is a common smoothing constant for the EMA algorithm.
	DefaultEMASmoothing = 2
)
View Source
const (

	// RequiredSamplesForDefaultMACDSignal is the required number of period samples for an MACD signal using the default
	// arguments.
	RequiredSamplesForDefaultMACDSignal = DefaultLongMACDPeriod + DefaultSignalEMAPeriod
)

Variables

This section is empty.

Functions

func NewBigMACDSignal added in v0.1.0

func NewBigMACDSignal(macd BigMACD, signalEMA *BigEMA, next *big.Float) (*BigMACDSignal, BigMACDSignalResults)

NewBigMACDSignal creates a new MACD and signal EMA pair. It's calculations are done in tandem to produced buy/sell signals.

func NewMACDSignal added in v0.1.0

func NewMACDSignal(macd MACD, signalEMA *EMA, next float64) (*MACDSignal, MACDSignalResults)

NewMACDSignal creates a new MACD and signal EMA pair. It's calculations are done in tandem to produced buy/sell signals.

Types

type BigEMA added in v0.1.0

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

BigEMA represents the state of an Exponential Moving Average (EMA) algorithm.

func NewBigEMA added in v0.1.0

func NewBigEMA(periods uint, sma, smoothing *big.Float) *BigEMA

NewBigEMA creates a new EMA data structure.

func (*BigEMA) Calculate added in v0.1.0

func (ema *BigEMA) Calculate(next *big.Float) (result *big.Float)

Calculate produces the next EMA result given the next input.

type BigMACD added in v0.1.0

type BigMACD struct {
	Long  *BigEMA
	Short *BigEMA
}

BigMACD represents the state of a Moving Average Convergence Divergence (MACD) algorithm.

func NewBigMACD added in v0.1.0

func NewBigMACD(long, short *BigEMA) BigMACD

NewBigMACD creates a new MACD data structure and returns the initial result.

func (BigMACD) Calculate added in v0.1.0

func (macd BigMACD) Calculate(next *big.Float) BigMACDResults

Calculate produces the next MACD result given the next input.

func (BigMACD) SignalEMA added in v0.1.0

func (macd BigMACD) SignalEMA(firstMACDResult *big.Float, next []*big.Float, smoothing *big.Float) (signalEMA *BigEMA, signalResult *big.Float, macdResults []BigMACDResults)

SignalEMA creates a signal EMA for the current MACD.

The first MACD result *must* be saved in order to create the signal EMA. Then, the next period samples required for the creation of the signal EMA must be given. The period length of the EMA is `1 + len(next)`.

type BigMACDResults added in v0.1.0

type BigMACDResults struct {
	Long   *big.Float
	Result *big.Float
	Short  *big.Float
}

BigMACDResults holds the results fo an MACD calculation.

type BigMACDSignal added in v0.1.0

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

BigMACDSignal represents an MACD and signal EMA pair.

func DefaultBigMACDSignal added in v0.1.0

func DefaultBigMACDSignal(initial []*big.Float) *BigMACDSignal

DefaultBigMACDSignal is a helper function to create an MACD and signal EMA pair from the default parameters given the initial input data points.

There must be at least 35 data points in the initial slice. This accounts for the number of data points required to make the MACD (26) and the number of data points to make the signal EMA from the MACD (9).

If the initial input slice does not have enough data points, the function will return `nil`.

If the initial input slice does has too many data points, the MACD and signal EMA pair will be "caught" up to the last data point given, but no results will be accessible.

func (*BigMACDSignal) Calculate added in v0.1.0

func (m *BigMACDSignal) Calculate(next *big.Float) BigMACDSignalResults

Calculate computes the next MACD and signal EMA pair's results. It may also trigger a buy/sell signal.

type BigMACDSignalResults added in v0.1.0

type BigMACDSignalResults struct {
	BuySignal *bool
	MACD      BigMACDResults
	SignalEMA *big.Float
}

BigMACDSignalResults holds the results of an MACD and signal EMA pair's calculation.

If the calculation triggered a buy signal BuySignal will not be `nil`. It will be a pointer to `true`, if the signal indicates a buy and a pointer to `false` if the signal indicates a sell.

type EMA added in v0.1.0

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

EMA represents the state of an Exponential Moving Average (EMA).

func NewEMA added in v0.1.0

func NewEMA(periods uint, sma, smoothing float64) *EMA

NewEMA creates a new EMA data structure.

func (*EMA) Calculate added in v0.1.0

func (ema *EMA) Calculate(next float64) (result float64)

Calculate produces the next EMA result given the next input.

type MACD added in v0.1.0

type MACD struct {
	Long  *EMA
	Short *EMA
}

MACD represents the state of a Moving Average Convergence Divergence (MACD) algorithm.

func NewMACD added in v0.1.0

func NewMACD(long, short *EMA) MACD

NewMACD creates a new MACD data structure and returns the initial result.

func (MACD) Calculate added in v0.1.0

func (macd MACD) Calculate(next float64) MACDResults

Calculate produces the next MACD result given the next input.

func (MACD) SignalEMA added in v0.1.0

func (macd MACD) SignalEMA(firstMACDResult float64, next []float64, smoothing float64) (signalEMA *EMA, signalResult float64, macdResults []MACDResults)

SignalEMA creates a signal EMA for the current MACD.

The first MACD result *must* be saved in order to create the signal EMA. Then, the next period samples required for the creation of the signal EMA must be given. The period length of the EMA is `1 + len(next)`.

type MACDResults added in v0.1.0

type MACDResults struct {
	Long   float64
	Result float64
	Short  float64
}

MACDResults holds the results fo an MACD calculation.

type MACDSignal added in v0.1.0

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

MACDSignal represents an MACD and signal EMA pair.

func DefaultMACDSignal added in v0.1.0

func DefaultMACDSignal(initial []float64) *MACDSignal

DefaultMACDSignal is a helper function to create an MACD and signal EMA pair from the default parameters given the initial input data points.

There must be at least 35 data points in the initial slice. This accounts for the number of data points required to make the MACD (26) and the number of data points to make the signal EMA from the MACD (9).

If the initial input slice does not have enough data points, the function will return `nil`.

If the initial input slice does has too many data points, the MACD and signal EMA pair will be "caught" up to the last data point given, but no results will be accessible.

func (*MACDSignal) Calculate added in v0.1.0

func (m *MACDSignal) Calculate(next float64) MACDSignalResults

Calculate computes the next MACD and signal EMA pair's results. It may also trigger a buy/sell signal.

type MACDSignalResults added in v0.1.0

type MACDSignalResults struct {
	BuySignal *bool
	MACD      MACDResults
	SignalEMA float64
}

MACDSignalResults holds the results of an MACD and signal EMA pair's calculation.

If the calculation triggered a buy signal BuySignal will not be `nil`. It will be a pointer to `true`, if the signal indicates a buy and a pointer to `false` if the signal indicates a sell.

type SMA added in v0.1.0

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

SMA represents the state of a Simple Moving Average (SMA) algorithm.

func NewSMA added in v0.1.0

func NewSMA(initial []float64) (sma *SMA, result float64)

NewSMA creates a new SMA data structure and the initial result. The period used will be the length of the initial input slice.

func (*SMA) Calculate added in v0.1.0

func (sma *SMA) Calculate(next float64) (result float64)

Calculate produces the next SMA result given the next input.

type SMABig

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

SMABig represents the state of a Simple Moving Average (SMA) algorithm.

func NewBigSMA added in v0.1.0

func NewBigSMA(initial []*big.Float) (sma *SMABig, result *big.Float)

NewBigSMA creates a new SMA data structure and the initial result. The period used will be the length of the initial input slice.

func (*SMABig) Calculate

func (sma *SMABig) Calculate(next *big.Float) (result *big.Float)

Calculate produces the next SMA result given the next input.

Directories

Path Synopsis
examples
ema
sma

Jump to

Keyboard shortcuts

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