-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
105 lines (91 loc) · 3.11 KB
/
plot.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
105
import plotly.graph_objects as go
def plot(results, df):
"""Custom plot function to visualize trade entries and exits with indicators"""
# Prepare the data
indicators = results._strategy._indicators
trades = results["_trades"]
# Filter entries and exits
long_entries = trades.loc[trades.Size > 0, "EntryTime"]
long_entry_prices = trades.loc[trades.Size > 0, "EntryPrice"]
short_entries = trades.loc[trades.Size < 0, "EntryTime"]
short_entry_prices = trades.loc[trades.Size < 0, "EntryPrice"]
long_exits = trades.loc[trades.Size > 0, "ExitTime"]
long_exit_prices = trades.loc[trades.Size > 0, "ExitPrice"]
short_exits = trades.loc[trades.Size < 0, "ExitTime"]
short_exit_prices = trades.loc[trades.Size < 0, "ExitPrice"]
# Create a line plot for the asset's price data
price_trace = go.Scatter(
x=df.index,
y=df["Close"],
mode="lines",
line=dict(color="blue", width=2),
name="Price",
)
# Create a scatter plot for long (buy) entries
long_entry_trace = go.Scatter(
x=long_entries,
y=long_entry_prices,
mode="markers",
marker=dict(color="black", size=10),
name="Long Entry",
hoverinfo="text",
text=long_entries, # Show entry time on hover
)
# Create a scatter plot for exit trades
exit_long_trace = go.Scatter(
x=long_exits,
y=long_exit_prices,
mode="markers",
marker=dict(color="green", size=10),
name="Long Exit",
hoverinfo="text",
text=long_exits, # Show exit time on hover
)
# Create a scatter plot for short (sell) entries
short_entry_trace = go.Scatter(
x=short_entries,
y=short_entry_prices,
mode="markers",
marker=dict(color="orange", size=10),
name="Short Entry",
hoverinfo="text",
text=short_entries, # Show entry time on hover
)
# Create a scatter plot for short (sell) exit trades
exit_short_trace = go.Scatter(
x=short_exits,
y=short_exit_prices,
mode="markers",
marker=dict(color="red", size=10),
name="Short Exit",
hoverinfo="text",
text=short_exits, # Show exit time on hover
)
# Combine the plots
fig = go.Figure(
data=[
price_trace,
long_entry_trace,
short_entry_trace,
exit_long_trace,
exit_short_trace,
]
)
# Plot each indicator dynamically
for _, value in enumerate(indicators):
fig.add_trace(go.Scatter(x=df.index, y=value, mode="lines", name=value.name))
# Customize layout with a white background
fig.update_layout(
title="Trade Entries and Exits",
xaxis_title="Date",
yaxis_title="Price",
hovermode="closest",
plot_bgcolor="white",
xaxis=dict(linecolor="black"),
yaxis=dict(linecolor="black"),
)
# Set the grid color
fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor="lightgrey")
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor="lightgrey")
# Show the plot
fig.show()