-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathichimoku.py
90 lines (57 loc) · 3.2 KB
/
ichimoku.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
import sys
from pyqtgraph.functions import Color
sys.path.append('../finplot')
import finplot as fplt
import backtrader as bt
import backtrader.indicators as btind
import pandas as pd
class Ichimoku():
'''
THIS CLASS TAKE A PANDA DATAFRAME AS INPUT, AND NOT A BACKTRADER DATA FEED (Use bt.Ind.Ichimoku instead)
'''
'''
Developed and published in his book in 1969 by journalist Goichi Hosoda
Formula:
- tenkan_sen = (Highest(High, tenkan) + Lowest(Low, tenkan)) / 2.0
- kijun_sen = (Highest(High, kijun) + Lowest(Low, kijun)) / 2.0
The next 2 are pushed 26 bars into the future
- senkou_span_a = (tenkan_sen + kijun_sen) / 2.0
- senkou_span_b = ((Highest(High, senkou) + Lowest(Low, senkou)) / 2.0
This is pushed 26 bars into the past
- chikou = close
The cloud (Kumo) is formed by the area between the senkou_spans
See:
- http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:ichimoku_cloud
'''
'''
THIS METHOD TAKE A PANDA DATAFRAME AS INPUT, AND NOT A BACKTRADER DATA FEED (Use bt.Ind.Ichimoku instead)
'''
def __init__(self, dataFrames, tenkan = 9, kijun = 26, senkou = 52, senkou_lead = 26, chikou = 26):
# Tenkan-sen (Conversion Line): (9-period high + 9-period low)/2))
period9_high = dataFrames['High'].rolling(window=tenkan).max()
period9_low = dataFrames['Low'].rolling(window=tenkan).min()
self.tenkan_sen = (period9_high + period9_low) / 2
# Kijun-sen (Base Line): (26-period high + 26-period low)/2))
period26_high = dataFrames['High'].rolling(window=kijun).max()
period26_low = dataFrames['Low'].rolling(window=kijun).min()
self.kijun_sen = (period26_high + period26_low) / 2
# Senkou Span A (Leading Span A): (Conversion Line + Base Line)/2))
self.senkou_span_a = ((self.tenkan_sen + self.kijun_sen) / 2).shift(senkou_lead)
# Senkou Span B (Leading Span B): (52-period high + 52-period low)/2))
period52_high = dataFrames['High'].rolling(window=senkou).max()
period52_low = dataFrames['Low'].rolling(window=senkou).min()
self.senkou_span_b = ((period52_high + period52_low) / 2).shift(senkou_lead)
# The most current closing price plotted 26 time periods behind (optional)
self.chikou_span = dataFrames['Close'].shift(-chikou) # 26 according to investopedia
pass
def draw(self, ax, tenkan_color = "magenta", kijun_color = "blue", senkou_a_color = "gray", senkou_b_color = "gray", chikou_color = "yellow"):
self.tenkan_sen_plot = fplt.plot(self.tenkan_sen, ax = ax, color=tenkan_color, width=1 )
self.kijun_sen_plot = fplt.plot(self.kijun_sen, ax = ax, color=kijun_color, width=2 )
self.chikou_span_plot = fplt.plot(self.chikou_span, ax = ax, color=chikou_color, width=2 )
self.senkou_span_a_plot = fplt.plot(self.senkou_span_a, ax = ax, color=senkou_a_color )
self.senkou_span_b_plot = fplt.plot(self.senkou_span_b, ax = ax, color=senkou_b_color )
fplt.fill_between( self.senkou_span_a_plot, self.senkou_span_b_plot, color = Color("darkGray") )
pass
def clear(self):
pass