forked from the-quantclub-iitbhu/Technical-indicators
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculate_ichimoku.py
More file actions
56 lines (48 loc) · 1.91 KB
/
calculate_ichimoku.py
File metadata and controls
56 lines (48 loc) · 1.91 KB
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
def calculate_ichimoku(data, conversion_periods, base_periods, lagging_span2_periods):
"""
Calculates the Ichimoku Kinko Hyo components.
The Ichimoku indicator consists of:
- Conversion Line (Tenkan-sen): Midpoint of the highest high and lowest low over the conversion period.
- Base Line (Kijun-sen): Midpoint of the highest high and lowest low over the base period.
- Leading Span 1 (Senkou Span A): Average of Conversion and Base Lines, plotted in the future.
- Leading Span 2 (Senkou Span B): Midpoint of the highest high and lowest low over the lagging span 2 period.
Parameters:
----------
data : pd.DataFrame
A DataFrame containing 'high' and 'low' columns.
conversion_periods : int
Lookback period for the Conversion Line.
base_periods : int
Lookback period for the Base Line.
lagging_span2_periods : int
Lookback period for the Leading Span 2.
Returns:
-------
tuple
A tuple containing the following components:
- Conversion Line (pd.Series)
- Base Line (pd.Series)
- Lead Line 1 (pd.Series)
- Lead Line 2 (pd.Series)
Example Usage:
--------------
```python
conversion, base, lead1, lead2 = calculate_ichimoku(data, 9, 26, 52)
data['Conversion_Line'] = conversion
data['Base_Line'] = base
data['Lead_Line1'] = lead1
data['Lead_Line2'] = lead2
```
"""
# Conversion Line (Tenkan-sen)
conversion_line = donchian(data, conversion_periods)
# Base Line (Kijun-sen)
base_line = donchian(data, base_periods)
# Leading Span 1 (Senkou Span A)
lead_line1 = (conversion_line + base_line) / 2
# Leading Span 2 (Senkou Span B)
lead_line2 = (
data['low'].rolling(window=lagging_span2_periods).min() +
data['high'].rolling(window=lagging_span2_periods).max()
) / 2
return conversion_line, base_line, lead_line1, lead_line2