Skip to content

Commit

Permalink
Merge pull request #1183 from c9s/refactor/indicator-replace-index-call
Browse files Browse the repository at this point in the history
REFACTOR: [indicator] replace all Index(i) callers
  • Loading branch information
c9s authored Jun 1, 2023
2 parents 10e84c1 + c9c13b2 commit 9435478
Show file tree
Hide file tree
Showing 32 changed files with 139 additions and 266 deletions.
12 changes: 2 additions & 10 deletions pkg/indicator/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,11 @@ func (inc *AD) Update(high, low, cloze, volume float64) {
}

func (inc *AD) Last(i int) float64 {
length := len(inc.Values)
if length == 0 || length-i-1 < 0 {
return 0
}
return inc.Values[length-i-1]
return inc.Values.Last(i)
}

func (inc *AD) Index(i int) float64 {
length := len(inc.Values)
if length == 0 || length-i-1 < 0 {
return 0
}
return inc.Values[length-i-1]
return inc.Last(i)
}

func (inc *AD) Length() int {
Expand Down
10 changes: 2 additions & 8 deletions pkg/indicator/alma.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,11 @@ func (inc *ALMA) Update(value float64) {
}

func (inc *ALMA) Last(i int) float64 {
if i >= len(inc.Values) {
return 0
}
return inc.Values[len(inc.Values)-i-1]
return inc.Values.Last(i)
}

func (inc *ALMA) Index(i int) float64 {
if i >= len(inc.Values) {
return 0
}
return inc.Values[len(inc.Values)-i-1]
return inc.Last(i)
}

func (inc *ALMA) Length() int {
Expand Down
5 changes: 1 addition & 4 deletions pkg/indicator/atr.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ func (inc *ATR) Last(i int) float64 {
}

func (inc *ATR) Index(i int) float64 {
if inc.RMA == nil {
return 0
}
return inc.RMA.Index(i)
return inc.Last(i)
}

func (inc *ATR) Length() int {
Expand Down
5 changes: 1 addition & 4 deletions pkg/indicator/atrp.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ func (inc *ATRP) Last(i int) float64 {
}

func (inc *ATRP) Index(i int) float64 {
if inc.RMA == nil {
return 0
}
return inc.RMA.Index(i)
return inc.Last(i)
}

func (inc *ATRP) Length() int {
Expand Down
11 changes: 4 additions & 7 deletions pkg/indicator/emv.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ func (inc *EMV) Update(high, low, vol float64) {
inc.Values.Update(result)
}

func (inc *EMV) Index(i int) float64 {
if inc.Values == nil {
return 0
}
return inc.Values.Index(i)
}

func (inc *EMV) Last(i int) float64 {
if inc.Values == nil {
return 0
Expand All @@ -63,6 +56,10 @@ func (inc *EMV) Last(i int) float64 {
return inc.Values.Last(i)
}

func (inc *EMV) Index(i int) float64 {
return inc.Last(i)
}

func (inc *EMV) Length() int {
if inc.Values == nil {
return 0
Expand Down
4 changes: 0 additions & 4 deletions pkg/indicator/ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ func (inc *EWMA) Update(value float64) {
}

func (inc *EWMA) Last(i int) float64 {
if len(inc.Values) == 0 {
return 0
}

return inc.Values.Last(i)
}

Expand Down
14 changes: 4 additions & 10 deletions pkg/indicator/ghfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,18 @@ func (inc *GHFilter) update(value, uncertainty float64) {
inc.lastMeasurement = value
}

func (inc *GHFilter) Index(i int) float64 {
return inc.Last(i)
}

func (inc *GHFilter) Length() int {
if inc.Values == nil {
return 0
}
return inc.Values.Length()
}

func (inc *GHFilter) Last(i int) float64 {
if inc.Values == nil {
return 0.0
}
return inc.Values.Last(i)
}

func (inc *GHFilter) Index(i int) float64 {
return inc.Last(i)
}

// interfaces implementation check
var _ Simple = &GHFilter{}
var _ types.SeriesExtend = &GHFilter{}
Expand Down
7 changes: 2 additions & 5 deletions pkg/indicator/hull.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@ func (inc *HULL) Last(i int) float64 {
if inc.result == nil {
return 0
}
return inc.result.Index(i)
return inc.result.Last(i)
}

func (inc *HULL) Index(i int) float64 {
if inc.result == nil {
return 0
}
return inc.result.Index(i)
return inc.Last(i)
}

func (inc *HULL) Length() int {
Expand Down
8 changes: 2 additions & 6 deletions pkg/indicator/pivothigh.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ func (inc *PivotHigh) Length() int {
return inc.Values.Length()
}

func (inc *PivotHigh) Last(int) float64 {
if len(inc.Values) == 0 {
return 0.0
}

return inc.Values.Last(0)
func (inc *PivotHigh) Last(i int) float64 {
return inc.Values.Last(i)
}

func (inc *PivotHigh) Update(value float64) {
Expand Down
8 changes: 2 additions & 6 deletions pkg/indicator/pivotlow.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ func (inc *PivotLow) Length() int {
return inc.Values.Length()
}

func (inc *PivotLow) Last(int) float64 {
if len(inc.Values) == 0 {
return 0.0
}

return inc.Values.Last(0)
func (inc *PivotLow) Last(i int) float64 {
return inc.Values.Last(i)
}

func (inc *PivotLow) Update(value float64) {
Expand Down
7 changes: 2 additions & 5 deletions pkg/indicator/psar.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ type PSAR struct {
UpdateCallbacks []func(value float64)
}

func (inc *PSAR) Last(int) float64 {
if len(inc.Values) == 0 {
return 0
}
return inc.Values.Last(0)
func (inc *PSAR) Last(i int) float64 {
return inc.Values.Last(i)
}

func (inc *PSAR) Length() int {
Expand Down
4 changes: 2 additions & 2 deletions pkg/strategy/drift/driftma.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func (s *DriftMA) Update(value, weight float64) {
s.ma2.Update(s.drift.Last(0))
}

func (s *DriftMA) Last(int) float64 {
return s.ma2.Last(0)
func (s *DriftMA) Last(i int) float64 {
return s.ma2.Last(i)
}

func (s *DriftMA) Index(i int) float64 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/strategy/elliottwave/ewo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ type ElliottWave struct {
}

func (s *ElliottWave) Index(i int) float64 {
return s.maQuick.Index(i)/s.maSlow.Index(i) - 1.0
return s.Last(i)
}

func (s *ElliottWave) Last(int) float64 {
return s.maQuick.Last(0)/s.maSlow.Last(0) - 1.0
func (s *ElliottWave) Last(i int) float64 {
return s.maQuick.Index(i)/s.maSlow.Index(i) - 1.0
}

func (s *ElliottWave) Length() int {
Expand Down
6 changes: 3 additions & 3 deletions pkg/strategy/ewoDgtrd/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ type VWEMA struct {
V types.UpdatableSeries
}

func (inc *VWEMA) Last(int) float64 {
return inc.PV.Last(0) / inc.V.Last(0)
func (inc *VWEMA) Index(i int) float64 {
return inc.Last(i)
}

func (inc *VWEMA) Index(i int) float64 {
func (inc *VWEMA) Last(i int) float64 {
if i >= inc.PV.Length() {
return 0
}
Expand Down
15 changes: 3 additions & 12 deletions pkg/strategy/factorzoo/factors/momentum.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,14 @@ type MOM struct {
}

func (inc *MOM) Index(i int) float64 {
if inc.Values == nil {
return 0
}
return inc.Values.Last(i)
return inc.Last(i)
}

func (inc *MOM) Last(int) float64 {
if inc.Values.Length() == 0 {
return 0
}
return inc.Values.Last(0)
func (inc *MOM) Last(i int) float64 {
return inc.Values.Last(i)
}

func (inc *MOM) Length() int {
if inc.Values == nil {
return 0
}
return inc.Values.Length()
}

Expand Down
18 changes: 5 additions & 13 deletions pkg/strategy/factorzoo/factors/price_mean_reversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type PMR struct {
types.IntervalWindow
types.SeriesBase

Values floats.Slice
SMA *indicator.SMA
Values floats.Slice
SMA *indicator.SMA
EndTime time.Time

updateCallbacks []func(value float64)
Expand All @@ -40,20 +40,12 @@ func (inc *PMR) Update(price float64) {
}
}

func (inc *PMR) Last(int) float64 {
if len(inc.Values) == 0 {
return 0
}

return inc.Values[len(inc.Values)-1]
func (inc *PMR) Last(i int) float64 {
return inc.Values.Last(i)
}

func (inc *PMR) Index(i int) float64 {
if i >= len(inc.Values) {
return 0
}

return inc.Values[len(inc.Values)-1-i]
return inc.Last(i)
}

func (inc *PMR) Length() int {
Expand Down
18 changes: 5 additions & 13 deletions pkg/strategy/factorzoo/factors/price_volume_divergence.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type PVD struct {
types.IntervalWindow
types.SeriesBase

Values floats.Slice
Prices *types.Queue
Values floats.Slice
Prices *types.Queue
Volumes *types.Queue
EndTime time.Time

Expand All @@ -47,20 +47,12 @@ func (inc *PVD) Update(price float64, volume float64) {
}
}

func (inc *PVD) Last(int) float64 {
if len(inc.Values) == 0 {
return 0
}

return inc.Values[len(inc.Values)-1]
func (inc *PVD) Last(i int) float64 {
return inc.Values.Last(i)
}

func (inc *PVD) Index(i int) float64 {
if i >= len(inc.Values) {
return 0
}

return inc.Values[len(inc.Values)-1-i]
return inc.Last(i)
}

func (inc *PVD) Length() int {
Expand Down
18 changes: 5 additions & 13 deletions pkg/strategy/factorzoo/factors/return_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,12 @@ func (inc *RR) Update(price float64) {

}

func (inc *RR) Last(int) float64 {
if len(inc.Values) == 0 {
return 0
}

return inc.Values[len(inc.Values)-1]
func (inc *RR) Last(i int) float64 {
return inc.Values.Last(i)
}

func (inc *RR) Index(i int) float64 {
if i >= len(inc.Values) {
return 0
}

return inc.Values[len(inc.Values)-1-i]
return inc.Last(i)
}

func (inc *RR) Length() int {
Expand Down Expand Up @@ -101,7 +93,7 @@ func (inc *RR) LoadK(allKLines []types.KLine) {
inc.EmitUpdate(inc.Last(0))
}

//func calculateReturn(klines []types.KLine, window int, val KLineValueMapper) (float64, error) {
// func calculateReturn(klines []types.KLine, window int, val KLineValueMapper) (float64, error) {
// length := len(klines)
// if length == 0 || length < window {
// return 0.0, fmt.Errorf("insufficient elements for calculating VOL with window = %d", window)
Expand All @@ -110,4 +102,4 @@ func (inc *RR) LoadK(allKLines []types.KLine) {
// rate := val(klines[length-1])/val(klines[length-2]) - 1
//
// return rate, nil
//}
// }
15 changes: 3 additions & 12 deletions pkg/strategy/factorzoo/factors/volume_momentum.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,14 @@ type VMOM struct {
}

func (inc *VMOM) Index(i int) float64 {
if inc.Values == nil {
return 0
}
return inc.Values.Last(i)
return inc.Last(i)
}

func (inc *VMOM) Last(int) float64 {
if inc.Values.Length() == 0 {
return 0
}
return inc.Values.Last(0)
func (inc *VMOM) Last(i int) float64 {
return inc.Values.Last(i)
}

func (inc *VMOM) Length() int {
if inc.Values == nil {
return 0
}
return inc.Values.Length()
}

Expand Down
Loading

0 comments on commit 9435478

Please sign in to comment.