mirror of
https://github.com/lukewilson2002/autotrader.git
synced 2025-06-15 08:23:51 +00:00
Use a page to display multiple charts
This commit is contained in:
parent
a9df8cd8e3
commit
8fcd7f5cc9
@ -9,6 +9,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-echarts/go-echarts/v2/charts"
|
"github.com/go-echarts/go-echarts/v2/charts"
|
||||||
|
"github.com/go-echarts/go-echarts/v2/components"
|
||||||
"github.com/go-echarts/go-echarts/v2/opts"
|
"github.com/go-echarts/go-echarts/v2/opts"
|
||||||
"golang.org/x/exp/rand"
|
"golang.org/x/exp/rand"
|
||||||
)
|
)
|
||||||
@ -23,29 +24,33 @@ func Backtest(trader *Trader) {
|
|||||||
switch broker := trader.Broker.(type) {
|
switch broker := trader.Broker.(type) {
|
||||||
case *TestBroker:
|
case *TestBroker:
|
||||||
trader.Init() // Initialize the trader and strategy.
|
trader.Init() // Initialize the trader and strategy.
|
||||||
|
start := time.Now()
|
||||||
for !trader.EOF {
|
for !trader.EOF {
|
||||||
trader.Tick() // Allow the trader to process the current candlesticks.
|
trader.Tick() // Allow the trader to process the current candlesticks.
|
||||||
broker.Advance() // Give the trader access to the next candlestick.
|
broker.Advance() // Give the trader access to the next candlestick.
|
||||||
}
|
}
|
||||||
log.Println("Backtest complete.")
|
log.Println("Backtest complete. Opening report...")
|
||||||
log.Println("Stats:")
|
|
||||||
log.Println(trader.Stats().String())
|
|
||||||
|
|
||||||
|
page := components.NewPage()
|
||||||
|
|
||||||
|
// Create a new line chart based on account equity and add it to the page.
|
||||||
chart := charts.NewLine()
|
chart := charts.NewLine()
|
||||||
chart.SetGlobalOptions(charts.WithTitleOpts(opts.Title{
|
chart.SetGlobalOptions(charts.WithTitleOpts(opts.Title{
|
||||||
Title: fmt.Sprintf("Backtest (%s)", time.Now().Format(time.DateTime)),
|
Title: fmt.Sprintf("Backtest (%s)", time.Now().Format(time.DateTime)),
|
||||||
Subtitle: fmt.Sprintf("%s %s %T", trader.Symbol, trader.Frequency, trader.Strategy),
|
Subtitle: fmt.Sprintf("%s %s %T (took %.2f seconds)", trader.Symbol, trader.Frequency, trader.Strategy, time.Since(start).Seconds()),
|
||||||
}))
|
}))
|
||||||
chart.SetXAxis(seriesStringArray(trader.Stats().Dates())).
|
chart.SetXAxis(seriesStringArray(trader.Stats().Dates())).
|
||||||
AddSeries("Equity", lineDataFromSeries(trader.Stats().Series("Equity"))).
|
AddSeries("Equity", lineDataFromSeries(trader.Stats().Series("Equity"))).
|
||||||
AddSeries("Drawdown", lineDataFromSeries(trader.Stats().Series("Drawdown")))
|
AddSeries("Drawdown", lineDataFromSeries(trader.Stats().Series("Drawdown")))
|
||||||
|
|
||||||
// Draw the chart to a file.
|
page.AddCharts(chart)
|
||||||
|
|
||||||
|
// Draw the page to a file.
|
||||||
f, err := os.Create("backtest.html")
|
f, err := os.Create("backtest.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
chart.Render(f)
|
page.Render(f)
|
||||||
f.Close()
|
f.Close()
|
||||||
|
|
||||||
// Open the chart in the default browser.
|
// Open the chart in the default browser.
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
|
|
||||||
auto "github.com/fivemoreminix/autotrader"
|
auto "github.com/fivemoreminix/autotrader"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,7 +14,6 @@ func (s *SMAStrategy) Init(_ *auto.Trader) {
|
|||||||
func (s *SMAStrategy) Next(t *auto.Trader) {
|
func (s *SMAStrategy) Next(t *auto.Trader) {
|
||||||
sma1 := t.Data().Closes().Rolling(s.period1).Mean()
|
sma1 := t.Data().Closes().Rolling(s.period1).Mean()
|
||||||
sma2 := t.Data().Closes().Rolling(s.period2).Mean()
|
sma2 := t.Data().Closes().Rolling(s.period2).Mean()
|
||||||
log.Println(t.Data().Close(-1) - sma1.Float(-1))
|
|
||||||
// If the shorter SMA crosses above the longer SMA, buy.
|
// If the shorter SMA crosses above the longer SMA, buy.
|
||||||
if crossover(sma1, sma2) {
|
if crossover(sma1, sma2) {
|
||||||
t.Buy(1000)
|
t.Buy(1000)
|
||||||
|
@ -99,7 +99,7 @@ func (t *Trader) Tick() {
|
|||||||
"Drawdown": func() float64 {
|
"Drawdown": func() float64 {
|
||||||
var bal float64
|
var bal float64
|
||||||
if t.stats.Len() > 0 {
|
if t.stats.Len() > 0 {
|
||||||
bal = t.stats.Value("Equity", 0).(float64) // Take starting balance
|
bal = t.stats.Float("Equity", 0) // Take starting balance
|
||||||
} else {
|
} else {
|
||||||
bal = t.Broker.NAV()
|
bal = t.Broker.NAV()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user