go-chart/_examples/candlestick_series/main.go

83 lines
1.6 KiB
Go
Raw Normal View History

2017-05-15 01:33:48 +02:00
package main
import (
"math/rand"
2017-05-15 01:33:48 +02:00
"net/http"
"time"
chart "github.com/wcharczuk/go-chart"
util "github.com/wcharczuk/go-chart/util"
)
func stockData() (times []time.Time, prices []float64) {
start := time.Date(2017, 05, 15, 9, 30, 0, 0, util.Date.Eastern())
price := 256.0
for day := 0; day < 60; day++ {
cursor := start.AddDate(0, 0, day)
2017-05-15 01:33:48 +02:00
if util.Date.IsNYSEHoliday(cursor) {
continue
}
2017-05-15 01:33:48 +02:00
for minute := 0; minute < ((6 * 60) + 30); minute++ {
cursor = cursor.Add(time.Minute)
if rand.Float64() >= 0.5 {
price = price + (rand.Float64() * (price * 0.01))
} else {
price = price - (rand.Float64() * (price * 0.01))
}
2017-05-15 01:33:48 +02:00
times = append(times, cursor)
prices = append(prices, price)
}
2017-05-15 01:33:48 +02:00
}
return
}
func drawChart(res http.ResponseWriter, req *http.Request) {
xv, yv := stockData()
priceSeries := chart.TimeSeries{
Name: "SPY",
Style: chart.Style{
Show: false,
2017-05-15 01:33:48 +02:00
StrokeColor: chart.GetDefaultColor(0),
},
XValues: xv,
YValues: yv,
}
candleSeries := chart.CandlestickSeries{
Name: "SPY",
XValues: xv,
YValues: yv,
}
graph := chart.Chart{
XAxis: chart.XAxis{
Style: chart.Style{Show: true, FontSize: 8, TextRotationDegrees: 45},
TickPosition: chart.TickPositionUnderTick,
Range: &chart.MarketHoursRange{},
2017-05-15 01:33:48 +02:00
},
YAxis: chart.YAxis{
Style: chart.Style{Show: true},
},
Series: []chart.Series{
candleSeries,
priceSeries,
},
}
res.Header().Set("Content-Type", "image/png")
err := graph.Render(chart.PNG, res)
if err != nil {
panic(err)
}
}
func main() {
http.HandleFunc("/", drawChart)
http.ListenAndServe(":8080", nil)
}