rsi

package module
v2.0.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: 0

README

Go Reference Go Report Card

go-rsi

The Relative Strength Index (RSI) technical analysis algorithm implemented in Golang.

import "github.com/MicahParks/go-rsi/v2"

Usage

For full examples, please see the examples directory.

Preconditions

  1. Gather test data.
  2. Decide on the number of periods, p, for the RSI algorithm. Populate a slice of prices whose length is p + 1.
// Determine the number of periods for the initial inputs. Defaults to 14.
const initialLength = rsi.DefaultPeriods + 1
initial := prices[:initialLength]

Step 1

Create the RSI data structure and get the first result.

r, result := rsi.New(initial)

Step 2

Use the remaining data to calculate the RSI value for that period.

remaining := prices[initialLength:]
for i, next := range remaining {
	result = r.Calculate(next)
}

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-rsi/v2
cpu: Intel(R) Core(TM) i5-9600K CPU @ 3.70GHz
BenchmarkBigRSI_Calculate-6     1000000000               0.0001744 ns/op
BenchmarkRSI_Calculate-6        1000000000               0.0000017 ns/op
PASS
ok      github.com/MicahParks/go-rsi/v2    0.005s

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

Examples

Constants

View Source
const DefaultPeriods = 14

DefaultPeriods is the default number of periods for the averages for the RSI algorithm.

Variables

This section is empty.

Functions

This section is empty.

Types

type BigRSI

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

BigRSI represents the state of a Relative Strength Index (RSI) algorithm.

func NewBig

func NewBig(initial []*big.Float) (r *BigRSI, result *big.Float)

NewBig creates a new RSI data structure and returns the initial result.

The length of the initial input slice should be 1 + `periods`. Where `periods` is the length of the lookback period.

func (*BigRSI) Calculate

func (r *BigRSI) Calculate(next *big.Float) (result *big.Float)

Calculate produces the next RSI result given the next input.

type RSI

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

RSI represents the state of a Relative Strength Index (RSI) algorithm.

func New

func New(initial []float64) (r *RSI, result float64)

New creates a new RSI data structure and returns the initial result.

The length of the initial input slice should be 1 + `periods`. Where `periods` is the length of the lookback period.

func (*RSI) Calculate

func (r *RSI) Calculate(next float64) (result float64)

Calculate produces the next RSI result given the next input.

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

// Create the RSI data structure and get the first result.
//
// The slice argument should be the number of periods
const initialLength = rsi.DefaultPeriods + 1
r, result := rsi.New(prices[:initialLength])
logger.Printf("Period index: %d\nFirst RSI result: %.8f", rsi.DefaultPeriods, result)

// Use the remaining data to generate the RSI for each period.
for _, next := range prices[initialLength:] {
	result = r.Calculate(next)
}
logger.Printf("Period index: %d\nLast RSI result: %.8f", len(prices)-1, result)
Output:

Period index: 14
First RSI result: 72.36421725
Period index: 166
Last RSI result: 37.34374224

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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