Skip to content

Commit 165730c

Browse files
committed
Ichimoku Cloud indicator added.
1 parent 3ac7bf5 commit 165730c

4 files changed

Lines changed: 70 additions & 0 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Indicator is a Golang module providing various stock technical analysis indicato
1919
- [On-Balance Volume (OBV)](#on-balance-volume-obv)
2020
- [Actual True Range (ATR)](#actual-true-range-atr)
2121
- [Chandelier Exit](#chandelier-exit)
22+
- [Ichimoku Cloud](#ichimoku-cloud)
2223

2324
## Usage
2425

@@ -202,6 +203,22 @@ Chandelier Exit Short = 22-Period SMA Low + ATR(22) * 3
202203
chandelierExitLong, chandelierExitShort := indicator.ChandelierExit(high, low, close)
203204
```
204205

206+
#### Ichimoku Cloud
207+
208+
The [IchimokuCloud](https://pkg.go.dev/github.com/cinar/indicator#IchimokuCloud), also known as Ichimoku Kinko Hyo, calculates a versatile indicator that defines support and resistence, identifies tred direction, gauges momentum, and provides trading signals.
209+
210+
```
211+
Tenkan-sen (Conversion Line) = (9-Period High + 9-Period Low) / 2
212+
Kijun-sen (Base Line) = (26-Period High + 26-Period Low) / 2
213+
Senkou Span A (Leading Span A) = (Conversion Line + Base Line) / 2
214+
Senkou Span B (Leading Span B) = (52-Period High + 52-Period Low) / 2
215+
Chikou Span (Lagging Span) = Close plotted 26 days in the past.
216+
```
217+
218+
```Golang
219+
conversionLine, baseLine, leadingSpanA, leadingSpanB, laggingLine := indicator.IchimokuCloud(high, low, close)
220+
```
221+
205222
## License
206223

207224
The source code is provided under MIT License.

helper.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,14 @@ func groupPositivesAndNegatives(values []float64) ([]float64, []float64) {
6565

6666
return positives, negatives
6767
}
68+
69+
// Shift right for period.
70+
func shiftRight(period int, values []float64) []float64 {
71+
result := make([]float64, len(values))
72+
73+
for i := period; i < len(result); i++ {
74+
result[i] = values[i-period]
75+
}
76+
77+
return result
78+
}

helper_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,21 @@ func TestGroupPositivesAndNegatives(t *testing.T) {
147147
}
148148
}
149149
}
150+
151+
func TestShiftRight(t *testing.T) {
152+
values := []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
153+
expected := []float64{0, 0, 0, 0, 1, 2, 3, 4, 5, 6}
154+
period := 4
155+
156+
actual := shiftRight(period, values)
157+
158+
if len(actual) != len(expected) {
159+
t.Fatalf("actual %d expected %d", len(actual), len(expected))
160+
}
161+
162+
for i := 0; i < len(actual); i++ {
163+
if actual[i] != expected[i] {
164+
t.Fatalf("at %d actual %f expected %f", i, actual[i], expected[i])
165+
}
166+
}
167+
}

indicator.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,27 @@ func ChandelierExit(high, low, close []float64) ([]float64, []float64) {
216216

217217
return chandelierExitLong, chandelierExitShort
218218
}
219+
220+
// Ichimoku Cloud. Also known as Ichimoku Kinko Hyo, is a versatile indicator that defines support and
221+
// resistence, identifies trend direction, gauges momentum, and provides trading signals.
222+
//
223+
// Tenkan-sen (Conversion Line) = (9-Period High + 9-Period Low) / 2
224+
// Kijun-sen (Base Line) = (26-Period High + 26-Period Low) / 2
225+
// Senkou Span A (Leading Span A) = (Conversion Line + Base Line) / 2
226+
// Senkou Span B (Leading Span B) = (52-Period High + 52-Period Low) / 2
227+
// Chikou Span (Lagging Span) = Close plotted 26 days in the past.
228+
//
229+
// Returns conversionLine, baseLine, leadingSpanA, leadingSpanB, laggingSpan
230+
func IchimokuCloud(high, low, close []float64) ([]float64, []float64, []float64, []float64, []float64) {
231+
if len(high) != len(low) || len(low) != len(close) {
232+
panic("not all same size")
233+
}
234+
235+
conversionLine := divide(add(Max(9, high), Min(9, low)), float64(2))
236+
baseLine := divide(add(Max(26, high), Min(26, low)), float64(2))
237+
leadingSpanA := divide(add(conversionLine, baseLine), float64(2))
238+
leadingSpanB := divide(add(Max(52, high), Min(52, low)), float64(2))
239+
laggingLine := shiftRight(26, close)
240+
241+
return conversionLine, baseLine, leadingSpanA, leadingSpanB, laggingLine
242+
}

0 commit comments

Comments
 (0)