-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request c9s#890 from zenixls2/feature/wdrift
weighted drift
- Loading branch information
Showing
12 changed files
with
432 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package indicator | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
// Geometric Moving Average | ||
//go:generate callbackgen -type GMA | ||
type GMA struct { | ||
types.SeriesBase | ||
types.IntervalWindow | ||
SMA *SMA | ||
UpdateCallbacks []func(value float64) | ||
} | ||
|
||
func (inc *GMA) Last() float64 { | ||
if inc.SMA == nil { | ||
return 0.0 | ||
} | ||
return math.Exp(inc.SMA.Last()) | ||
} | ||
|
||
func (inc *GMA) Index(i int) float64 { | ||
if inc.SMA == nil { | ||
return 0.0 | ||
} | ||
return math.Exp(inc.SMA.Index(i)) | ||
} | ||
|
||
func (inc *GMA) Length() int { | ||
return inc.SMA.Length() | ||
} | ||
|
||
func (inc *GMA) Update(value float64) { | ||
if inc.SMA == nil { | ||
inc.SMA = &SMA{IntervalWindow: inc.IntervalWindow} | ||
} | ||
inc.SMA.Update(math.Log(value)) | ||
} | ||
|
||
func (inc *GMA) Clone() (out *GMA) { | ||
out = &GMA{ | ||
IntervalWindow: inc.IntervalWindow, | ||
SMA: inc.SMA.Clone().(*SMA), | ||
} | ||
out.SeriesBase.Series = out | ||
return out | ||
} | ||
|
||
func (inc *GMA) TestUpdate(value float64) *GMA { | ||
out := inc.Clone() | ||
out.Update(value) | ||
return out | ||
} | ||
|
||
var _ types.SeriesExtend = &GMA{} | ||
|
||
func (inc *GMA) PushK(k types.KLine) { | ||
inc.Update(k.Close.Float64()) | ||
} | ||
|
||
func (inc *GMA) LoadK(allKLines []types.KLine) { | ||
for _, k := range allKLines { | ||
inc.PushK(k) | ||
} | ||
} | ||
|
||
func (inc *GMA) BindK(target KLineClosedEmitter, symbol string, interval types.Interval) { | ||
target.OnKLineClosed(types.KLineWith(symbol, interval, inc.PushK)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package indicator | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/c9s/bbgo/pkg/fixedpoint" | ||
"github.com/c9s/bbgo/pkg/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
/* | ||
python: | ||
import pandas as pd | ||
from scipy.stats.mstats import gmean | ||
data = pd.Series([1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9]) | ||
gmean(data[-5:]) | ||
gmean(data[-6:-1]) | ||
gmean(pd.concat(data[-4:], pd.Series([1.3]))) | ||
*/ | ||
func Test_GMA(t *testing.T) { | ||
var randomPrices = []byte(`[1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9]`) | ||
var input []fixedpoint.Value | ||
if err := json.Unmarshal(randomPrices, &input); err != nil { | ||
panic(err) | ||
} | ||
tests := []struct { | ||
name string | ||
kLines []types.KLine | ||
want float64 | ||
next float64 | ||
update float64 | ||
updateResult float64 | ||
all int | ||
}{ | ||
{ | ||
name: "test", | ||
kLines: buildKLines(input), | ||
want: 1.6940930229200213, | ||
next: 1.5937204331251167, | ||
update: 1.3, | ||
updateResult: 1.6462950504034335, | ||
all: 24, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gma := GMA{IntervalWindow: types.IntervalWindow{Window: 5}} | ||
for _, k := range tt.kLines { | ||
gma.PushK(k) | ||
} | ||
assert.InDelta(t, tt.want, gma.Last(), Delta) | ||
assert.InDelta(t, tt.next, gma.Index(1), Delta) | ||
gma.Update(tt.update) | ||
assert.InDelta(t, tt.updateResult, gma.Last(), Delta) | ||
assert.Equal(t, tt.all, gma.Length()) | ||
}) | ||
} | ||
} |
Oops, something went wrong.