0

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 [ [ ], [ ] ]

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Hugues
  • 197
  • 1
  • 5
  • 19

1 Answers1

0

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 ++
}
  • thanks for your input, i've modified my code as per above, now i get: cannot use row[28] (type string) as type float64 in assignment. My data is only numbers in my excel file. (P.S:if i want to show my updated code, where should i put it ? i have to edit my initial post ? or use the Answer button ? – Hugues Jul 19 '18 at 07:35
  • @Hugues I had written in the comments that I didn't bother converting your strings to float as you could do that yourself. I've updated the code now to show you how you can –  Jul 19 '18 at 07:38
  • sorry i missed that part. Now it's working, although your code df3[idx].T = strconv.ParseFloat(row[28], 64) returns multiple-value strconv.ParseFloat() in single-value context. I had to modifiy it that way: df3[idx].T, err = strconv.ParseFloat(row[28], 64) as per https://stackoverflow.com/questions/42146467/string-to-float64-multiple-value-strconv-parsefloat-in-single-value-context I can mark you code as the correct answer, but can you update it ? – Hugues Jul 19 '18 at 07:50