Files
gocryptotrader/gctscript/modules/ta/indicators/rsi.go
Ryan O'Hara-Reid 0adf39de35 Update to GCT script library (#496)
* * Adds script link to GCT logger package
* Adds ability to save data as csv via script

* addr nits

* go mod tidy

* add glorious suggestion

* rm unused function

* fix linter issues

* clean up some issues

* Add in configuration fields to object for reflection to the csv file

* RM line :D

* address nits

* update to check for target already being set and add more test coverage

* force usage of .csv file extention && append date to client filename as to not overwrite file if collision occurs

* fix whoopsie

* linter issues

* purge getter methods

* Added glorious suggestion

* go mod tidy after merge

* niterinos
2020-05-14 11:05:46 +10:00

79 lines
1.8 KiB
Go

package indicators
import (
"errors"
"fmt"
"math"
"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.([]interface{})
if !valid {
return nil, fmt.Errorf(modules.ErrParameterConvertFailed, OHLCV)
}
var ohlcvClose []float64
var allErrors []string
for x := range ohlcvInputData {
t := ohlcvInputData[x].([]interface{})
value, err := toFloat64(t[4])
if err != nil {
allErrors = append(allErrors, err.Error())
}
ohlcvClose = append(ohlcvClose, 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: math.Round(ret[x]*100) / 100})
}
return r, nil
}