gitee.com/quant1x/engine@v1.8.4/factors/feature_history.go (about) 1 package factors 2 3 import ( 4 "context" 5 "gitee.com/quant1x/engine/cache" 6 "gitee.com/quant1x/engine/datasource/base" 7 "gitee.com/quant1x/engine/utils" 8 "gitee.com/quant1x/exchange" 9 "gitee.com/quant1x/gox/api" 10 "gitee.com/quant1x/pandas" 11 . "gitee.com/quant1x/pandas/formula" 12 ) 13 14 // IncompleteData 不完整的数据 15 type IncompleteData struct { 16 No1 HousNo1 17 } 18 19 type CompleteData struct { 20 No1 HousNo1 21 } 22 23 const ( 24 cacheL5KeyHistory = "history" 25 ) 26 27 // History 历史整合数据 28 // 29 // 记录重要的截止上一个交易日的数据 30 type History struct { 31 cache.DataSummary `dataframe:"-"` 32 Date string `name:"日期" dataframe:"date"` // 日期, 数据落地的日期 33 Code string `name:"代码" dataframe:"code"` // 代码 34 MA2 float64 `name:"2日均价" dataframe:"ma2"` // 2日均价 35 MA3 float64 `name:"3日均价" dataframe:"ma3"` // 3日均价 36 MV3 float64 `name:"3日均量" dataframe:"mv3"` // 3日均量 37 MA4 float64 `name:"4日均价" dataframe:"ma4"` // 4日均价 38 MA5 float64 `name:"5日均价" dataframe:"ma5"` // 5日均价 39 MV5 float64 `name:"5日均量" dataframe:"mv5"` // 5日均量 40 MA9 float64 `name:"9日均价" dataframe:"ma9"` // 9日均价 41 MV9 float64 `name:"9日均量" dataframe:"mv9"` // 9日均量 42 MA10 float64 `name:"10日均价" dataframe:"ma10"` // 10日均价 43 MV10 float64 `name:"10日均量" dataframe:"mv10"` // 10日均量 44 MA19 float64 `name:"19日均价" dataframe:"ma19"` // 19日均价 45 MV19 float64 `name:"19日均量" dataframe:"mv19"` // 19日均量 46 MA20 float64 `name:"20日均价" dataframe:"ma20"` // 20日均价 47 MV20 float64 `name:"20日均量" dataframe:"mv20"` // 20日均量 48 OPEN float64 `name:"开盘" dataframe:"open"` // 昨日开盘 49 CLOSE float64 `name:"收盘" dataframe:"close"` // 昨日收盘 50 HIGH float64 `name:"最高" dataframe:"high"` // 昨日最高 51 LOW float64 `name:"最低" dataframe:"low"` // 昨日最低 52 VOL float64 `name:"成交量" dataframe:"vol"` // 昨日成交量 53 AMOUNT float64 `name:"成交额" dataframe:"amount"` // 昨日成交额 54 AveragePrice float64 `name:"均价" dataframe:"average_price"` // 昨日均价 55 Payloads IncompleteData `name:"payloads" dataframe:"payloads"` // 扩展的半成品数据 56 Last CompleteData `name:"last" dataframe:"last"` // 上一个交易日的数据 57 UpdateTime string `name:"更新时间" dataframe:"update_time"` // 更新时间 58 State uint64 `name:"样本状态" dataframe:"样本状态"` 59 } 60 61 func NewHistory(date, code string) *History { 62 summary := __mapFeatures[FeatureHistory] 63 v := History{ 64 DataSummary: summary, 65 Date: date, 66 Code: code, 67 } 68 return &v 69 } 70 71 func (this *History) GetDate() string { 72 return this.Date 73 } 74 75 func (this *History) GetSecurityCode() string { 76 return this.Code 77 } 78 79 func (this *History) Factory(date string, code string) Feature { 80 v := NewHistory(date, code) 81 return v 82 } 83 84 func (this *History) Init(ctx context.Context, date string) error { 85 _ = ctx 86 _ = date 87 return nil 88 } 89 90 func (this *History) FromHistory(history History) Feature { 91 _ = api.Copy(this, &history) 92 return this 93 } 94 95 func (this *History) Update(code, cacheDate, featureDate string, complete bool) { 96 this.Repair(code, cacheDate, featureDate, complete) 97 } 98 99 func (this *History) Repair(code, cacheDate, featureDate string, complete bool) { 100 securityCode := exchange.CorrectSecurityCode(this.Code) 101 tradeDate := exchange.FixTradeDate(featureDate) 102 klines := base.CheckoutKLines(securityCode, tradeDate) 103 if len(klines) < cache.KLineMin { 104 return 105 } 106 df := pandas.LoadStructs(klines) 107 var ( 108 OPEN = df.ColAsNDArray("open") 109 CLOSE = df.ColAsNDArray("close") 110 HIGH = df.ColAsNDArray("high") 111 LOW = df.ColAsNDArray("low") 112 VOL = df.ColAsNDArray("volume") 113 AMOUNT = df.ColAsNDArray("amount") 114 ) 115 ma2 := MA(CLOSE, 2) 116 this.MA2 = utils.Float64IndexOf(ma2, -1) 117 //MA3 float64 // 3日均价 118 ma3 := MA(CLOSE, 3) 119 this.MA3 = utils.Float64IndexOf(ma3, -1) 120 // MV3 float64 // 3日均量 121 mv3 := MA(VOL, 3) 122 this.MV3 = utils.Float64IndexOf(mv3, -1) 123 ma4 := MA(CLOSE, 4) 124 this.MA4 = utils.Float64IndexOf(ma4, -1) 125 // MA5 float64 // 5日均价 126 ma5 := MA(CLOSE, 5) 127 this.MA5 = utils.Float64IndexOf(ma5, -1) 128 // MV5 float64 // 5日均量 129 mv5 := MA(VOL, 5) 130 this.MV5 = utils.Float64IndexOf(mv5, -1) 131 // MA9 float64 // 9日均价 132 ma9 := MA(CLOSE, 9) 133 this.MA9 = utils.Float64IndexOf(ma9, -1) 134 // MV9 float64 // 9日均量 135 mv9 := MA(VOL, 9) 136 this.MV9 = utils.Float64IndexOf(mv9, -1) 137 // MA10 float64 // 10日均价 138 ma10 := MA(CLOSE, 10) 139 this.MA10 = utils.Float64IndexOf(ma10, -1) 140 // MV10 float64 // 10日均量 141 mv10 := MA(VOL, 10) 142 this.MV10 = utils.Float64IndexOf(mv10, -1) 143 // MA19 float64 // 19日均价 144 ma19 := MA(CLOSE, 19) 145 this.MA19 = utils.Float64IndexOf(ma19, -1) 146 // MV19 float64 // 19日均量 147 mv19 := MA(VOL, 19) 148 this.MV19 = utils.Float64IndexOf(mv19, -1) 149 // MA20 float64 // 20日均价 150 ma20 := MA(CLOSE, 20) 151 this.MA20 = utils.Float64IndexOf(ma20, -1) 152 // MV20 float64 // 20日均量 153 mv20 := MA(VOL, 20) 154 this.MV20 = utils.Float64IndexOf(mv20, -1) 155 //OPEN float64 `name:"开盘" dataframe:"open"` // 开盘价 156 this.OPEN = utils.Float64IndexOf(OPEN, -1) 157 //CLOSE float64 `name:"收盘" dataframe:"close"` // 收盘价 158 this.CLOSE = utils.Float64IndexOf(CLOSE, -1) 159 this.HIGH = utils.Float64IndexOf(HIGH, -1) 160 this.LOW = utils.Float64IndexOf(LOW, -1) 161 //VOL float64 `name:"成交量" dataframe:"vol"` // 昨日成交量 162 this.VOL = utils.Float64IndexOf(VOL, -1) 163 //AMOUNT float64 `name:"成交额" dataframe:"amount"` // 昨日成交额 164 this.AMOUNT = utils.Float64IndexOf(AMOUNT, -1) 165 ap := AMOUNT.Div(VOL) 166 this.AveragePrice = utils.Float64IndexOf(ap, -1) 167 // 扩展数据 修复 168 { 169 // hous_no1 170 this.Payloads.No1.Repair(securityCode, cacheDate, featureDate, false) 171 this.Last.No1.Repair(securityCode, cacheDate, featureDate, true) 172 } 173 _ = OPEN 174 this.UpdateTime = GetTimestamp() 175 this.State |= this.Kind() 176 } 177 178 func (this *History) Increase(snapshot QuoteSnapshot) Feature { 179 //TODO implement me 180 panic("implement me") 181 } 182 183 func (this *History) ValidateSample() error { 184 if this.State > 0 { 185 return nil 186 } 187 return ErrInvalidFeatureSample 188 } 189 190 // GetMV5 前5日分钟均量 191 func (this *History) GetMV5() float64 { 192 //minutes := trading.Minutes(this.GetDate()) 193 //if minutes < 1 { 194 // minutes = 1 195 //} 196 minutes := exchange.CN_DEFAULT_TOTALFZNUM 197 return this.MV5 / float64(minutes) 198 }