Skip to content

Commit

Permalink
cmd获取指数信息
Browse files Browse the repository at this point in the history
  • Loading branch information
axiaoxin committed Jun 12, 2022
1 parent 2996036 commit 004fdee
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,12 @@ OPTIONS:

## index

获取指数信息

```
./investool index -i 000905 -d
```

获取指数成分股:

```
Expand Down
40 changes: 35 additions & 5 deletions cmds/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,47 @@ import (
"github.com/olekukonko/tablewriter"
)

func showIndexData(data *eastmoney.IndexData) {
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetRowSeparator("")
table.SetBorder(false)
table.SetNoWhiteSpace(true)
headers := []string{}
table.SetHeader(headers)
table.SetCaption(true, "指数信息")
rows := [][]string{
{"指数名称", data.FullIndexName},
{"指数说明", data.Reaprofile},
{"编制方", data.MakerName},
{"估值", data.IndexValueCN()},
{"估值PE值", data.Petim},
{"估值PE百分位", data.Pep100},
{"指数代码", data.IndexCode},
{"板块名称", data.BKName},
{"当前点数", data.NewPrice},
{"最新涨幅", data.NewCHG},
{"最近一周涨幅", data.W},
{"最近一月涨幅", data.M},
{"最近三月涨幅", data.Q},
{"最近六月涨幅", data.Hy},
{"最近一年涨幅", data.Y},
{"最近两年涨幅", data.Twy},
{"最近三年涨幅", data.Try},
{"最近五年涨幅", data.Fy},
{"今年来涨幅", data.Sy},
}
table.AppendBulk(rows)

table.Render()
}
func showIndexStocks(stocks []eastmoney.ZSCFGItem) {
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetRowLine(true)
table.SetCaption(true, "指数成分股")
headers := []string{"股票名称", "股票代码", "持仓占比"}
table.SetHeader(headers)
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold, tablewriter.BgBlackColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.BgBlackColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.BgBlackColor},
)

sum := 0.0
for _, stock := range stocks {
Expand Down
18 changes: 18 additions & 0 deletions cmds/index_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cmds

import (
"context"
"fmt"

"github.com/axiaoxin-com/investool/datacenter"
"github.com/urfave/cli/v2"
Expand All @@ -24,6 +25,13 @@ func FlagsIndex() []cli.Flag {
Usage: "指定指数代码",
Required: true,
},
&cli.BoolFlag{
Name: "desc",
Aliases: []string{"d"},
Value: false,
Usage: "返回指数信息",
Required: false,
},
&cli.BoolFlag{
Name: "stocks",
Aliases: []string{"s"},
Expand All @@ -39,6 +47,16 @@ func ActionIndex() func(c *cli.Context) error {
return func(c *cli.Context) error {
ctx := context.Background()
indexCode := c.String("index")

showDesc := c.Bool("desc")
if showDesc {
indexData, err := datacenter.EastMoney.Index(ctx, indexCode)
if err != nil {
fmt.Println(err)
}
showIndexData(indexData)
}

showStocks := c.Bool("stocks")
if showStocks {
stocks, err := datacenter.EastMoney.ZSCFG(ctx, indexCode)
Expand Down
19 changes: 18 additions & 1 deletion datacenter/eastmoney/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type IndexData struct {
FullIndexName string `json:"FullIndexName"`
NewPrice string `json:"NewPrice"` // 指数点数
NewPriceDate string `json:"NewPriceDate"`
NewCHG string `json:"NewCHG"`
NewCHG string `json:"NewCHG"` // 最新涨幅
Reaprofile string `json:"reaprofile"` // 指数说明
MakerName string `json:"MakerName"` // 指数编制方
Bkid string `json:"BKID"`
Expand Down Expand Up @@ -49,6 +49,23 @@ type IndexData struct {
Isstatic string `json:"ISSTATIC"`
}

// IndexValueCN 指数估值
func (i *IndexData) IndexValueCN() string {
switch i.IndexvaluaCN {
case "-2":
return "低估"
case "-1":
return "较为低估"
case "0":
return "适中"
case "1":
return "较为高估"
case "2":
return "高估"
}
return "--"
}

// RspIndex Index接口返回结构
type RspIndex struct {
Datas IndexData `json:"Datas"`
Expand Down

0 comments on commit 004fdee

Please sign in to comment.