Skip to content

Commit

Permalink
all: add parameter index to the Last method
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed May 31, 2023
1 parent 2a074ba commit 5515f58
Show file tree
Hide file tree
Showing 142 changed files with 843 additions and 996 deletions.
2 changes: 1 addition & 1 deletion pkg/bbgo/exit_lower_shadow_take_profit.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *LowerShadowTakeProfit) Bind(session *ExchangeSession, orderExecutor *Ge
}

// skip close price higher than the ewma
if closePrice.Float64() > ewma.Last() {
if closePrice.Float64() > ewma.Last(0) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/bbgo/stop_ema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (s *StopEMA) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExec
}

func (s *StopEMA) Allowed(closePrice fixedpoint.Value) bool {
ema := fixedpoint.NewFromFloat(s.stopEWMA.Last())
ema := fixedpoint.NewFromFloat(s.stopEWMA.Last(0))
if ema.IsZero() {
logrus.Infof("stopEMA protection: value is zero, skip")
return false
Expand Down
4 changes: 2 additions & 2 deletions pkg/bbgo/trend_ema.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func (s *TrendEMA) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExe
}

s.last = s.ewma.Values[s.ewma.Length()-2]
s.current = s.ewma.Last()
s.current = s.ewma.Last(0)
})

session.MarketDataStream.OnKLineClosed(types.KLineWith(symbol, s.Interval, func(kline types.KLine) {
s.last = s.current
s.current = s.ewma.Last()
s.current = s.ewma.Last(0)
}))
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/datatype/floats/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ func (s Slice) Addr() *Slice {
}

// Last, Index, Length implements the types.Series interface
func (s Slice) Last() float64 {
func (s Slice) Last(i int) float64 {
length := len(s)
if length > 0 {
return s[length-1]
if i < 0 || length-1-i < 0 {
return 0.0
}
return 0.0
return s[length-1-i]
}

// Index fetches the element from the end of the slice
Expand Down
13 changes: 7 additions & 6 deletions pkg/indicator/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ func (inc *AD) Update(high, low, cloze, volume float64) {
moneyFlowVolume = ((2*cloze - high - low) / (high - low)) * volume
}

ad := inc.Last() + moneyFlowVolume
ad := inc.Last(0) + moneyFlowVolume
inc.Values.Push(ad)
}

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

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

inc.EmitUpdate(inc.Last())
inc.EmitUpdate(inc.Last(0))
inc.EndTime = kLines[len(kLines)-1].EndTime.Time()
}

Expand Down
11 changes: 6 additions & 5 deletions pkg/indicator/alma.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
//
// @param offset: Gaussian applied to the combo line. 1->ema, 0->sma
// @param sigma: the standard deviation applied to the combo line. This makes the combo line sharper
//
//go:generate callbackgen -type ALMA
type ALMA struct {
types.SeriesBase
Expand Down Expand Up @@ -64,11 +65,11 @@ func (inc *ALMA) Update(value float64) {
}
}

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

func (inc *ALMA) Index(i int) float64 {
Expand All @@ -88,12 +89,12 @@ func (inc *ALMA) CalculateAndUpdate(allKLines []types.KLine) {
if inc.input == nil {
for _, k := range allKLines {
inc.Update(k.Close.Float64())
inc.EmitUpdate(inc.Last())
inc.EmitUpdate(inc.Last(0))
}
return
}
inc.Update(allKLines[len(allKLines)-1].Close.Float64())
inc.EmitUpdate(inc.Last())
inc.EmitUpdate(inc.Last(0))
}

func (inc *ALMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/alma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Test_ALMA(t *testing.T) {
Sigma: 6,
}
alma.CalculateAndUpdate(tt.kLines)
assert.InDelta(t, tt.want, alma.Last(), Delta)
assert.InDelta(t, tt.want, alma.Last(0), Delta)
assert.InDelta(t, tt.next, alma.Index(1), Delta)
assert.Equal(t, tt.all, alma.Length())
})
Expand Down
8 changes: 4 additions & 4 deletions pkg/indicator/atr.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ func (inc *ATR) Update(high, low, cloze float64) {

// apply rolling moving average
inc.RMA.Update(trueRange)
atr := inc.RMA.Last()
atr := inc.RMA.Last(0)
inc.PercentageVolatility.Push(atr / cloze)
if len(inc.PercentageVolatility) > MaxNumOfATR {
inc.PercentageVolatility = inc.PercentageVolatility[MaxNumOfATRTruncateSize-1:]
}
}

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

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

inc.Update(k.High.Float64(), k.Low.Float64(), k.Close.Float64())
inc.EndTime = k.EndTime.Time()
inc.EmitUpdate(inc.Last())
inc.EmitUpdate(inc.Last(0))
}
2 changes: 1 addition & 1 deletion pkg/indicator/atr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func Test_calculateATR(t *testing.T) {
atr.PushK(k)
}

