-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate.py
128 lines (98 loc) · 4.55 KB
/
evaluate.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
""" Auxiliary functions for displaying the evaluation of a population of investors.
@Author: Gabriel Nogueira (Talendar)
"""
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
def static_plot(best_investors, ibov_var):
""" Plots the performances of IBOVESPA and the best investors of the population.
:param best_investors: performance history of the best investors.
:param ibov_var: price history of IBOVESPA.
"""
interval = range(len(ibov_var))
ax = plt.subplot()
plt.plot(interval, ibov_var, "g", linewidth=3, alpha=0.6)
plt.plot(interval, best_investors[0][0], "r", linewidth=3, alpha=0.6)
for i in best_investors[1:]:
plt.plot(interval, i[0], linewidth=2, alpha=0.6)
for line in ax.lines:
y = line.get_ydata()[-1]
ax.annotate('%0.2f%%' % y, xy=(1, y), xytext=(8, 0), color=line.get_color(),
xycoords=('axes fraction', 'data'), textcoords='offset points', weight="bold")
plt.legend(['IBOV'] + ["Investor %d" % (i + 1) for i in range(len(best_investors))], loc='upper left')
plt.xlabel("Time (days)")
plt.gca().yaxis.set_major_formatter(mticker.FormatStrFormatter('%.1f%%'))
plt.show()
def dynamic_plot(investor_history, ibov_var, print_ops, max_num_ops=7):
""" Dynamically plots, over time, the performance of IBOVESPA and on investor.
:param investor_history: performance history of the investor.
:param ibov_var: price history of IBOVESPA.
:param print_ops: if True, the investor's decisions (buy and sell operations) will be plot.
:param max_num_ops: max number of operations to display on the screen at a time.
"""
# init
plt.ion()
figure, ax = plt.subplots()
lines_ibov, = ax.plot([], [], "g", linewidth=3, alpha=0.6)
lines_inv, = ax.plot([], [], "r", linewidth=3, alpha=0.6)
BASE_PAUSE_TIME = 1
pause_time = BASE_PAUSE_TIME
ax.set_autoscaley_on(True)
ax.set_xlim(0, len(ibov_var))
ax.grid()
plt.legend(['IBOV', "Investor"], loc='upper left')
plt.xlabel("Time (days)")
plt.gca().yaxis.set_major_formatter(mticker.FormatStrFormatter('%.1f%%'))
figure.subplots_adjust(left=0.25, bottom=0.25)
# speed slider
spd_slider_ax = figure.add_axes([0.42, 0.07, 0.3, 0.05], facecolor='lightgoldenrodyellow')
spd_slider = plt.Slider(spd_slider_ax, 'Speed', 0.2, 20, valinit=BASE_PAUSE_TIME)
def spd_slider_on_changed(val):
nonlocal pause_time
pause_time = BASE_PAUSE_TIME / val
spd_slider.on_changed(spd_slider_on_changed)
# plot
xdata = []
ydata_inv = []
ydata_ibov = []
pc_ann_inv = pc_ann_ibov = None
op_points = []
op_annotations = []
for x in range(len(ibov_var)):
# set data
xdata.append(x)
ydata_inv.append(investor_history[0][x])
ydata_ibov.append(ibov_var[x])
lines_inv.set_xdata(xdata)
lines_inv.set_ydata(ydata_inv)
lines_ibov.set_xdata(xdata)
lines_ibov.set_ydata(ydata_ibov)
# rescale
ax.relim()
ax.autoscale_view()
# percentage annotation
if pc_ann_ibov is not None:
pc_ann_ibov.remove()
pc_ann_ibov = ax.annotate('%0.2f%%' % ydata_ibov[-1], xy=(1, ydata_ibov[-1]),
xytext=(8, 0), color=lines_ibov.get_color(),
xycoords=('axes fraction', 'data'), textcoords='offset points', weight="bold")
if pc_ann_inv is not None:
pc_ann_inv.remove()
pc_ann_inv = ax.annotate('%0.2f%%' % ydata_inv[-1], xy=(1, ydata_inv[-1]),
xytext=(8, 0), color=lines_inv.get_color(),
xycoords=('axes fraction', 'data'), textcoords='offset points', weight="bold")
# op annotation
if print_ops and investor_history[1][x][1] != 0:
color = "g" if investor_history[1][x][0] == "BUY" else "r"
p, = ax.plot([xdata[-1]], [ydata_inv[-1]], marker='o', markersize=5, color=color)
op_points.append(p)
op_annotations.append(
ax.annotate("%d" % investor_history[1][x][1],
xy=(xdata[-1], ydata_inv[-1]), xytext=(xdata[-1] - 0.25, ydata_inv[-1] - 0.25),
color=color, weight="bold", fontsize=8, arrowprops={"arrowstyle": "->"}))
if len(op_annotations) > max_num_ops:
op_points.pop(0).remove()
op_annotations.pop(0).remove() # remove the oldest annotation
# draw and delay
plt.pause(pause_time)
plt.ioff()
plt.show()