From 9b973a15b33555bad9ad0960f98e047992092cbc Mon Sep 17 00:00:00 2001 From: "Tianlang (Jackie) Chen" <305351710+jackiectl2@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:06:26 -0500 Subject: [PATCH 1/3] fix(momentum): use a neutral IBS value for flat bars A flat bar has no location within its high-low range. Return the neutral midpoint 0.5 rather than the low endpoint 0; cover float32 and float64 streams with flat and ordinary bars. Fixes #532. Signed-off-by: Tianlang (Jackie) Chen <305351710+jackiectl2@users.noreply.github.com> --- momentum/README.md | 2 +- momentum/ibs.go | 4 ++-- momentum/ibs_test.go | 17 +++++++++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/momentum/README.md b/momentum/README.md index c0cce400..879910e1 100644 --- a/momentum/README.md +++ b/momentum/README.md @@ -1067,7 +1067,7 @@ String is the string representation of the Ichimoku Cloud. ## type [InternalBarStrength]() -InternalBarStrength represents the parameters for calculating the Internal Bar Strength \(IBS\). It tracks price location within a daily high\-low range. +InternalBarStrength represents the parameters for calculating the Internal Bar Strength \(IBS\). It tracks price location within a daily high\-low range. Bars with no range return the neutral midpoint, 0.5. ``` IBS = (Close - Low) / (High - Low) diff --git a/momentum/ibs.go b/momentum/ibs.go index 29a81119..bff17d49 100644 --- a/momentum/ibs.go +++ b/momentum/ibs.go @@ -12,7 +12,7 @@ import ( // InternalBarStrength represents the parameters for calculating the // Internal Bar Strength (IBS). It tracks price location within a daily -// high-low range. +// high-low range. Bars with no range return the neutral midpoint, 0.5. // // IBS = (Close - Low) / (High - Low) // @@ -31,7 +31,7 @@ func NewInternalBarStrength[T helper.Float]() *InternalBarStrength[T] { func (ibs *InternalBarStrength[T]) ComputeWithContext(ctx context.Context, highs, lows, closings <-chan T) <-chan T { return helper.Operate3WithContext(ctx, highs, lows, closings, func(high, low, closing T) T { denom := high - low - return helper.SafeDivide(closing-low, denom, T(0)) + return helper.SafeDivide(closing-low, denom, T(0.5)) }) } diff --git a/momentum/ibs_test.go b/momentum/ibs_test.go index 996db97d..48b4c89c 100644 --- a/momentum/ibs_test.go +++ b/momentum/ibs_test.go @@ -5,6 +5,7 @@ package momentum_test import ( + "context" "testing" "github.com/cinar/indicator/v2/helper" @@ -68,7 +69,19 @@ func TestInternalBarStrengthZeroDenom(t *testing.T) { actual := ibs.Compute(highs, lows, closings) val := <-actual - if val != 0.0 { - t.Fatalf("expected 0.0 when high == low, got %f", val) + if val != 0.5 { + t.Fatalf("expected neutral 0.5 when high == low, got %f", val) + } +} + +func TestInternalBarStrengthFlatBarsFloat32(t *testing.T) { + ibs := momentum.NewInternalBarStrength[float32]() + actual := ibs.ComputeWithContext(context.Background(), + helper.SliceToChan([]float32{100, 102, 100, 104}), + helper.SliceToChan([]float32{100, 100, 100, 100}), + helper.SliceToChan([]float32{100, 100, 100, 104})) + expected := helper.SliceToChan([]float32{0.5, 0, 0.5, 1}) + if err := helper.CheckEquals(actual, expected); err != nil { + t.Fatal(err) } } From c7ea6c6a9e93b7b831051a2bfefb2dfd7d466520 Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Tue, 24 Nov 2026 07:38:11 -0800 Subject: [PATCH 2/3] test(talibcompat): unskip InternalBarStrength after flat-bar fix --- internal/talibcompat/talibcompat_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/talibcompat/talibcompat_test.go b/internal/talibcompat/talibcompat_test.go index 13e5aa0d..01d5bd6f 100644 --- a/internal/talibcompat/talibcompat_test.go +++ b/internal/talibcompat/talibcompat_test.go @@ -204,7 +204,7 @@ var testCases = []testCase{ go helper.Drain(lagging) return outs(conversion, base, spanA, spanB) }}, - {name: "InternalBarStrength", columns: []string{"IBS"}, skip: "https://github.com/cinar/indicator/issues/532", compute: func(b *bars) []<-chan float64 { + {name: "InternalBarStrength", columns: []string{"IBS"}, compute: func(b *bars) []<-chan float64 { return outs(momentum.NewInternalBarStrength[float64]().Compute(in(b.high), in(b.low), in(b.close))) }}, {name: "Ppo", columns: []string{"PPO", "PPO_SIGNAL", "PPO_HIST"}, compute: func(b *bars) []<-chan float64 { From b03034ba309d860d8f810dfff1ad4d57d03be234 Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Tue, 24 Nov 2026 07:49:17 -0800 Subject: [PATCH 3/3] docs(volume): drop outdated IBS reference from MFM doc comment --- volume/README.md | 14 +++++++------- volume/mfm.go | 7 +++---- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/volume/README.md b/volume/README.md index 92f61838..0e224c85 100644 --- a/volume/README.md +++ b/volume/README.md @@ -660,7 +660,7 @@ func (m *Mfi[T]) String() string String is the string representation of the MFI. -## type [Mfm]() +## type [Mfm]() Mfm holds configuration parameters for calculating the Money Flow Multiplier \(MFM\), which adjusts volume based on the closing price's position within the high\-low range: @@ -670,7 +670,7 @@ MFM = ((Closing - Low) - (High - Closing)) / (High - Low) \- Positive MFM: Close in upper half of range, indicating buying pressure. \- Negative MFM: Close in lower half of range, indicating selling pressure. \- MFM of 1: Close equals high, strongest buying pressure. \- MFM of \-1: Close equals low, strongest selling pressure. -On a flat bar \(High == Low\), the close's position within the range is undefined \(0/0\). MFM returns 0, the neutral point of its \[\-1, 1\] scale, matching the InternalBarStrength precedent for the identical High\-Low denominator. Since MFM feeds Mfv, Ad, and Cmf, a neutral 0 flows through as "zero contribution" to all of them. +On a flat bar \(High == Low\), the close's position within the range is undefined \(0/0\). MFM returns 0, the neutral point of its \[\-1, 1\] scale. Since MFM feeds Mfv, Ad, and Cmf, a neutral 0 flows through as "zero contribution" to all of them. Example: @@ -684,7 +684,7 @@ type Mfm[T helper.Float] struct{} ``` -### func [NewMfm]() +### func [NewMfm]() ```go func NewMfm[T helper.Float]() *Mfm[T] @@ -693,7 +693,7 @@ func NewMfm[T helper.Float]() *Mfm[T] NewMfm function initializes a new MFM instance with the default parameters. -### func \(\*Mfm\[T\]\) [Compute]() +### func \(\*Mfm\[T\]\) [Compute]() ```go func (i *Mfm[T]) Compute(highs, lows, closings <-chan T) <-chan T @@ -704,7 +704,7 @@ Compute wraps ComputeWithContext for backwards compatibility. Deprecated: Use ComputeWithContext instead. -### func \(\*Mfm\[T\]\) [ComputeWithContext]() +### func \(\*Mfm\[T\]\) [ComputeWithContext]() ```go func (i *Mfm[T]) ComputeWithContext(ctx context.Context, highs, lows, closings <-chan T) <-chan T @@ -713,7 +713,7 @@ func (i *Mfm[T]) ComputeWithContext(ctx context.Context, highs, lows, closings < ComputeWithContext function takes a channel of numbers and computes the MFM. -### func \(\*Mfm\[T\]\) [IdlePeriod]() +### func \(\*Mfm\[T\]\) [IdlePeriod]() ```go func (*Mfm[T]) IdlePeriod() int @@ -722,7 +722,7 @@ func (*Mfm[T]) IdlePeriod() int IdlePeriod is the initial period that MFM won't yield any results. -### func \(\*Mfm\[T\]\) [String]() +### func \(\*Mfm\[T\]\) [String]() ```go func (*Mfm[T]) String() string diff --git a/volume/mfm.go b/volume/mfm.go index 3bc8f5ed..cd7454d9 100644 --- a/volume/mfm.go +++ b/volume/mfm.go @@ -21,10 +21,9 @@ import ( // - MFM of -1: Close equals low, strongest selling pressure. // // On a flat bar (High == Low), the close's position within the range is undefined -// (0/0). MFM returns 0, the neutral point of its [-1, 1] scale, matching the -// InternalBarStrength precedent for the identical High-Low denominator. Since MFM -// feeds Mfv, Ad, and Cmf, a neutral 0 flows through as "zero contribution" to all -// of them. +// (0/0). MFM returns 0, the neutral point of its [-1, 1] scale. Since MFM feeds +// Mfv, Ad, and Cmf, a neutral 0 flows through as "zero contribution" to all of +// them. // // Example: //