From cef5249b9b4e503729520f062e3d5a4d89a56560 Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Tue, 25 Jan 2022 11:55:44 -0800 Subject: [PATCH] Issue 41 Fixing Money Flow Index. (#42) * Fixing Money Flow Index Issue #41. * New test. --- volume_indicators.go | 2 +- volume_indicators_test.go | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/volume_indicators.go b/volume_indicators.go index 32d822f..76b622e 100644 --- a/volume_indicators.go +++ b/volume_indicators.go @@ -85,7 +85,7 @@ func MoneyFlowIndex(period int, high, low, closing []float64, volume []int64) [] moneyRatio := divide( Sum(period, positiveMoneyFlow), - Sum(period, negativeMoneyFlow)) + Sum(period, multiplyBy(negativeMoneyFlow, -1))) moneyFlowIndex := addBy(multiplyBy(pow(addBy(moneyRatio, 1), -1), -100), 100) diff --git a/volume_indicators_test.go b/volume_indicators_test.go index b885111..bb4d7b2 100644 --- a/volume_indicators_test.go +++ b/volume_indicators_test.go @@ -12,13 +12,25 @@ func TestMoneyFlowIndex(t *testing.T) { low := []float64{6, 7, 9, 12, 10} closing := []float64{9, 11, 7, 10, 8} volume := []int64{100, 110, 80, 120, 90} - expected := []float64{100, 100, 406.85, 207.69, 266.67} + expected := []float64{100, 100, 57.01, 65.85, 61.54} period := 2 actual := roundDigitsAll(MoneyFlowIndex(period, high, low, closing, volume), 2) testEquals(t, actual, expected) } +func TestMoneyFlowIndex2(t *testing.T) { + high := []float64{2390.9, 2386.3, 2395.33, 2399.0, 2402.46, 2401.15, 2421.98, 2430.31, 2426.33, 2434.93, 2470.83, 2483.36, 2467.19, 2450.72} + low := []float64{2373.15, 2370.0, 2380.77, 2384.28, 2387.46, 2385.02, 2383.18, 2408.39, 2410.59, 2420.89, 2428.92, 2456.77, 2437.65, 2440.87} + closing := []float64{2373.39, 2382.47, 2394.4, 2387.51, 2395.64, 2389.47, 2410.24, 2425.37, 2422.33, 2430.29, 2465.76, 2466.27, 2440.07, 2445.85} + volume := []int64{1621, 1387, 1444, 1298, 1629, 1598, 2311, 2934, 2128, 1823, 5078, 6693, 3960, 1927} + expected := []float64{100, 53.884998, 68.888204, 53.300054, 63.645476, 52.296425, 62.119198, 70.011701, 60.826077, 54.659774, 64.728457, 72.749002, 64.185176, 60.710993} + period := 14 + + actual := roundDigitsAll(MoneyFlowIndex(period, high, low, closing, volume), 6) + testEquals(t, actual, expected) +} + func TestForceIndex(t *testing.T) { closing := []float64{9, 11, 7, 10, 8} volume := []int64{100, 110, 80, 120, 90}