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

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/engine/cache"
     6  	"gitee.com/quant1x/engine/factors"
     7  	"gitee.com/quant1x/exchange"
     8  	"gitee.com/quant1x/gotdx/securities"
     9  	cmder "github.com/spf13/cobra"
    10  )
    11  
    12  var (
    13  	printModules = []cmdFlag[string]{}
    14  )
    15  
    16  const (
    17  	printCommand     = "print"
    18  	printDescription = "打印数据概要"
    19  )
    20  
    21  var (
    22  	CmdPrint *cmder.Command = nil // CmdPrint 打印命令
    23  )
    24  
    25  func initPrint() {
    26  	CmdPrint = &cmder.Command{
    27  		Use:     printCommand,
    28  		Example: Application + " print sh000001",
    29  		Short:   printDescription,
    30  		//Args:  cmder.MinimumNArgs(1),
    31  		Run: func(cmd *cmder.Command, args []string) {
    32  			tradeDate := cache.DefaultCanReadDate()
    33  			if len(flagDate.Value) > 0 {
    34  				tradeDate = exchange.FixTradeDate(flagDate.Value)
    35  			}
    36  			keywords := ""
    37  			code := ""
    38  			for _, m := range printModules {
    39  				if len(m.Value) > 0 {
    40  					keywords = m.Name
    41  					code = m.Value
    42  					break
    43  				}
    44  			}
    45  			if len(keywords) > 0 {
    46  				plugins := cache.PluginsWithName(cache.PluginMaskFeature, keywords)
    47  				if len(plugins) == 0 {
    48  					fmt.Printf("没有找到名字是[%s]的数据插件\n", keywords)
    49  				} else {
    50  					handlePrintData(code, tradeDate, plugins[0])
    51  				}
    52  			} else {
    53  				if len(args) != 1 {
    54  					fmt.Println(cmd.Help())
    55  					return
    56  				}
    57  				// 默认输出K线信息
    58  				securityCode := args[0]
    59  				printKline(securityCode, tradeDate)
    60  			}
    61  		},
    62  	}
    63  
    64  	commandInit(CmdPrint, &flagDate)
    65  	plugins := cache.Plugins(cache.PluginMaskFeature)
    66  	printModules = make([]cmdFlag[string], len(plugins))
    67  	for i, plugin := range plugins {
    68  		key := plugin.Key()
    69  		name := plugin.Name()
    70  		printModules[i] = cmdFlag[string]{Name: key, Usage: plugin.Owner() + ": " + name, Value: ""}
    71  		CmdPrint.Flags().StringVar(&(printModules[i].Value), printModules[i].Name, "", printModules[i].Usage)
    72  	}
    73  }
    74  
    75  // 输出结构化信息
    76  func handlePrintData(code, date string, plugin cache.DataAdapter) {
    77  	securityCode := exchange.CorrectSecurityCode(code)
    78  	name := securities.GetStockName(securityCode)
    79  	tradeDate := exchange.FixTradeDate(date)
    80  	cacheDate, featureDate := cache.CorrectDate(tradeDate)
    81  	fmt.Printf("%s: %s, cache: %s, feature: %s\n", securityCode, name, cacheDate, featureDate)
    82  	plugin.Print(securityCode, tradeDate)
    83  }
    84  
    85  //// 输出K线概要数据列表
    86  //func v1PrintKline(securityCode string) {
    87  //	securityCode = proto.CorrectSecurityCode(securityCode)
    88  //	name := securities.GetStockName(securityCode)
    89  //	fmt.Printf("%s: %s\n", securityCode, name)
    90  //	df := datasets.KLine(securityCode)
    91  //	fmt.Println(df)
    92  //}
    93  
    94  // 输出K线概要数据列表
    95  func printKline(securityCode string, tradeDate string) {
    96  	securityCode = exchange.CorrectSecurityCode(securityCode)
    97  	name := securities.GetStockName(securityCode)
    98  	fmt.Printf("%s: %s, %s\n", securityCode, name, tradeDate)
    99  	df := factors.BasicKLine(securityCode)
   100  	fmt.Println(df)
   101  }