mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
* modernise: Run new gopls modernise tool against codebase
* Address shazbert's nits
* apichecker, gctcli: Simplify HTML scraping functions and improve depth limit handling
* refactor: Create minSyncInterval const and update order book limit handling for binance and binanceUS
* refactor: Various slice usage improvements and rename TODO
* tranches: Revert deleteByID changes due to performance decrease
Shazbert was a F1 driver in a past lifetime 🏎️
* tranches: Simply retrieve copy
Thanks to shazbert
* documentation: Sort contributors list by contributions
* tranches: Remove deadcode in deleteByID
84 lines
2.0 KiB
Go
84 lines
2.0 KiB
Go
package indicators
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
objects "github.com/d5/tengo/v2"
|
|
"github.com/thrasher-corp/gct-ta/indicators"
|
|
"github.com/thrasher-corp/gocryptotrader/gctscript/modules"
|
|
"github.com/thrasher-corp/gocryptotrader/gctscript/wrappers/validator"
|
|
)
|
|
|
|
// RsiModule relative strength index indicator commands
|
|
var RsiModule = map[string]objects.Object{
|
|
"calculate": &objects.UserFunction{Name: "calculate", Value: rsi},
|
|
}
|
|
|
|
// RelativeStrengthIndex is the string constant
|
|
const RelativeStrengthIndex = "Relative Strength Index"
|
|
|
|
// RSI defines a custom Relative Strength Index indicator tengo object type
|
|
type RSI struct {
|
|
objects.Array
|
|
Period int
|
|
}
|
|
|
|
// TypeName returns the name of the custom type.
|
|
func (o *RSI) TypeName() string {
|
|
return RelativeStrengthIndex
|
|
}
|
|
|
|
func rsi(args ...objects.Object) (objects.Object, error) {
|
|
if len(args) != 2 {
|
|
return nil, objects.ErrWrongNumArguments
|
|
}
|
|
|
|
r := new(RSI)
|
|
if validator.IsTestExecution.Load() == true {
|
|
return r, nil
|
|
}
|
|
|
|
ohlcvInput := objects.ToInterface(args[0])
|
|
ohlcvInputData, valid := ohlcvInput.([]any)
|
|
if !valid {
|
|
return nil, fmt.Errorf(modules.ErrParameterConvertFailed, OHLCV)
|
|
}
|
|
|
|
ohlcvClose := make([]float64, len(ohlcvInputData))
|
|
var allErrors []string
|
|
for x := range ohlcvInputData {
|
|
t, ok := ohlcvInputData[x].([]any)
|
|
if !ok {
|
|
return nil, errors.New("ohlcvInputData type assert failed")
|
|
}
|
|
if len(t) < 5 {
|
|
return nil, errors.New("ohlcvInputData invalid data length")
|
|
}
|
|
|
|
value, err := toFloat64(t[4])
|
|
if err != nil {
|
|
allErrors = append(allErrors, err.Error())
|
|
}
|
|
ohlcvClose[x] = value
|
|
}
|
|
|
|
inTimePeriod, ok := objects.ToInt(args[1])
|
|
if !ok {
|
|
return nil, fmt.Errorf(modules.ErrParameterConvertFailed, inTimePeriod)
|
|
}
|
|
|
|
if len(allErrors) > 0 {
|
|
return nil, errors.New(strings.Join(allErrors, ", "))
|
|
}
|
|
|
|
r.Period = inTimePeriod
|
|
ret := indicators.RSI(ohlcvClose, inTimePeriod)
|
|
for x := range ret {
|
|
r.Value = append(r.Value, &objects.Float{Value: ret[x]})
|
|
}
|
|
|
|
return r, nil
|
|
}
|