You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.1 KiB
61 lines
1.1 KiB
package indicator |
|
|
|
import ( |
|
"fmt" |
|
"sig-pub/pkg/trader" |
|
"sig-pub/pkg/types" |
|
"sig-pub/pkg/types/series" |
|
|
|
"github.com/spf13/cast" |
|
) |
|
|
|
// RSI: 相对强弱指数 (RSI) |
|
// rsi define: https://www.investopedia.com/terms/r/rsi.asp |
|
type RSI struct { |
|
trader.Indicator |
|
series.Series |
|
values series.Floats |
|
prices series.Floats |
|
|
|
argBaseDay int32 |
|
} |
|
|
|
func NewRSI() *RSI { |
|
return &RSI{} |
|
} |
|
|
|
func (ind RSI) Meta() trader.IndicatorMeta { |
|
return trader.IndicatorMeta{ |
|
Name: "RSI", |
|
Desc: "", |
|
Args: []trader.Arg{ |
|
{Name: "基准天数", Desc: "", ArgType: trader.ArgTypeUInt}, |
|
}, |
|
} |
|
} |
|
|
|
func (ind *RSI) Init(indId int64, exchange any, args []string) (code trader.ErrorCode, err error) { |
|
arg0, err := cast.ToInt32E(args[0]) |
|
if err != nil { |
|
return |
|
} |
|
ind.argBaseDay = arg0 |
|
|
|
cast.ToIntE("1") |
|
return |
|
} |
|
|
|
func (ind *RSI) Update(klines []types.Kline) (err error) { |
|
for _, kline := range klines { |
|
c, ok := kline.Close.Float64() |
|
if !ok { |
|
err = fmt.Errorf("kline close to float64 error: %s", kline.Close.String()) |
|
return |
|
} |
|
ind.prices.Push(c) |
|
} |
|
|
|
diff := ind.prices.Diff() |
|
_ = diff |
|
return |
|
}
|
|
|