-
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#823 from c9s/refactor/indicator-api
refactor: refactor bollinger band indicator with the new series extend component
- Loading branch information
Showing
8 changed files
with
153 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package indicator | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
//go:generate callbackgen -type StdDev | ||
type StdDev struct { | ||
types.SeriesBase | ||
types.IntervalWindow | ||
Values types.Float64Slice | ||
rawValues *types.Queue | ||
|
||
EndTime time.Time | ||
updateCallbacks []func(value float64) | ||
} | ||
|
||
func (inc *StdDev) Last() float64 { | ||
if inc.Values.Length() == 0 { | ||
return 0.0 | ||
} | ||
return inc.Values.Last() | ||
} | ||
|
||
func (inc *StdDev) Index(i int) float64 { | ||
if i >= inc.Values.Length() { | ||
return 0.0 | ||
} | ||
|
||
return inc.Values.Index(i) | ||
} | ||
|
||
func (inc *StdDev) Length() int { | ||
return inc.Values.Length() | ||
} | ||
|
||
var _ types.SeriesExtend = &StdDev{} | ||
|
||
func (inc *StdDev) Update(value float64) { | ||
if inc.rawValues == nil { | ||
inc.rawValues = types.NewQueue(inc.Window) | ||
inc.SeriesBase.Series = inc | ||
} | ||
|
||
inc.rawValues.Update(value) | ||
|
||
var std = inc.rawValues.Stdev() | ||
inc.Values.Push(std) | ||
} | ||
|
||
func (inc *StdDev) PushK(k types.KLine) { | ||
inc.Update(k.Close.Float64()) | ||
inc.EndTime = k.EndTime.Time() | ||
} | ||
|
||
func (inc *StdDev) CalculateAndUpdate(allKLines []types.KLine) { | ||
var last = allKLines[len(allKLines)-1] | ||
|
||
if inc.rawValues == nil { | ||
for _, k := range allKLines { | ||
if inc.EndTime != zeroTime && k.EndTime.Before(inc.EndTime) { | ||
continue | ||
} | ||
inc.PushK(k) | ||
} | ||
} else { | ||
inc.PushK(last) | ||
} | ||
|
||
inc.EmitUpdate(inc.Values.Last()) | ||
} | ||
|
||
func (inc *StdDev) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) { | ||
if inc.Interval != interval { | ||
return | ||
} | ||
|
||
inc.CalculateAndUpdate(window) | ||
} | ||
|
||
func (inc *StdDev) Bind(updater KLineWindowUpdater) { | ||
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate) | ||
} |
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
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