Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add G-H filter and Kalman filter #915

Merged
merged 3 commits into from
Sep 5, 2022

Conversation

COLDTURNIP
Copy link
Collaborator

@COLDTURNIP COLDTURNIP commented Sep 4, 2022

Observer filters are realtime estimation algorithms to measure the status changes in a noisy environment. These recursive filtering algorithms estimate the true target states by minimize the uncertainties from system noise and observation noise. Such technologies are useful in financial econometrics as estimators without any latency, and more accurate than moving average based indicators.

The G-H filter in this PR implement John Ehlers' work. The system uncertainty is defined as the change of middle prices of K-lines, and the observation uncertainty is the price range of each K-line. Instead of fixed alpha & beta of conventional G-H filter, these two factor are calculated using an exponential recursive method controlled by configured window length.

More generalized than G-H filter, Kalman filter replaces these update factors by an adaptive factor Kalman gain, calculates the gain from uncertainties of estimation and observation, and optimize the estimation base on the gain, the measurement, and the previous prediction. In our implementation, we add an additional factor AdditionalSmoothWindow in need of smoothness, and set to 0 (classic Kalman filter) by default.

Estimation Accurateness

In this PR, tests are created to compare the estimation accurateness between the two filters and EMA by calculating the square errors.

References

@codecov
Copy link

codecov bot commented Sep 4, 2022

Codecov Report

Merging #915 (65cd17d) into main (843b81e) will increase coverage by 0.12%.
The diff coverage is 53.53%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #915      +/-   ##
==========================================
+ Coverage   19.71%   19.84%   +0.12%     
==========================================
  Files         398      402       +4     
  Lines       29950    30049      +99     
==========================================
+ Hits         5904     5962      +58     
- Misses      23473    23510      +37     
- Partials      573      577       +4     
Impacted Files Coverage Δ
pkg/bbgo/standard_indicator_set.go 4.81% <0.00%> (-0.38%) ⬇️
pkg/indicator/ghfilter_callbacks.go 0.00% <0.00%> (ø)
pkg/indicator/kalmanfilter_callbacks.go 0.00% <0.00%> (ø)
pkg/indicator/ghfilter.go 58.33% <58.33%> (ø)
pkg/indicator/kalmanfilter.go 71.11% <71.11%> (ø)
pkg/indicator/ewma.go 60.78% <0.00%> (+7.84%) ⬆️
pkg/indicator/ewma_callbacks.go 16.66% <0.00%> (+16.66%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 843b81e...65cd17d. Read the comment docs.

- implement G-H (alpha beta) filter and Kalman filter
- compare the predict accurateness with other indicator
@COLDTURNIP COLDTURNIP force-pushed the feature/observer_filters branch from 0a0dd89 to 9c684c1 Compare September 4, 2022 13:49
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filter := &GHFilter{IntervalWindow: types.IntervalWindow{Window: tt.args.window}}
ewma := &EWMA{IntervalWindow: types.IntervalWindow{Window: tt.args.window}}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EWMA is widely used to calculate average price with less latency compare to SMA. Do we expect the comparison of ZLEMA?

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filter := &KalmanFilter{IntervalWindow: types.IntervalWindow{Window: tt.args.window}}
ewma := &EWMA{IntervalWindow: types.IntervalWindow{Window: tt.args.window}}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, change to ZLEMA?

@c9s
Copy link
Owner

c9s commented Sep 5, 2022

You can add these two indicators to the standardIndicator struct

@COLDTURNIP COLDTURNIP merged commit 7416f10 into c9s:main Sep 5, 2022
@COLDTURNIP COLDTURNIP deleted the feature/observer_filters branch September 5, 2022 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants