如何为我的数据获取此特定形状

How to get this specific shape for my data

在 Go 中,我正在使用这个函数 bars, err := custplotter.NewCandlesticks(data) 从这里: https://github.com/pplcc/plotext/tree/master/custplotter

它需要这种形状的数据:

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

但我下面的代码正在创建这种形状的数据:

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

这给了我这个错误信息: 不能在 custplotter.NewCandlesticks:

的参数中使用数据 (type [ ][ ]string) 作为类型 custplotter.TOHLCVer

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

我认为问题出在数据形状上。我如何更改我的代码以创建所需的数据形状(使用 { } 而不是 [ ])?

   //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++
    }

我在 Go 文献中找到的所有切片示例仅使用 [ [ ]、[ ] ]

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

看来你需要的是一个custplotter.TOHLCVs,它只是一个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 }

所以基本上您的解决方案可能与此类似:

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++
}

或者您也可以实现 TOHLCVer 接口 :)

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 ++
}