代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。