gitee.com/quant1x/engine@v1.8.4/models/snapshot_quote.go (about)

     1  package models
     2  
     3  import (
     4  	"gitee.com/quant1x/engine/factors"
     5  	"gitee.com/quant1x/exchange"
     6  	"gitee.com/quant1x/gotdx"
     7  	"gitee.com/quant1x/gotdx/quotes"
     8  	"gitee.com/quant1x/gox/api"
     9  	"gitee.com/quant1x/gox/logger"
    10  	"gitee.com/quant1x/num"
    11  )
    12  
    13  func QuoteSnapshotFromProtocol(v quotes.Snapshot) factors.QuoteSnapshot {
    14  	snapshot := factors.QuoteSnapshot{}
    15  	_ = api.Copy(&snapshot, &v)
    16  	securityCode := exchange.GetSecurityCode(v.Market, v.Code)
    17  	snapshot.OpeningChangeRate = num.NetChangeRate(snapshot.LastClose, snapshot.Open)
    18  	snapshot.ChangeRate = num.NetChangeRate(snapshot.LastClose, snapshot.Price)
    19  	snapshot.PremiumRate = num.NetChangeRate(snapshot.Open, snapshot.Price)
    20  	snapshot.OpenBiddingDirection, snapshot.OpenVolumeDirection = v.CheckDirection()
    21  	// 涨跌力度
    22  	snapshot.ChangePower = float64(snapshot.OpenVolume) / snapshot.OpeningChangeRate
    23  	snapshot.AverageBiddingVolume = v.AverageBiddingVolume()
    24  
    25  	// 补全F10相关
    26  	f10 := factors.GetL5F10(securityCode)
    27  	if f10 != nil {
    28  		snapshot.Name = f10.SecurityName
    29  		snapshot.Capital = f10.Capital
    30  		snapshot.FreeCapital = f10.FreeCapital
    31  		snapshot.OpenTurnZ = f10.TurnZ(snapshot.OpenVolume)
    32  	}
    33  	// 补全扩展相关
    34  	history := factors.GetL5History(securityCode)
    35  	if history != nil && history.MV5 > 0 {
    36  		lastMinuteVolume := history.GetMV5()
    37  		snapshot.OpenQuantityRatio = float64(snapshot.OpenVolume) / lastMinuteVolume
    38  		minuteVolume := float64(snapshot.Vol) / float64(exchange.Minutes(snapshot.Date))
    39  		snapshot.QuantityRatio = minuteVolume / lastMinuteVolume
    40  	}
    41  	return snapshot
    42  }
    43  
    44  // BatchSnapShot 批量获取即时行情数据快照
    45  func BatchSnapShot(codes []string) []factors.QuoteSnapshot {
    46  	tdxApi := gotdx.GetTdxApi()
    47  	list := []factors.QuoteSnapshot{}
    48  	var err error
    49  	var hq []quotes.Snapshot
    50  	retryTimes := 0
    51  	for retryTimes < quotes.DefaultRetryTimes {
    52  		hq, err = tdxApi.GetSnapshot(codes)
    53  		if err == nil && hq != nil {
    54  			break
    55  		}
    56  		retryTimes++
    57  	}
    58  
    59  	if err != nil {
    60  		logger.Errorf("获取即时行情数据失败", err)
    61  		return list
    62  	}
    63  
    64  	for _, v := range hq {
    65  		if v.State != quotes.TDX_SECURITY_TRADE_STATE_NORMAL {
    66  			// 非正常交易的记录忽略掉
    67  			continue
    68  		}
    69  		snapshot := QuoteSnapshotFromProtocol(v)
    70  		list = append(list, snapshot)
    71  	}
    72  	return list
    73  }
    74  
    75  // GetTick 获取一只股票的tick数据
    76  //
    77  //	该函数用于测试
    78  func GetTick(securityCode string) *factors.QuoteSnapshot {
    79  	list := BatchSnapShot([]string{securityCode})
    80  	if len(list) > 0 {
    81  		v := list[0]
    82  		return &v
    83  	}
    84  	return nil
    85  }