代码拉取完成,页面将自动刷新
package factors
import (
"context"
"gitee.com/quant1x/engine/cache"
"gitee.com/quant1x/engine/datasource/base"
"gitee.com/quant1x/exchange"
"gitee.com/quant1x/gotdx/quotes"
"gitee.com/quant1x/gox/api"
"gitee.com/quant1x/num"
"slices"
)
// DataWideKLine 宽表
type DataWideKLine struct {
Manifest
}
func init() {
summary := __mapDataSets[BaseWideKLine]
_ = cache.Register(&DataWideKLine{Manifest: Manifest{DataSummary: summary}})
}
func (this *DataWideKLine) Clone(date string, code string) DataSet {
summary := __mapDataSets[BaseWideKLine]
var dest = DataWideKLine{
Manifest: Manifest{
DataSummary: summary,
Date: date,
Code: code,
},
}
return &dest
}
func (this *DataWideKLine) Init(ctx context.Context, date string) error {
// 恢复交易数据的最早日期默认值
base.RestoreBeginDateOfHistoricalTradingData()
_ = ctx
_ = date
return nil
}
func (this *DataWideKLine) Update(date string) {
pullWideByDate(this.GetSecurityCode(), date)
}
func (this *DataWideKLine) Repair(date string) {
this.Update(date)
}
func (this *DataWideKLine) Increase(snapshot quotes.Snapshot) {
_ = snapshot
}
func (this *DataWideKLine) Print(code string, date ...string) {
_ = code
_ = date
}
// 通过日期拉取宽表数据
func pullWideByDate(securityCode, date string) []SecurityFeature {
securityCode = exchange.CorrectSecurityCode(securityCode)
// 1. 加载缓存
filename := cache.WideFilename(securityCode)
var list []SecurityFeature
var beginDate string // 补数据的开始日期
var endDate string // 补数据的结束日期
var cacheBeginDate, cacheEndDate string
err := api.CsvToSlices(filename, &list)
if err != nil || len(list) == 0 {
// 如果文件为空, 暂定从1990-12-19
cacheBeginDate = exchange.MARKET_CH_FIRST_LISTTIME
cacheEndDate = cacheBeginDate
beginDate = cacheBeginDate
} else {
//list = list[:len(list)-1]
cacheBeginDate = list[0].Date
last := list[len(list)-1]
cacheEndDate = last.Date
// 以缓存文件最后一条记录的日期
beginDate = cacheEndDate
}
// 2. 确定补齐数据的日期
endDate = exchange.FixTradeDate(date)
// 2.1 结束日期经过交易日历的校对处理一次
//logger.Warnf("[%s]: begin=%s, end= %s", securityCode, beginDate, endDate)
if len(beginDate) == 0 {
beginDate = exchange.MARKET_CH_FIRST_LISTTIME
}
dates := exchange.TradingDateRange(beginDate, endDate)
n := len(dates)
if n == 0 {
// 这种情况的原因是传入的date小于缓存的最后一条记录的日期
beginDate = endDate
} else if n == 1 {
// 传入日期和缓存最后一条记录的日期相同
beginDate = dates[0]
endDate = dates[0]
} else {
beginDate = dates[0]
endDate = dates[n-1]
}
// 3. 补齐日线, 日线是必须要有的, 也肯定会有
// 数据为空, 从基础K线获取K线部分
klines := base.CheckoutKLines(securityCode, endDate)
kline_length := len(klines)
if kline_length == 0 {
// K线为空, 返回空
return nil
} else {
// 校验wide缓存和k线缓存的开始日期是否对齐
klsBeginDate := klines[0].Date
klsEndDate := klines[kline_length-1].Date
if cacheBeginDate == klsBeginDate {
// 如果缓存的开始日期和k线的开始日期相同, 没有问题
} else {
// 如果缓存的开始日期和k线的开始日期不同, 则认为数据错乱, 清空
clear(list)
// 设置缓存开始日期为k线的开始日期
cacheBeginDate = klsBeginDate
// 设置缓存结束日期为k线的开始日期
cacheEndDate = klsBeginDate
beginDate = cacheBeginDate
}
_ = klsEndDate
}
// 4. 确定缓存记录数
list_length := len(list)
// 5. 如果kline比wide数据多
if kline_length > list_length {
list = slices.Grow(list, kline_length)
}
transBeginDate := base.GetBeginDateOfHistoricalTradingData()
transBeginDate = exchange.FixTradeDate(transBeginDate)
for i, v := range klines {
featureDate := v.Date
cacheDate := v.Date
// 强制更新标志
forceUpdate := false
if i < list_length {
// 更新数据, 存在历史数据前复权的可能
list[i].Date = v.Date
list[i].Open = v.Open
list[i].Close = v.Close
list[i].High = v.High
list[i].Low = v.Low
list[i].Volume = int64(v.Volume)
list[i].Amount = v.Amount
list[i].Up = v.Up
list[i].Down = v.Down
if featureDate >= transBeginDate {
checksum := list[i].CheckSum()
forceUpdate = checksum == 0
}
}
if !forceUpdate && v.Date < beginDate {
continue
}
if v.Date > endDate {
break
}
var info SecurityFeature
// 复制k线
info.Date = v.Date
info.Open = v.Open
info.Close = v.Close
info.High = v.High
info.Low = v.Low
info.Volume = int64(v.Volume)
info.Amount = v.Amount
info.Up = v.Up
info.Down = v.Down
// 附加成交数据
if featureDate >= transBeginDate {
// 成交数据
trans := base.CheckoutTransactionData(securityCode, featureDate, true)
if len(trans) > 0 {
cover := CountInflow(trans, securityCode, featureDate)
// 修正f10的缓存, 应该是缓存日期为准
f10 := GetL5F10(securityCode, cacheDate)
if f10 != nil {
cover.OpenTurnZ = f10.TurnZ(cover.OpenVolume)
cover.CloseTurnZ = f10.TurnZ(cover.CloseVolume)
}
info.OpenVolume = cover.OpenVolume
info.OpenTurnZ = cover.OpenTurnZ
info.CloseVolume = cover.CloseVolume
info.CloseTurnZ = cover.CloseTurnZ
info.Volume = cover.InnerVolume + cover.OuterVolume
info.InnerVolume = cover.InnerVolume
info.OuterVolume = cover.OuterVolume
info.InnerAmount = cover.InnerAmount
info.OuterAmount = cover.OuterAmount
}
}
if i < list_length {
list[i] = info
} else {
list = append(list, info)
}
}
// 6. 最后再修订数据
lastClose := 0.000
for i := 0; i < len(list); i++ {
v := &list[i]
// 6.1 修正last_close和change_rate
if i == 0 {
v.LastClose = v.Open
} else {
v.LastClose = lastClose
}
v.ChangeRate = num.NetChangeRate(v.LastClose, v.Close)
lastClose = v.Close
// 6.2 修正up和down
//// 指数类, up和down对应涨跌家数
//// 股票类, 对应内外盘
//if v.Up == 0 {
// v.Up = int(v.OuterVolume)
//}
//if v.Down == 0 {
// v.Down = int(v.InnerVolume)
//}
}
// 7. 保存文件
_ = api.SlicesToCsv(filename, list)
return list
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。