Skip to content

Commit

Permalink
Volatility: Donchian Channel (DC)
Browse files Browse the repository at this point in the history
Fixes #44
  • Loading branch information
cinar committed Jan 26, 2022
1 parent 4ff0442 commit 9c9f132
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions volatility_indicators.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,31 @@ 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
}

// The default ulcer index with the default period of 14.
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
}
15 changes: 15 additions & 0 deletions volatility_indicators.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions volatility_indicators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 9c9f132

Please sign in to comment.