-
Notifications
You must be signed in to change notification settings - Fork 3
/
ichimoku.py
104 lines (80 loc) · 4.39 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import numpy as np
import pandas as pd
from pandas_datareader import data, wb
import yfinance as yf
import datetime as dt
from chart_studio import plotly
from plotly.subplots import make_subplots
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, plot, iplot
def get_ichimoku_plot():
# Range of Crypto
#def fetch_data():
ticker = ["ETH-USD"]
#start = pd.to_datetime('2020-01-01')
end = dt.datetime.now().date()
crypto_df = yf.download(ticker, period = "1y", group_by = 'ticker', threads = True, progress=False)[['Open','High','Low','Close']].dropna()
#return crypto_df
#fetch_data().tail()
index = pd.date_range(end, periods=26, freq='D')
columns = crypto_df.columns
dfna = pd.DataFrame(index=index, columns=columns)
crypto_df = pd.concat([crypto_df,dfna])
# Tenkan-sen (Conversion Line): (nine-period high + nine-period low)/2))
nine_period_high = crypto_df['High'].rolling(window= 9).max()
nine_period_low = crypto_df['Low'].rolling(window= 9).min()
crypto_df['tenkan_sen'] = (nine_period_high + nine_period_low) /2
# Kijun-sen (Base Line): (26-period high + 26-period low)/2))
period26_high = crypto_df['High'].rolling(window=26).max()
period26_low = crypto_df['Low'].rolling(window=26).min()
crypto_df['kijun_sen'] = (period26_high + period26_low) / 2
# The most current closing price plotted 26 time periods behind (optional)
crypto_df['chikou_span'] = crypto_df['Close'].shift(-26)
# Senkou Span A (Leading Span A): (Conversion Line + Base Line)/2))
crypto_df['senkou_span_a'] = ((crypto_df['tenkan_sen'] + crypto_df['kijun_sen']) / 2).shift(26)
# Senkou Span B (Leading Span B): (52-period high + 52-period low)/2))
period52_high = crypto_df['High'].rolling(window=52).max()
period52_low = crypto_df['Low'].rolling(window=52).min()
crypto_df['senkou_span_b'] = ((period52_high + period52_low) / 2).shift(26)
# Grab just the `date` and `close` from the IEX dataset
#ichimoku_signals_df = pd.DataFrame(crypto_df['Close'])
crypto_df.index.name = 'Date'
# Initialize the new `Signal1` column
crypto_df['Signal1'] = 0
# Generate the trading signal (1 or 0) to when the -----Conversion cross over the Base-----
# Note: Use 1 when Conversion is more than/crosses over the Base and 0 for when it is not/lower.
#Conversion
crypto_df['Signal1'] = np.where(
(crypto_df['tenkan_sen'].shift(1) <= crypto_df['kijun_sen'].shift(1)) & (crypto_df['tenkan_sen'] > crypto_df['kijun_sen']), 1, crypto_df['Signal1'])
#Base
crypto_df['Signal1'] = np.where(
(crypto_df['tenkan_sen'].shift(1) >= crypto_df['kijun_sen'].shift(1)) & (crypto_df['tenkan_sen'] < crypto_df['kijun_sen']), -1, crypto_df['Signal1'])
df_new = crypto_df[crypto_df['Signal1'] == 1.0][['Close']]
#declare figure
fig = go.Figure()
# Set up traces
fig.add_trace(go.Candlestick(x=crypto_df.index,
open=crypto_df['Open'],
high=crypto_df['High'],
low=crypto_df['Low'],
close=crypto_df['Close'], name = 'market data'))
fig.add_trace(go.Scatter(x=crypto_df.index, y= crypto_df['tenkan_sen'],line=dict(color='royalblue', width=.8), name = 'Conversion Line'))
fig.add_trace(go.Scatter(x=crypto_df.index, y= crypto_df['kijun_sen'],line=dict(color='darkorange', width=.8), name = 'Base Line'))
fig.add_trace(go.Scatter(x=crypto_df.index, y= crypto_df['senkou_span_a'],line=dict(color='green', width=.8), name = 'Leading A'))
fig.add_trace(go.Scatter(x=crypto_df.index, y= crypto_df['senkou_span_b'],line=dict(color='red', width=.8), name = 'Leading B'))
fig.add_trace(go.Scatter(x=crypto_df.index, y= crypto_df['chikou_span'],line=dict(color='black', width=.8), name = 'Lagging Span'))
# Visualize entry position relative to close price
fig.add_trace(go.Scatter(x= df_new.index,
y= df_new['Close'],
marker_color='yellow',
mode="markers",
name = 'Entry',
marker=dict(showscale=True)))
#Show
fig.update_layout(height=900, title_text="Entry and Exit Points of Ichimoku Span A & B crossover Trading Strategy", legend=dict(
yanchor="top",
y=0.99,
xanchor="left",
x=0.01
))
return fig