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

     1  package tracker
     2  
     3  import (
     4  	"gitee.com/quant1x/engine/config"
     5  	"gitee.com/quant1x/engine/factors"
     6  	"gitee.com/quant1x/engine/models"
     7  	"gitee.com/quant1x/engine/permissions"
     8  	"gitee.com/quant1x/exchange"
     9  	"gitee.com/quant1x/gox/api"
    10  	"gitee.com/quant1x/gox/logger"
    11  	"gitee.com/quant1x/gox/progressbar"
    12  	"gitee.com/quant1x/gox/runtime"
    13  	"sort"
    14  	"time"
    15  )
    16  
    17  // Tracker 盘中跟踪
    18  func Tracker(strategyNumbers ...uint64) {
    19  	for {
    20  		updateInRealTime, status := exchange.CanUpdateInRealtime()
    21  		isTrading := updateInRealTime && status == exchange.ExchangeTrading
    22  		if !runtime.Debug() && !isTrading {
    23  			// 非调试且非交易时段返回
    24  			return
    25  		}
    26  		barIndex := 1
    27  		models.SyncAllSnapshots(&barIndex)
    28  		//stockCodes := radar.ScanSectorForTick(barIndex)
    29  		for _, strategyNumber := range strategyNumbers {
    30  			model, err := models.CheckoutStrategy(strategyNumber)
    31  			if err != nil || model == nil {
    32  				continue
    33  			}
    34  			err = permissions.CheckPermission(model)
    35  			if err != nil {
    36  				logger.Error(err)
    37  				continue
    38  			}
    39  			strategyParameter := config.GetStrategyParameterByCode(strategyNumber)
    40  			if strategyParameter == nil {
    41  				continue
    42  			}
    43  			if strategyParameter.Session.IsTrading() {
    44  				snapshotTracker(&barIndex, model, strategyParameter)
    45  			} else {
    46  				if runtime.Debug() {
    47  					snapshotTracker(&barIndex, model, strategyParameter)
    48  				} else {
    49  					break
    50  				}
    51  			}
    52  		}
    53  		time.Sleep(time.Second * 1)
    54  	}
    55  }
    56  
    57  func snapshotTracker(barIndex *int, model models.Strategy, tradeRule *config.StrategyParameter) {
    58  	if tradeRule == nil {
    59  		return
    60  	}
    61  	stockCodes := tradeRule.StockList()
    62  	if len(stockCodes) == 0 {
    63  		return
    64  	}
    65  	var stockSnapshots []factors.QuoteSnapshot
    66  	stockCount := len(stockCodes)
    67  	bar := progressbar.NewBar(*barIndex, "执行["+model.Name()+"全市场扫描]", stockCount)
    68  	for start := 0; start < stockCount; start++ {
    69  		bar.Add(1)
    70  		code := stockCodes[start]
    71  		securityCode := exchange.CorrectSecurityCode(code)
    72  		if exchange.AssertIndexBySecurityCode(securityCode) {
    73  			continue
    74  		}
    75  		v := models.GetTickFromMemory(securityCode)
    76  		if v != nil {
    77  			snapshot := models.QuoteSnapshotFromProtocol(*v)
    78  			stockSnapshots = append(stockSnapshots, snapshot)
    79  		}
    80  	}
    81  	if len(stockSnapshots) == 0 {
    82  		return
    83  	}
    84  	// 过滤不符合条件的个股
    85  	stockSnapshots = api.Filter(stockSnapshots, func(snapshot factors.QuoteSnapshot) bool {
    86  		err := model.Filter(tradeRule.Rules, snapshot)
    87  		return err == nil
    88  	})
    89  	// 结果集排序
    90  	sortedStatus := model.Sort(stockSnapshots)
    91  	if sortedStatus == models.SortDefault || sortedStatus == models.SortNotExecuted {
    92  		// 默认排序或者排序未执行, 使用默认排序
    93  		sort.Slice(stockSnapshots, func(i, j int) bool {
    94  			a := stockSnapshots[i]
    95  			b := stockSnapshots[j]
    96  			if a.OpenTurnZ > b.OpenTurnZ {
    97  				return true
    98  			}
    99  			return a.OpenTurnZ == b.OpenTurnZ && a.OpeningChangeRate > b.OpeningChangeRate
   100  		})
   101  	}
   102  	// 输出表格
   103  	OutputTable(model, stockSnapshots)
   104  }