From 9a486388faebcdf94c3248971516e03413a93c37 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 1 Jun 2023 17:17:14 +0800 Subject: [PATCH 1/2] indicator: add v2 pivot low indicator --- pkg/indicator/float64updater.go | 14 +++++++++----- pkg/indicator/v2_pivotlow.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 pkg/indicator/v2_pivotlow.go diff --git a/pkg/indicator/float64updater.go b/pkg/indicator/float64updater.go index 8d2d9e3b4..13adfa6fa 100644 --- a/pkg/indicator/float64updater.go +++ b/pkg/indicator/float64updater.go @@ -40,6 +40,14 @@ func (f *Float64Series) PushAndEmit(x float64) { f.EmitUpdate(x) } +func (f *Float64Series) Subscribe(source Float64Source, c func(x float64)) { + if sub, ok := source.(Float64Subscription); ok { + sub.AddSubscriber(c) + } else { + source.OnUpdate(c) + } +} + // Bind binds the source event to the target (Float64Calculator) // A Float64Calculator should be able to calculate the float64 result from a single float64 argument input func (f *Float64Series) Bind(source Float64Source, target Float64Calculator) { @@ -60,9 +68,5 @@ func (f *Float64Series) Bind(source Float64Source, target Float64Calculator) { } } - if sub, ok := source.(Float64Subscription); ok { - sub.AddSubscriber(c) - } else { - source.OnUpdate(c) - } + f.Subscribe(source, c) } diff --git a/pkg/indicator/v2_pivotlow.go b/pkg/indicator/v2_pivotlow.go new file mode 100644 index 000000000..a20e00f19 --- /dev/null +++ b/pkg/indicator/v2_pivotlow.go @@ -0,0 +1,29 @@ +package indicator + +import ( + "github.com/c9s/bbgo/pkg/datatype/floats" +) + +type PivotLowStream struct { + Float64Series + + rawValues floats.Slice + + window, rightWindow int +} + +func PivotLow2(source Float64Source, window, rightWindow int) *PivotLowStream { + s := &PivotLowStream{ + Float64Series: NewFloat64Series(), + window: window, + rightWindow: rightWindow, + } + + s.Subscribe(source, func(x float64) { + s.rawValues.Push(x) + if low, ok := calculatePivotLow(s.rawValues, s.window, s.rightWindow); ok { + s.PushAndEmit(low) + } + }) + return s +} From 15d1caef314d76ea51420b668c88ddc250f8a59d Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 1 Jun 2023 17:31:12 +0800 Subject: [PATCH 2/2] indicator: add pivothigh v2 indicator --- pkg/indicator/v2_pivothigh.go | 27 +++++++++++++++++++++++++++ pkg/indicator/v2_pivotlow.go | 4 +--- 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 pkg/indicator/v2_pivothigh.go diff --git a/pkg/indicator/v2_pivothigh.go b/pkg/indicator/v2_pivothigh.go new file mode 100644 index 000000000..eb0352a73 --- /dev/null +++ b/pkg/indicator/v2_pivothigh.go @@ -0,0 +1,27 @@ +package indicator + +import ( + "github.com/c9s/bbgo/pkg/datatype/floats" +) + +type PivotHighStream struct { + Float64Series + rawValues floats.Slice + window, rightWindow int +} + +func PivotHigh2(source Float64Source, window, rightWindow int) *PivotHighStream { + s := &PivotHighStream{ + Float64Series: NewFloat64Series(), + window: window, + rightWindow: rightWindow, + } + + s.Subscribe(source, func(x float64) { + s.rawValues.Push(x) + if low, ok := calculatePivotHigh(s.rawValues, s.window, s.rightWindow); ok { + s.PushAndEmit(low) + } + }) + return s +} diff --git a/pkg/indicator/v2_pivotlow.go b/pkg/indicator/v2_pivotlow.go index a20e00f19..47d76308f 100644 --- a/pkg/indicator/v2_pivotlow.go +++ b/pkg/indicator/v2_pivotlow.go @@ -6,9 +6,7 @@ import ( type PivotLowStream struct { Float64Series - - rawValues floats.Slice - + rawValues floats.Slice window, rightWindow int }