gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 03:22:27 25 4
gpt4 key购买 nike

在 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 未实现 custplotter.TOHLCVer(缺少 Len 方法)

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

   //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 文献中找到的所有 slice 示例仅使用 [ [ ]、[ ] ]

最佳答案

根据 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 接口(interface) :)

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

关于go - 如何为我的数据获取此特定形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51415804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com