Files
gocryptotrader/backtester/report/report.go
Ryan O'Hara-Reid 4e135c9590 logger: reduce go routine generation (#992)
* logger: reduce go routine generation

* logger: shift most of processing and prep work to the worker pool, add pool for fields because each log we are pushing the struct to the heap, has better segregation now and includes a buffer in scope instead of relying on a pool

* logger: shift fmt package calls to worker pool

* logger: conform tests to new design

* linter: fix issues

* Update log/logger_test.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update log/logger_test.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* UN-GLORIOUS: nits

* logger: Handle config variable

* logger: NITERINOS BY GLORIOUS CODE

* logger: revert

* glorious: nits

* Panic at the disco: fix

* Panic at the disco: fix

* logger: make sure logger closed and job channel emptied on start up error

* fix tests

* logger: reduce globals

* logger: finished reduces globals, reduce workers to one too keep everything in line.

* logger: remove comments

* logger/exhchange: linter issues

* db/test: fix linter

* logger: add tests shift wait before unlock

* logger: consolidate worker code; fix linter issue and make sure we can sustain writing for external testing.

* logger: fix race and warn for conflict in config

* logger: fix name and add to tests

* logger: remove zero value field

* glorious: panic fix and removal of code

* logger: reinstate channels in close

* logger: shift reinstate processing to SetupGlobalLogger

* logger: segregate config.json from internal log.Config

* logger: fix silly mistake that is silly

* engine: Add protection for nil issues and implement new constructor in tests

* logger: Force singular mutex usage throughout package, throw away funcs that are not used outside of this package, unexport a bunch. Fix tests.

* logger: actually set advanced settings

* Update log/loggers.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update log/loggers.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update log/loggers.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update log/loggers.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update log/logger_multiwriter.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits

* logger: test issue when not purging temp file and contents

* loggertest: add more protections for the panics

* linter: fix

* glorious: nits

* cleanup

* logger: linter fix

* linter: fix(?) :/

* linter: revert change

* linter: fix

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
2022-10-24 11:46:18 +11:00

236 lines
7.6 KiB
Go

