Skip to content

Commit 5515f58

Browse files
committed
all: add parameter index to the Last method
1 parent 2a074ba commit 5515f58

File tree

142 files changed

+843
-996
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+843
-996
lines changed

pkg/bbgo/exit_lower_shadow_take_profit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (s *LowerShadowTakeProfit) Bind(session *ExchangeSession, orderExecutor *Ge
4848
}
4949

5050
// skip close price higher than the ewma
51-
if closePrice.Float64() > ewma.Last() {
51+
if closePrice.Float64() > ewma.Last(0) {
5252
return
5353
}
5454

pkg/bbgo/stop_ema.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (s *StopEMA) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExec
2121
}
2222

2323
func (s *StopEMA) Allowed(closePrice fixedpoint.Value) bool {
24-
ema := fixedpoint.NewFromFloat(s.stopEWMA.Last())
24+
ema := fixedpoint.NewFromFloat(s.stopEWMA.Last(0))
2525
if ema.IsZero() {
2626
logrus.Infof("stopEMA protection: value is zero, skip")
2727
return false

pkg/bbgo/trend_ema.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ func (s *TrendEMA) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExe
2929
}
3030

3131
s.last = s.ewma.Values[s.ewma.Length()-2]
32-
s.current = s.ewma.Last()
32+
s.current = s.ewma.Last(0)
3333
})
3434

3535
session.MarketDataStream.OnKLineClosed(types.KLineWith(symbol, s.Interval, func(kline types.KLine) {
3636
s.last = s.current
37-
s.current = s.ewma.Last()
37+
s.current = s.ewma.Last(0)
3838
}))
3939
}
4040

pkg/datatype/floats/slice.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@ func (s Slice) Addr() *Slice {
183183
}
184184