got := atr.Last()
got := atr.Last(0)
diff := math.Trunc((got-tt.want)*100) / 100
if diff != 0 {
t.Errorf("calculateATR() = %v, want %v", got, tt.want)
Expand Down
10 changes: 5 additions & 5 deletions pkg/indicator/atrp.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
//
// Calculation:
//
// ATRP = (Average True Range / Close) * 100
// ATRP = (Average True Range / Close) * 100
//
//go:generate callbackgen -type ATRP
type ATRP struct {
Expand Down Expand Up @@ -69,15 +69,15 @@ func (inc *ATRP) Update(high, low, cloze float64) {

// apply rolling moving average
inc.RMA.Update(trueRange)
atr := inc.RMA.Last()
atr := inc.RMA.Last(0)
inc.PercentageVolatility.Push(atr / cloze)
}

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

func (inc *ATRP) Index(i int) float64 {
Expand Down Expand Up @@ -109,7 +109,7 @@ func (inc *ATRP) CalculateAndUpdate(kLines []types.KLine) {
inc.PushK(k)
}

inc.EmitUpdate(inc.Last())
inc.EmitUpdate(inc.Last(0))
inc.EndTime = kLines[len(kLines)-1].EndTime.Time()
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/indicator/boll.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (inc *BOLL) Update(value float64) {
inc.SMA.Update(value)
inc.StdDev.Update(value)

var sma = inc.SMA.Last()
var stdDev = inc.StdDev.Last()
var sma = inc.SMA.Last(0)
var stdDev = inc.StdDev.Last(0)
var band = inc.K * stdDev

var upBand = sma + band
Expand All @@ -105,15 +105,15 @@ func (inc *BOLL) PushK(k types.KLine) {
}
inc.Update(k.Close.Float64())
inc.EndTime = k.EndTime.Time()
inc.EmitUpdate(inc.SMA.Last(), inc.UpBand.Last(), inc.DownBand.Last())
inc.EmitUpdate(inc.SMA.Last(0), inc.UpBand.Last(0), inc.DownBand.Last(0))
}

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

inc.EmitUpdate(inc.SMA.Last(), inc.UpBand.Last(), inc.DownBand.Last())
inc.EmitUpdate(inc.SMA.Last(0), inc.UpBand.Last(0), inc.DownBand.Last(0))
}

func (inc *BOLL) CalculateAndUpdate(allKLines []types.KLine) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/indicator/boll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func TestBOLL(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
boll := BOLL{IntervalWindow: types.IntervalWindow{Window: tt.window}, K: tt.k}
boll.CalculateAndUpdate(tt.kLines)
assert.InDelta(t, tt.up, boll.UpBand.Last(), Delta)
assert.InDelta(t, tt.down, boll.DownBand.Last(), Delta)
assert.InDelta(t, tt.up, boll.UpBand.Last(0), Delta)
assert.InDelta(t, tt.down, boll.DownBand.Last(0), Delta)
})
}

Expand Down
20 changes: 7 additions & 13 deletions pkg/indicator/cci.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (inc *CCI) Update(value float64) {
}

inc.Input.Push(value)
tp := inc.TypicalPrice.Last() - inc.Input.Index(inc.Window) + value
tp := inc.TypicalPrice.Last(0) - inc.Input.Index(inc.Window) + value
inc.TypicalPrice.Push(tp)
if len(inc.Input) < inc.Window {
return
Expand All @@ -55,7 +55,7 @@ func (inc *CCI) Update(value float64) {
}
md := 0.
for i := 0; i < inc.Window; i++ {
diff := inc.Input.Index(i) - ma
diff := inc.Input.Last(i) - ma
md += diff * diff
}
md = math.Sqrt(md / float64(inc.Window))
Expand All @@ -68,18 +68,12 @@ func (inc *CCI) Update(value float64) {
}
}

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

func (inc *CCI) 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 *CCI) Length() int {
Expand All @@ -96,12 +90,12 @@ func (inc *CCI) CalculateAndUpdate(allKLines []types.KLine) {
if inc.TypicalPrice.Length() == 0 {
for _, k := range allKLines {
inc.PushK(k)
inc.EmitUpdate(inc.Last())
inc.EmitUpdate(inc.Last(0))
}
} else {
k := allKLines[len(allKLines)-1]
inc.PushK(k)
inc.EmitUpdate(inc.Last())
inc.EmitUpdate(inc.Last(0))
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/cci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Test_CCI(t *testing.T) {
cci.Update(value)
}

last := cci.Last()
last := cci.Last(0)
assert.InDelta(t, 93.250481, last, Delta)
assert.InDelta(t, 81.813449, cci.Index(1), Delta)
assert.Equal(t, 50-16+1, cci.Length())
Expand Down
23 changes: 9 additions & 14 deletions pkg/indicator/cma.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import (

// Refer: Cumulative Moving Average, Cumulative Average
// Refer: https://en.wikipedia.org/wiki/Moving_average
//
//go:generate callbackgen -type CA
type CA struct {
types.SeriesBase
Interval types.Interval
Values floats.Slice
length float64
Interval types.Interval
Values floats.Slice
length float64
UpdateCallbacks []func(value float64)
}

func (inc *CA) Update(x float64) {
if len(inc.Values) == 0 {
inc.SeriesBase.Series = inc
}
newVal := (inc.Values.Last()*inc.length + x) / (inc.length + 1.)
newVal := (inc.Values.Last(0)*inc.length + x) / (inc.length + 1.)
inc.length += 1
inc.Values.Push(newVal)
if len(inc.Values) > MaxNumOfEWMA {
Expand All @@ -29,18 +30,12 @@ func (inc *CA) Update(x float64) {
}
}

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

func (inc *CA) 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 *CA) Length() int {
Expand All @@ -56,7 +51,7 @@ func (inc *CA) PushK(k types.KLine) {
func (inc *CA) CalculateAndUpdate(allKLines []types.KLine) {
for _, k := range allKLines {
inc.PushK(k)
inc.EmitUpdate(inc.Last())
inc.EmitUpdate(inc.Last(0))
}
}

Expand Down
Loading

0 comments on commit 5515f58

Please sign in to comment.