mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-04 23:16:54 +00:00
(GCTScript) Add technical analysis support via script bindings (#467)
* added mfi and example * renamed to moving average * converted to array return type and added obv and mfi * started work on test coverage * test coverage added for rsi & mfi * test coverage added for all indicators removed go mod replace moved to append helper method * moved all indicators to new appendTo and increased test coverage * added additional test and bumped go-talib to latest commi * go.mod update * linter fixes * go mod clean up * small fixes * reverted changes from previous attempt to rework as data is still incorrect now passing full OHLCV data back to script binding * testing new structure of passing full ohlcv data * started linking ohlcv to gctscript * OHCLV link up completed reworking passing back to indicators started * OHCLV link up completed reworking passing back to indicators started * added test coverage for tofloat * linter fixes (gofmt) * removed unused value * improved test coverage * added correct detection for 1w added ParseInterval test coverage moved OHCLV string to const * removed unused value * first round of changes addressed * all indicators have been split with packages named after each indicator and a new calculate() method added * linters * fixed tests * added check to check ta is running in validator for uploading * Added test data for OHLCV testing new indicator interface for wrapper * typed const to float64 * reworked validator data to generate previous timestamps * rewored macd to return slice of array * adding bbands linking and example * why didn't this pick it up before :D * bumped up total number of modules for test * moved parseIndicator to exchange added comments * test coverage added for ParseMAType & ParseIndicatorSelector * gofmt * WIP changes * updated tests for bbands & obv bumped to latest go-talib * move multiple use strong to const * reverted rpc.pb.go to master * added 4w option * removed selector from obv as unneeded * improved test coverage and reworked all indicator methods on how they pass errors back * order incoming OHCLV data * revert go.mod * removed verbose toggles * added spot asset type * removed 4w as its unused/uncommon * renamed * reworked further tests * converted all examples to use coinbasepro for consistency * updated all date ranges to 2019 + 6 months * backported binance OHLCV wrapper from #479 * removed o * rounded numbers * chnage requests addressed and attempt to fix MACD... today has been really unproctive code wise :D * Migrated to gct-ta library * Corrected test import * wording changes on test * removed TA lib from go.mod * PR changes addressed Removed parallel running from tests due to slight possibility in very extreme cases TestExecution might not be set to the expected value and will cause lower test coverage * removed pkg folder * bumped gct-ta version * gct-ta version bump
This commit is contained in:
@@ -3,8 +3,10 @@ package gct
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
objects "github.com/d5/tengo/v2"
|
||||
"github.com/thrasher-corp/gocryptotrader/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
@@ -24,6 +26,7 @@ var exchangeModule = map[string]objects.Object{
|
||||
"ordersubmit": &objects.UserFunction{Name: "ordersubmit", Value: ExchangeOrderSubmit},
|
||||
"withdrawcrypto": &objects.UserFunction{Name: "withdrawcrypto", Value: ExchangeWithdrawCrypto},
|
||||
"withdrawfiat": &objects.UserFunction{Name: "withdrawfiat", Value: ExchangeWithdrawFiat},
|
||||
"ohlcv": &objects.UserFunction{Name: "ohlcv", Value: exchangeOHLCV},
|
||||
}
|
||||
|
||||
// ExchangeOrderbook returns orderbook for requested exchange & currencypair
|
||||
@@ -494,3 +497,93 @@ func ExchangeWithdrawFiat(args ...objects.Object) (objects.Object, error) {
|
||||
|
||||
return &objects.String{Value: rtn}, nil
|
||||
}
|
||||
|
||||
func exchangeOHLCV(args ...objects.Object) (objects.Object, error) {
|
||||
if len(args) != 7 {
|
||||
return nil, objects.ErrWrongNumArguments
|
||||
}
|
||||
|
||||
exchangeName, ok := objects.ToString(args[0])
|
||||
if !ok {
|
||||
return nil, fmt.Errorf(ErrParameterConvertFailed, exchangeName)
|
||||
}
|
||||
currencyPair, ok := objects.ToString(args[1])
|
||||
if !ok {
|
||||
return nil, fmt.Errorf(ErrParameterConvertFailed, currencyPair)
|
||||
}
|
||||
delimiter, ok := objects.ToString(args[2])
|
||||
if !ok {
|
||||
return nil, fmt.Errorf(ErrParameterConvertFailed, delimiter)
|
||||
}
|
||||
assetTypeParam, ok := objects.ToString(args[3])
|
||||
if !ok {
|
||||
return nil, fmt.Errorf(ErrParameterConvertFailed, assetTypeParam)
|
||||
}
|
||||
|
||||
startTime, ok := objects.ToTime(args[4])
|
||||
if !ok {
|
||||
return nil, fmt.Errorf(ErrParameterConvertFailed, startTime)
|
||||
}
|
||||
|
||||
endTime, ok := objects.ToTime(args[5])
|
||||
if !ok {
|
||||
return nil, fmt.Errorf(ErrParameterConvertFailed, endTime)
|
||||
}
|
||||
|
||||
intervalStr, ok := objects.ToString(args[6])
|
||||
if !ok {
|
||||
return nil, fmt.Errorf(ErrParameterConvertFailed, endTime)
|
||||
}
|
||||
interval, err := parseInterval(intervalStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pairs := currency.NewPairDelimiter(currencyPair, delimiter)
|
||||
assetType := asset.Item(assetTypeParam)
|
||||
|
||||
ret, err := wrappers.GetWrapper().OHLCV(exchangeName, pairs, assetType, startTime, endTime, interval)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var candles objects.Array
|
||||
for x := range ret.Candles {
|
||||
candle := &objects.Array{}
|
||||
candle.Value = append(candle.Value, &objects.Time{Value: ret.Candles[x].Time},
|
||||
&objects.Float{Value: ret.Candles[x].Open},
|
||||
&objects.Float{Value: ret.Candles[x].High},
|
||||
&objects.Float{Value: ret.Candles[x].Low},
|
||||
&objects.Float{Value: ret.Candles[x].Close},
|
||||
&objects.Float{Value: ret.Candles[x].Volume},
|
||||
)
|
||||
|
||||
candles.Value = append(candles.Value, candle)
|
||||
}
|
||||
|
||||
retValue := make(map[string]objects.Object, 5)
|
||||
retValue["exchange"] = &objects.String{Value: ret.Exchange}
|
||||
retValue["pair"] = &objects.String{Value: ret.Pair.String()}
|
||||
retValue["asset"] = &objects.String{Value: ret.Asset.String()}
|
||||
retValue["intervals"] = &objects.String{Value: ret.Interval.String()}
|
||||
retValue["candles"] = &candles
|
||||
|
||||
return &objects.Map{
|
||||
Value: retValue,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// parseInterval will parse the interval param of indictors that have them and convert to time.Duration
|
||||
func parseInterval(in string) (time.Duration, error) {
|
||||
if !common.StringDataContainsInsensitive(supportedDurations, in) {
|
||||
return time.Nanosecond, errInvalidInterval
|
||||
}
|
||||
switch in {
|
||||
case "1d":
|
||||
in = "24h"
|
||||
case "3d":
|
||||
in = "72h"
|
||||
case "1w":
|
||||
in = "168h"
|
||||
}
|
||||
return time.ParseDuration(in)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
objects "github.com/d5/tengo/v2"
|
||||
"github.com/thrasher-corp/gocryptotrader/gctscript/modules"
|
||||
@@ -268,3 +269,44 @@ func TestExchangeWithdrawFiat(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseInterval(t *testing.T) {
|
||||
v, err := parseInterval("1h")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v != time.Hour {
|
||||
t.Fatalf("unexpected value return expected %v received %v", time.Hour, v)
|
||||
}
|
||||
|
||||
v, err = parseInterval("1d")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v != time.Hour*24 {
|
||||
t.Fatalf("unexpected value return expected %v received %v", time.Hour*24, v)
|
||||
}
|
||||
|
||||
v, err = parseInterval("3d")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v != time.Hour*72 {
|
||||
t.Fatalf("unexpected value return expected %v received %v", time.Hour*72, v)
|
||||
}
|
||||
|
||||
v, err = parseInterval("1w")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v != time.Hour*168 {
|
||||
t.Fatalf("unexpected value return expected %v received %v", time.Hour*168, v)
|
||||
}
|
||||
|
||||
_, err = parseInterval("6m")
|
||||
if err != nil {
|
||||
if !errors.Is(err, errInvalidInterval) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package gct
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/d5/tengo/v2"
|
||||
)
|
||||
|
||||
@@ -9,6 +11,9 @@ const (
|
||||
ErrParameterConvertFailed = "%v failed conversion"
|
||||
)
|
||||
|
||||
var errInvalidInterval = errors.New("invalid interval")
|
||||
var supportedDurations = []string{"1m", "3m", "5m", "15m", "30m", "1h", "2h", "4h", "6h", "12h", "24h", "1d", "3d", "1w"}
|
||||
|
||||
// Modules map of all loadable modules
|
||||
var Modules = map[string]map[string]tengo.Object{
|
||||
"exchange": exchangeModule,
|
||||
|
||||
@@ -3,8 +3,8 @@ package loader
|
||||
import (
|
||||
"github.com/d5/tengo/v2"
|
||||
"github.com/d5/tengo/v2/stdlib"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/gctscript/modules/gct"
|
||||
"github.com/thrasher-corp/gocryptotrader/gctscript/modules/ta"
|
||||
)
|
||||
|
||||
// GetModuleMap returns the module map that includes all modules
|
||||
@@ -19,6 +19,13 @@ func GetModuleMap() *tengo.ModuleMap {
|
||||
}
|
||||
}
|
||||
|
||||
taModuleList := ta.AllModuleNames()
|
||||
for _, name := range taModuleList {
|
||||
if mod := ta.Modules[name]; mod != nil {
|
||||
modules.AddBuiltinModule(name, mod)
|
||||
}
|
||||
}
|
||||
|
||||
stdLib := stdlib.AllModuleNames()
|
||||
for _, name := range stdLib {
|
||||
if mod := stdlib.BuiltinModules[name]; mod != nil {
|
||||
|
||||
80
gctscript/modules/ta/indicators/atr.go
Normal file
80
gctscript/modules/ta/indicators/atr.go
Normal file
@@ -0,0 +1,80 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// AtrModule range indicator commands
|
||||
var AtrModule = map[string]objects.Object{
|
||||
"calculate": &objects.UserFunction{Name: "calculate", Value: atr},
|
||||
}
|
||||
|
||||
func atr(args ...objects.Object) (objects.Object, error) {
|
||||
if len(args) != 2 {
|
||||
return nil, objects.ErrWrongNumArguments
|
||||
}
|
||||
|
||||
r := &objects.Array{}
|
||||
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)
|
||||
}
|
||||
|
||||
ohlcvData := make([][]float64, 6)
|
||||
var allErrors []string
|
||||
for x := range ohlcvInputData {
|
||||
t := ohlcvInputData[x].([]interface{})
|
||||
value, err := toFloat64(t[2])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[2] = append(ohlcvData[2], value)
|
||||
|
||||
value, err = toFloat64(t[3])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[3] = append(ohlcvData[3], value)
|
||||
|
||||
value, err = toFloat64(t[4])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[4] = append(ohlcvData[4], value)
|
||||
|
||||
value, err = toFloat64(t[5])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[5] = append(ohlcvData[5], 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, ", "))
|
||||
}
|
||||
|
||||
ret := indicators.ATR(ohlcvData[2], ohlcvData[3], ohlcvData[4], inTimePeriod)
|
||||
for x := range ret {
|
||||
r.Value = append(r.Value, &objects.Float{Value: math.Round(ret[x]*100) / 100})
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
118
gctscript/modules/ta/indicators/bbands.go
Normal file
118
gctscript/modules/ta/indicators/bbands.go
Normal file
@@ -0,0 +1,118 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// BBandsModule bollinger bands indicator commands
|
||||
var BBandsModule = map[string]objects.Object{
|
||||
"calculate": &objects.UserFunction{Name: "calculate", Value: bbands},
|
||||
}
|
||||
|
||||
func bbands(args ...objects.Object) (objects.Object, error) {
|
||||
if len(args) != 6 {
|
||||
return nil, objects.ErrWrongNumArguments
|
||||
}
|
||||
|
||||
var ret objects.Array
|
||||
if validator.IsTestExecution.Load() == true {
|
||||
return &ret, nil
|
||||
}
|
||||
|
||||
ohlcIndicatorType, ok := objects.ToString(args[0])
|
||||
if !ok {
|
||||
return nil, fmt.Errorf(modules.ErrParameterConvertFailed, ohlcIndicatorType)
|
||||
}
|
||||
|
||||
selector, errIndSelector := ParseIndicatorSelector(ohlcIndicatorType)
|
||||
if errIndSelector != nil {
|
||||
return nil, errIndSelector
|
||||
}
|
||||
|
||||
ohlcvInput := objects.ToInterface(args[1])
|
||||
ohlcvInputData, valid := ohlcvInput.([]interface{})
|
||||
if !valid {
|
||||
return nil, fmt.Errorf(modules.ErrParameterConvertFailed, OHLCV)
|
||||
}
|
||||
|
||||
ohlcvData := make([][]float64, 6)
|
||||
var allErrors []string
|
||||
for x := range ohlcvInputData {
|
||||
t := ohlcvInputData[x].([]interface{})
|
||||
value, err := toFloat64(t[2])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[2] = append(ohlcvData[2], value)
|
||||
|
||||
value, err = toFloat64(t[3])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[3] = append(ohlcvData[3], value)
|
||||
|
||||
value, err = toFloat64(t[4])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[4] = append(ohlcvData[4], value)
|
||||
|
||||
value, err = toFloat64(t[5])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[5] = append(ohlcvData[5], value)
|
||||
}
|
||||
|
||||
inTimePeriod, ok := objects.ToInt(args[2])
|
||||
if !ok {
|
||||
allErrors = append(allErrors, fmt.Sprintf(modules.ErrParameterConvertFailed, inTimePeriod))
|
||||
}
|
||||
|
||||
inNbDevUp, ok := objects.ToFloat64(args[3])
|
||||
if !ok {
|
||||
allErrors = append(allErrors, fmt.Sprintf(modules.ErrParameterConvertFailed, inNbDevUp))
|
||||
}
|
||||
|
||||
inNbDevDn, ok := objects.ToFloat64(args[4])
|
||||
if !ok {
|
||||
allErrors = append(allErrors, fmt.Sprintf(modules.ErrParameterConvertFailed, inNbDevDn))
|
||||
}
|
||||
|
||||
inMAType, ok := objects.ToString(args[5])
|
||||
if !ok {
|
||||
allErrors = append(allErrors, fmt.Sprintf(modules.ErrParameterConvertFailed, inMAType))
|
||||
}
|
||||
|
||||
if len(allErrors) > 0 {
|
||||
return nil, errors.New(strings.Join(allErrors, ", "))
|
||||
}
|
||||
|
||||
MAType, err := ParseMAType(inMAType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
retUpper, retMiddle, retLower := indicators.BBANDS(ohlcvData[selector], inTimePeriod, inNbDevDn, inNbDevDn, MAType)
|
||||
for x := range retMiddle {
|
||||
temp := &objects.Array{}
|
||||
temp.Value = append(temp.Value, &objects.Float{Value: math.Round(retMiddle[x]*100) / 100})
|
||||
if retUpper != nil {
|
||||
temp.Value = append(temp.Value, &objects.Float{Value: math.Round(retUpper[x]*100) / 100})
|
||||
}
|
||||
if retLower != nil {
|
||||
temp.Value = append(temp.Value, &objects.Float{Value: math.Round(retLower[x]*100) / 100})
|
||||
}
|
||||
ret.Value = append(ret.Value, temp)
|
||||
}
|
||||
|
||||
return &ret, nil
|
||||
}
|
||||
63
gctscript/modules/ta/indicators/ema.go
Normal file
63
gctscript/modules/ta/indicators/ema.go
Normal file
@@ -0,0 +1,63 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// EMAModule EMA indicator commands
|
||||
var EMAModule = map[string]objects.Object{
|
||||
"calculate": &objects.UserFunction{Name: "calculate", Value: ema},
|
||||
}
|
||||
|
||||
func ema(args ...objects.Object) (objects.Object, error) {
|
||||
if len(args) != 2 {
|
||||
return nil, objects.ErrWrongNumArguments
|
||||
}
|
||||
|
||||
r := &objects.Array{}
|
||||
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 {
|
||||
allErrors = append(allErrors, fmt.Sprintf(modules.ErrParameterConvertFailed, inTimePeriod))
|
||||
}
|
||||
|
||||
if len(allErrors) > 0 {
|
||||
return nil, errors.New(strings.Join(allErrors, ", "))
|
||||
}
|
||||
|
||||
ret := indicators.EMA(ohlcvClose, inTimePeriod)
|
||||
for x := range ret {
|
||||
r.Value = append(r.Value, &objects.Float{Value: math.Round(ret[x]*100) / 100})
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
61
gctscript/modules/ta/indicators/indicators.go
Normal file
61
gctscript/modules/ta/indicators/indicators.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package indicators
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/thrasher-corp/gct-ta/indicators"
|
||||
"github.com/thrasher-corp/gocryptotrader/gctscript/modules"
|
||||
)
|
||||
|
||||
// OHLCV locale string for OHLCV data conversion failure
|
||||
const OHLCV = "OHLCV data"
|
||||
|
||||
var errInvalidSelector = errors.New("invalid selector")
|
||||
|
||||
func toFloat64(data interface{}) (float64, error) {
|
||||
switch d := data.(type) {
|
||||
case float64:
|
||||
return d, nil
|
||||
case int:
|
||||
return float64(d), nil
|
||||
case int32:
|
||||
return float64(d), nil
|
||||
case int64:
|
||||
return float64(d), nil
|
||||
default:
|
||||
return 0, fmt.Errorf(modules.ErrParameterConvertFailed, d)
|
||||
}
|
||||
}
|
||||
|
||||
// ParseIndicatorSelector returns indicator number from string for slice selection
|
||||
func ParseIndicatorSelector(in string) (int, error) {
|
||||
switch in {
|
||||
case "open":
|
||||
return 1, nil
|
||||
case "high":
|
||||
return 2, nil
|
||||
case "low":
|
||||
return 3, nil
|
||||
case "close":
|
||||
return 4, nil
|
||||
case "vol":
|
||||
return 5, nil
|
||||
default:
|
||||
return 0, errInvalidSelector
|
||||
}
|
||||
}
|
||||
|
||||
// ParseMAType returns moving average from sring
|
||||
func ParseMAType(in string) (indicators.MaType, error) {
|
||||
in = strings.ToLower(in)
|
||||
switch in {
|
||||
case "sma":
|
||||
return indicators.Sma, nil
|
||||
case "ema":
|
||||
return indicators.Ema, nil
|
||||
default:
|
||||
return 0, errInvalidSelector
|
||||
}
|
||||
}
|
||||
563
gctscript/modules/ta/indicators/indicators_test.go
Normal file
563
gctscript/modules/ta/indicators/indicators_test.go
Normal file
@@ -0,0 +1,563 @@
|
||||
package indicators
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
objects "github.com/d5/tengo/v2"
|
||||
"github.com/thrasher-corp/gct-ta/indicators"
|
||||
"github.com/thrasher-corp/gocryptotrader/gctscript/wrappers/validator"
|
||||
)
|
||||
|
||||
const errFailedConversion = "0 failed conversion"
|
||||
|
||||
var (
|
||||
ohlcvData = &objects.Array{}
|
||||
ohlcvDataInvalid = &objects.Array{}
|
||||
testString = "1D10TH0R53"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
for x := 0; x < 100; x++ {
|
||||
v := rand.Float64()
|
||||
candle := &objects.Array{}
|
||||
candle.Value = append(candle.Value, &objects.Time{Value: time.Now()},
|
||||
&objects.Float{Value: v},
|
||||
&objects.Float{Value: v + float64(x)},
|
||||
&objects.Float{Value: v - float64(x)},
|
||||
&objects.Float{Value: v},
|
||||
&objects.Float{Value: v},
|
||||
)
|
||||
ohlcvData.Value = append(ohlcvData.Value, candle)
|
||||
}
|
||||
|
||||
for x := 0; x < 5; x++ {
|
||||
candle := &objects.Array{}
|
||||
candle.Value = append(candle.Value, &objects.String{Value: testString},
|
||||
&objects.String{Value: testString},
|
||||
&objects.String{Value: testString},
|
||||
&objects.String{Value: testString},
|
||||
&objects.String{Value: testString},
|
||||
&objects.String{Value: testString},
|
||||
)
|
||||
ohlcvDataInvalid.Value = append(ohlcvDataInvalid.Value, candle)
|
||||
}
|
||||
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func TestMfi(t *testing.T) {
|
||||
_, err := mfi()
|
||||
if err != nil {
|
||||
if !errors.Is(err, objects.ErrWrongNumArguments) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
v := &objects.String{Value: testString}
|
||||
_, err = mfi(ohlcvData, v)
|
||||
if err != nil {
|
||||
if err.Error() != errFailedConversion {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = mfi(ohlcvDataInvalid, &objects.Int{Value: 14})
|
||||
if err == nil {
|
||||
t.Error("expected conversion failed error")
|
||||
}
|
||||
|
||||
_, err = mfi(ohlcvData, &objects.Int{Value: 14})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = mfi(v, &objects.Int{Value: 14})
|
||||
if err != nil {
|
||||
if err.Error() != "OHLCV data failed conversion" {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
validator.IsTestExecution.Store(true)
|
||||
ret, err := mfi(ohlcvData, &objects.Int{Value: 10})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if (ret == &objects.Array{}) {
|
||||
t.Error("expected empty Array on test execution received data")
|
||||
}
|
||||
validator.IsTestExecution.Store(false)
|
||||
}
|
||||
|
||||
func TestRsi(t *testing.T) {
|
||||
_, err := rsi()
|
||||
if err != nil {
|
||||
if !errors.Is(err, objects.ErrWrongNumArguments) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
v := &objects.String{Value: testString}
|
||||
_, err = rsi(ohlcvData, v)
|
||||
if err != nil {
|
||||
if err.Error() != errFailedConversion {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = rsi(ohlcvData, &objects.Int{Value: 14})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = rsi(v, &objects.Int{Value: 14})
|
||||
if err == nil {
|
||||
if err.Error() != "OHLCV data failed conversion" {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = rsi(ohlcvDataInvalid, &objects.Int{Value: 14})
|
||||
if err == nil {
|
||||
if err.Error() != "OHLCV data failed conversion" {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
validator.IsTestExecution.Store(true)
|
||||
ret, err := rsi(ohlcvData, &objects.Int{Value: 14})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if (ret == &objects.Array{}) {
|
||||
t.Error("expected empty Array on test execution received data")
|
||||
}
|
||||
validator.IsTestExecution.Store(false)
|
||||
}
|
||||
|
||||
func TestEMA(t *testing.T) {
|
||||
_, err := ema()
|
||||
if err != nil {
|
||||
if !errors.Is(err, objects.ErrWrongNumArguments) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
v := &objects.String{Value: testString}
|
||||
_, err = ema(ohlcvData, v)
|
||||
if err != nil {
|
||||
if err.Error() != errFailedConversion {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = ema(ohlcvData, &objects.Int{Value: 14})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = ema(ohlcvDataInvalid, &objects.String{Value: testString})
|
||||
if err == nil {
|
||||
t.Error("expected conversion failed error")
|
||||
}
|
||||
|
||||
_, err = ema(&objects.String{Value: testString}, &objects.String{Value: testString})
|
||||
if err == nil {
|
||||
t.Error("expected conversion failed error")
|
||||
}
|
||||
|
||||
validator.IsTestExecution.Store(true)
|
||||
ret, err := ema(ohlcvData, &objects.Int{Value: 14})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if (ret == &objects.Array{}) {
|
||||
t.Error("expected empty Array on test execution received data")
|
||||
}
|
||||
validator.IsTestExecution.Store(false)
|
||||
}
|
||||
|
||||
func TestSMA(t *testing.T) {
|
||||
_, err := sma()
|
||||
if err != nil {
|
||||
if !errors.Is(err, objects.ErrWrongNumArguments) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
v := &objects.String{Value: testString}
|
||||
_, err = sma(ohlcvData, v)
|
||||
if err != nil {
|
||||
if err.Error() != errFailedConversion {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = sma(ohlcvData, &objects.Int{Value: 14})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = sma(ohlcvDataInvalid, &objects.String{Value: testString})
|
||||
if err == nil {
|
||||
t.Error("expected conversion failed error")
|
||||
}
|
||||
|
||||
_, err = sma(&objects.String{Value: testString}, &objects.String{Value: testString})
|
||||
if err == nil {
|
||||
t.Error("expected conversion failed error")
|
||||
}
|
||||
|
||||
validator.IsTestExecution.Store(true)
|
||||
ret, err := sma(ohlcvData, &objects.Int{Value: 14})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if (ret == &objects.Array{}) {
|
||||
t.Error("expected empty Array on test execution received data")
|
||||
}
|
||||
validator.IsTestExecution.Store(false)
|
||||
}
|
||||
|
||||
func TestMACD(t *testing.T) {
|
||||
_, err := macd()
|
||||
if err != nil {
|
||||
if !errors.Is(err, objects.ErrWrongNumArguments) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
v := &objects.String{Value: testString}
|
||||
_, err = macd(ohlcvData, &objects.Int{Value: 12}, &objects.Int{Value: 26}, v)
|
||||
if err != nil {
|
||||
if err.Error() != errFailedConversion {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = macd(ohlcvData, &objects.Int{Value: 12}, &objects.Int{Value: 26}, &objects.Int{Value: 9})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = macd(ohlcvDataInvalid,
|
||||
&objects.String{Value: testString},
|
||||
&objects.String{Value: testString},
|
||||
&objects.String{Value: testString})
|
||||
if err == nil {
|
||||
t.Error("expected conversion failed error")
|
||||
}
|
||||
|
||||
_, err = macd(&objects.String{Value: testString},
|
||||
&objects.String{Value: testString},
|
||||
&objects.String{Value: testString},
|
||||
&objects.String{Value: testString})
|
||||
if err == nil {
|
||||
t.Error("expected conversion failed error")
|
||||
}
|
||||
|
||||
validator.IsTestExecution.Store(true)
|
||||
ret, err := macd(ohlcvData, &objects.Int{Value: 12}, &objects.Int{Value: 26}, &objects.Int{Value: 9})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if (ret == &objects.Array{}) {
|
||||
t.Error("expected empty Array on test execution received data")
|
||||
}
|
||||
validator.IsTestExecution.Store(false)
|
||||
}
|
||||
|
||||
func TestAtr(t *testing.T) {
|
||||
_, err := atr()
|
||||
if err != nil {
|
||||
if !errors.Is(err, objects.ErrWrongNumArguments) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
v := &objects.String{Value: testString}
|
||||
_, err = atr(ohlcvData, v)
|
||||
if err != nil {
|
||||
if err.Error() != errFailedConversion {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = atr(ohlcvData, &objects.Int{Value: 14})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = atr(v, &objects.Int{Value: 14})
|
||||
if err == nil {
|
||||
t.Error("expected conversion failed error")
|
||||
}
|
||||
|
||||
_, err = atr(ohlcvDataInvalid, &objects.Int{Value: 14})
|
||||
if err == nil {
|
||||
t.Error("expected conversion failed error")
|
||||
}
|
||||
|
||||
validator.IsTestExecution.Store(true)
|
||||
ret, err := atr(ohlcvData, &objects.Int{Value: 14})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if (ret == &objects.Array{}) {
|
||||
t.Error("expected empty Array on test execution received data")
|
||||
}
|
||||
validator.IsTestExecution.Store(false)
|
||||
}
|
||||
|
||||
func TestBbands(t *testing.T) {
|
||||
_, err := bbands()
|
||||
if err != nil {
|
||||
if !errors.Is(err, objects.ErrWrongNumArguments) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = bbands(&objects.String{Value: testString}, ohlcvData,
|
||||
&objects.Int{Value: 5},
|
||||
&objects.Float{Value: 2.0},
|
||||
&objects.Float{Value: 2.0},
|
||||
&objects.String{Value: "sma"})
|
||||
if err != nil {
|
||||
if err != errInvalidSelector {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = bbands(&objects.String{Value: "close"}, ohlcvData,
|
||||
&objects.Int{Value: 5},
|
||||
&objects.Float{Value: 2.0},
|
||||
&objects.Float{Value: 2.0},
|
||||
&objects.String{Value: "sma"})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
validator.IsTestExecution.Store(true)
|
||||
ret, err := bbands(&objects.String{Value: "close"}, ohlcvData,
|
||||
&objects.Int{Value: 5},
|
||||
&objects.Float{Value: 2.0},
|
||||
&objects.Float{Value: 2.0},
|
||||
&objects.String{Value: "sma"})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if (ret == &objects.Array{}) {
|
||||
t.Error("expected empty Array on test execution received data")
|
||||
}
|
||||
validator.IsTestExecution.Store(false)
|
||||
|
||||
_, err = bbands(&objects.String{Value: "close"}, ohlcvDataInvalid,
|
||||
&objects.String{Value: testString},
|
||||
&objects.String{Value: testString},
|
||||
&objects.String{Value: testString},
|
||||
objects.UndefinedValue)
|
||||
if err == nil {
|
||||
t.Error("expected conversion failed error")
|
||||
}
|
||||
|
||||
_, err = bbands(&objects.String{Value: "close"}, &objects.String{Value: testString},
|
||||
&objects.String{Value: testString},
|
||||
&objects.String{Value: testString},
|
||||
&objects.String{Value: testString},
|
||||
&objects.String{Value: "ema"})
|
||||
if err == nil {
|
||||
t.Error("expected conversion failed error")
|
||||
}
|
||||
|
||||
_, err = bbands(&objects.String{Value: "close"}, ohlcvData,
|
||||
&objects.Int{Value: 5},
|
||||
&objects.Float{Value: 2.0},
|
||||
&objects.Float{Value: 2.0},
|
||||
&objects.String{Value: testString})
|
||||
if err != nil {
|
||||
if !errors.Is(err, errInvalidSelector) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = bbands(objects.UndefinedValue, ohlcvData,
|
||||
&objects.Int{Value: 5},
|
||||
&objects.Float{Value: 2.0},
|
||||
&objects.Float{Value: 2.0},
|
||||
&objects.String{Value: testString})
|
||||
if err == nil {
|
||||
t.Error("expected conversion failed error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOBV(t *testing.T) {
|
||||
_, err := obv()
|
||||
if err != nil {
|
||||
if !errors.Is(err, objects.ErrWrongNumArguments) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = obv(ohlcvData)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = obv(ohlcvDataInvalid)
|
||||
if err == nil {
|
||||
t.Error("expected conversion failed error")
|
||||
}
|
||||
|
||||
_, err = obv(&objects.String{Value: testString})
|
||||
if err == nil {
|
||||
t.Error("expected conversion failed error")
|
||||
}
|
||||
|
||||
validator.IsTestExecution.Store(true)
|
||||
ret, err := obv(ohlcvData)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if (ret == &objects.Array{}) {
|
||||
t.Error("expected empty Array on test execution received data")
|
||||
}
|
||||
validator.IsTestExecution.Store(false)
|
||||
}
|
||||
|
||||
func TestToFloat64(t *testing.T) {
|
||||
value := 54.0
|
||||
v, err := toFloat64(value)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reflect.TypeOf(v).Kind() != reflect.Float64 {
|
||||
t.Fatalf("expected toFloat to return kind float64 received: %v", reflect.TypeOf(v).Kind())
|
||||
}
|
||||
|
||||
v, err = toFloat64(int(value))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reflect.TypeOf(v).Kind() != reflect.Float64 {
|
||||
t.Fatalf("expected toFloat to return kind float64 received: %v", reflect.TypeOf(v).Kind())
|
||||
}
|
||||
|
||||
v, err = toFloat64(int32(value))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reflect.TypeOf(v).Kind() != reflect.Float64 {
|
||||
t.Fatalf("expected toFloat to return kind float64 received: %v", reflect.TypeOf(v).Kind())
|
||||
}
|
||||
|
||||
v, err = toFloat64(int64(value))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reflect.TypeOf(v).Kind() != reflect.Float64 {
|
||||
t.Fatalf("expected toFloat to return kind float64 received: %v", reflect.TypeOf(v).Kind())
|
||||
}
|
||||
|
||||
_, err = toFloat64("54")
|
||||
if err == nil {
|
||||
t.Fatalf("attempting to convert a string should fail but test passed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseIndicatorSelector(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
expected int
|
||||
err error
|
||||
}{
|
||||
{
|
||||
"open",
|
||||
1,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"high",
|
||||
2,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"low",
|
||||
3,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"close",
|
||||
4,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"vol",
|
||||
5,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"invalid",
|
||||
0,
|
||||
errInvalidSelector,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tests := range testCases {
|
||||
test := tests
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
v, err := ParseIndicatorSelector(test.name)
|
||||
if err != nil {
|
||||
if err != test.err {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if v != test.expected {
|
||||
t.Fatalf("expected %v received %v", test.expected, v)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMAType(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
expected indicators.MaType
|
||||
err error
|
||||
}{
|
||||
{
|
||||
"sma",
|
||||
indicators.Sma,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"ema",
|
||||
indicators.Ema,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"no",
|
||||
indicators.Sma,
|
||||
errInvalidSelector,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tests := range testCases {
|
||||
test := tests
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
v, err := ParseMAType(test.name)
|
||||
if err != nil {
|
||||
if err != test.err {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if v != test.expected {
|
||||
t.Fatalf("expected %v received %v", test.expected, v)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
80
gctscript/modules/ta/indicators/macd.go
Normal file
80
gctscript/modules/ta/indicators/macd.go
Normal file
@@ -0,0 +1,80 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// MACDModule MACD indicator commands
|
||||
var MACDModule = map[string]objects.Object{
|
||||
"calculate": &objects.UserFunction{Name: "calculate", Value: macd},
|
||||
}
|
||||
|
||||
func macd(args ...objects.Object) (objects.Object, error) {
|
||||
if len(args) != 4 {
|
||||
return nil, objects.ErrWrongNumArguments
|
||||
}
|
||||
|
||||
r := &objects.Array{}
|
||||
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)
|
||||
}
|
||||
|
||||
inFastPeriod, ok := objects.ToInt(args[1])
|
||||
if !ok {
|
||||
allErrors = append(allErrors, fmt.Sprintf(modules.ErrParameterConvertFailed, inFastPeriod))
|
||||
}
|
||||
|
||||
inSlowPeriod, ok := objects.ToInt(args[2])
|
||||
if !ok {
|
||||
allErrors = append(allErrors, fmt.Sprintf(modules.ErrParameterConvertFailed, inSlowPeriod))
|
||||
}
|
||||
|
||||
inTimePeriod, ok := objects.ToInt(args[3])
|
||||
if !ok {
|
||||
allErrors = append(allErrors, fmt.Sprintf(modules.ErrParameterConvertFailed, inTimePeriod))
|
||||
}
|
||||
|
||||
if len(allErrors) > 0 {
|
||||
return nil, errors.New(strings.Join(allErrors, ", "))
|
||||
}
|
||||
|
||||
macd, macdSignal, macdHist := indicators.MACD(ohlcvClose, inFastPeriod, inSlowPeriod, inTimePeriod)
|
||||
for x := range macdHist {
|
||||
tempMACD := &objects.Array{}
|
||||
tempMACD.Value = append(tempMACD.Value, &objects.Float{Value: math.Round(macdHist[x]*100) / 100})
|
||||
if macd != nil {
|
||||
tempMACD.Value = append(tempMACD.Value, &objects.Float{Value: math.Round(macd[x]*100) / 100})
|
||||
}
|
||||
if macdSignal != nil {
|
||||
tempMACD.Value = append(tempMACD.Value, &objects.Float{Value: math.Round(macdSignal[x]*100) / 100})
|
||||
}
|
||||
r.Value = append(r.Value, tempMACD)
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
78
gctscript/modules/ta/indicators/mfi.go
Normal file
78
gctscript/modules/ta/indicators/mfi.go
Normal file
@@ -0,0 +1,78 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// MfiModule index indicator commands
|
||||
var MfiModule = map[string]objects.Object{
|
||||
"calculate": &objects.UserFunction{Name: "calculate", Value: mfi},
|
||||
}
|
||||
|
||||
func mfi(args ...objects.Object) (objects.Object, error) {
|
||||
if len(args) != 2 {
|
||||
return nil, objects.ErrWrongNumArguments
|
||||
}
|
||||
|
||||
r := &objects.Array{}
|
||||
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)
|
||||
}
|
||||
ohlcvData := make([][]float64, 6)
|
||||
var allErrors []string
|
||||
|
||||
for x := range ohlcvInputData {
|
||||
t := ohlcvInputData[x].([]interface{})
|
||||
value, err := toFloat64(t[2])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[2] = append(ohlcvData[2], value)
|
||||
|
||||
value, err = toFloat64(t[3])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[3] = append(ohlcvData[3], value)
|
||||
|
||||
value, err = toFloat64(t[4])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[4] = append(ohlcvData[4], value)
|
||||
|
||||
value, err = toFloat64(t[5])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[5] = append(ohlcvData[5], value)
|
||||
}
|
||||
if len(allErrors) > 0 {
|
||||
return nil, errors.New(strings.Join(allErrors, ", "))
|
||||
}
|
||||
inTimePeriod, ok := objects.ToInt(args[1])
|
||||
if !ok {
|
||||
return nil, fmt.Errorf(modules.ErrParameterConvertFailed, inTimePeriod)
|
||||
}
|
||||
|
||||
ret := indicators.MFI(ohlcvData[2], ohlcvData[3], ohlcvData[4], ohlcvData[5], inTimePeriod)
|
||||
for x := range ret {
|
||||
r.Value = append(r.Value, &objects.Float{Value: math.Round(ret[x]*100) / 100})
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
75
gctscript/modules/ta/indicators/obv.go
Normal file
75
gctscript/modules/ta/indicators/obv.go
Normal file
@@ -0,0 +1,75 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// ObvModule volume indicator commands
|
||||
var ObvModule = map[string]objects.Object{
|
||||
"calculate": &objects.UserFunction{Name: "calculate", Value: obv},
|
||||
}
|
||||
|
||||
func obv(args ...objects.Object) (objects.Object, error) {
|
||||
if len(args) != 1 {
|
||||
return nil, objects.ErrWrongNumArguments
|
||||
}
|
||||
|
||||
r := &objects.Array{}
|
||||
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)
|
||||
}
|
||||
|
||||
ohlcvData := make([][]float64, 6)
|
||||
var allErrors []string
|
||||
for x := range ohlcvInputData {
|
||||
t := ohlcvInputData[x].([]interface{})
|
||||
value, err := toFloat64(t[2])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[2] = append(ohlcvData[2], value)
|
||||
|
||||
value, err = toFloat64(t[3])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[3] = append(ohlcvData[3], value)
|
||||
|
||||
value, err = toFloat64(t[4])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[4] = append(ohlcvData[4], value)
|
||||
|
||||
value, err = toFloat64(t[5])
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, err.Error())
|
||||
}
|
||||
ohlcvData[5] = append(ohlcvData[5], value)
|
||||
}
|
||||
|
||||
if len(allErrors) > 0 {
|
||||
return nil, errors.New(strings.Join(allErrors, ", "))
|
||||
}
|
||||
|
||||
ret := indicators.OBV(ohlcvData[4], ohlcvData[5])
|
||||
for x := range ret {
|
||||
temp := &objects.Float{Value: math.Round(ret[x]*100) / 100}
|
||||
r.Value = append(r.Value, temp)
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
63
gctscript/modules/ta/indicators/rsi.go
Normal file
63
gctscript/modules/ta/indicators/rsi.go
Normal file
@@ -0,0 +1,63 @@
|
||||
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},
|
||||
}
|
||||
|
||||
func rsi(args ...objects.Object) (objects.Object, error) {
|
||||
if len(args) != 2 {
|
||||
return nil, objects.ErrWrongNumArguments
|
||||
}
|
||||
|
||||
r := &objects.Array{}
|
||||
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, ", "))
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
61
gctscript/modules/ta/indicators/sma.go
Normal file
61
gctscript/modules/ta/indicators/sma.go
Normal file
@@ -0,0 +1,61 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// SMAModule simple moving average indicator commands
|
||||
var SMAModule = map[string]objects.Object{
|
||||
"calculate": &objects.UserFunction{Name: "calculate", Value: sma},
|
||||
}
|
||||
|
||||
func sma(args ...objects.Object) (objects.Object, error) {
|
||||
if len(args) != 2 {
|
||||
return nil, objects.ErrWrongNumArguments
|
||||
}
|
||||
|
||||
r := &objects.Array{}
|
||||
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 {
|
||||
allErrors = append(allErrors, fmt.Sprintf(modules.ErrParameterConvertFailed, inTimePeriod))
|
||||
}
|
||||
|
||||
if len(allErrors) > 0 {
|
||||
return nil, errors.New(strings.Join(allErrors, ", "))
|
||||
}
|
||||
ret := indicators.SMA(ohlcvClose, inTimePeriod)
|
||||
for x := range ret {
|
||||
r.Value = append(r.Value, &objects.Float{Value: math.Round(ret[x]*100) / 100})
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
10
gctscript/modules/ta/ta.go
Normal file
10
gctscript/modules/ta/ta.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package ta
|
||||
|
||||
// AllModuleNames returns a list of all default module names.
|
||||
func AllModuleNames() []string {
|
||||
var names []string
|
||||
for name := range Modules {
|
||||
names = append(names, name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
17
gctscript/modules/ta/ta_test.go
Normal file
17
gctscript/modules/ta/ta_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package ta
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetModuleMap(t *testing.T) {
|
||||
x := AllModuleNames()
|
||||
xType := reflect.TypeOf(x).Kind()
|
||||
if xType != reflect.Slice {
|
||||
t.Fatalf("AllModuleNames() should return slice instead received: %v", x)
|
||||
}
|
||||
if len(x) != 8 {
|
||||
t.Fatalf("unexpected results received expected 7 received: %v", len(x))
|
||||
}
|
||||
}
|
||||
18
gctscript/modules/ta/ta_types.go
Normal file
18
gctscript/modules/ta/ta_types.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package ta
|
||||
|
||||
import (
|
||||
"github.com/d5/tengo/v2"
|
||||
"github.com/thrasher-corp/gocryptotrader/gctscript/modules/ta/indicators"
|
||||
)
|
||||
|
||||
// Modules map of all loadable modules
|
||||
var Modules = map[string]map[string]tengo.Object{
|
||||
"indicator/bbands": indicators.BBandsModule,
|
||||
"indicator/macd": indicators.MACDModule,
|
||||
"indicator/ema": indicators.EMAModule,
|
||||
"indicator/sma": indicators.SMAModule,
|
||||
"indicator/rsi": indicators.RsiModule,
|
||||
"indicator/obv": indicators.ObvModule,
|
||||
"indicator/mfi": indicators.MfiModule,
|
||||
"indicator/atr": indicators.AtrModule,
|
||||
}
|
||||
@@ -1,15 +1,24 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
|
||||
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
|
||||
)
|
||||
|
||||
const (
|
||||
// ErrParameterConvertFailed error to return when type conversion fails
|
||||
ErrParameterConvertFailed = "%v failed conversion"
|
||||
ErrParameterWithPositionConvertFailed = "%v at position %v failed conversion"
|
||||
)
|
||||
|
||||
// Wrapper instance of GCT to use for modules
|
||||
var Wrapper GCT
|
||||
|
||||
@@ -32,6 +41,7 @@ type Exchange interface {
|
||||
DepositAddress(exch string, currencyCode currency.Code) (string, error)
|
||||
WithdrawalFiatFunds(exch, bankAccountID string, request *withdraw.Request) (out string, err error)
|
||||
WithdrawalCryptoFunds(exch string, request *withdraw.Request) (out string, err error)
|
||||
OHLCV(exch string, pair currency.Pair, item asset.Item, start, end time.Time, interval time.Duration) (kline.Item, error)
|
||||
}
|
||||
|
||||
// SetModuleWrapper link the wrapper and interface to use for modules
|
||||
|
||||
Reference in New Issue
Block a user