gitee.com/quant1x/engine@v1.8.4/datasource/tdxweb/safetyscore.go (about)

     1  package tdxweb
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/engine/market"
     6  	"gitee.com/quant1x/exchange"
     7  	"gitee.com/quant1x/gox/concurrent"
     8  	"gitee.com/quant1x/gox/http"
     9  	"gitee.com/quant1x/gox/logger"
    10  	"gitee.com/quant1x/pkg/fastjson"
    11  )
    12  
    13  const (
    14  	urlRiskAssessment            = "http://page3.tdx.com.cn:7615/site/pcwebcall_static/bxb/json/"
    15  	defaultSafetyScore           = 100
    16  	defaultSafetyScoreOfNotFound = 100
    17  	defaultSafetyScoreOfIgnore   = 0
    18  )
    19  
    20  var (
    21  	__mapSafetyScore = concurrent.NewTreeMap[string, int]()
    22  )
    23  
    24  func GetSafetyScore(securityCode string) (score int) {
    25  	if !exchange.AssertStockBySecurityCode(securityCode) {
    26  		return defaultSafetyScore
    27  	}
    28  	if market.IsNeedIgnore(securityCode) {
    29  		return defaultSafetyScoreOfIgnore
    30  	}
    31  	score = defaultSafetyScore
    32  	_, _, code := exchange.DetectMarket(securityCode)
    33  	if len(code) == 6 {
    34  		url := fmt.Sprintf("%s%s.json", urlRiskAssessment, code)
    35  		data, err := http.Get(url)
    36  		if err != nil || err == http.NotFound {
    37  			score = defaultSafetyScoreOfNotFound
    38  		} else {
    39  			obj, err := fastjson.ParseBytes(data)
    40  			if err != nil {
    41  				logger.Errorf("%+v\n", err)
    42  				tmpScore, ok := __mapSafetyScore.Get(securityCode)
    43  				if ok {
    44  					score = tmpScore
    45  				} else {
    46  					score = defaultSafetyScore
    47  				}
    48  			} else {
    49  				result := obj.GetArray("data")
    50  				if result != nil && len(result) > 0 {
    51  					tmpScore := 100
    52  					for _, v := range result {
    53  						rows := v.GetArray("rows")
    54  						for _, row := range rows {
    55  							trig := row.GetInt("trig")
    56  							if trig == 1 {
    57  								tmpScore = tmpScore - row.GetInt("fs")
    58  							}
    59  						}
    60  					}
    61  					score = tmpScore
    62  					__mapSafetyScore.Put(securityCode, score)
    63  				}
    64  			}
    65  		}
    66  	}
    67  	return score
    68  }