92 lines
1.9 KiB
Go
92 lines
1.9 KiB
Go
package com
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/banbox/banbot/config"
|
|
"github.com/banbox/banbot/core"
|
|
"github.com/banbox/banbot/utils"
|
|
ta "github.com/banbox/banta"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
)
|
|
|
|
const (
|
|
MaSMA = "sma"
|
|
MaEMA = "ema"
|
|
MaRMA = "rma"
|
|
MaWMA = "wma"
|
|
MaVWMA = "vwma"
|
|
MaHMA = "hma"
|
|
MaKAMA = "kama"
|
|
MaALMA = "alma"
|
|
)
|
|
|
|
var MATypes = []string{MaSMA, MaEMA, MaRMA, MaWMA, MaVWMA, MaHMA, MaKAMA, MaALMA}
|
|
|
|
var maMap = make(map[string]bool)
|
|
|
|
func init() {
|
|
for _, name := range MATypes {
|
|
maMap[name] = true
|
|
}
|
|
}
|
|
|
|
func DefMaType(p *config.RunPolicyConfig, key, defVal string, allows ...string) string {
|
|
if len(allows) == 0 {
|
|
allows = MATypes
|
|
} else {
|
|
for _, k := range allows {
|
|
if _, ok := maMap[k]; !ok {
|
|
panic(fmt.Sprintf("unsupport maType: %s", k))
|
|
}
|
|
}
|
|
}
|
|
if _, ok := maMap[defVal]; !ok {
|
|
panic(fmt.Sprintf("unsupport maType: %s", defVal))
|
|
}
|
|
defValInt := slices.Index(allows, defVal)
|
|
if defValInt < 0 {
|
|
defValInt = 0
|
|
allows = append([]string{defVal}, allows...)
|
|
}
|
|
pVal := p.DefInt(key, defValInt, core.PUniform(0, float64(len(allows)-1)))
|
|
return allows[pVal]
|
|
}
|
|
|
|
func GetMA(price, vol *ta.Series, maType string, maLen int) *ta.Series {
|
|
switch maType {
|
|
case MaSMA:
|
|
return ta.SMA(price, maLen)
|
|
case MaEMA:
|
|
return ta.EMA(price, maLen)
|
|
case MaRMA:
|
|
return ta.RMA(price, maLen)
|
|
case MaWMA:
|
|
return ta.WMA(price, maLen)
|
|
case MaVWMA:
|
|
return ta.VWMA(price, vol, maLen)
|
|
case MaHMA:
|
|
return ta.HMA(price, maLen)
|
|
case MaKAMA:
|
|
return ta.KAMA(price, maLen)
|
|
case MaALMA:
|
|
return ta.ALMA(price, maLen, 6, 0.85)
|
|
default:
|
|
panic(fmt.Errorf("unknown maType: " + maType))
|
|
}
|
|
}
|
|
|
|
func GetTmpPath(name string) (string, error) {
|
|
path, err := os.Getwd()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
tmpDir := filepath.Join(path, "tmp")
|
|
err = utils.EnsureDir(tmpDir, 0644)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(tmpDir, name), nil
|
|
}
|