11 changed files with 40 additions and 232 deletions
@ -0,0 +1,4 @@
|
||||
package okx |
||||
|
||||
type OkxAccountHolder struct { |
||||
} |
||||
@ -0,0 +1,14 @@
|
||||
package trade |
||||
|
||||
import ( |
||||
"sig-pub/api/pb" |
||||
|
||||
"github.com/govalues/decimal" |
||||
) |
||||
|
||||
type IAccountHolder interface { |
||||
Exchange() pb.ExchangeType |
||||
GetValueCash() decimal.Decimal // 获取账户总市值
|
||||
GetTradeCash() decimal.Decimal // 获取可交易空闲资金
|
||||
GetOpenTrades() []int64 // 获取未平仓交易单
|
||||
} |
||||
@ -1,4 +1,4 @@
|
||||
package trader |
||||
package trade |
||||
|
||||
type ErrorCode int32 |
||||
|
||||
@ -1,34 +0,0 @@
|
||||
package trader |
||||
|
||||
import ( |
||||
"sig-pub/pkg/types" |
||||
|
||||
"github.com/govalues/decimal" |
||||
) |
||||
|
||||
// 定义
|
||||
// Dependes() []IndicatorDef // 需要订阅的指标列表(包括k线, 实时k线/关闭k线)
|
||||
type IndicatorDef struct { |
||||
IndicatorName string |
||||
Intervals []types.Interval |
||||
} |
||||
|
||||
// 指标定义接口
|
||||
type Indicator interface { |
||||
Meta() IndicatorMeta |
||||
// init id, 计算完成 publish 时使用id,
|
||||
// exchange: 封装indicator访问, 封装历史k线访问
|
||||
// args: 动态指标参数定义, 执行时创建
|
||||
Init(ctx Context, exchange any, args map[string]any) (code ErrorCode, err error) // 初始化指标参数, 上下文
|
||||
Update(kline types.Kline) (err error) // 驱动k线数据, 待驱动k线到达后, 再待subscribe计算完成后执行
|
||||
Emit(func(klineTs int64, indicators map[string]decimal.Decimal)) // 指标计算完成后发送, 由指标执行器进行存储或分发
|
||||
} |
||||
|
||||
// 窗口函数
|
||||
type SeriesWindow interface { |
||||
Range(count int) // 当前k到
|
||||
} |
||||
|
||||
// 指标能力
|
||||
type Context interface { |
||||
} |
||||
@ -1,42 +0,0 @@
|
||||
package trader |
||||
|
||||
import "sig-pub/pkg/types" |
||||
|
||||
type StrategyName string |
||||
|
||||
// 下单策略接口
|
||||
type Strategy interface { |
||||
Meta() StrategyMeta |
||||
// init id, 计算完成 publish 时使用id,
|
||||
// exchange: 封装indicator访问, 封装历史k线访问
|
||||
// args: 动态策略参数, 执行时创建
|
||||
Init(ctx StrategyContext, args map[string]any) (code ErrorCode, err error) |
||||
Update(ctx StrategyContext, kline types.Kline) (code ErrorCode, err error) |
||||
// Emit() // 下单: sell/buy -> 持单,双方向?, score(分数权重)
|
||||
} |
||||
|
||||
type StrategyMeta struct { |
||||
// Id string `json:"id"` // 策略注册/执行器系统分配
|
||||
Name string |
||||
Desc string |
||||
SubIntervals []types.Interval // 订阅k线周期
|
||||
} |
||||
|
||||
// KlineServiceClient
|
||||
// IndicatorClient.Sub(MACD5) stream[Indicator]
|
||||
// IndicatorClient.Sub(ALMA) stream[Indicator]
|
||||
//
|
||||
|
||||
// 持单策略接口,动态止盈/止损/加仓/减仓
|
||||
type OrderStrategy interface { |
||||
} |
||||
|
||||
// 策略外部访问能力
|
||||
type StrategyContext interface { |
||||
Previous(offset int) types.Kline |
||||
Range(offset int) []types.Kline |
||||
} |
||||
|
||||
// 策略a
|
||||
type StrategyA struct { |
||||
} |
||||
@ -1,78 +0,0 @@
|
||||
package trader |
||||
|
||||
import ( |
||||
"fmt" |
||||
"sig-pub/pkg/types" |
||||
"sig-pub/pkg/types/series" |
||||
"sig-pub/pkg/zlog" |
||||
|
||||
"github.com/spf13/cast" |
||||
) |
||||
|
||||
// stateful
|
||||
type StrategyTrendTrace struct { |
||||
intervalDatas map[types.Interval]*series.LimitFloats |
||||
|
||||
argBaseLine int |
||||
} |
||||
|
||||
func NewStrategyTrendTrace() *StrategyTrendTrace { |
||||
return &StrategyTrendTrace{} |
||||
} |
||||
|
||||
func (StrategyTrendTrace) Meta() StrategyMeta { |
||||
return StrategyMeta{ |
||||
SubIntervals: []types.Interval{ |
||||
types.Interval5m, types.Interval15m, types.Interval1d, |
||||
}, |
||||
} |
||||
} |
||||
|
||||
func (s *StrategyTrendTrace) initArgs(args map[string]string) (err error) { |
||||
|
||||
return |
||||
} |
||||
|
||||
func (s *StrategyTrendTrace) Init(ctx StrategyContext, args map[string]string) (code ErrorCode, err error) { |
||||
s.argBaseLine, err = cast.ToIntE(args["baseLine"]) |
||||
if err != nil { |
||||
return |
||||
} |
||||
if s.argBaseLine <= 0 { |
||||
|
||||
} |
||||
|
||||
s.intervalDatas = map[types.Interval]*series.LimitFloats{ |
||||
types.Interval5m: series.NewLimitFloats(s.argBaseLine), |
||||
types.Interval15m: series.NewLimitFloats(s.argBaseLine), |
||||
types.Interval1d: series.NewLimitFloats(s.argBaseLine), |
||||
} |
||||
return |
||||
} |
||||
|
||||
func (s *StrategyTrendTrace) Update(ctx StrategyContext, kline types.Kline) (code ErrorCode, err error) { |
||||
data, ok := s.intervalDatas[kline.Interval] |
||||
if !ok { |
||||
zlog.Warningf("ignore interval kline: %v", kline.Interval) |
||||
return |
||||
} |
||||
|
||||
v, ok := kline.Close.Float64() |
||||
if !ok { |
||||
code = ErrorCodeKline |
||||
err = fmt.Errorf("kline close value error: %#v", kline) |
||||
return |
||||
} |
||||
data.Push(v) |
||||
|
||||
// calc klines
|
||||
s.caculate() |
||||
return |
||||
} |
||||
|
||||
func (s *StrategyTrendTrace) caculate() { |
||||
// 5m判断趋势, 15m/1h 确认趋势
|
||||
datas5m := s.intervalDatas[types.Interval5m] |
||||
minV, maxV := datas5m.MinMax() |
||||
_, _ = minV, maxV |
||||
} |
||||
@ -1,66 +0,0 @@
|
||||
package trader |
||||
|
||||
import "github.com/spf13/cast" |
||||
|
||||
// 指标注册/执行器系统分配
|
||||
// k线频率, 执行时指定
|
||||
// 指标数据(频率)存储, 实时计算 ?
|
||||
type IndicatorMeta struct { |
||||
Name string |
||||
Desc string |
||||
Args []Arg |
||||
} |
||||
|
||||
type Arg struct { |
||||
Name string |
||||
Desc string |
||||
ArgType ArgType // 参数类型
|
||||
Options []ArgOption // 单选/多选选项列表
|
||||
} |
||||
|
||||
type ArgOption struct { |
||||
Name string |
||||
Desc string |
||||
} |
||||
|
||||
// CastValidate 数据类型校验
|
||||
func (t Arg) CastValidate(v string) bool { |
||||
switch t.ArgType { |
||||
default: |
||||
return false |
||||
case ArgTypeString: |
||||
return true |
||||
case ArgTypeInt: |
||||
fallthrough |
||||
case ArgTypeUInt: |
||||
if r, e := cast.ToIntE(v); e != nil { |
||||
return false |
||||
} else if t.ArgType == ArgTypeUInt { |
||||
return r >= 0 |
||||
} |
||||
return true |
||||
case ArgTypeFloat: |
||||
fallthrough |
||||
case ArgTypeUFloat: |
||||
if r, e := cast.ToFloat64E(v); e != nil { |
||||
return false |
||||
} else if t.ArgType == ArgTypeUFloat { |
||||
return r >= 0 |
||||
} |
||||
return true |
||||
} |
||||
} |
||||
|
||||
// 参数类型
|
||||
type ArgType int8 |
||||
|
||||
const ( |
||||
_ ArgType = iota |
||||
ArgTypeString |
||||
ArgTypeInt |
||||
ArgTypeUInt |
||||
ArgTypeFloat |
||||
ArgTypeUFloat |
||||
ArgTypeSelect // 单选
|
||||
ArgTypeCheckBox // 多选
|
||||
) |
||||
Loading…
Reference in new issue