mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-18 07:26:50 +00:00
* 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
119 lines
3.1 KiB
Go
119 lines
3.1 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"
|
|
)
|
|
|
|
// 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
|
|
}
|