Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ _testmain.go
.DS_Store
.vscode
.python-version
ta-lib-0.4.0-src.tar.gz
ta-lib/**
ta-lib/
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/markcheno/go-talib
module github.com/nenjotsu/go-talib

go 1.23.2

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 2 additions & 0 deletions lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Page

3 changes: 3 additions & 0 deletions lib/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Table of contents

* [Page](README.md)
186 changes: 186 additions & 0 deletions talib.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Licensed under terms of MIT license (see LICENSE)
package talib

import (
"errors"
"math"
)

Expand Down Expand Up @@ -1177,6 +1178,66 @@ func Sma(inReal []float64, inTimePeriod int) []float64 {
return outReal
}

// SMI - Stochastic Momentum Index
// https://analyzingalpha.com/stochastic-momentum-index
func Smi(prices []float64, period int, kPeriod int, dPeriod int, emaPeriod int) ([]float64, []float64) {
if len(prices) < period {
return []float64{}, []float64{}
}

smiValues := make([]float64, len(prices)-period+1)

for i := period - 1; i < len(prices); i++ {
// Calculate the lowest low and highest high over the period
lowPeriod := prices[i-period+1 : i+1]
lowestLow := Min(lowPeriod, period)
highestHigh := Max(lowPeriod, period)

// RawK calculation
rawK := ((prices[i] - lowestLow[i]) / (highestHigh[i] - lowestLow[i])) * 100

// Calculate the smoothed %K using SMA
kSmoothed := Sma([]float64{rawK}, kPeriod)

// Calculate double-smoothed %K (another SMA)
dSmoothed := Sma(kSmoothed, dPeriod)

// Calculate SMI
doubleSmoothedRange := (highestHigh[i] - lowestLow[i]) / 2.0
if doubleSmoothedRange != 0 {
smiValues[i-period+1] = (dSmoothed[0] / doubleSmoothedRange) * 100
} else {
smiValues[i-period+1] = 0
}
}

// Calculate EMA using the provided emaPeriod
emaValues := Ema(prices, emaPeriod)

// Return both SMI values and EMA values
return smiValues, emaValues
}

// Function to check if the SMI is overbought (above 40) within the lookback period
func IsSmiOverbought(smiValues []float64, lookback int) bool {
for i := len(smiValues) - lookback; i < len(smiValues); i++ {
if smiValues[i] > 40 {
return true
}
}
return false
}

// Function to check if the SMI is oversold (below -40) within the lookback period
func IsSmiOversold(smiValues []float64, lookback int) bool {
for i := len(smiValues) - lookback; i < len(smiValues); i++ {
if smiValues[i] < -40 {
return true
}
}
return false
}

// T3 - Triple Exponential Moving Average (T3) (lookback=6*inTimePeriod)
func T3(inReal []float64, inTimePeriod int, inVFactor float64) []float64 {

Expand Down Expand Up @@ -5862,3 +5923,128 @@ func Sum(inReal []float64, inTimePeriod int) []float64 {

return outReal
}

// HeikinashiCandles - from candle values extracts heikinashi candle values.
//
// Returns highs, opens, closes and lows of the heikinashi candles (in this order).
//
// NOTE: The number of Heikin-Ashi candles will always be one less than the number of provided candles, due to the fact
// that a previous candle is necessary to calculate the Heikin-Ashi candle, therefore the first provided candle is not considered
// as "current candle" in the algorithm, but only as "previous candle".
func HeikinashiCandles(highs []float64, opens []float64, closes []float64, lows []float64) ([]float64, []float64, []float64, []float64) {
N := len(highs)

heikinHighs := make([]float64, N)
heikinOpens := make([]float64, N)
heikinCloses := make([]float64, N)
heikinLows := make([]float64, N)

for currentCandle := 1; currentCandle < N; currentCandle++ {
previousCandle := currentCandle - 1

heikinHighs[currentCandle] = math.Max(highs[currentCandle], math.Max(opens[currentCandle], closes[currentCandle]))
heikinOpens[currentCandle] = (opens[previousCandle] + closes[previousCandle]) / 2
heikinCloses[currentCandle] = (highs[currentCandle] + opens[currentCandle] + closes[currentCandle] + lows[currentCandle]) / 4
heikinLows[currentCandle] = math.Min(highs[currentCandle], math.Min(opens[currentCandle], closes[currentCandle]))
}

return heikinHighs, heikinOpens, heikinCloses, heikinLows
}

// Hlc3 returns the Hlc3 values
//
// NOTE: Every Hlc item is defined as follows : (high + low + close) / 3
// It is used as AvgPrice candle.
func Hlc3(highs []float64, lows []float64, closes []float64) []float64 {
N := len(highs)
result := make([]float64, N)
for i := range highs {
result[i] = (highs[i] + lows[i] + closes[i]) / 3
}

return result
}

// Crossover returns true if series1 is crossing over series2.
//
// NOTE: Usually this is used with Media Average Series to check if it crosses for buy signals.
// It assumes first values are the most recent.
// The crossover function does not use most recent value, since usually it's not a complete candle.
// The second recent values and the previous are used, instead.
func Crossover(series1 []float64, series2 []float64) bool {
if len(series1) < 3 || len(series2) < 3 {
return false
}

N := len(series1)

return series1[N-2] <= series2[N-2] && series1[N-1] > series2[N-1]
}

// Crossunder returns true if series1 is crossing under series2.
//
// NOTE: Usually this is used with Media Average Series to check if it crosses for sell signals.
func Crossunder(series1 []float64, series2 []float64) bool {
if len(series1) < 3 || len(series2) < 3 {
return false
}

N := len(series1)

return series1[N-1] <= series2[N-1] && series1[N-2] > series2[N-2]
}

// GroupCandles groups a set of candles in another set of candles, basing on a grouping factor.
//
// This is pretty useful if you want to transform, for example, 15min candles into 1h candles using same data.
//
// This avoid calling multiple times the exchange for multiple contexts.
//
// Example:
//
// To transform 15 minute candles in 30 minutes candles you have a grouping factor = 2
//
// To transform 15 minute candles in 1 hour candles you have a grouping factor = 4
//
// To transform 30 minute candles in 1 hour candles you have a grouping factor = 2
func GroupCandles(highs []float64, opens []float64, closes []float64, lows []float64, groupingFactor int) ([]float64, []float64, []float64, []float64, error) {
N := len(highs)
if groupingFactor == 0 {
return nil, nil, nil, nil, errors.New("Grouping factor must be > 0")
} else if groupingFactor == 1 {
return highs, opens, closes, lows, nil // no need to group in this case, return the parameters.
}
if N%groupingFactor > 0 {
return nil, nil, nil, nil, errors.New("Cannot group properly, need a groupingFactor which is a factor of the number of candles")
}

groupedN := N / groupingFactor

groupedHighs := make([]float64, groupedN)
groupedOpens := make([]float64, groupedN)
groupedCloses := make([]float64, groupedN)
groupedLows := make([]float64, groupedN)

lastOfCurrentGroup := groupingFactor - 1

k := 0
for i := 0; i < N; i += groupingFactor { // scan all param candles
groupedOpens[k] = opens[i]
groupedCloses[k] = closes[i+lastOfCurrentGroup]

groupedHighs[k] = highs[i]
groupedLows[k] = lows[i]

endOfCurrentGroup := i + lastOfCurrentGroup
for j := i + 1; j <= endOfCurrentGroup; j++ { // group high lows candles here
if lows[j] < groupedLows[k] {
groupedLows[k] = lows[j]
}
if highs[j] > groupedHighs[k] {
groupedHighs[k] = highs[j]
}
}
k++
}
return groupedHighs, groupedOpens, groupedCloses, groupedLows, nil
}
Loading