Open Mikeardy opened 2 years ago
This sounds similar to https://github.com/matplotlib/mplfinance/issues/386 but not quite the same. You may want to read through that to get some ideas.
The first thing is to make sure you are using matplotlib backend "Agg" ... If all you are doing is saving the plots to files, then the correct backend to use for that is "Agg". Based on your walkback trace, however it does seem to be using Agg, however you may want to confirm this in your code for at least one run.
It also appears that you are calling mpf.plot()
from something called deve-Multi.py
... is it possible you are running multiple instances at the same time? If so, that would be important information to have.
Can you please clarify:
iterates "n" times ... let say from 50 to 300, but the real number of plot generate are 5 to 10.
Please show all of the surrounding code, and/or clarify what it means to iterate 50 to 300 times to in order to generate only 5 or 10 plot files.
Three other possibilities that you may try are:
Set returnfig=True
. This will give you access to the Figure object which you can delete after saving the figure to a file.
Note well: When setting returnfig=True
do not pass your savefig=dict(fname=filename, dpi=150, pad_inches=0.50)
into mpf.plot()
(since it will be ignored anyway); rather after calling mpf.plot()
call Figure.savefig()
... something like this:
fig, axlist = mpf.plot(re, type="candle", style=style,
title=f"BOT{b} {symbol} {timeframe} {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} {Algorithm}",
addplot=adp, volume=False, returnfig=True )
fig.savefig=(filename, dpi=150, pad_inches=0.50)
del fig
import gc
gc.collect()
apscheduler
both before and after you call run_job. Also what's going on with deve-Multi.py
and the code around it?Good luck. All the best. --Daniel
I saw the same Issue. It appears only with returnfig=True. Your ideas with Del fig does not fix it. After around 180 charts the process stops. A bit wired is that without external activities (returnfig=close), the script generates more then 700 charts in one loop.
@fxhuhn Markus, I'm not sure what you described is the same.
Are you indeed seeing "MemoryError: In RendererAgg: Out of memory
" or something else?
Also, as @Mikeardy described it in the original post, it does not use returnfig=True
.
Please confirm whether you are seeing the exact same memory error message (with RendererAgg
). Thank you.
@DanielGoldfarb I'm on "something else". I don't get a message at all, it just stops
This snippet works as expected:
mpf.plot(df[['Open', 'High','Low','Close','Volume']],
type='candle',
#returnfig=True,
savefig=save,
closefig=True
)
And this one stops without forcing any exception error:
fig, _ = mpf.plot(df[['Open', 'High','Low','Close','Volume']],
type='candle',
returnfig=True,
#savefig=save,
#closefig=True
)
fig.savefig(save['fname'])
del fig
@fxhuhn
I'm on "something else". I don't get a message at all, it just stops
I will try to reproduce on my end. What OS are you running? Python version? mplfinance version? matplotlib version?
As a separate question, I am trying to understand why anyone would save hundreds of plots all at once. Please, anyone who can, explain to me what you are trying to accomplish, so that I can better understand how mplfinance is being used.
In my own approach, if I am developing and testing a trading strategy, my approach is something like this: I will make several plots, maybe 10 or 20 at most, to try to refine my ideas. When I get to a point where I want to try out hundreds or thousands of scenarios, I will loop through all the scenarios but I won't make plots of them. Instead I will calculate the profit or loss, and then sort them. Then after all that I may make another 10 or 20 or 30 plots to examine the best and the worst scenarios, particularly the worst, so that I can visualize and perhaps understand why the strategy did not work as well in those scenarios. Then I use that information to refine my strategy. Ultimately however I take my signals from a running the strategy without plotting. Only if I get a signal to I plot that particular case so that I can visually examine it and decide if I agree that its a good signal.
That said, I am at a loss to understand why someone would make hundreds of plots in one loop. Can someone please explain what you are doing with them. Much appreciated! All the best. --Daniel
Hi @DanielGoldfarb , Absolutely right! At the first moment, it was simply faster to realize. In the meantime, I filter the data beforehand, now there are only a few charts.
Eg: This table shows some stocks with a lower RSI and a hight volume, and the charts were build for them if needed. Long_status | ticker | name | Long_active | Long_TP1 | Long_Range | rsi_7 |
---|---|---|---|---|---|---|
enph_us | Enphase Energy | 1 | ||||
ebay_us | eBay | 1 | ||||
tlry_us | Tilray | 2 | ||||
sant_de | S&T | 2 | ||||
shop_us | Shopify A | 2 | ||||
plug_us | Plug Power | 2 | ||||
pvh_us | PVH | 2 | ||||
snap_us | SNAP | 3 | ||||
tsla_us | Tesla | 3 | ||||
twtr_us | 2 | |||||
tcom_us | Trip.com Group Ltd Sp ADR | 2 | ||||
luv_us | Southwest Airlines Co | 2 | ||||
lulu_us | Lululemon Athletica | 2 | ||||
mdt_us | Medtronic | 1 | ||||
oxy_us | Occidental Petroleum | 3 | ||||
alb_us | Albemarle | 2 | ||||
-1 R | pvh_us | PVH | 2021-12-07 | 2021-12-13 | 3 | |
1 R | pton_us | Peloton Interactive | 2021-12-07 | 2021-12-08 | 2021-12-09 | 3 |
2 R | sfq_de | SAF Holland | 2021-12-07 | 2021-12-08 | 2021-12-10 | 3 |
2 R | pdd_us | Pinduoduo | 2021-12-15 | 2021-12-07 | 2 |
Hi, I used the following code in a loop generation plots:
It runs every 15 minutes and iterates "n" times depending on how many source vectors are available, let say from 50 to 300, but the real number of plot generate are 5 to 10. It worked fine for hours but suddenly I got the following error:
should I release the memory closing the plot in such a way? How? thanks