185185
// Last, Index, Length implements the types.Series interface
186-
func (s Slice) Last() float64 {
186+
func (s Slice) Last(i int) float64 {
187187
length := len(s)
188-
if length > 0 {
189-
return s[length-1]
188+
if i < 0 || length-1-i < 0 {
189+
return 0.0
190190
}
191-
return 0.0
191+
return s[length-1-i]
192192
}
193193

194194
// Index fetches the element from the end of the slice

pkg/indicator/ad.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ func (inc *AD) Update(high, low, cloze, volume float64) {
3535
moneyFlowVolume = ((2*cloze - high - low) / (high - low)) * volume
3636
}
3737

38-
ad := inc.Last() + moneyFlowVolume
38+
ad := inc.Last(0) + moneyFlowVolume
3939
inc.Values.Push(ad)
4040
}
4141

42-
func (inc *AD) Last() float64 {
43-
if len(inc.Values) == 0 {
44-
return 0.0
42+
func (inc *AD) Last(i int) float64 {
43+
length := len(inc.Values)
44+
if length == 0 || length-i-1 < 0 {
45+
return 0
4546
}
46-
return inc.Values[len(inc.Values)-1]
47+
return inc.Values[length-i-1]
4748
}
4849

4950
func (inc *AD) Index(i int) float64 {
@@ -68,7 +69,7 @@ func (inc *AD) CalculateAndUpdate(kLines []types.KLine) {
6869
inc.Update(k.High.Float64(), k.Low.Float64(), k.Close.Float64(), k.Volume.Float64())
6970
}
7071

71-
inc.EmitUpdate(inc.Last())
72+
inc.EmitUpdate(inc.Last(0))
7273
inc.EndTime = kLines[len(kLines)-1].EndTime.Time()
7374
}
7475

pkg/indicator/alma.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
//
2121
// @param offset: Gaussian applied to the combo line. 1->ema, 0->sma
2222
// @param sigma: the standard deviation applied to the combo line. This makes the combo line sharper
23+
//
2324
//go:generate callbackgen -type ALMA
2425
type ALMA struct {
2526
types.SeriesBase
@@ -64,11 +65,11 @@ func (inc *ALMA) Update(value float64) {
6465
}
6566
}
6667

67-
func (inc *ALMA) Last() float64 {
68-
if len(inc.Values) == 0 {
68+
func (inc *ALMA) Last(i int) float64 {
69+
if i >= len(inc.Values) {
6970
return 0
7071
}
71-
return inc.Values[len(inc.Values)-1]
72+
return inc.Values[len(inc.Values)-i-1]
7273
}
7374

7475
func (inc *ALMA) Index(i int) float64 {
@@ -88,12 +89,12 @@ func (inc *ALMA) CalculateAndUpdate(allKLines []types.KLine) {
8889
if inc.input == nil {
8990
for _, k := range allKLines {
9091
inc.Update(k.Close.Float64())
91-
inc.EmitUpdate(inc.Last())
92+
inc.EmitUpdate(inc.Last(0))
9293
}
9394
return
9495
}
9596
inc.Update(allKLines[len(allKLines)-1].Close.Float64())
96-
inc.EmitUpdate(inc.Last())
97+
inc.EmitUpdate(inc.Last(0))
9798
}
9899

99100
func (inc *ALMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {

pkg/indicator/alma_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func Test_ALMA(t *testing.T) {
5353
Sigma: 6,
5454
}
5555
alma.CalculateAndUpdate(tt.kLines)
56-
assert.InDelta(t, tt.want, alma.Last(), Delta)
56+
assert.InDelta(t, tt.want, alma.Last(0), Delta)
5757
assert.InDelta(t, tt.next, alma.Index(1), Delta)
5858
assert.Equal(t, tt.all, alma.Length())
5959
})

pkg/indicator/atr.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,18 @@ func (inc *ATR) Update(high, low, cloze float64) {
7474

7575
// apply rolling moving average
7676
inc.RMA.Update(trueRange)
77-
atr := inc.RMA.Last()
77+
atr := inc.RMA.Last(0)
7878
inc.PercentageVolatility.Push(atr / cloze)
7979
if len(inc.PercentageVolatility) > MaxNumOfATR {
8080
inc.PercentageVolatility = inc.PercentageVolatility[MaxNumOfATRTruncateSize-1:]
8181
}
8282
}
8383

84-
func (inc *ATR) Last() float64 {
84+
func (inc *ATR) Last(i int) float64 {
8585
if inc.RMA == nil {
8686
return 0
8787
}
88-
return inc.RMA.Last()
88+
return inc.RMA.Last(i)
8989
}
9090

9191
func (inc *ATR) Index(i int) float64 {
@@ -110,5 +110,5 @@ func (inc *ATR) PushK(k types.KLine) {
110110

111111
inc.Update(k.High.Float64(), k.Low.Float64(), k.Close.Float64())
112112
inc.EndTime = k.EndTime.Time()
113-
inc.EmitUpdate(inc.Last())
113+
inc.EmitUpdate(inc.Last(0))
114114
}

pkg/indicator/atr_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func Test_calculateATR(t *testing.T) {
6565
atr.PushK(k)
6666
}
6767

68-
got := atr.Last()
68+
got := atr.Last(0)
6969
diff := math.Trunc((got-tt.want)*100) / 100
7070
if diff != 0 {
7171
t.Errorf("calculateATR() = %v, want %v", got, tt.want)

pkg/indicator/atrp.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
//
2222
// Calculation:
2323
//
24-
// ATRP = (Average True Range / Close) * 100
24+
// ATRP = (Average True Range / Close) * 100
2525
//
2626
//go:generate callbackgen -type ATRP
2727
type ATRP struct {
@@ -69,15 +69,15 @@ func (inc *ATRP) Update(high, low, cloze float64) {
6969

7070
// apply rolling moving average
7171
inc.RMA.Update(trueRange)
72-
atr := inc.RMA.Last()
72+
atr := inc.RMA.Last(0)
7373
inc.PercentageVolatility.Push(atr / cloze)
7474
}
7575

76-
func (inc *ATRP) Last() float64 {
76+
func (inc *ATRP) Last(i int) float64 {
7777
if inc.RMA == nil {
7878
return 0
7979
}
80-
return inc.RMA.Last()
80+
return inc.RMA.Last(i)
8181
}
8282

8383
func (inc *ATRP) Index(i int) float64 {
@@ -109,7 +109,7 @@ func (inc *ATRP) CalculateAndUpdate(kLines []types.KLine) {
109109
inc.PushK(k)
110110
}
111111

112-
inc.EmitUpdate(inc.Last())
112+
inc.EmitUpdate(inc.Last(0))
113113
inc.EndTime = kLines[len(kLines)-1].EndTime.Time()
114114
}
115115

pkg/indicator/boll.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ func (inc *BOLL) Update(value float64) {
8484
inc.SMA.Update(value)
8585
inc.StdDev.Update(value)
8686

87-
var sma = inc.SMA.Last()
88-
var stdDev = inc.StdDev.Last()
87+
var sma = inc.SMA.Last(0)
88+
var stdDev = inc.StdDev.Last(0)
8989
var band = inc.K * stdDev
9090

9191
var upBand = sma + band
@@ -105,15 +105,15 @@ func (inc *BOLL) PushK(k types.KLine) {
105105
}
106106
inc.Update(k.Close.Float64())
107107
inc.EndTime = k.EndTime.Time()
108-
inc.EmitUpdate(inc.SMA.Last(), inc.UpBand.Last(), inc.DownBand.Last())
108+
inc.EmitUpdate(inc.SMA.Last(0), inc.UpBand.Last(0), inc.DownBand.Last(0))
109109
}
110110

111111
func (inc *BOLL) LoadK(allKLines []types.KLine) {
112112
for _, k := range allKLines {
113113
inc.PushK(k)
114114
}
115115

116-
inc.EmitUpdate(inc.SMA.Last(), inc.UpBand.Last(), inc.DownBand.Last())
116+
inc.EmitUpdate(inc.SMA.Last(0), inc.UpBand.Last(0), inc.DownBand.Last(0))
117117
}
118118

119119
func (inc *BOLL) CalculateAndUpdate(allKLines []types.KLine) {

pkg/indicator/boll_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func TestBOLL(t *testing.T) {
6161
t.Run(tt.name, func(t *testing.T) {
6262
boll := BOLL{IntervalWindow: types.IntervalWindow{Window: tt.window}, K: tt.k}
6363
boll.CalculateAndUpdate(tt.kLines)
64-
assert.InDelta(t, tt.up, boll.UpBand.Last(), Delta)
65-
assert.InDelta(t, tt.down, boll.DownBand.Last(), Delta)
64+
assert.InDelta(t, tt.up, boll.UpBand.Last(0), Delta)
65+
assert.InDelta(t, tt.down, boll.DownBand.Last(0), Delta)
6666
})
6767
}
6868

pkg/indicator/cci.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (inc *CCI) Update(value float64) {
4343
}
4444

4545
inc.Input.Push(value)
46-
tp := inc.TypicalPrice.Last() - inc.Input.Index(inc.Window) + value
46+
tp := inc.TypicalPrice.Last(0) - inc.Input.Index(inc.Window) + value
4747
inc.TypicalPrice.Push(tp)
4848
if len(inc.Input) < inc.Window {
4949
return
@@ -55,7 +55,7 @@ func (inc *CCI) Update(value float64) {
5555
}
5656
md := 0.
5757
for i := 0; i < inc.Window; i++ {
58-
diff := inc.Input.Index(i) - ma
58+
diff := inc.Input.Last(i) - ma
5959
md += diff * diff
6060
}
6161
md = math.Sqrt(md / float64(inc.Window))
@@ -68,18 +68,12 @@ func (inc *CCI) Update(value float64) {
6868
}
6969
}
7070

71-
func (inc *CCI) Last() float64 {
72-
if len(inc.Values) == 0 {
73-
return 0
74-
}
75-
return inc.Values[len(inc.Values)-1]
71+
func (inc *CCI) Last(i int) float64 {
72+
return inc.Values.Last(i)
7673
}
7774

7875
func (inc *CCI) Index(i int) float64 {
79-
if i >= len(inc.Values) {
80-
return 0
81-
}
82-
return inc.Values[len(inc.Values)-1-i]
76+
return inc.Last(i)
8377
}
8478

8579
func (inc *CCI) Length() int {
@@ -96,12 +90,12 @@ func (inc *CCI) CalculateAndUpdate(allKLines []types.KLine) {
9690
if inc.TypicalPrice.Length() == 0 {
9791
for _, k := range allKLines {
9892
inc.PushK(k)
99-
inc.EmitUpdate(inc.Last())
93+
inc.EmitUpdate(inc.Last(0))
10094
}
10195
} else {
10296
k := allKLines[len(allKLines)-1]
10397
inc.PushK(k)
104-
inc.EmitUpdate(inc.Last())
98+
inc.EmitUpdate(inc.Last(0))
10599
}
106100
}
107101

pkg/indicator/cci_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func Test_CCI(t *testing.T) {
2929
cci.Update(value)
3030
}
3131

32-
last := cci.Last()
32+
last := cci.Last(0)
3333
assert.InDelta(t, 93.250481, last, Delta)
3434
assert.InDelta(t, 81.813449, cci.Index(1), Delta)
3535
assert.Equal(t, 50-16+1, cci.Length())

pkg/indicator/cma.go

+9-14
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ import (
77

88
// Refer: Cumulative Moving Average, Cumulative Average
99
// Refer: https://en.wikipedia.org/wiki/Moving_average
10+
//
1011
//go:generate callbackgen -type CA
1112
type CA struct {
1213
types.SeriesBase
13-
Interval types.Interval
14-
Values floats.Slice
15-
length float64
14+
Interval types.Interval
15+
Values floats.Slice
16+
length float64
1617
UpdateCallbacks []func(value float64)
1718
}
1819

1920
func (inc *CA) Update(x float64) {
2021
if len(inc.Values) == 0 {
2122
inc.SeriesBase.Series = inc
2223
}
23-
newVal := (inc.Values.Last()*inc.length + x) / (inc.length + 1.)
24+
newVal := (inc.Values.Last(0)*inc.length + x) / (inc.length + 1.)
2425
inc.length += 1
2526
inc.Values.Push(newVal)
2627
if len(inc.Values) > MaxNumOfEWMA {
@@ -29,18 +30,12 @@ func (inc *CA) Update(x float64) {
2930
}
3031
}
3132

32-
func (inc *CA) Last() float64 {
33-
if len(inc.Values) == 0 {
34-
return 0
35-
}
36-
return inc.Values[len(inc.Values)-1]
33+
func (inc *CA) Last(i int) float64 {
34+
return inc.Values.Last(i)
3735
}
3836

3937
func (inc *CA) Index(i int) float64 {
40-
if i >= len(inc.Values) {
41-
return 0
42-
}
43-
return inc.Values[len(inc.Values)-1-i]
38+
return inc.Last(i)
4439
}
4540

4641
func (inc *CA) Length() int {
@@ -56,7 +51,7 @@ func (inc *CA) PushK(k types.KLine) {
5651
func (inc *CA) CalculateAndUpdate(allKLines []types.KLine) {
5752
for _, k := range allKLines {
5853
inc.PushK(k)
59-
inc.EmitUpdate(inc.Last())
54+
inc.EmitUpdate(inc.Last(0))
6055
}
6156
}
6257

0 commit comments

Comments
 (0)