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.
65 lines
1.5 KiB
65 lines
1.5 KiB
package trade |
|
|
|
import ( |
|
"sig-pub/pkg/types" |
|
"sig-pub/pkg/types/decimals" |
|
|
|
"github.com/govalues/decimal" |
|
) |
|
|
|
// ITradeStrategy 下单策略 |
|
// 根据购买信号和账户信息生成下单参数 |
|
// TradeStrategy 下单买入策略接口(控制滑点, 仓位管理) |
|
type ITradeStrategy interface { |
|
// 下单, 币种,方向,杠杆 |
|
Trade(arg ...string) |
|
|
|
// 市场价格更新 |
|
Update(ctx ITradeStrategyContext, account ITradeAccount) |
|
} |
|
|
|
type ITradeStrategyContext interface { |
|
// 最新价格 |
|
LastPrice() decimal.Decimal |
|
} |
|
|
|
type TradeStrategyParam struct { |
|
MaxPosPct float64 // 单笔交易最大仓位占比 |
|
MaxExposurePct float64 // 最大总敞口占比 |
|
MaxLots float64 // 最大手数/数量 (optional) |
|
} |
|
|
|
type TradeStrategy struct { |
|
param TradeStrategyParam |
|
} |
|
|
|
func NewTradeStrategy(param TradeStrategyParam) (*TradeStrategy, error) { |
|
return &TradeStrategy{ |
|
param: param, |
|
}, nil |
|
} |
|
|
|
func (s *TradeStrategy) SigTrade(side types.Side, k types.Kline) (ta TradeArg, err error) { |
|
price := decimals.MustToFloat64(k.Close) |
|
time := k.Interval.MustAddMul(k.Ts, 1) |
|
ta = TradeArg{ |
|
Side: side, |
|
Price: price, |
|
Leverage: 1, |
|
Qty: 0.02, |
|
KInterval: string(k.Interval), |
|
KTime: k.Ts, |
|
Time: time, |
|
} |
|
return |
|
} |
|
|
|
type TradeArg struct { |
|
Side types.Side // 开仓方向 |
|
Price float64 // 开仓价格 |
|
Leverage int32 // 杠杆倍数 |
|
Qty float64 // 交易量 qty为基础货币数量 |
|
KInterval string // 交易k线周期 |
|
KTime int64 // 交易k线时间 |
|
Time int64 // 交易时间 |
|
}
|
|
|