dongqucheng3851 2018-07-19 06:32
浏览 109
已采纳

如何获得我的数据的特定形状

In Go, I'm using this function bars, err := custplotter.NewCandlesticks(data) from here: https://github.com/pplcc/plotext/tree/master/custplotter

It's expecting this shape for data:

[{2 16435 16458 16435 16446 1} {3 16446 16458 16435.04 16455 1} .....]

But my code below is creating my data in this shape instead:

[[2 16435 16458 16435 16446 1] [3 16446 16458 16435.04 16455 1] .....]

Which gives me this error message: cannot use data (type [ ][ ]string) as type custplotter.TOHLCVer in argument to custplotter.NewCandlesticks:

[ ][ ]string does not implement custplotter.TOHLCVer (missing Len method)

I believe the problem is the data shape. How can i change my code to create the required data shape (with { } instead of [ ]) ?

   //read excel file******************************************
    xlsx, err := excelize.OpenFile("/media/Snaps/test snaps.xlsm")
    if err != nil {
        fmt.Println(err)
        return
    }

    //read all rows into df
    df := xlsx.GetRows("ticker_2")

    //get only TOHLCV columns and 60 rows
    df3 := make([][]string, 60) // create slice for 60 rows
    idx := 0
    for _, row := range df[1:61] { // read 60 rows
        df3row := make([]string, 6) // create slice for 6 columns
        copy(df3row, row[28:34]) // copy desired columns to new row slice
        df3[idx] = df3row
        idx++
    }

All examples of slices i found in Go litterature uses only [ [ ], [ ] ]

  • 写回答

1条回答 默认 最新

  • douhuanbai6729 2018-07-19 07:01
    关注

    as per https://github.com/pplcc/plotext/blob/68ab3c6e05c34baf5af21c9f5c3341f527a110ac/examples/tohlcvexampledata.go#L42

    It seems that what you need is a custplotter.TOHLCVs which is just a slice of a struct of float64.

    https://github.com/pplcc/plotext/blob/master/custplotter/tohlcv.go:

    type TOHLCVer interface {
        // Len returns the number of time, open, high, low, close, volume tuples.
        Len() int
    
        // TOHLCV returns an time, open, high, low, close, volume tuple.
        TOHLCV(int) (float64, float64, float64, float64, float64, float64)
    }
    
    // TOHLCVs implements the TOHLCVer interface using a slice.
    type TOHLCVs []struct{ T, O, H, L, C, V float64 }
    

    so basically your solution could resemble this:

    df3 := make(TOHLCVs, 60) // create slice for 60 rows
    idx := 0
    for _, row := range df[1:61] { // read 60 rows
        df3[idx].T, err = strconv.ParseFloat(row[28], 64)
        df3[idx].O, err = strconv.ParseFloat(row[29], 64)
        df3[idx].H, err = strconv.ParseFloat(row[30], 64)
        df3[idx].L, err = strconv.ParseFloat(row[31], 64)
        df3[idx].C, err = strconv.ParseFloat(row[32], 64)
        df3[idx].V, err = strconv.ParseFloat(row[33], 64)
        idx++
    }
    

    Or you could just implement the TOHLCVer interface too :)

    type SlicesOfTOHLCV [][6]float64
    
    func (s SlicesOfTOHLCV) Len() int {
        return len(s)
    }
    
    func (s SlicesOfTOHLCV) TOHLCV(i int) (float64, float64, float64, float64, float64) {
        return s[i][0], s[i][1], s[i][2], s[i][3], s[i][4], s[i][5]
    }
    
    mySlice := make(SlicesOfTOHLCV, 60)
    i := 0
    for _, row := range df[1:61] {
        mySlice[i] = [6]float64{}
        for j := 0; j < 6; j ++ {
            mySlice[i][j], err = strconv.ParseFloat(row[28+j], 64)
            if err != nil {
                panic(err)
            }
        }
        i ++
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用