Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    optstrategy() and plotting

    General Code/Help
    2
    2
    3925
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      holicst last edited by

      Hi Everybody, dear @backtrader,

      When I try to use optstrategy() with a Pandas data source, I cannot plot my results to png files. As I understand, this should be possible, according to the documentation:

      Cerebro.plot()
      savefig: set to True to save to a file rather than plot
      figfilename: name of the file. Use {j} in the name for the strategy index to which the figure corresponds and use `{i}`` to insert figure number if multiple figures are being used per strategy plot

      I'd like to plot my results after each run has finished.

      This is my code:

                 if g_save_plot==True:
                     plot_file_path = log_file_path.replace('.txt','_{j}_{i}.png')
                     cerebro.plot(savefig=True, figfilename=plot_file_path) #numfigs=len(g_instruments), 
                     cerebro.plot()
      

      However, when I call this, I get an error:

      Traceback (most recent call last):
       File "C:\Baixinho\train_keras_model.py", line 962, in <module>
         cancelled = bt_backtest.backtest(g_data_source, g_backtest_mode, g_resolution, loaded_training_df, training_fold_ranges_df, model, g_db, test_df, g_log_file_path)                
       File "C:\Baixinho\simulate_backtrader.py", line 995, in backtest
         cerebro.plot(savefig=True, figfilename=plot_file_path) #numfigs=len(g_instruments), 
       File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 927, in plot
         start=start, end=end)
       File "C:\Users\peter\Anaconda3\lib\site-packages\backtrader\plot\plot.py", line 119, in plot
         if not strategy.datas:
      AttributeError: 'OptReturn' object has no attribute 'datas'
      

      And this is line 119 and 120 in plot.py (I've just upgraded backtrader to the latest version):

             if not strategy.datas:
                 return
      

      I don’t really get why “strategy” is an OptReturn object, but I’m not a Python expert.

      Do I misunderstand the usage of cerebro.plot() when optimizing strategies?

      Alternatively, it would be great to be able to save the plots separately, when a strategy finished (method stop()). Is that possible?

      Cheers,
      Tamás

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        OptReturn is a simplified return instance which mimics a strategy, to reduce the overhead of message passing from worker processes to the main process.

        See: Docs - Optimization Improvements

        Plotting has never been tested with optimization (not an expected use case)

        1 Reply Last reply Reply Quote -2
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors