mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
* Bump CI versions * Specifically set go version as 1.17.x bumps it to 1.18 * Another * Adjust AppVeyor * Part 1 of linter issues * Part 2 * Fix various linters and improvements * Part 3 * Finishing touches * Tests and EqualFold * Fix nitterinos plus bonus requester jobs bump for exchanges with large number of tests * Fix nitterinos and bump golangci-lint timeout for AppVeyor * Address nits, ensure all books are returned on err due to syncer regression * Fix the wiggins * Fix duplication * Fix nitterinos
82 lines
2.0 KiB
Go
82 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"
|
|
)
|
|
|
|
// SMAModule simple moving average indicator commands
|
|
var SMAModule = map[string]objects.Object{
|
|
"calculate": &objects.UserFunction{Name: "calculate", Value: sma},
|
|
}
|
|
|
|
// SimpleMovingAverage is the string constant
|
|
const SimpleMovingAverage = "Simple Moving Average"
|
|
|
|
// SMA defines a custom Simple Moving Average indicator tengo object type
|
|
type SMA struct {
|
|
objects.Array
|
|
Period int
|
|
}
|
|
|
|
// TypeName returns the name of the custom type.
|
|
func (o *SMA) TypeName() string {
|
|
return SimpleMovingAverage
|
|
}
|
|
|
|
func sma(args ...objects.Object) (objects.Object, error) {
|
|
if len(args) != 2 {
|
|
return nil, objects.ErrWrongNumArguments
|
|
}
|
|
|
|
r := new(SMA)
|
|
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)
|
|
}
|
|
|
|
ohlcvClose := make([]float64, len(ohlcvInputData))
|
|
var allErrors []string
|
|
for x := range ohlcvInputData {
|
|
t, ok := ohlcvInputData[x].([]interface{})
|
|
if !ok {
|
|
return nil, errors.New("unable to type assert ohlcvInputData")
|
|
}
|
|
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 {
|
|
allErrors = append(allErrors, fmt.Sprintf(modules.ErrParameterConvertFailed, inTimePeriod))
|
|
}
|
|
|
|
if len(allErrors) > 0 {
|
|
return nil, errors.New(strings.Join(allErrors, ", "))
|
|
}
|
|
r.Period = inTimePeriod
|
|
ret := indicators.SMA(ohlcvClose, inTimePeriod)
|
|
for x := range ret {
|
|
r.Value = append(r.Value, &objects.Float{Value: ret[x]})
|
|
}
|
|
|
|
return r, nil
|
|
}
|