happydasch / btplotting

btplotting provides plotting for backtests, optimization results and live data from backtrader.
GNU General Public License v3.0
338 stars 84 forks source link

Fix the pandas PerformanceWarning. #34

Closed GF-Huang closed 2 years ago

GF-Huang commented 2 years ago

..\btplotting\app.py:514: PerformanceWarning: DataFrame is highly fragmented. This is usually the result of calling frame.insert many times, which has poor performance. Consider joining all columns at once using pd.concat(axis=1) instead. To get a de-fragmented frame, use newframe = frame.copy() df[source_id] = new_line

happydasch commented 2 years ago

hey @GF-Huang , you seem to use btplotting actively. if you need any help feel free to get in touch.

GF-Huang commented 2 years ago

Hi @happydasch, can you explain how btplotting draws the graph? I'm trying to backtest stock and options together in 1 minute bars. Because options expire every week, even if every 2 days (SPY's options), and each stock has many options at different strikes. So I had to call cerebro.datas.clear() in every daily backtesting to save memory (RAM).

My code:

image

I had try to replace strategy.observers, but not work. As you see in the above image, I only take 2 days data for testing my idea: df_underlying_list[:2].

image

Then I plotting:

image

But the DataTrades still existing.

image

And seems only one Broker observer was plotted. (df_underlying_list[0] is 2021-05-03, df_underlying_list[1] is 2021-05-04)

image

Am I wrong? Or do you have any way to combine the results of multiple calls to cerebro.run() then plotting them into together by once call cerebro.plot()?

happydasch commented 2 years ago

so btplotting is using the result coming from backtrader. It will only plot the data that backtrader returns.

for your code above, i do not fully understand, how your strategy should work or what you want to achieve. But i noticed you reuse the instance of cerebro.

plotting multiple runs (or some combined result) will not be possible with current code. you could look into the optbrowser to plot multiple results within a browser or you could create multiple html files with results and browse them later.

for the observers, the info about observers is obtained here: https://github.com/happydasch/btplotting/blob/512780f8ab71395fef3667e872deca3a8a2f2e9a/btplotting/app.py#L197

happydasch commented 2 years ago

also you add a observer with the same name multiple times, you could give them unique names, then they would plot i believe.

GF-Huang commented 2 years ago

Thanks. My strategy is empty now, I just want to confirm btplotting can do this before writing strategy, I will take a look into _get_plotobjects. Thank you.