gitee.com/quant1x/engine@v1.8.4/factors/feature_misc_kline_shape.go (about)

     1  package factors
     2  
     3  import (
     4  	"gitee.com/quant1x/engine/cache"
     5  	"gitee.com/quant1x/engine/config"
     6  	"gitee.com/quant1x/engine/utils"
     7  	"gitee.com/quant1x/exchange"
     8  	"gitee.com/quant1x/num"
     9  	"gitee.com/quant1x/pandas"
    10  	. "gitee.com/quant1x/pandas/formula"
    11  	"math"
    12  )
    13  
    14  // ShapeType K线形态类型
    15  type ShapeType = uint64
    16  
    17  const (
    18  	KLineShapeYiZi                 ShapeType = 1 << iota // 1一字
    19  	KLineShapeLimitUp                                    // 2涨停板
    20  	KLineShapeLimitDown                                  // 3跌停板
    21  	KLineShapeNoChanged                                  // 4平盘
    22  	KLineShapeUp                                         // 5上涨
    23  	KLineShapeDown                                       // 6下跌
    24  	KLineShapeDoji                                       // 7十字星
    25  	KLineShapeLongUpShadow                               // 8长上影线
    26  	KLineShapeLongDownShadow                             // 9长下影线
    27  	KLineShapeDaYangLine                                 // 10大阳线
    28  	KLineShapeDaYinLine                                  // 11大阴线
    29  	KLineShapeZhongYangLine                              // 12中阳线
    30  	KLineShapeZhongYinLine                               // 13中阴线
    31  	KLineShapeXiaoYangLine                               // 14小阳线
    32  	KLineShapeXiaoYinLine                                // 15小阴线
    33  	KLineShapeNotLimitUp                                 // 16炸板, 曾涨停
    34  	KLineShapeNotLimitDown                               // 17曾跌停
    35  	KLineShapeShrinkToHalf                               // 18第3日缩量到一半
    36  	KLineShapeGrabChipInFinalRound                       // 19尾盘抢筹
    37  	KLineShapeChant                                      // 20吟唱, 蓄力 配合大阳线
    38  	KLineShapeStepBackDailyMA                            // 21日线-连续2个周期回踩均线
    39  	KLineShapeStepBackWeeklyMA                           // 22周线-连续2个周期回踩均线
    40  	KLineShapeStepBackMonthlyMA                          // 23月线-连续2个周期回踩均线
    41  )
    42  
    43  // K线实体大小
    44  const (
    45  	kBoxLarge      = float64(4.50) // 大
    46  	kBoxMedium     = float64(1.50) // 中
    47  	kBoxSmall      = float64(0.00) // 小
    48  	kBoxDojiHeight = float64(0.50) // 十字星K线实体最大比例
    49  )
    50  
    51  // KLineShape K线形态
    52  func KLineShape(df pandas.DataFrame, securityCode string) (shape ShapeType) {
    53  	if df.Nrow() <= cache.KLineMin {
    54  		return
    55  	}
    56  	var (
    57  		VOLs   = df.ColAsNDArray("volume")
    58  		HIGHs  = df.ColAsNDArray("high")
    59  		OPENs  = df.ColAsNDArray("open")
    60  		CLOSEs = df.ColAsNDArray("close")
    61  	)
    62  
    63  	// 1. 捡出基本数据
    64  	m := df.IndexOf(-1)
    65  	// 1.1 最高价
    66  	tmpValue, _ := m["high"]
    67  	HIGH := num.AnyToFloat64(tmpValue)
    68  	// 1.2 最低价
    69  	tmpValue, _ = m["low"]
    70  	LOW := num.AnyToFloat64(tmpValue)
    71  	// 1.3 开盘价
    72  	tmpValue, _ = m["open"]
    73  	OPEN := num.AnyToFloat64(tmpValue)
    74  	// 1.4 收盘价
    75  	tmpValue, _ = m["close"]
    76  	CLOSE := num.AnyToFloat64(tmpValue)
    77  	// 1.5 昨日收盘价
    78  	//tmpValue, _ = m["last_close"]
    79  	LAST_CLOSE := utils.Float64IndexOf(CLOSEs, -2)
    80  
    81  	// 2. 判断基本形态
    82  	if CLOSE > LAST_CLOSE {
    83  		// 2.1 上涨
    84  		shape |= KLineShapeUp
    85  	} else if CLOSE < LAST_CLOSE {
    86  		// 2.2 下跌
    87  		shape |= KLineShapeDown
    88  	} else {
    89  		// 2.3 平盘
    90  		shape |= KLineShapeNoChanged
    91  	}
    92  
    93  	// 3. 一字
    94  	if OPEN == CLOSE && HIGH == LOW && OPEN == HIGH {
    95  		// 3.1 一字
    96  		shape |= KLineShapeYiZi
    97  	}
    98  	// 4 判断是否涨跌停板
    99  	limitRate := exchange.MarketLimit(securityCode)
   100  	lastClose := num.Decimal(LAST_CLOSE)
   101  	lastHigh := num.Decimal(HIGH)
   102  	lastLow := num.Decimal(LOW)
   103  	priceLimitUp := num.Decimal(lastClose * (1.000 + limitRate))
   104  	priceLimitDown := num.Decimal(lastClose * (1.000 - limitRate))
   105  	price := num.Decimal(CLOSE)
   106  	if price == priceLimitUp {
   107  		// 4.1 涨停板
   108  		shape |= KLineShapeLimitUp
   109  	} else if lastHigh == priceLimitUp {
   110  		// 4.2 炸板
   111  		shape |= KLineShapeNotLimitUp
   112  	} else if price == priceLimitDown {
   113  		// 4.2 跌停板
   114  		shape |= KLineShapeLimitDown
   115  	} else if lastLow == priceLimitDown {
   116  		// 4.4 曾跌停
   117  		shape |= KLineShapeNotLimitDown
   118  	}
   119  	// 5. 计算K线实体
   120  	boxTop := CLOSE
   121  	boxBottom := OPEN
   122  	// 5.1 确定K线实体顶部和底部
   123  	if CLOSE < OPEN {
   124  		boxTop, boxBottom = boxBottom, boxTop
   125  	}
   126  	// 6. 计算影线长度
   127  	shadowTop := HIGH - boxTop
   128  	shadowBottom := boxBottom - LOW
   129  	// 7. 十字星
   130  	boxHeight := boxTop - boxBottom
   131  	klineHeight := HIGH - LOW
   132  	boxRatio := num.ChangeRate(klineHeight, boxHeight)
   133  	boxDojiHeight := config.GetDataConfig().Feature.CrossStarRatio
   134  	minHeight := min(boxHeight, shadowTop, shadowBottom)
   135  	doji := math.Abs(boxRatio) < boxDojiHeight && minHeight == boxHeight
   136  	if doji {
   137  		shape |= KLineShapeDoji
   138  	}
   139  	// 7.1 确定上下影线
   140  	if shadowTop > shadowBottom {
   141  		// 7.1 长上影线
   142  		shape |= KLineShapeLongUpShadow
   143  	} else if shadowTop < shadowBottom {
   144  		// 7.2 长下影线
   145  		shape |= KLineShapeLongDownShadow
   146  	}
   147  	// 8. 判断K线的大小
   148  	changeRate := num.NetChangeRate(OPEN, CLOSE)
   149  	if changeRate >= kBoxLarge {
   150  		// 8.1 大阳线
   151  		shape |= KLineShapeDaYangLine
   152  	} else if changeRate <= -kBoxLarge {
   153  		// 8.2 大阴线
   154  		shape |= KLineShapeDaYinLine
   155  	} else if changeRate >= kBoxMedium {
   156  		// 8.3 中阳线
   157  		shape |= KLineShapeZhongYangLine
   158  	} else if changeRate <= -kBoxMedium {
   159  		// 8.4 中阴线
   160  		shape |= KLineShapeZhongYinLine
   161  	} else if changeRate >= kBoxSmall {
   162  		// 8.5 小阳线
   163  		shape |= KLineShapeXiaoYangLine
   164  	} else if changeRate <= -kBoxSmall {
   165  		// 8.6 小阴线
   166  		shape |= KLineShapeXiaoYinLine
   167  	}
   168  	// 9. 成交量判断
   169  	hv3 := HHV(VOLs, 5) // 5日内最大的量
   170  	hh3 := HHV(HIGHs, 5)
   171  	hx1 := REF(VOLs, 2).Eq(hv3)
   172  	hx2 := REF(HIGHs, 2).Eq(hh3)
   173  	hvn := BARSLAST(hx1.And(hx2))
   174  	x1 := hvn.Eq(0) // __+__
   175  	x2 := hv3.Div(VOLs).Gte(2.00)
   176  	x := x1.And(x2)
   177  	//DATE := df.Col("date")
   178  	//df1 := pandas.NewDataFrame(DATE, hv3, hvn, x1, x2, x)
   179  	//fmt.Println(df1)
   180  	if x.IndexOf(-1).(bool) {
   181  		shape |= KLineShapeShrinkToHalf
   182  	}
   183  	// 10. 尾盘抢筹
   184  	tmpValue, _ = m["open_turnz"]
   185  	openTurnZ := num.AnyToFloat64(tmpValue)
   186  	tmpValue, _ = m["close_turnz"]
   187  	closeTurnZ := num.AnyToFloat64(tmpValue)
   188  	if openTurnZ > 0 && closeTurnZ > 0 && closeTurnZ > openTurnZ {
   189  		shape |= KLineShapeGrabChipInFinalRound
   190  	}
   191  	// 11. 吟唱, 蓄力
   192  	//LZL:VOL/REF(VOL,1) -1,NODRAW;
   193  	//BOXH:=MAX(OPEN,CLOSE);
   194  	//BOXL:=MIN(OPEN,CLOSE);
   195  	//C1:REF(LZL>=1,1),NODRAW;
   196  	//C2:CLOSE>=REF((BOXH+BOXL)/2,1),NODRAW;
   197  	//
   198  	lzl := VOLs.Div(REF(VOLs, 1)).Sub(1.00) // 立桩量
   199  	boxHalf := ABS(CLOSEs.Add(OPENs)).Div(2.00)
   200  	xc1 := REF(lzl, 1).Gte(1.00)
   201  	xc2 := CLOSEs.Gte(REF(boxHalf, 1))
   202  	xc3 := lzl.Lt(0).And(HIGHs.Lte(REF(HIGHs, 1)))
   203  	if xc1.And(xc2).And(xc3).IndexOf(-1).(bool) {
   204  		shape |= KLineShapeChant
   205  	}
   206  	//DATE := df.Col("date")
   207  	//df1 := pandas.NewDataFrame(DATE, xc1, xc2, xc3)
   208  	//fmt.Println(df1)
   209  
   210  	// 12. 趋势改变
   211  	//MA5:=MA(CLOSE,5);
   212  	//X1:=CLOSE>=MA5;
   213  	//C3:BARSLASTCOUNT(X1),NODRAW;
   214  	//B:(REF(C3,1)>=2) AND LOW<=MA5,NODRAW;
   215  	//DRAWICON(B,50,1);
   216  	//X2:=CLOSE<MA5;
   217  	//C4:BARSLASTCOUNT(X2),NODRAW;
   218  	//S:(REF(C4,1)>=2) AND HIGH>MA5,NODRAW;
   219  	//DRAWICON(S,50,2);
   220  	//DRAWICON(CLOSE<MA5,90,8);
   221  
   222  	// 吊顶线
   223  	//OUT:HIGH=MAX(OPEN,CLOSE)&&
   224  	//HIGH-LOW>3*(HIGH-MIN(OPEN,CLOSE))&&
   225  	//CLOSE>MA(CLOSE,5);
   226  
   227  	// 倒转锤头
   228  	// KSTAR:MIN(OPEN,CLOSE)=LOW&&
   229  	//HIGH-LOW>3*(MAX(OPEN,CLOSE)-LOW)&&
   230  	//CLOSE<MA(CLOSE,5);
   231  	return shape
   232  }