mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
backtester: Refactor sortByMFI to use slices.SortFunc (#2122)
* 2122: slices funcs * Update backtester/eventhandlers/strategies/top2bottom2/top2bottom2.go Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com> --------- Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com> Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
@@ -3,7 +3,7 @@ package top2bottom2
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
@@ -69,22 +69,6 @@ type mfiFundEvent struct {
|
|||||||
funds funding.IFundReader
|
funds funding.IFundReader
|
||||||
}
|
}
|
||||||
|
|
||||||
// ByPrice used for sorting orders by order date
|
|
||||||
type byMFI []mfiFundEvent
|
|
||||||
|
|
||||||
func (b byMFI) Len() int { return len(b) }
|
|
||||||
func (b byMFI) Less(i, j int) bool { return b[i].mfi.LessThan(b[j].mfi) }
|
|
||||||
func (b byMFI) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
|
||||||
|
|
||||||
// sortOrdersByPrice the caller function to sort orders
|
|
||||||
func sortByMFI(o *[]mfiFundEvent, reverse bool) {
|
|
||||||
if reverse {
|
|
||||||
sort.Sort(sort.Reverse(byMFI(*o)))
|
|
||||||
} else {
|
|
||||||
sort.Sort(byMFI(*o))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// OnSimultaneousSignals analyses multiple data points simultaneously, allowing flexibility
|
// OnSimultaneousSignals analyses multiple data points simultaneously, allowing flexibility
|
||||||
// in allowing a strategy to only place an order for X currency if Y currency's price is Z
|
// 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, f funding.IFundingTransferer, _ portfolio.Handler) ([]signal.Event, error) {
|
func (s *Strategy) OnSimultaneousSignals(d []data.Handler, f funding.IFundingTransferer, _ portfolio.Handler) ([]signal.Event, error) {
|
||||||
@@ -181,7 +165,7 @@ func (s *Strategy) selectTopAndBottomPerformers(mfiFundEvents []mfiFundEvent, re
|
|||||||
if len(mfiFundEvents) == 0 {
|
if len(mfiFundEvents) == 0 {
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
sortByMFI(&mfiFundEvents, true)
|
slices.SortFunc(mfiFundEvents, func(a, b mfiFundEvent) int { return b.mfi.Compare(a.mfi) })
|
||||||
buyingOrSelling := false
|
buyingOrSelling := false
|
||||||
for i := range mfiFundEvents {
|
for i := range mfiFundEvents {
|
||||||
if i < 2 && mfiFundEvents[i].mfi.GreaterThanOrEqual(s.mfiHigh) {
|
if i < 2 && mfiFundEvents[i].mfi.GreaterThanOrEqual(s.mfiHigh) {
|
||||||
@@ -191,7 +175,7 @@ func (s *Strategy) selectTopAndBottomPerformers(mfiFundEvents []mfiFundEvent, re
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sortByMFI(&mfiFundEvents, false)
|
slices.Reverse(mfiFundEvents)
|
||||||
for i := range mfiFundEvents {
|
for i := range mfiFundEvents {
|
||||||
if i < 2 && mfiFundEvents[i].mfi.LessThanOrEqual(s.mfiLow) {
|
if i < 2 && mfiFundEvents[i].mfi.LessThanOrEqual(s.mfiLow) {
|
||||||
mfiFundEvents[i].event.SetDirection(order.Buy)
|
mfiFundEvents[i].event.SetDirection(order.Buy)
|
||||||
|
|||||||
Reference in New Issue
Block a user