-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPlotFunctions.py
More file actions
150 lines (126 loc) · 4.01 KB
/
PlotFunctions.py
File metadata and controls
150 lines (126 loc) · 4.01 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import pandas as pd
from AlgoFunctions import Algo
import hvplot.pandas
from pandas.core.frame import DataFrame
import matplotlib
import matplotlib.pyplot as plt
def process_df(df, look_back, window_short, window_long):
df["Current_WR"] = Algo.get_william_r(df["High"], df["Low"], df["Close"], look_back)
df["Previous_WR"] = df["Current_WR"].shift(periods=look_back)
# Generate the short and long window simple moving averages (short and long days, respectively)
df["SMA50"] = df["Close"].rolling(window=window_short).mean()
df["SMA100"] = df["Close"].rolling(window=window_long).mean()
df.drop(columns=["Adj Close", 'Volume'], inplace=True)
return df
def plot_strategy_1(df, name: str, year_list):
# Slice the plot data with year from the date: get the year out of date
df['Year'] = pd.to_datetime(df.index).to_period("Y")
df['Year'] = df['Year'].dt.strftime('%Y')
df = df[df["Year"].isin(year_list)]
# Visualize exit_point position relative to close price
exit_point = df[df['Entry/Exit_S1'] < 0.0][['Close', 'Year']].hvplot.scatter(
y="Close",
color='red',
marker='v',
size=200,
legend=False,
ylabel='Price in $',
width=1000,
height=400,
)
# Visualize entry position relative to close price
entry = df[df['Entry/Exit_S1'] > 0.0][['Close', 'Year']].hvplot.scatter(
y="Close",
color='green',
marker='^',
size=200,
legend=False,
ylabel='Price in $',
width=1000,
height=400,
)
df_close = df[['Close', 'Year']].hvplot(
y="Close",
line_color='lightgrey',
ylabel='Price in $',
width=1000,
height=400,
)
plot = exit_point * entry * df_close
plot.opts(
title=name + " BOV Entry and Exit Points with Strategy 1"
)
return plot
def plot_strategy_2(df, name: str, year_list):
# Slice the plot data with year from the date: get the year out of date
df['Year'] = pd.to_datetime(df.index).to_period("Y")
df['Year'] = df['Year'].dt.strftime('%Y')
df = df[df["Year"].isin(year_list)]
# Visualize exit_point position relative to close price
exit_point = df[df['Entry/Exit_S2'] < 0.0][['Close', 'Year']].hvplot.scatter(
y="Close",
color='red',
marker='v',
size=200,
legend=False,
ylabel='Price in $',
width=1000,
height=400,
)
# Visualize entry position relative to close price
entry = df[df['Entry/Exit_S2'] > 0.0][['Close', 'Year']].hvplot.scatter(
y="Close",
color='green',
marker='^',
size=200,
legend=False,
ylabel='Price in $',
width=1000,
height=400,
)
df_close = df[['Close', 'Year']].hvplot(
y="Close",
line_color='lightgrey',
ylabel='Price in $',
width=1000,
height=400,
)
plot = exit_point * entry * df_close
plot.opts(
title=name + " BOV Entry and Exit Points with Strategy 2"
)
return plot
def plot_strategy_3(df, name: str, year_list):
df['Year'] = pd.to_datetime(df.index).to_period("Y")
df['Year'] = df['Year'].dt.strftime('%Y')
df = df[df["Year"].isin(year_list)]
exit_point = df[df['Cross Below Range'] == -1]['Close'].hvplot.scatter(
color='red',
marker='v',
size=200,
legend=False,
ylabel='Price in $',
width=1000,
height=400
)
# Visualize entry position relative to close price
entry = df[df['Cross Above Range'] == 1]['Close'].hvplot.scatter(
color='green',
marker='^',
size=200,
legend=False,
ylabel='Price in $',
width=1000,
height=400
)
btc_close = df[['Close']].hvplot(
line_color='lightgrey',
ylabel='Price in $',
width=1000,
height=400
)
plot = exit_point * entry * btc_close
plot.opts(
title=name + " BOV Entry and Exit Points with Strategy 3"
)
return plot