gitee.com/quant1x/engine@v1.8.4/market/sentiments.go (about)

     1  package market
     2  
     3  import (
     4  	"gitee.com/quant1x/exchange"
     5  	"gitee.com/quant1x/gotdx"
     6  	"gitee.com/quant1x/gotdx/quotes"
     7  	"gitee.com/quant1x/gox/logger"
     8  	"gitee.com/quant1x/num"
     9  )
    10  
    11  const (
    12  	kSentimentCriticalValue = float64(61.80) // 情绪临界值
    13  )
    14  
    15  type SentimentType = int
    16  
    17  const (
    18  	SentimentZero SentimentType = iota // 情绪一般
    19  	SentimentHigh SentimentType = 1    // 情绪高涨
    20  	SentimentLow  SentimentType = -1   // 情绪低迷
    21  )
    22  
    23  // IndexSentiment 情绪指数, 50%为平稳, 低于50%为情绪差, 高于50%为情绪好
    24  func IndexSentiment(codes ...string) (sentiment float64, consistent int) {
    25  	// 默认上证指数
    26  	securityCode := "sh000001"
    27  	if len(codes) > 0 {
    28  		securityCode = exchange.CorrectSecurityCode(codes[0])
    29  	}
    30  	// 非指数不判断情绪
    31  	if !exchange.AssertIndexBySecurityCode(securityCode) {
    32  		return
    33  	}
    34  	if len(securityCode) != 8 {
    35  		return
    36  	}
    37  	tdxApi := gotdx.GetTdxApi()
    38  	hq, err := tdxApi.GetSnapshot([]string{securityCode})
    39  	if err != nil && len(hq) != len(codes) {
    40  		logger.Errorf("获取即时行情数据失败", err)
    41  		return
    42  	}
    43  
    44  	return SnapshotSentiment(hq[0])
    45  }
    46  
    47  // SecuritySentiment 计算证券情绪
    48  func SecuritySentiment[E ~int | ~int64 | ~float32 | ~float64](up, down E) (sentiment float64, consistent int) {
    49  	sentiment = 100 * float64(up) / float64(up+down)
    50  	if num.Float64IsNaN(sentiment) {
    51  		sentiment = 0
    52  		return
    53  	}
    54  	consistent = SentimentZero
    55  	if sentiment >= kSentimentCriticalValue {
    56  		consistent = SentimentHigh
    57  	} else if sentiment < 100-kSentimentCriticalValue {
    58  		consistent = SentimentLow
    59  	}
    60  	return
    61  }
    62  
    63  // SnapshotSentiment 情绪指数, 50%为平稳, 低于50%为情绪差, 高于50%为情绪好
    64  func SnapshotSentiment(snapshot quotes.Snapshot) (sentiment float64, consistent int) {
    65  	if exchange.AssertIndexByMarketAndCode(snapshot.Market, snapshot.Code) {
    66  		// 指数和板块按照上涨和下跌家数计算
    67  		return SecuritySentiment(snapshot.IndexUp, snapshot.IndexDown)
    68  	}
    69  	// 个股按照内外盘计算
    70  	return SecuritySentiment(snapshot.BVol, snapshot.SVol)
    71  }