Skip to content

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

License

Notifications You must be signed in to change notification settings

MicahParks/go-ma

Repository files navigation

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:

About

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

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages