5
5
6
6
# Visualizations for trades
7
7
8
+
8
9
def plot_trades_gant (all_trades : List [List [Trade ]]):
9
10
fig , ax = plt .subplots (figsize = (10 , 6 ))
10
11
legend_labels = []
@@ -23,59 +24,87 @@ def plot_trades_gant(all_trades: List[List[Trade]]):
23
24
trade_start_dates_num = date2num (trade_start_dates )
24
25
trade_end_dates_num = date2num (trade_end_dates )
25
26
26
- ax .barh (trade_labels , trade_end_dates_num - trade_start_dates_num , left = trade_start_dates_num , height = 0.5 )
27
+ ax .barh (
28
+ trade_labels ,
29
+ trade_end_dates_num - trade_start_dates_num ,
30
+ left = trade_start_dates_num ,
31
+ height = 0.5 ,
32
+ )
27
33
28
- ax .set_xlabel (' Timeline' )
29
- ax .set_ylabel (' Trades' )
30
- ax .set_title (' Trade Openings Overlapping Timelines' )
34
+ ax .set_xlabel (" Timeline" )
35
+ ax .set_ylabel (" Trades" )
36
+ ax .set_title (" Trade Openings Overlapping Timelines" )
31
37
ax .legend (legend_labels )
32
38
ax .xaxis_date ()
33
39
34
40
plt .show ()
35
41
42
+
36
43
# plot a list of values with dates
37
- def plot_list_dates (values : List [float ], dates : List , title : str , ylabel : str , peaks , show_cursor = True ):
44
+ def plot_list_dates (
45
+ values : List [float ], dates : List , title : str , ylabel : str , peaks , show_cursor = True
46
+ ):
38
47
from datetime import datetime
39
48
import mplcursors
40
49
import matplotlib .dates as mdates
41
50
42
51
fig , ax = plt .subplots (figsize = (10 , 6 ))
43
52
dates = [datetime .fromisoformat (d ) for d in dates ]
44
53
ax .plot (dates , values )
45
- ax .set_xlabel (' Date' )
54
+ ax .set_xlabel (" Date" )
46
55
ax .set_ylabel (ylabel )
47
56
ax .set_title (title )
48
57
ax .xaxis_date ()
49
58
50
59
if show_cursor :
51
60
cursor = mplcursors .cursor (ax , hover = True )
61
+
52
62
# Customize the annotation
53
63
@cursor .connect ("add" )
54
64
def on_add (sel ):
55
65
x_date = mdates .num2date (sel .target [0 ]) # Convert to datetime
56
- sel .annotation .set (text = f"{ x_date .strftime ('%Y-%m-%d' )} \n Value: { sel .target [1 ]:.2f} " )
66
+ sel .annotation .set (
67
+ text = f"{ x_date .strftime ('%Y-%m-%d' )} \n Value: { sel .target [1 ]:.2f} "
68
+ )
69
+
57
70
average_value = sum (values ) / len (values )
58
71
# plot average as dotted line
59
- ax .axhline (average_value , color = ' orange' , linestyle = '--' , label = ' Average' )
72
+ ax .axhline (average_value , color = " orange" , linestyle = "--" , label = " Average" )
60
73
61
74
# plot standard deviation above and below average
62
75
from Trading .utils .calculations import calculate_standard_deviation
76
+
63
77
std_dev = calculate_standard_deviation (values )
64
78
STD_SCALER = 1.5
65
- ax .axhline (average_value + STD_SCALER * std_dev , color = 'green' , linestyle = '--' , label = f'Above { STD_SCALER } Std Dev' )
66
- ax .axhline (average_value - STD_SCALER * std_dev , color = 'red' , linestyle = '--' , label = f'Below { STD_SCALER } Std Dev' )
79
+ ax .axhline (
80
+ average_value + STD_SCALER * std_dev ,
81
+ color = "green" ,
82
+ linestyle = "--" ,
83
+ label = f"Above { STD_SCALER } Std Dev" ,
84
+ )
85
+ ax .axhline (
86
+ average_value - STD_SCALER * std_dev ,
87
+ color = "red" ,
88
+ linestyle = "--" ,
89
+ label = f"Below { STD_SCALER } Std Dev" ,
90
+ )
67
91
68
92
peak_values = peaks ["values" ]
69
93
peak_dates = peaks ["dates" ]
70
- ax .plot (peak_dates , peak_values , 'ro' , label = ' Peaks' )
94
+ ax .plot (peak_dates , peak_values , "ro" , label = " Peaks" )
71
95
72
96
peaks_above_std = []
73
97
peaks_above_std_dates = []
74
98
for i in range (len (peak_values )):
75
- if abs (peak_values [i ] - average_value ) > STD_SCALER * std_dev :
99
+ if abs (peak_values [i ] - average_value ) > STD_SCALER * std_dev :
76
100
peaks_above_std .append (peak_values [i ])
77
101
peaks_above_std_dates .append (peak_dates [i ])
78
- ax .plot (peaks_above_std_dates , peaks_above_std , 'go' , label = f'Peaks outside { STD_SCALER } Std Dev' )
102
+ ax .plot (
103
+ peaks_above_std_dates ,
104
+ peaks_above_std ,
105
+ "go" ,
106
+ label = f"Peaks outside { STD_SCALER } Std Dev" ,
107
+ )
79
108
80
109
plt .xticks (rotation = 45 )
81
110
plt .grid (True )
0 commit comments