mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-02 07:26:53 +00:00
common: update Errors type (#1129)
* common: adjust common error slice to allow multi errors.Is matching and conform to interface better * zb: forgot to save? * linties: fixies * linties: word change as well. * nitters: glorious * buts * nitters: fix glorious bug * Update common/common.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * nitters: shifty --------- Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io> Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
This commit is contained in:
@@ -14,8 +14,6 @@ import (
|
||||
|
||||
// CalculateResults calculates all statistics for the exchange, asset, currency pair
|
||||
func (c *CurrencyPairStatistic) CalculateResults(riskFreeRate decimal.Decimal) error {
|
||||
var errs gctcommon.Errors
|
||||
var err error
|
||||
first := c.Events[0]
|
||||
sep := fmt.Sprintf("%v %v %v |\t", first.DataEvent.GetExchange(), first.DataEvent.GetAssetType(), first.DataEvent.Pair())
|
||||
|
||||
@@ -54,7 +52,7 @@ func (c *CurrencyPairStatistic) CalculateResults(riskFreeRate decimal.Decimal) e
|
||||
c.StrategyMovement = last.Holdings.TotalValue.Sub(first.Holdings.TotalValue).Div(first.Holdings.TotalValue).Mul(oneHundred)
|
||||
}
|
||||
c.analysePNLGrowth()
|
||||
err = c.calculateHighestCommittedFunds()
|
||||
err := c.calculateHighestCommittedFunds()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -87,9 +85,10 @@ func (c *CurrencyPairStatistic) CalculateResults(riskFreeRate decimal.Decimal) e
|
||||
// ratio calculations as no movement has been made
|
||||
benchmarkRates = benchmarkRates[1:]
|
||||
returnsPerCandle = returnsPerCandle[1:]
|
||||
var errs error
|
||||
c.MaxDrawdown, err = CalculateBiggestEventDrawdown(allDataEvents)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
errs = gctcommon.AppendError(errs, err)
|
||||
}
|
||||
|
||||
interval := first.DataEvent.GetInterval()
|
||||
@@ -109,7 +108,7 @@ func (c *CurrencyPairStatistic) CalculateResults(riskFreeRate decimal.Decimal) e
|
||||
decimal.NewFromInt(int64(len(c.Events))),
|
||||
)
|
||||
if err != nil && !errors.Is(err, gctmath.ErrPowerDifferenceTooSmall) {
|
||||
errs = append(errs, err)
|
||||
errs = gctcommon.AppendError(errs, err)
|
||||
}
|
||||
c.CompoundAnnualGrowthRate = cagr
|
||||
}
|
||||
@@ -124,10 +123,7 @@ func (c *CurrencyPairStatistic) CalculateResults(riskFreeRate decimal.Decimal) e
|
||||
c.UnrealisedPNL = last.PNL.GetUnrealisedPNL().PNL
|
||||
c.RealisedPNL = last.PNL.GetRealisedPNL().PNL
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
return nil
|
||||
return errs
|
||||
}
|
||||
|
||||
func (c *CurrencyPairStatistic) calculateHighestCommittedFunds() error {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package statistics
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
@@ -71,8 +72,7 @@ func (s *Statistic) PrintTotalResults() {
|
||||
// grouped by time to allow a clearer picture of events
|
||||
func (s *Statistic) PrintAllEventsChronologically() {
|
||||
log.Info(common.Statistics, common.CMDColours.H1+"------------------Events-------------------------------------"+common.CMDColours.Default)
|
||||
var errs gctcommon.Errors
|
||||
var err error
|
||||
var errs error
|
||||
var results []eventOutputHolder
|
||||
for _, exchangeMap := range s.ExchangeAssetPairStatistics {
|
||||
for _, assetMap := range exchangeMap {
|
||||
@@ -81,25 +81,26 @@ func (s *Statistic) PrintAllEventsChronologically() {
|
||||
for i := range currencyStatistic.Events {
|
||||
var result string
|
||||
var tt time.Time
|
||||
var err error
|
||||
switch {
|
||||
case currencyStatistic.Events[i].FillEvent != nil:
|
||||
result, err = s.CreateLog(currencyStatistic.Events[i].FillEvent)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
errs = gctcommon.AppendError(errs, err)
|
||||
continue
|
||||
}
|
||||
tt = currencyStatistic.Events[i].FillEvent.GetTime()
|
||||
case currencyStatistic.Events[i].SignalEvent != nil:
|
||||
result, err = s.CreateLog(currencyStatistic.Events[i].SignalEvent)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
errs = gctcommon.AppendError(errs, err)
|
||||
continue
|
||||
}
|
||||
tt = currencyStatistic.Events[i].SignalEvent.GetTime()
|
||||
case currencyStatistic.Events[i].DataEvent != nil:
|
||||
result, err = s.CreateLog(currencyStatistic.Events[i].DataEvent)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
errs = gctcommon.AppendError(errs, err)
|
||||
continue
|
||||
}
|
||||
tt = currencyStatistic.Events[i].DataEvent.GetTime()
|
||||
@@ -121,10 +122,10 @@ func (s *Statistic) PrintAllEventsChronologically() {
|
||||
log.Info(common.Statistics, results[i].Events[j])
|
||||
}
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
if errs != nil {
|
||||
log.Info(common.Statistics, common.CMDColours.Error+"------------------Errors-------------------------------------"+common.CMDColours.Default)
|
||||
for i := range errs {
|
||||
log.Error(common.Statistics, errs[i].Error())
|
||||
for err := errors.Unwrap(errs); err != nil; err = errors.Unwrap(errs) {
|
||||
log.Error(common.Statistics, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -206,7 +207,6 @@ func (s *Statistic) CreateLog(data common.Event) (string, error) {
|
||||
|
||||
// PrintResults outputs all calculated statistics to the command line
|
||||
func (c *CurrencyPairStatistic) PrintResults(e string, a asset.Item, p currency.Pair, usingExchangeLevelFunding bool) {
|
||||
var errs gctcommon.Errors
|
||||
sort.Slice(c.Events, func(i, j int) bool {
|
||||
return c.Events[i].Time.Before(c.Events[j].Time)
|
||||
})
|
||||
@@ -302,12 +302,6 @@ func (c *CurrencyPairStatistic) PrintResults(e string, a asset.Item, p currency.
|
||||
log.Infof(common.CurrencyStatistics, "%s Final Unrealised PNL: %s", sep, convert.DecimalToHumanFriendlyString(unrealised.PNL, 8, ".", ","))
|
||||
log.Infof(common.CurrencyStatistics, "%s Final Realised PNL: %s", sep, convert.DecimalToHumanFriendlyString(realised.PNL, 8, ".", ","))
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
log.Info(common.CurrencyStatistics, common.CMDColours.Error+"------------------Errors-------------------------------------"+common.CMDColours.Default)
|
||||
for i := range errs {
|
||||
log.Error(common.CurrencyStatistics, errs[i].Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PrintResults outputs all calculated funding statistics to the command line
|
||||
|
||||
@@ -74,20 +74,16 @@ func (s *Strategy) SupportsSimultaneousProcessing() bool {
|
||||
// For dollarcostaverage, the strategy is always "buy", so it uses the OnSignal function
|
||||
func (s *Strategy) OnSimultaneousSignals(d []data.Handler, _ funding.IFundingTransferer, _ portfolio.Handler) ([]signal.Event, error) {
|
||||
var resp []signal.Event
|
||||
var errs gctcommon.Errors
|
||||
var errs error
|
||||
for i := range d {
|
||||
sigEvent, err := s.OnSignal(d[i], nil, nil)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
errs = gctcommon.AppendError(errs, err)
|
||||
} else {
|
||||
resp = append(resp, sigEvent)
|
||||
}
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
return nil, errs
|
||||
}
|
||||
return resp, nil
|
||||
return resp, errs
|
||||
}
|
||||
|
||||
// SetCustomSettings not required for DCA
|
||||
|
||||
@@ -114,7 +114,7 @@ func (s *Strategy) SupportsSimultaneousProcessing() bool {
|
||||
// in allowing a strategy to only place an order for X currency if Y currency's price is Z
|
||||
func (s *Strategy) OnSimultaneousSignals(d []data.Handler, _ funding.IFundingTransferer, _ portfolio.Handler) ([]signal.Event, error) {
|
||||
var resp []signal.Event
|
||||
var errs gctcommon.Errors
|
||||
var errs error
|
||||
for i := range d {
|
||||
latest, err := d[i].Latest()
|
||||
if err != nil {
|
||||
@@ -122,16 +122,16 @@ func (s *Strategy) OnSimultaneousSignals(d []data.Handler, _ funding.IFundingTra
|
||||
}
|
||||
sigEvent, err := s.OnSignal(d[i], nil, nil)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("%v %v %v %w", latest.GetExchange(), latest.GetAssetType(), latest.Pair(), err))
|
||||
errs = gctcommon.AppendError(errs, fmt.Errorf("%v %v %v %w",
|
||||
latest.GetExchange(),
|
||||
latest.GetAssetType(),
|
||||
latest.Pair(),
|
||||
err))
|
||||
} else {
|
||||
resp = append(resp, sigEvent)
|
||||
}
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
return nil, errs
|
||||
}
|
||||
return resp, nil
|
||||
return resp, errs
|
||||
}
|
||||
|
||||
// SetCustomSettings allows a user to modify the RSI limits in their config
|
||||
|
||||
Reference in New Issue
Block a user