Search code examples
pythonpycharm

Graphic does not appear


I'm using PyCharm to run this code and the output of it must be a graphic. However, the graphic does not appear.

import vectorbt as vbt
from datetime import datetime, timedelta
import pytz



start_date = datetime(2021, 11, 30, tzinfo=pytz.utc)  # time period for analysis, must be timezone-aware
end_date = datetime(2022, 8, 19, tzinfo=pytz.utc)
price = vbt.YFData.download('BBAS3.SA',start= start_date, end=end_date).get('Close')


fast_ma = vbt.MA.run(price, 7)
slow_ma = vbt.MA.run(price, 21)
entries = fast_ma.ma_crossed_above(slow_ma)
exits = fast_ma.ma_crossed_below(slow_ma)

pf1 = vbt.Portfolio.from_signals(price, entries, exits, init_cash=1000,fees=0)
pf1.orders.records_readable

fig = price.vbt.plot(trace_kwargs=dict(name='Close'))
fast_ma.ma.vbt.plot(trace_kwargs=dict(name='Fast MA'), fig=fig)
slow_ma.ma.vbt.plot(trace_kwargs=dict(name='Slow MA'), fig=fig)
pf1.positions.plot(close_trace_kwargs=dict(visible=True), fig=fig)

Solution

  • You're missing a fig.show() at the end of your script. Note that this will open the graph in your browser, not PyCharm itself.

    If you want to display the image in PyCharm you'll need to implement this answer.