gitee.com/quant1x/engine@v1.8.4/factors/feature_ism.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/num"
    10  	"gitee.com/quant1x/pandas"
    11  	. "gitee.com/quant1x/pandas/formula"
    12  )
    13  
    14  const (
    15  	cacheL5KeyInvestmentSentimentMaster = "ism"
    16  	S8PeriodOfLimitUp                   = 30
    17  )
    18  
    19  // InvestmentSentimentMaster 情绪大师
    20  type InvestmentSentimentMaster struct {
    21  	cache.DataSummary `dataframe:"-"`
    22  	Date              string  `name:"日期" dataframe:"日期"`     // 数据日期
    23  	Code              string  `name:"证券代码" dataframe:"证券代码"` // 证券代码
    24  	PN                int     `name:"观察周期" dataframe:"pn"`
    25  	ZDF               float64 `name:"涨跌幅" dataframe:"zdf"`
    26  	R1CLOSE           float64 `name:"昨收盘" dataframe:"r1Close"`
    27  	ZTJ               float64 `name:"涨停价" dataframe:"ztj"`
    28  	CZT               bool    `name:"涨停" dataframe:"czt"`
    29  	BN                int     `name:"板数" dataframe:"bn"`
    30  	FZT               int     `name:"距离首次涨停" dataframe:"fzt"`
    31  	TN                int     `name:"天数" dataframe:"tn"`
    32  	TIAN              int     `name:"天" dataframe:"tian"`
    33  	BAN               int     `name:"板" dataframe:"ban"`
    34  	ZHANG             int     `name:"涨" dataframe:"zhang"`
    35  	PING              int     `name:"平" dataframe:"ping"`
    36  	DIE               int     `name:"跌" dataframe:"die"`
    37  	OH                float64 `name:"周期内最高价" dataframe:"oh"`
    38  	COH               bool    `name:"是否周期内最高价" dataframe:"coh"`
    39  	OHN               int     `name:"最高价周期" dataframe:"ohn"`
    40  	OL                float64 `name:"周期内最低价" dataframe:"ol"`
    41  	COL               bool    `name:"是否周期内最低价" dataframe:"col"`
    42  	OLN               int     `name:"最低价周期" dataframe:"oln"`
    43  	OHV               float64 `name:"最高价量" dataframe:"ohv"`
    44  	OHBL              float64 `name:"最高价量比" dataframe:"ohbl"`
    45  	OLV               float64 `name:"最低价量" dataframe:"olv"`
    46  	OLBL              float64 `name:"最低价量比" dataframe:"olbl"`
    47  	UpdateTime        string  `name:"更新时间" dataframe:"update_time"` // 更新时间
    48  	State             uint64  `name:"样本状态" dataframe:"样本状态"`        // 样本状态
    49  }
    50  
    51  func NewInvestmentSentimentMaster(date, code string) *InvestmentSentimentMaster {
    52  	summary := __mapFeatures[FeatureInvestmentSentimentMaster]
    53  	v := InvestmentSentimentMaster{
    54  		DataSummary: summary,
    55  		Date:        date,
    56  		Code:        code,
    57  	}
    58  	return &v
    59  }
    60  
    61  func (this *InvestmentSentimentMaster) Factory(date string, code string) Feature {
    62  	v := NewInvestmentSentimentMaster(date, code)
    63  	return v
    64  }
    65  
    66  func (this *InvestmentSentimentMaster) GetDate() string {
    67  	return this.Date
    68  }
    69  
    70  func (this *InvestmentSentimentMaster) GetSecurityCode() string {
    71  	return this.Code
    72  }
    73  
    74  func (this *InvestmentSentimentMaster) Init(ctx context.Context, date string) error {
    75  	_ = ctx
    76  	_ = date
    77  	return nil
    78  }
    79  
    80  func (this *InvestmentSentimentMaster) Update(code, cacheDate, featureDate string, whole bool) {
    81  	securityCode := exchange.CorrectSecurityCode(code)
    82  	this.Date = exchange.FixTradeDate(cacheDate)
    83  	this.Code = securityCode
    84  	tradeDate := exchange.FixTradeDate(featureDate)
    85  	klines := base.CheckoutKLines(securityCode, tradeDate)
    86  	if len(klines) < cache.KLineMin {
    87  		return
    88  	}
    89  	df := pandas.LoadStructs(klines)
    90  	var (
    91  		DATE  = df.Col("date")
    92  		OPEN  = df.ColAsNDArray("open")
    93  		CLOSE = df.ColAsNDArray("close")
    94  		HIGH  = df.ColAsNDArray("high")
    95  		LOW   = df.ColAsNDArray("low")
    96  		VOL   = df.ColAsNDArray("volume")
    97  	)
    98  	//PN:30,COLORWHITE,NODRAW;
    99  	PN := S8PeriodOfLimitUp
   100  	//R1CLOSE:=REF(CLOSE,1);
   101  	R1CLOSE := REF(CLOSE, 1)
   102  	//CST:=NOT(NAMELIKE('S') OR NAMELIKE('*S')) AND VOL>1;
   103  	//ZDF:=IFF(INBLOCK('创业板'), 0.2, IFF(INBLOCK('科创板'),0.2, IFF(INBLOCK('ST板块'), 0.05, IFF(INBLOCK('北证A股'),0.3,0.1))));
   104  	ZDF := exchange.MarketLimit(securityCode)
   105  	//ZTJ:=ZTPRICE(R1CLOSE,ZDF);
   106  	ZTJ := R1CLOSE.Mul(1.00+ZDF).Apply2(func(idx int, v any) any {
   107  		f := v.(float64)
   108  		return num.Decimal(f)
   109  	}, true)
   110  	//CZT:=CLOSE=ZTJ;
   111  	CZT := CLOSE.Gte(ZTJ)
   112  	//BN:COUNT(CZT,PN);
   113  	BN := COUNT(CZT, PN)
   114  	//FTZ:=BARSSINCEN(CZT,PN);
   115  	FTZ := BARSSINCEN(CZT, PN)
   116  	//TN:FTZ+1,COLORWHITE,NODRAW;
   117  	TN := FTZ.Add(1)
   118  	//天:TN,COLORYELLOW,NODRAW;
   119  	TIAN := TN
   120  	//板:BN,COLORRED,NODRAW;
   121  	BAN := BN
   122  	//CUP:=CLOSE>R1CLOSE;
   123  	CUP := CLOSE.Gt(R1CLOSE)
   124  	//CPING:=CLOSE=R1CLOSE;
   125  	CPING := CLOSE.Eq(R1CLOSE)
   126  	//CDOWN:=CLOSE<R1CLOSE;
   127  	CDOWN := CLOSE.Lt(R1CLOSE)
   128  	//涨:COUNT(CUP,TN),COLORRED,NODRAW;
   129  	ZHANG := COUNT(CUP, TN)
   130  	//平:COUNT(CPING,TN),COLORWHITE,NODRAW;
   131  	PING := COUNT(CPING, TN)
   132  	//跌:COUNT(CDOWN,TN),COLORGREEN,NODRAW;
   133  	DIE := COUNT(CDOWN, TN)
   134  
   135  	//{首板以来的最高价}
   136  	//OH:HHV(HIGH,TN),COLORRED,DOTLINE;
   137  	OH := HHV(HIGH, TN)
   138  	//{首板以来最高价到现在的周期}
   139  	//OHN:BARSLAST(HIGH=OH),COLORYELLOW,NODRAW;
   140  	COH := HIGH.Eq(OH)
   141  	OHN := BARSLAST(COH)
   142  	//{首板以来的最低价}
   143  	//OL:LLV(LOW,TN),COLORGREEN,DOTLINE;
   144  	OL := LLV(LOW, TN)
   145  	//{首板以来最低价到现在的周期}
   146  	//OLN:BARSLAST(LOW=OL),COLORYELLOW,NODRAW;
   147  	COL := LOW.Eq(OL)
   148  	OLN := BARSLAST(COL)
   149  	//{首板以来最高价当日的成交量}
   150  	//OHV:REF(VOL,OHN),COLORWHITE,NODRAW;
   151  	OHV := REF(VOL, OHN)
   152  	//{今天成交量和最高价当日成交量的比值}
   153  	//OHBL:VOL/OHV,COLORWHITE,NODRAW;
   154  	OHBL := VOL.Div(OHV)
   155  	//{首板以来最低价当日的成交量}
   156  	//OLV:REF(VOL,OLN),COLORWHITE,NODRAW;
   157  	OLV := REF(VOL, OLN)
   158  	//{今天成交量和最低价当日成交量的比值}
   159  	//OLBL:VOL/OLV,COLORWHITE,NODRAW;
   160  	OLBL := VOL.Div(OLV)
   161  	{
   162  		// 特征数据采集
   163  		this.PN = PN
   164  		this.ZDF = ZDF
   165  		this.R1CLOSE = utils.Float64IndexOf(CLOSE, -1)
   166  		this.ZTJ = utils.Float64IndexOf(ZTJ, -1)
   167  		this.CZT = utils.BoolIndexOf(CZT, -1)
   168  		this.BN = utils.IntegerIndexOf(BN, -1)
   169  		this.FZT = utils.IntegerIndexOf(FTZ, -1)
   170  		this.TN = utils.IntegerIndexOf(TN, -1)
   171  		this.TIAN = utils.IntegerIndexOf(TIAN, -1)
   172  		this.BAN = utils.IntegerIndexOf(BAN, -1)
   173  		this.ZHANG = utils.IntegerIndexOf(ZHANG, -1)
   174  		this.PING = utils.IntegerIndexOf(PING, -1)
   175  		this.DIE = utils.IntegerIndexOf(DIE, -1)
   176  		this.OH = utils.Float64IndexOf(OH, -1)
   177  		this.COH = utils.BoolIndexOf(COH, -1)
   178  		this.OHN = utils.IntegerIndexOf(OHN, -1)
   179  		this.OL = utils.Float64IndexOf(OL, -1)
   180  		this.COL = utils.BoolIndexOf(COL, -1)
   181  		this.OLN = utils.IntegerIndexOf(OLN, -1)
   182  		this.OHV = utils.Float64IndexOf(OHV, -1)
   183  		this.OHBL = utils.Float64IndexOf(OHBL, -1)
   184  		this.OLV = utils.Float64IndexOf(OLV, -1)
   185  		this.OLBL = utils.Float64IndexOf(OLBL, -1)
   186  	}
   187  	//{
   188  	//	// 调试
   189  	//	df = pandas.NewDataFrame(DATE, CLOSE, ZTJ, CZT, BN, FTZ, TN)
   190  	//	fmt.Println(df)
   191  	//}
   192  	this.UpdateTime = GetTimestamp()
   193  	this.State |= this.Kind()
   194  	_ = DATE
   195  	_ = OPEN
   196  }
   197  
   198  func (this *InvestmentSentimentMaster) Repair(securityCode, cacheDate, featureDate string, whole bool) {
   199  	this.Update(securityCode, cacheDate, featureDate, whole)
   200  }
   201  
   202  func (this *InvestmentSentimentMaster) FromHistory(history History) Feature {
   203  	_ = history
   204  	return this
   205  }
   206  
   207  func (this *InvestmentSentimentMaster) Increase(snapshot QuoteSnapshot) Feature {
   208  	_ = snapshot
   209  	return this
   210  }
   211  
   212  func (this *InvestmentSentimentMaster) ValidateSample() error {
   213  	if this.State > 0 {
   214  		return nil
   215  	}
   216  	return ErrInvalidFeatureSample
   217  }