2 Star 7 Fork 11

王布衣/engine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
feature_misc_base.go 6.14 KB
一键复制 编辑 原始数据 按行查看 历史
package factors
import (
"gitee.com/quant1x/engine/cache"
"gitee.com/quant1x/engine/datasource/base"
"gitee.com/quant1x/engine/market"
"gitee.com/quant1x/engine/utils"
"gitee.com/quant1x/exchange"
"gitee.com/quant1x/gotdx/securities"
"gitee.com/quant1x/num"
"gitee.com/quant1x/pandas"
. "gitee.com/quant1x/pandas/formula"
)
// MiscKLine K线特征
type MiscKLine struct {
Date string // 日期
Code string // 证券代码
Shape uint64 // K线形态
MV3 float64 // 3日均量
MA3 float64 // 3日均价
MV5 float64 // 5日均量
MA5 float64 // 5日均价
MA10 float64 // 10日均价
MA20 float64 // 20日均价
VolumeRatio float64 // 成交量比
TurnoverRate float64 // 换手率
AmplitudeRatio float64 // 振幅
AveragePrice float64 // 均价线
Change5 float64 // 5日涨幅
Change10 float64 // 10日涨幅
InitialPrice float64 // 短线底部(Short-Term Bottom),股价最近一次上串5日均线
ShortIntensity float64 // 短线强度
ShortIntensityDiff float64 // 短线强度增幅
MediumIntensity float64 // 中线强度
MediumIntensityDiff float64 // 中线强度增幅
Vix float64 // 波动率
Sentiment float64 // 情绪值
Consistent int // 情绪一致
}
// NewMiscKLine 构建制定日期的K线数据
func NewMiscKLine(code, date string) *MiscKLine {
securityCode := exchange.CorrectSecurityCode(code)
klines := base.CheckoutKLines(securityCode, date)
if len(klines) < cache.KLineMin {
return nil
}
digits := 2
securityInfo, ok := securities.CheckoutSecurityInfo(securityCode)
if ok {
digits = int(securityInfo.DecimalPoint)
}
ek := &MiscKLine{
Date: date,
Code: securityCode,
}
df := pandas.LoadStructs(klines)
var (
OPEN = df.ColAsNDArray("open")
CLOSE = df.ColAsNDArray("close")
HIGH = df.ColAsNDArray("high")
LOW = df.ColAsNDArray("low")
VOL = df.ColAsNDArray("volume")
AMOUNT = df.ColAsNDArray("amount")
UP = df.ColAsNDArray("up")
DOWN = df.ColAsNDArray("down")
)
// 2. 计算3日均量
mv3 := MA(VOL, 3)
ma3 := MA(CLOSE, 3)
ek.MV3 = utils.Float64IndexOf(mv3, -1)
ek.MA3 = utils.Float64IndexOf(ma3, -1)
// 2. 计算5日分钟均量
mv5 := MA(VOL, 5) // 5日均量
mv5m := mv5.Div(exchange.CN_DEFAULT_TOTALFZNUM)
ma5Volume := utils.Float64IndexOf(mv5m, -1)
ek.MV5 = num.Decimal(ma5Volume, digits)
ma5 := MA(CLOSE, 5)
ek.MA5 = utils.Float64IndexOf(ma5, -1)
ma10 := MA(CLOSE, 10)
ek.MA10 = utils.Float64IndexOf(ma10, -1)
ma20 := MA(CLOSE, 20)
ek.MA20 = utils.Float64IndexOf(ma20, -1)
// 3. 隔日成交量放大比例
vr := VOL.Div(REF(VOL, 1))
volumeChangeRate := utils.Float64IndexOf(vr, -1)
ek.VolumeRatio = num.Decimal(volumeChangeRate, digits)
// 换手率
f10 := GetL5F10(securityCode, date)
if f10 != nil {
turnoverRate := VOL.Div(f10.Capital).Mul(100.00)
ek.TurnoverRate = num.Decimal(utils.Float64IndexOf(turnoverRate, -1))
}
// 振幅, 这里只比对最高价和最低价的幅度, 不参考前一天的收盘价
ar := HIGH.Div(LOW).Sub(1.00).Mul(100.00)
ek.AmplitudeRatio = utils.Float64IndexOf(ar, -1)
// 4. 当日K线图形概要
shape := KLineShape(df, securityCode)
ek.Shape = shape
// 均价线
averagePrice := AMOUNT.Div(VOL)
ap := utils.Float64IndexOf(averagePrice, -1)
ek.AveragePrice = num.Decimal(ap, digits)
// 5. 计算阶段涨幅
chg5 := CLOSE.Div(REF(CLOSE, 5)).Sub(1.00).Mul(100)
chg10 := CLOSE.Div(REF(CLOSE, 10)).Sub(1.00).Mul(100)
change5 := utils.Float64IndexOf(chg5, -1)
change10 := utils.Float64IndexOf(chg10, -1)
ek.Change5 = num.Decimal(change5, digits)
ek.Change10 = num.Decimal(change10, digits)
// 6. 多空强度
//QDMA5:=MA(CLOSE,5);
//ma5 := MA(CLOSE, 5)
//QDMA10:=MA(CLOSE,10);
//ma10 := MA(CLOSE, 10)
//QDMA20:=MA(CLOSE,20);
//ma20 := MA(CLOSE, 20)
//QDV1:=QDMA5*100/QDMA20-100;
//qdv1 := ma5.Div(ma20).Sub(1.00).Mul(100)
qdv1 := utils.SeriesChangeRate(ma20, ma5)
//QDV2:=QDMA10*100/QDMA20-100;
qdv2 := utils.SeriesChangeRate(ma20, ma10)
//QD1:=ABS(QDV1-REF(QDV1,1));
//qd1 := ABS(qdv1.Sub(REF(qdv1, 1)))
qd1 := qdv1.Sub(REF(qdv1, 1))
//QD2:=ABS(QDV2-REF(QDV2,1));
//qd2 := ABS(qdv2.Sub(REF(qdv2, 1)))
qd2 := qdv2.Sub(REF(qdv2, 1))
//QDCD:=CLOSE*100/REF(CLOSE,1)-100;
//超短线:QDCD-REF(QDCD,1),NODRAW;
//短线强度:QD1-REF(QD1,1),NODRAW;
shortIntensity := qd1
//中线强度:QD2-REF(QD2,1),NODRAW;
mediumIntensity := qd2
ek.ShortIntensity = utils.Float64IndexOf(shortIntensity, -1)
shortIntensityDiff := qd1.Sub(REF(qd1, 1))
ek.ShortIntensityDiff = utils.Float64IndexOf(shortIntensityDiff, -1)
ek.MediumIntensity = utils.Float64IndexOf(mediumIntensity, -1)
mediumIntensityDiff := qd2.Sub(REF(qd2, 1))
ek.MediumIntensityDiff = utils.Float64IndexOf(mediumIntensityDiff, -1)
// 7. 波动率
N := 3
M := 21
//TYPICAL_PRICE:=(OPEN+CLOSE+HIGH+LOW)/4;
TYPICAL_PRICE := OPEN.Add(CLOSE).Add(HIGH).Add(LOW).Div(4.00)
//METHOD:=EMA(TYPICAL_PRICE,N);
METHOD := EMA(TYPICAL_PRICE, N)
//APPLY_TO:=TYPICAL_PRICE;
APPLY_TO := TYPICAL_PRICE
//MYSTDDEV:=SQRT(SUM(POW(APPLY_TO-METHOD,2),N)/N);
var1 := APPLY_TO.Sub(METHOD)
var2 := var1.Mul(var1)
var3 := SUM(var2, N)
var4 := SQRT(var3.Div(N))
MYSTDDEV := pandas.ToSeries[num.DType](var4...)
//MAXB:=HHV(MYSTDDEV,M);
MAXB := HHV(MYSTDDEV, M)
//MINB:=LLV(MYSTDDEV,M);
MINB := LLV(MYSTDDEV, M)
//VIX:100*(MYSTDDEV-MINB)/(MAXB-MINB);
//10,DOTLINE,COLORGREEN;
//50,DOTLINE,COLORYELLOW;
vix := MYSTDDEV.Sub(MINB).Div(MAXB.Sub(MINB)).Mul(100)
ek.Vix = utils.Float64IndexOf(vix, -1)
// 8. 短线底部(Short-Term Bottom),股价最近一次上穿5日均线
closeCrossMa5 := CROSS(CLOSE, ma5)
crossPeriod := BARSLAST(closeCrossMa5)
crossPrice := REF(OPEN, crossPeriod)
ek.InitialPrice = num.Decimal(utils.Float64IndexOf(crossPrice, -1))
// 指数类情绪值 情绪一致
up := utils.Float64IndexOf(UP, -1)
down := utils.Float64IndexOf(DOWN, -1)
ek.Sentiment, ek.Consistent = market.SecuritySentiment(up, down)
return ek
}
func (k *MiscKLine) Kind() cache.Kind {
return FeatureKLineShap
}
func (k *MiscKLine) Name() string {
return "K线形态"
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/quant1x/engine.git
git@gitee.com:quant1x/engine.git
quant1x
engine
engine
v1.8.16

搜索帮助