package report
import (
"fmt"
"html/template"
"os"
"path/filepath"
"strings"
"time"
"github.com/shopspring/decimal"
"github.com/thrasher-corp/gocryptotrader/backtester/common"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/log"
)
// GenerateReport sends final data from statistics to a template
// to create a lovely final report for someone to view
func (d *Data) GenerateReport() error {
if d.TemplatePath == "" || d.OutputPath == "" {
return nil
}
log.Info(common.Report, "Generating report")
err := d.enhanceCandles()
if err != nil {
return err
}
for i := range d.OriginalCandles {
for j := range d.OriginalCandles[i].Candles {
if d.OriginalCandles[i].Candles[j].ValidationIssues == "" {
continue
}
d.Warnings = append(d.Warnings, Warning{
Exchange: d.OriginalCandles[i].Exchange,
Asset: d.OriginalCandles[i].Asset,
Pair: d.OriginalCandles[i].Pair,
Message: fmt.Sprintf("candle data %v", d.OriginalCandles[i].Candles[j].ValidationIssues),
})
}
}
for i := range d.EnhancedCandles {
if len(d.EnhancedCandles[i].Candles) >= maxChartLimit {
d.EnhancedCandles[i].IsOverLimit = true
d.EnhancedCandles[i].Candles = d.EnhancedCandles[i].Candles[:maxChartLimit]
}
}
if d.Statistics.FundingStatistics != nil {
d.HoldingsOverTimeChart, err = createHoldingsOverTimeChart(d.Statistics.FundingStatistics.Items)
if err != nil {
return err
}
if !d.Statistics.FundingStatistics.Report.DisableUSDTracking {
d.USDTotalsChart, err = createUSDTotalsChart(d.Statistics.FundingStatistics.TotalUSDStatistics.HoldingValues, d.Statistics.FundingStatistics.Items)
if err != nil {
return err
}
}
}
if d.Statistics.HasCollateral {
d.PNLOverTimeChart, err = createPNLCharts(d.Statistics.ExchangeAssetPairStatistics)
if err != nil {
return err
}
d.FuturesSpotDiffChart, err = createFuturesSpotDiffChart(d.Statistics.ExchangeAssetPairStatistics)
if err != nil {
return err
}
}
tmpl := template.Must(
template.ParseFiles(d.TemplatePath),
)
fn := d.Config.Nickname
if fn != "" {
fn += "-"
}
fn += d.Statistics.StrategyName + "-"
fn += time.Now().Format("2006-01-02-15-04-05")
fileName, err := common.GenerateFileName(fn, "html")
if err != nil {
return err
}
var f *os.File
f, err = os.Create(
filepath.Join(d.OutputPath,
fileName,
),
)
if err != nil {
return err
}
defer func() {
err = f.Close()
if err != nil {
log.Error(common.Report, err)
}
}()
err = tmpl.Execute(f, d)
if err != nil {
return err
}
log.Infof(common.Report, "Successfully saved report to %v", filepath.Join(d.OutputPath, fileName))
return nil
}
// AddKlineItem appends a SET of candles for the report to enhance upon
// generation
func (d *Data) AddKlineItem(k *kline.Item) {
d.OriginalCandles = append(d.OriginalCandles, k)
}
// UpdateItem updates an existing kline item for LIVE data usage
func (d *Data) UpdateItem(k *kline.Item) {
if len(d.OriginalCandles) == 0 {
d.OriginalCandles = append(d.OriginalCandles, k)
} else {
d.OriginalCandles[0].Candles = append(d.OriginalCandles[0].Candles, k.Candles...)
d.OriginalCandles[0].RemoveDuplicates()
}
}
// enhanceCandles will enhance candle data with order information allowing
// report charts to have annotations to highlight buy and sell events
func (d *Data) enhanceCandles() error {
if len(d.OriginalCandles) == 0 {
return errNoCandles
}
if d.Statistics == nil {
return errStatisticsUnset
}
d.Statistics.RiskFreeRate = d.Statistics.RiskFreeRate.Mul(decimal.NewFromInt(100))
for intVal := range d.OriginalCandles {
lookup := d.OriginalCandles[intVal]
enhancedKline := EnhancedKline{
Exchange: lookup.Exchange,
Asset: lookup.Asset,
Pair: lookup.Pair,
Interval: lookup.Interval,
Watermark: fmt.Sprintf("%s - %s - %s", strings.Title(lookup.Exchange), lookup.Asset.String(), lookup.Pair.Upper()), //nolint:staticcheck // Ignore Title usage warning
}
statsForCandles :=
d.Statistics.ExchangeAssetPairStatistics[lookup.Exchange][lookup.Asset][lookup.Pair]
if statsForCandles == nil {
continue
}
requiresIteration := false
if len(statsForCandles.Events) != len(d.OriginalCandles[intVal].Candles) {
requiresIteration = true
}
for j := range d.OriginalCandles[intVal].Candles {
_, offset := time.Now().Zone()
tt := d.OriginalCandles[intVal].Candles[j].Time.Add(time.Duration(offset) * time.Second)
enhancedCandle := DetailedCandle{
UnixMilli: tt.UTC().UnixMilli(),
Open: d.OriginalCandles[intVal].Candles[j].Open,
High: d.OriginalCandles[intVal].Candles[j].High,
Low: d.OriginalCandles[intVal].Candles[j].Low,
Close: d.OriginalCandles[intVal].Candles[j].Close,
Volume: d.OriginalCandles[intVal].Candles[j].Volume,
VolumeColour: "rgba(50, 204, 30, 0.5)",
}
if j != 0 {
if d.OriginalCandles[intVal].Candles[j].Close < d.OriginalCandles[intVal].Candles[j-1].Close {
enhancedCandle.VolumeColour = "rgba(232, 3, 3, 0.5)"
}
}
if !requiresIteration {
if statsForCandles.Events[intVal].Time.Equal(d.OriginalCandles[intVal].Candles[j].Time) &&
(statsForCandles.Events[intVal].SignalEvent == nil || statsForCandles.Events[intVal].SignalEvent.GetDirection() == order.MissingData) &&
len(enhancedKline.Candles) > 0 {
enhancedCandle.copyCloseFromPreviousEvent(&enhancedKline)
}
} else {
for k := range statsForCandles.Events {
if statsForCandles.Events[k].SignalEvent.GetTime().Equal(d.OriginalCandles[intVal].Candles[j].Time) &&
statsForCandles.Events[k].SignalEvent.GetDirection() == order.MissingData &&
len(enhancedKline.Candles) > 0 {
enhancedCandle.copyCloseFromPreviousEvent(&enhancedKline)
}
}
}
for k := range statsForCandles.FinalOrders.Orders {
if statsForCandles.FinalOrders.Orders[k].Order == nil ||
!statsForCandles.FinalOrders.Orders[k].Order.Date.Equal(d.OriginalCandles[intVal].Candles[j].Time) {
continue
}
// an order was placed here, can enhance chart!
enhancedCandle.MadeOrder = true
enhancedCandle.OrderAmount = decimal.NewFromFloat(statsForCandles.FinalOrders.Orders[k].Order.Amount)
enhancedCandle.PurchasePrice = statsForCandles.FinalOrders.Orders[k].Order.Price
enhancedCandle.OrderDirection = statsForCandles.FinalOrders.Orders[k].Order.Side
if enhancedCandle.OrderDirection == order.Buy {
enhancedCandle.Colour = "green"
enhancedCandle.Position = "aboveBar"
enhancedCandle.Shape = "arrowDown"
} else if enhancedCandle.OrderDirection == order.Sell {
enhancedCandle.Colour = "red"
enhancedCandle.Position = "belowBar"
enhancedCandle.Shape = "arrowUp"
}
enhancedCandle.Text = enhancedCandle.OrderDirection.String()
break
}
enhancedKline.Candles = append(enhancedKline.Candles, enhancedCandle)
}
d.EnhancedCandles = append(d.EnhancedCandles, enhancedKline)
}
return nil
}
func (d *DetailedCandle) copyCloseFromPreviousEvent(ek *EnhancedKline) {
// if the data is missing, ensure that all values just continue the previous candle's close price visually
d.Open = ek.Candles[len(ek.Candles)-1].Close
d.High = ek.Candles[len(ek.Candles)-1].Close
d.Low = ek.Candles[len(ek.Candles)-1].Close
d.Close = ek.Candles[len(ek.Candles)-1].Close
d.Colour = "white"
d.Position = "aboveBar"
d.Shape = "arrowDown"
d.Text = order.MissingData.String()
}
// UseDarkMode sets whether to use a dark theme by default
// for the html generated report
func (d *Data) UseDarkMode(use bool) {
d.UseDarkTheme = use
}