rsi

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2021 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.

Usage

For full examples, please see the examples directory.

Preconditions

  1. Gather test data.
  2. Decide on the number of periods for the averages for the RSI algorithm.
// Gather some test data.
//
// For production systems, it'd be best to gather test data asynchronously.
avgGains, avgLosses, _ := testData()

// Determine the number of periods for the initial inputs. Defaults to 14.
periods := rsi.DefaultPeriods

Step 1

Create the initial input. This is the average of the gains and losses over a given number of periods.

// Average the gains and losses over the given period.
avgGain := avg(avgGains[0:periods])
avgLoss := avg(avgLosses[0:periods])
initialInput := rsi.Input{
	AverageGain: avgGain,
	AverageLoss: avgLoss,
}

Step 2

Create the RSI data structure and get the first result.

// Create the RSI data structure and get the first result.
//
// If the first argument, the initial periods is 0, the default value, 14, will be used.
r, result := rsi.New(uint(periods), initialInput)

Step 3

Use the remaining data per period to calculate the RSI result at that period.

// Use the remaining data to generate the RSI for each period.
for i := periods; i < len(avgGains); i++ {
	avgGain = avgGains[i]
	avgLoss = avgLosses[i]
	result = r.Calculate(rsi.Input{
		AverageGain: avgGain,
		AverageLoss: avgLoss,
	})
}

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

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 BigInput

type BigInput struct {
	AverageGain *big.Float
	AverageLoss *big.Float
}

BigInput represents the averages for a period that are inputted into the RSI algorithm.

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(periods uint, initial BigInput) (r *BigRSI, result *big.Float)

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

func (*BigRSI) Calculate

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

Calculate produces the next RSI result given the next input.

type Input

type Input struct {
	AverageGain float64
	AverageLoss float64
}

Input represents the averages for a period that are inputted into the RSI algorithm.

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(periods uint, initial Input) (r *RSI, result float64)

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

func (*RSI) Calculate

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

Calculate produces the next RSI result given the next input.

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

// Gather some data.
//
// For production systems, it'd be best to gather test data asynchronously.
avgGains, avgLosses = avgGains, avgLosses

// Determine the number of periods for the initial inputs. Defaults to 14.
periods := rsi.DefaultPeriods

// Average the gains and losses over the given period.
avgGain := avg(avgGains[0:periods])
avgLoss := avg(avgLosses[0:periods])
initialInput := rsi.Input{
	AverageGain: avgGain,
	AverageLoss: avgLoss,
}

// Create the RSI data structure and get the first result.
//
// If the first argument, the initial periods is 0, the default value, 14, will be used.
r, result := rsi.New(uint(periods), initialInput)
logger.Printf("Period index: %d\nAverage gain: %.2f\nAverage loss: %.2f\nRSI: %.2f", periods-1, avgGain, avgLoss, result)

// Use the remaining data to generate the RSI for each period.
for i := periods; i < len(avgGains); i++ {
	avgGain = avgGains[i]
	avgLoss = avgLosses[i]
	result = r.Calculate(rsi.Input{
		AverageGain: avgGain,
		AverageLoss: avgLoss,
	})
}
logger.Printf("Period index: %d\nAverage gain: %.2f\nAverage loss: %.2f\nRSI: %.2f", len(avgGains)-1, avgGain, avgLoss, result)
Output:

Period index: 13
Average gain: -0.11
Average loss: -0.11
RSI: 50.37
Period index: 99
Average gain: 0.35
Average loss: 0.00
RSI: 271.62

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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