Files
gocryptotrader/backtester/report/chart_test.go
Scott 85403fe801 exchange/order/limits: Migrate to new package and integrate with exchanges (#1860)
* move limits, transition to key gen

* rollout NewExchangePairAssetKey everywhere

* test improvements

* self-review fixes

* ok, lets go

* fix merge issue

* slower value func,assertify,drop IsValidPairString

* remove binance reference for backtesting test

* Redundant nil checks removed due to redundancy

* Update order_test.go

* Move limits back into /exchanges/

* puts limits in a different box again

* SHAZBERT SPECIAL SUGGESTIONS

* Update gateio_wrapper.go

* fixes all build issues

* Many niteroos!

* something has gone awry

* bugfix

* gk's everywhere nits

* lint

* extra lint

* re-remove IsValidPairString

* lint fix

* standardise test

* revert some bads

* dupe rm

* another revert 360 mcgee

* un-in-revertify

* Update exchange/order/limits/levels_test.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* fix

* Update exchanges/binance/binance_test.go

HERE'S HOPING GITHUB FORMATS THIS CORRECTLY!

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* update text

* rn func, same line err gk4202000

---------

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>
2025-08-26 12:30:21 +10:00

209 lines
5.9 KiB
Go

package report
import (
"testing"
"time"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio"
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/statistics"
evkline "github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/kline"
"github.com/thrasher-corp/gocryptotrader/backtester/funding"
gctcommon "github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/key"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/futures"
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
)
func TestCreateUSDTotalsChart(t *testing.T) {
t.Parallel()
_, err := createUSDTotalsChart(nil, nil)
assert.ErrorIs(t, err, gctcommon.ErrNilPointer)
tt := time.Now()
items := []statistics.ValueAtTime{
{
Time: tt,
Value: decimal.NewFromInt(1337),
Set: true,
},
}
_, err = createUSDTotalsChart(items, nil)
assert.ErrorIs(t, err, gctcommon.ErrNilPointer)
stats := []statistics.FundingItemStatistics{
{
ReportItem: &funding.ReportItem{
Snapshots: []funding.ItemSnapshot{
{
Time: tt,
USDValue: decimal.NewFromInt(1337),
},
},
},
},
}
resp, err := createUSDTotalsChart(items, stats)
require.NoError(t, err)
if len(resp.Data) == 0 {
t.Fatal("expected not nil")
}
if resp.Data[0].Name != "Total USD value" {
t.Error("expected not nil")
}
if resp.Data[0].LinePlots[0].Value != 1337 {
t.Error("expected not nil")
}
}
func TestCreateHoldingsOverTimeChart(t *testing.T) {
t.Parallel()
_, err := createHoldingsOverTimeChart(nil)
assert.ErrorIs(t, err, gctcommon.ErrNilPointer)
tt := time.Now()
items := []statistics.FundingItemStatistics{
{
ReportItem: &funding.ReportItem{
Exchange: "hello",
Asset: asset.Spot,
Currency: currency.BTC,
Snapshots: []funding.ItemSnapshot{
{
Time: tt,
Available: decimal.NewFromInt(1337),
},
{
Time: tt,
},
},
},
},
}
resp, err := createHoldingsOverTimeChart(items)
assert.NoError(t, err)
if !resp.ShowZeroDisclaimer {
t.Error("expected ShowZeroDisclaimer")
}
}
func TestCreatePNLCharts(t *testing.T) {
t.Parallel()
_, err := createPNLCharts(nil)
assert.ErrorIs(t, err, gctcommon.ErrNilPointer)
tt := time.Now()
var d Data
d.Statistics = &statistics.Statistic{}
d.Statistics.ExchangeAssetPairStatistics = make(map[key.ExchangeAssetPair]*statistics.CurrencyPairStatistic)
d.Statistics.ExchangeAssetPairStatistics[key.NewExchangeAssetPair(testExchange, asset.Spot, currency.NewBTCUSDT())] = &statistics.CurrencyPairStatistic{
Events: []statistics.DataAtOffset{
{
PNL: &portfolio.PNLSummary{
Result: futures.PNLResult{
Time: tt,
UnrealisedPNL: decimal.NewFromInt(1337),
RealisedPNLBeforeFees: decimal.NewFromInt(1337),
RealisedPNL: decimal.NewFromInt(1337),
Price: decimal.NewFromInt(1337),
Exposure: decimal.NewFromInt(1337),
Direction: gctorder.Short,
},
},
},
},
}
err = d.SetKlineData(&gctkline.Item{
Exchange: testExchange,
Pair: currency.NewBTCUSDT(),
Asset: asset.Spot,
Interval: gctkline.OneDay,
Candles: []gctkline.Candle{
{
Time: tt,
Open: 1336,
High: 1338,
Low: 1336,
Close: 1337,
Volume: 1337,
},
},
})
assert.NoError(t, err)
err = d.enhanceCandles()
assert.NoError(t, err)
_, err = createPNLCharts(d.Statistics.ExchangeAssetPairStatistics)
assert.NoError(t, err)
}
func TestCreateFuturesSpotDiffChart(t *testing.T) {
t.Parallel()
_, err := createFuturesSpotDiffChart(nil)
assert.ErrorIs(t, err, gctcommon.ErrNilPointer)
tt := time.Now()
cp := currency.NewBTCUSD()
cp2 := currency.NewPair(currency.BTC, currency.DOGE)
var d Data
d.Statistics = &statistics.Statistic{}
d.Statistics.ExchangeAssetPairStatistics = make(map[key.ExchangeAssetPair]*statistics.CurrencyPairStatistic)
d.Statistics.ExchangeAssetPairStatistics[key.NewExchangeAssetPair(testExchange, asset.Spot, currency.NewBTCUSD())] = &statistics.CurrencyPairStatistic{
Currency: cp,
Events: []statistics.DataAtOffset{
{
Time: tt,
DataEvent: &evkline.Kline{Close: decimal.NewFromInt(1337)},
PNL: &portfolio.PNLSummary{
Result: futures.PNLResult{
Time: tt,
UnrealisedPNL: decimal.NewFromInt(1337),
RealisedPNLBeforeFees: decimal.NewFromInt(1337),
RealisedPNL: decimal.NewFromInt(1337),
Price: decimal.NewFromInt(1337),
Exposure: decimal.NewFromInt(1337),
Direction: gctorder.Buy,
},
},
},
},
}
d.Statistics.ExchangeAssetPairStatistics[key.NewExchangeAssetPair(testExchange, asset.Futures, currency.NewPair(currency.BTC, currency.DOGE))] = &statistics.CurrencyPairStatistic{
UnderlyingPair: cp,
Currency: cp2,
Events: []statistics.DataAtOffset{
{
Time: tt,
DataEvent: &evkline.Kline{Close: decimal.NewFromInt(1337)},
PNL: &portfolio.PNLSummary{
Result: futures.PNLResult{
Time: tt,
UnrealisedPNL: decimal.NewFromInt(1337),
RealisedPNLBeforeFees: decimal.NewFromInt(1337),
RealisedPNL: decimal.NewFromInt(1337),
Price: decimal.NewFromInt(1337),
Exposure: decimal.NewFromInt(1337),
Direction: gctorder.Short,
},
},
},
},
}
charty, err := createFuturesSpotDiffChart(d.Statistics.ExchangeAssetPairStatistics)
assert.NoError(t, err)
if len(charty.Data) == 0 {
t.Error("expected data")
}
}