chaikin

package module
v0.2.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: 3 Imported by: 0

README

Go Reference Go Report Card

go-chaikin

The Chaikin Oscillator technical analysis algorithm implemented in Golang.

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

Usage

For full examples, please see the examples directory.

Step 1

Gather the initial input. This is 10 periods of inputs for the Accumulation Distribution Line. It is the minimum number of periods required to produce one result for the Chaikin Oscillator. The value, 10 is stored in chaikin.LongEMA constant.

The input must be put into an array, not a slice. Make sure to fill all 10 indices of the array.

initial := [chaikin.LongEMA]ad.Input{}
for i := 0; i < chaikin.LongEMA; i++ {
	initial[i] = ad.Input{
		Close:  closePrices[i],
		Low:    low[i],
		High:   high[i],
		Volume: volume[i],
	}
}

Step 2

Create the Chaikin Oscillator data structure from the initial input array. This will also produce the first Chaikin Oscillator point as well as its corresponding Accumulation Distribution Line point.

cha, firstResult, adLine := chaikin.New(initial)

Step 3

Use the subsequent periods to calculate the next points for the Chaikin Oscillator and Accumulation Distribution Line.

result, adLine = cha.Calculate(ad.Input{
	Close:  closePrices[i],
	Low:    low[i],
	High:   high[i],
	Volume: volume[i],
})

Somewhat complete example (without data)

package main

import (
	"log"
	"os"

	"github.com/MicahParks/go-ad"

	"github.com/MicahParks/go-chaikin"
)

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

	// Create the initial input.
	initial := [chaikin.LongEMA]ad.Input{}
	for i := 0; i < chaikin.LongEMA; i++ {
		initial[i] = ad.Input{
			Close:  closePrices[i],
			Low:    low[i],
			High:   high[i],
			Volume: volume[i],
		}
	}

	// Create the Chaikin Oscillator data structure as well as its first data point and the corresponding Accumulation
	// Distribution Line point.
	cha, result, adLine := chaikin.New(initial)
	logger.Printf("%.4f, %.4f", adLine, result)

	// Use every subsequent period's data to calculate the next points on the Chaikin Oscillator and Accumulation
	// Distribution Line.
	for i := range open[chaikin.LongEMA:] {
		i += chaikin.LongEMA

		result, adLine = cha.Calculate(ad.Input{
			Close:  closePrices[i],
			Low:    low[i],
			High:   high[i],
			Volume: volume[i],
		})
		logger.Printf("%.4f, %.4f", adLine, result)
	}
}

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-chaikin
cpu: Intel(R) Core(TM) i5-9600K CPU @ 3.70GHz
BenchmarkChaikin_Calculate-6            1000000000               0.0000017 ns/op
BenchmarkBigChaikin_Calculate-6         1000000000               0.0000891 ns/op
PASS
ok      github.com/MicahParks/go-chaikin        0.004s

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 using these resources:

Documentation

Index

Constants

View Source
const (
	// ShortEMA is the number of periods in the short EMA of the Accumulation Distribution Line results. For the Chaikin
	// Oscillator.
	ShortEMA = 3

	// LongEMA is the number of periods in the long EMA of the Accumulation Distribution Line results. For the Chaikin
	// Oscillator.
	LongEMA = 10
)

Variables

This section is empty.

Functions

func New

func New(initial [LongEMA]ad.Input) (*Chaikin, Result)

New creates a new Chaikin Oscillator and returns its first point along with the corresponding Accumulation Distribution Line point.

func NewBig

func NewBig(initial [LongEMA]ad.BigInput) (*BigChaikin, BigResult)

NewBig creates a new Chaikin Oscillator and returns its first point along with the corresponding Accumulation Distribution Line point.

func NewBigCustom added in v0.2.2

func NewBigCustom(initial []ad.BigInput, shortPeriod uint, shortSmoothing, longSmoothing *big.Float) (*BigChaikin, BigResult)

NewBigCustom creates a new Chaikin Oscillator and returns its first point along with the corresponding Accumulation Distribution Line point. Custom (non-Chaikin approved) inputs are allowed. The length of the initial input slice is the length of the long EMA period.

func NewCustom added in v0.2.2

func NewCustom(initial []ad.Input, shortPeriod uint, shortSmoothing, longSmoothing float64) (*Chaikin, Result)

NewCustom creates a new Chaikin Oscillator and returns its first point along with the corresponding Accumulation Distribution Line point. Custom (non-Chaikin approved) inputs are allowed. The length of the initial input slice is the length of the long EMA period.

Types

type BigChaikin

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

BigChaikin represents the state of the Chaikin Oscillator.

func (*BigChaikin) Calculate

func (c *BigChaikin) Calculate(next ad.BigInput) BigResult

Calculate produces the next point on the Chaikin Oscillator given the current period's information.

type BigResult added in v0.2.0

type BigResult struct {
	ADLine      *big.Float
	BuySignal   *bool
	ChaikinLine *big.Float
}

BigResult holds the results of a BigChaikin calculation.

type Chaikin

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

Chaikin represents the state of the Chaikin Oscillator.

func (*Chaikin) Calculate

func (c *Chaikin) Calculate(next ad.Input) Result

Calculate produces the next point on the Chaikin Oscillator given the current period's information.

type Result added in v0.2.0

type Result struct {
	ADLine      float64
	BuySignal   *bool
	ChaikinLine float64
}

Result holds the results of a Chaikin calculation.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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