From 9c9f13260ee323b7b7be62968f652709c0baca48 Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Wed, 26 Jan 2022 14:27:53 -0800 Subject: [PATCH] Volatility: Donchian Channel (DC) Fixes #44 --- README.md | 1 + volatility_indicators.go | 21 +++++++++++++++++++++ volatility_indicators.md | 15 +++++++++++++++ volatility_indicators_test.go | 13 +++++++++++++ 4 files changed, 50 insertions(+) diff --git a/README.md b/README.md index 7da3a0a..4b948c2 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ The following list of indicators are currently supported by this package: - [Bollinger Band Width](volatility_indicators.md#bollinger-band-width) - [Bollinger Bands](volatility_indicators.md#bollinger-bands) - [Chandelier Exit](volatility_indicators.md#chandelier-exit) +- [Donchian Channel (DC)](volatility_indicators.md#donchian-channel-dc) - [Moving Standard Deviation (Std)](volatility_indicators.md#moving-standard-deviation-std) - [Projection Oscillator (PO)](volatility_indicators.md#projection-oscillator-po) - [Ulcer Index (UI)](volatility_indicators.md#ulcer-index-ui) diff --git a/volatility_indicators.go b/volatility_indicators.go index 191a867..7521e36 100644 --- a/volatility_indicators.go +++ b/volatility_indicators.go @@ -180,6 +180,7 @@ func UlcerIndex(period int, closing []float64) []float64 { percentageDrawdown := multiplyBy(divide(substract(closing, highClosing), highClosing), 100) squaredAverage := Sma(period, multiply(percentageDrawdown, percentageDrawdown)) ui := sqrt(squaredAverage) + return ui } @@ -187,3 +188,23 @@ func UlcerIndex(period int, closing []float64) []float64 { func DefaultUlcerIndex(closing []float64) []float64 { return UlcerIndex(14, closing) } + +// The Donchian Channel (DC) calculates three lines generated by moving average +// calculations that comprise an indicator formed by upper and lower bands +// around a midrange or median band. The upper band marks the highest +// price of an asset while the lower band marks the lowest price of +// an asset, and the area between the upper and lower bands +// represents the Donchian Channel. +// +// Upper Channel = Mmax(period, closings) +// Lower Channel = Mmin(period, closings) +// Middle Channel = (Upper Channel + Lower Channel) / 2 +// +// Returns upperChannel, middleChannel, lowerChannel. +func DonchianChannel(period int, closing []float64) ([]float64, []float64, []float64) { + upperChannel := Max(period, closing) + lowerChannel := Min(period, closing) + middleChannel := divideBy(add(upperChannel, lowerChannel), 2) + + return upperChannel, middleChannel, lowerChannel +} diff --git a/volatility_indicators.md b/volatility_indicators.md index 47a70c0..58bee77 100644 --- a/volatility_indicators.md +++ b/volatility_indicators.md @@ -7,6 +7,7 @@ Volatility indicators measure the rate of movement regardless of its direction. - [Bollinger Band Width](#bollinger-band-width) - [Bollinger Bands](#bollinger-bands) - [Chandelier Exit](#chandelier-exit) +- [Donchian Channel (DC)](#donchian-channel-dc) - [Moving Standard Deviation (Std)](#moving-standard-deviation-std) - [Projection Oscillator (PO)](#projection-oscillator-po) - [Ulcer Index (UI)](#ulcer-index-ui) @@ -79,6 +80,20 @@ Chandelier Exit Short = 22-Period SMA Low + ATR(22) * 3 chandelierExitLong, chandelierExitShort := indicator.ChandelierExit(high, low, closing) ``` +#### Donchian Channel (DC) + +The [DonchianChannel](https://pkg.go.dev/github.com/cinar/indicator#DonchianChannel) calculates three lines generated by moving average calculations that comprise an indicator formed by upper and lower bands around a midrange or median band. The upper band marks the highest price of an asset while the lower band marks the lowest price of an asset, and the area between the upper and lower bands represents the Donchian Channel. + +``` +Upper Channel = Mmax(period, closings) +Lower Channel = Mmin(period, closings) +Middle Channel = (Upper Channel + Lower Channel) / 2 +``` + +```golang +upperChannel, middleChannel, lowerChannel := indicator.DonchianChannel(period, closing) +``` + #### Moving Standard Deviation (Std) The [Std](https://pkg.go.dev/github.com/cinar/indicator#Std) function calculates the moving standard deviation for a given period. diff --git a/volatility_indicators_test.go b/volatility_indicators_test.go index 25c306d..6292a7f 100644 --- a/volatility_indicators_test.go +++ b/volatility_indicators_test.go @@ -26,3 +26,16 @@ func TestUlcerIndex(t *testing.T) { actual := DefaultUlcerIndex(closing) testEquals(t, roundDigitsAll(actual, 2), expected) } + +func TestDonchianChannel(t *testing.T) { + closing := []float64{9, 11, 7, 10, 8} + period := 4 + expectedUpperChannel := []float64{9, 11, 11, 11, 11} + expectedMiddleChannel := []float64{9, 10, 9, 9, 9} + expectedLowerChannel := []float64{9, 9, 7, 7, 7} + + actualUpperChannel, actualMiddleChannel, actualLowerChannel := DonchianChannel(period, closing) + testEquals(t, roundDigitsAll(actualUpperChannel, 2), expectedUpperChannel) + testEquals(t, roundDigitsAll(actualMiddleChannel, 2), expectedMiddleChannel) + testEquals(t, roundDigitsAll(actualLowerChannel, 2), expectedLowerChannel) +}