Basically I'm just running a for-loop that plots and saves a bunch of figures as PNG and when I'm up to like 25 figures to save in total I get this "Fail to allocate bitmap" error however I make sure to clear the axis, figure and figure window in between each one, so what gives?
Here's my code just in case:
def update_trade_graphs(instruments):
print('chokepoint 13')
for i in range(0, len(instruments)):
if instruments[i].linked_trade is False:
continue
# Update variables
bid = instruments[i].orderbook['bids'][0][0] if len(instruments[i].orderbook['bids']) > 0 else None
ask = instruments[i].orderbook['asks'][0][0] if len(instruments[i].orderbook['asks']) > 0 else None
trades[instruments[i].linked_trade].time_series.append(date2num(current_time_mark))
trades[instruments[i].linked_trade].bid_prices.append(bid)
trades[instruments[i].linked_trade].ask_prices.append(ask)
for timespan in timespans_all:
if timespan in trades[instruments[i].linked_trade].buy_targets:
pass
else:
trades[instruments[i].linked_trade].buy_targets[timespan] = []
trades[instruments[i].linked_trade].buy_targets[timespan].append(instruments[i].minmax[timespan][0])
if timespan in trades[instruments[i].linked_trade].sell_targets:
pass
else:
trades[instruments[i].linked_trade].sell_targets[timespan] = []
trades[instruments[i].linked_trade].sell_targets[timespan].append(instruments[i].minmax[timespan][1])
# Plot graph
fig = plt.figure()
ax1 = plt.subplot2grid((1, 1), (0, 0))
for timespan in timespans_all:
ax1.plot_date(trades[instruments[i].linked_trade].time_series,
trades[instruments[i].linked_trade].buy_targets[timespan], '-', label='Buy Target', color='c')
ax1.plot_date(trades[instruments[i].linked_trade].time_series,
trades[instruments[i].linked_trade].sell_targets[timespan], '-', label='Sell Target',
color='m')
ax1.plot_date(trades[instruments[i].linked_trade].time_series, trades[instruments[i].linked_trade].bid_prices,
'-', label='Bid', color='r')
ax1.plot_date(trades[instruments[i].linked_trade].time_series, trades[instruments[i].linked_trade].ask_prices,
'-', label='Ask', color='b')
ax1.axhline(trades[instruments[i].linked_trade].entry_price, linestyle=':', color='c')
if trades[instruments[i].linked_trade].exit_price > 0:
ax1.axhline(trades[instruments[i].linked_trade].exit_price, linestyle=':', color='m')
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(90)
ax1.grid(True)
plt.xlabel('Date')
plt.ylabel('Price')
plt.title(trades[instruments[i].linked_trade].symbol)
plt.subplots_adjust(left=0.09, bottom=0.23, right=0.94, top=0.95, wspace=0.2, hspace=0)
plt.savefig('trade_{0}.png'.format(instruments[i].linked_trade))
plt.close(fig)
plt.clf()
plt.cla()