strategy/supertrend: use pkg/data/tsv for tsv output

This commit is contained in:
Andy Cheng 2022-08-16 15:49:08 +08:00
parent f7feb7e0fc
commit 2b638d1f8f
2 changed files with 14 additions and 7 deletions

View File

@ -21,6 +21,15 @@ func NewWriterFile(filename string) (*Writer, error) {
return NewWriter(f), nil return NewWriter(f), nil
} }
func AppendWriterFile(filename string) (*Writer, error) {
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return nil, err
}
return NewWriter(f), nil
}
func NewWriter(file io.WriteCloser) *Writer { func NewWriter(file io.WriteCloser) *Writer {
tsv := csv.NewWriter(file) tsv := csv.NewWriter(file)
tsv.Comma = '\t' tsv.Comma = '\t'

View File

@ -3,8 +3,8 @@ package supertrend
import ( import (
"bufio" "bufio"
"context" "context"
"encoding/csv"
"fmt" "fmt"
"github.com/c9s/bbgo/pkg/data/tsv"
"github.com/c9s/bbgo/pkg/risk" "github.com/c9s/bbgo/pkg/risk"
"github.com/fatih/color" "github.com/fatih/color"
"os" "os"
@ -312,15 +312,13 @@ func (s *Strategy) PrintResult(o *os.File) {
hiyellow(f, "\n") hiyellow(f, "\n")
if s.AccumulatedProfitReportTsv != "" { if s.AccumulatedProfitReportTsv != "" {
tf, err := os.OpenFile(s.AccumulatedProfitReportTsv, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644) tsvwiter, err := tsv.AppendWriterFile(s.AccumulatedProfitReportTsv)
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer tf.Close() defer tsvwiter.Close()
csvwiter := csv.NewWriter(tf) // Output symbol, total acc. profit, acc. profit 60MA, 7d acc. profit
csvwiter.Comma = '\t' tsvwiter.Write([]string{s.Symbol, s.accumulatedProfit.String(), fmt.Sprintf("%f", s.accumulatedProfitMA.Last()), fmt.Sprintf("%f", s.dailyAccumulatedProfits.Sum())})
defer csvwiter.Flush()
csvwiter.Write([]string{s.Symbol, s.accumulatedProfit.String(), fmt.Sprintf("%f", s.accumulatedProfitMA.Last()), fmt.Sprintf("%f", s.dailyAccumulatedProfits.Sum())})
} }
} }