gitee.com/quant1x/engine@v1.8.4/tracker/tracker_view.go (about)

     1  package tracker
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/engine/factors"
     6  	"gitee.com/quant1x/engine/models"
     7  	"gitee.com/quant1x/engine/storages"
     8  	"gitee.com/quant1x/exchange"
     9  	"gitee.com/quant1x/gox/tags"
    10  	"gitee.com/quant1x/num"
    11  	"gitee.com/quant1x/pkg/tablewriter"
    12  	"os"
    13  	"time"
    14  )
    15  
    16  // OutputTable 输出表格
    17  func OutputTable(model models.Strategy, stockSnapshots []factors.QuoteSnapshot) {
    18  	today := exchange.IndexToday()
    19  	dates := exchange.TradeRange(exchange.MARKET_CN_FIRST_DATE, today)
    20  	days := len(dates)
    21  	currentlyDay := dates[days-1]
    22  	updateTime := "15:00:59"
    23  	//todayIsTradeDay := false
    24  	if today == currentlyDay {
    25  		//todayIsTradeDay = true
    26  		now := time.Now()
    27  		nowTime := now.Format(exchange.CN_SERVERTIME_FORMAT)
    28  		if nowTime < exchange.CN_TradingStartTime {
    29  			currentlyDay = dates[days-2]
    30  		} else if nowTime >= exchange.CN_TradingStartTime && nowTime <= exchange.CN_TradingStopTime {
    31  			updateTime = now.Format(exchange.TimeOnly)
    32  		}
    33  	}
    34  	// 控制台输出表格
    35  	tbl := tablewriter.NewWriter(os.Stdout)
    36  	tbl.SetHeader(tags.GetHeadersByTags(models.Statistics{}))
    37  	orderCount := models.CountTopN
    38  	if orderCount > len(stockSnapshots) {
    39  		orderCount = len(stockSnapshots)
    40  	}
    41  	votingResults := []models.Statistics{}
    42  	//stockSnapshots = stockSnapshots[:orderCount]
    43  	orderCreateTime := factors.GetTimestamp()
    44  	for _, v := range stockSnapshots {
    45  		ticket := models.Statistics{
    46  			Date:                 currentlyDay,              // 日期
    47  			Code:                 v.SecurityCode,            // 证券代码
    48  			Name:                 v.Name,                    // 证券名称
    49  			Active:               int(v.Active),             // 活跃度
    50  			LastClose:            v.LastClose,               // 昨收
    51  			Open:                 v.Open,                    // 开盘价
    52  			OpenRaise:            v.OpeningChangeRate,       // 开盘涨幅
    53  			Price:                v.Price,                   // 现价
    54  			UpRate:               v.ChangeRate,              // 涨跌幅
    55  			OpenPremiumRate:      v.PremiumRate,             // 集合竞价买入溢价率
    56  			OpenVolume:           v.OpenVolume,              // 集合竞价-开盘量, 单位是股
    57  			TurnZ:                v.OpenTurnZ,               // 开盘换手率z
    58  			QuantityRatio:        v.OpenQuantityRatio,       // 开盘量比
    59  			AveragePrice:         v.Amount / float64(v.Vol), // 均价
    60  			Speed:                v.Rate,                    // 涨速
    61  			ChangePower:          v.ChangePower,             // 涨跌力度
    62  			AverageBiddingVolume: v.AverageBiddingVolume,    // 委比
    63  			UpdateTime:           orderCreateTime,           // 更新时间
    64  		}
    65  		if v.Open < v.LastClose {
    66  			ticket.Tendency += "低开"
    67  		} else if v.Open == v.LastClose {
    68  			ticket.Tendency += "平开"
    69  		} else {
    70  			ticket.Tendency += "高开"
    71  		}
    72  		if ticket.AveragePrice < v.Open {
    73  			ticket.Tendency += ",回落"
    74  		} else {
    75  			ticket.Tendency += ",拉升"
    76  		}
    77  		if v.Price > ticket.AveragePrice {
    78  			ticket.Tendency += ",强势"
    79  		} else {
    80  			ticket.Tendency += ",弱势"
    81  		}
    82  
    83  		bs, ok := __stock2Block[ticket.Code]
    84  		if ok {
    85  			tb := bs[0]
    86  			if block, ok := __mapBlockData[tb.Code]; ok {
    87  				ticket.BlockName = block.Name
    88  				ticket.BlockRate = block.ChangeRate
    89  				ticket.BlockTop = block.Rank
    90  				shot, ok1 := __stock2Rank[ticket.Code]
    91  				if ok1 {
    92  					ticket.BlockRank = shot.TopNo
    93  				}
    94  			}
    95  		}
    96  		votingResults = append(votingResults, ticket)
    97  	}
    98  	gtP1 := 0 // 存在溢价
    99  	gtP2 := 0 // 超过1%
   100  	gtP3 := 0
   101  	gtP4 := 0
   102  	gtP5 := 0
   103  	yields := 0.00
   104  	for _, v := range votingResults {
   105  		rate := num.NetChangeRate(v.Open, v.Price)
   106  		if rate > 0 {
   107  			gtP1 += 1
   108  		}
   109  		if rate >= 1.00 {
   110  			gtP2 += 1
   111  		}
   112  		if rate >= 2.00 {
   113  			gtP3 += 1
   114  		}
   115  		if rate >= 3.00 {
   116  			gtP4 += 1
   117  		}
   118  		if rate >= 5.00 {
   119  			gtP5 += 1
   120  		}
   121  		yields += rate
   122  		tbl.Append(tags.GetValuesByTags(v))
   123  	}
   124  	yields /= float64(len(votingResults))
   125  	fmt.Println() // 输出一个换行
   126  	tbl.Render()
   127  	count := len(stockSnapshots)
   128  	fmt.Println()
   129  
   130  	fmt.Println(currentlyDay + " " + updateTime + ", 胜率统计:")
   131  	fmt.Printf("\t==> 胜    率: %d/%d, %.2f%%, 收益率: %.2f%%\n", gtP1, count, 100*float64(gtP1)/float64(count), yields)
   132  	fmt.Printf("\t==> 溢价超1%%: %d/%d, %.2f%%\n", gtP2, count, 100*float64(gtP2)/float64(count))
   133  	fmt.Printf("\t==> 溢价超2%%: %d/%d, %.2f%%\n", gtP3, count, 100*float64(gtP3)/float64(count))
   134  	fmt.Printf("\t==> 溢价超3%%: %d/%d, %.2f%%\n", gtP4, count, 100*float64(gtP4)/float64(count))
   135  	fmt.Printf("\t==> 溢价超5%%: %d/%d, %.2f%%\n", gtP5, count, 100*float64(gtP5)/float64(count))
   136  	fmt.Println()
   137  	// 存储
   138  	storages.OutputStatistics(model, currentlyDay, votingResults)
   139  }