2 Star 7 Fork 10

王布衣/engine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
storage_state.go 3.53 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2024-01-11 11:15 . 适配exchange工具包
package storages
import (
"fmt"
"gitee.com/quant1x/engine/cache"
"gitee.com/quant1x/engine/models"
"gitee.com/quant1x/engine/trader"
"gitee.com/quant1x/exchange"
"gitee.com/quant1x/gox/api"
"gitee.com/quant1x/gox/logger"
"os"
"path"
"path/filepath"
"strings"
)
const (
// 状态文件扩展名
orderStateFileExtension = ".done"
)
// Touch 创建一个空文件
func Touch(filename string) error {
_ = api.CheckFilepath(filename, true)
return os.WriteFile(filename, nil, 0644)
}
// 获取状态机路径
func state_filepath(state_date string) string {
flagPath := filepath.Join(cache.GetQmtCachePath(), "var", state_date)
return flagPath
}
// 获取状态文件前缀
func state_prefix(state_date, qmtStrategyName string, direction trader.Direction) string {
qmtStrategyName = strings.ToLower(qmtStrategyName)
prefix := fmt.Sprintf("%s-%s-%s-%s", state_date, traderConfig.AccountId, qmtStrategyName, direction.Flag())
return prefix
}
// 订单状态文件前缀
func order_state_prefix(state_date string, model models.Strategy, direction trader.Direction) string {
qmtStrategyName := models.QmtStrategyName(model)
prefix := state_prefix(state_date, qmtStrategyName, direction)
return prefix
}
// 获得订单标识文件名
func order_state_filename(date string, model models.Strategy, code string, direction trader.Direction) string {
state_date := exchange.FixTradeDate(date, cache.CACHE_DATE)
orderFlagPath := state_filepath(state_date)
prefix := order_state_prefix(date, model, direction)
securityCode := exchange.CorrectSecurityCode(code)
filename := fmt.Sprintf("%s-%s.done", prefix, securityCode)
state_filename := path.Join(orderFlagPath, filename)
return state_filename
}
// CheckOrderState 检查订单执行状态
func CheckOrderState(date string, model models.Strategy, code string, direction trader.Direction) bool {
filename := order_state_filename(date, model, code, direction)
return api.FileExist(filename)
}
// PushOrderState 推送订单完成状态
func PushOrderState(date string, model models.Strategy, code string, direction trader.Direction) error {
filename := order_state_filename(date, model, code, direction)
return Touch(filename)
}
// CountStrategyOrders 统计策略订单数
func CountStrategyOrders(date string, model models.Strategy, direction trader.Direction) int {
stateDate := exchange.FixTradeDate(date, cache.CACHE_DATE)
orderFlagPath := state_filepath(stateDate)
filenamePrefix := order_state_prefix(stateDate, model, direction)
pattern := filepath.Join(orderFlagPath, filenamePrefix+"-*"+orderStateFileExtension)
files, err := filepath.Glob(pattern)
if err != nil {
logger.Error(err)
return 0
}
return len(files)
}
// FetchListForFirstPurchase 获取指定日期交易的个股列表
func FetchListForFirstPurchase(date, qmtStrategyName string, direction trader.Direction) []string {
stateDate := exchange.FixTradeDate(date, cache.CACHE_DATE)
orderFlagPath := state_filepath(stateDate)
filenamePrefix := state_prefix(stateDate, qmtStrategyName, direction)
var list []string
prefix := filepath.Join(orderFlagPath, filenamePrefix+"-")
pattern := prefix + "*" + orderStateFileExtension
files, err := filepath.Glob(pattern)
if err != nil || len(files) == 0 {
logger.Errorf("没有指定日期的个股买入列表, error=%+v", err)
return list
}
for _, filename := range files {
after, found := strings.CutPrefix(filename, prefix)
if !found {
continue
}
before, found := strings.CutSuffix(after, orderStateFileExtension)
if !found {
continue
}
list = append(list, before)
}
return list
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/quant1x/engine.git
git@gitee.com:quant1x/engine.git
quant1x
engine
engine
v1.1.4

搜索帮助