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/

    Preventing subplots

    General Code/Help
    2
    6
    1229
    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.
    • E
      Emin Ozkan last edited by Emin Ozkan

      Hello,
      I am interested in having separate plots for equity line and drawdown lines. How does one go about doing this?
      I have set cerebro = bt.Cerebro(stdstats=False) and then added an observer for drawdown like this:
      cerebro.addobserver(bt.observers.DrawDown,plot=True, subplot=False)
      but cerebro.plot() only plots datafeed and the indicators.(i.e. doesn't plot newly added drawdown observer).

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

        @emin-ozkan said in Preventing subplots:

        I am interested in having separate plots for equity line and drawdown lines

        Don't really know what you mean. The "equity line" (known as "value" in backtrader) is not plotted in the same subplot as the `DrawDown´ observer (for starters, the latter is not part of the standard observers included in a non-customized plot)

        They do for sure plot separately.

        @emin-ozkan said in Preventing subplots:

        cerebro.addobserver(bt.observers.DrawDown,plot=True, subplot=False)
        

        This seems like a strange move. Why would you plot the DrawDown observer along with the data feed? The values are completely unrelated and the values of the observer are possible the chart, but cannot be seen due to the huge difference with the data.

        Because you don't show a chart (strange since you talk about charting), the bet is that the label for the DrawDown is there in the subplot of the data.

        1 Reply Last reply Reply Quote 0
        • E
          Emin Ozkan last edited by Emin Ozkan

          I am sorry that I wasn't clear. I have attached an image to see what I get currently:
          0_1546348809827_Figure_0.jpeg
          What I want to get is
          0_1546348964707_Figure_1.jpg.
          I think this is called Broker observer? But the relevant part is I want an observer such as Value or Broker in its own independent plot without including datafeed and indicators etc. (rather than a subplot). Is this possible? Thanks,

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

            A subplot is a different plot within a figure (a chart) and not an independent figure (chart).

            You are looking for a tear sheet, which implements independent figures (charts).

            No, that's not implemented. The implemented charting facility is a meant as visual aid and not as a reporting tool.

            You may try disabling the plotting of the data feed with plotinfo=False and see what happens (unknown if someone has tried that and succeeded)

            E 1 Reply Last reply Reply Quote 0
            • E
              Emin Ozkan @backtrader last edited by Emin Ozkan

              @backtrader Thanks actually I got what I need by adding plot=False to datafeed and the indicators. 0_1546349852787_Figure_100.jpeg

              E 1 Reply Last reply Reply Quote 0
              • E
                Emin Ozkan @Emin Ozkan last edited by Emin Ozkan

                Here is another way to plot an independent equity curve based on bokeh. I apologize for shitty code. This way, I don't have to turn off cerebro's built in
                pretty plotting such as buysell observers.

                0_1546364337821_Capture.JPG

                cerebro.run()
                # This assumes that you have added a strategy
                # the value observer 
                plot_observer(cerebro.runstrats[0][0])
                
                from mpl_toolkits.mplot3d import axes3d
                import matplotlib.pyplot as plt
                from matplotlib import cm
                import numpy as np
                from matplotlib.ticker import LinearLocator, FormatStrFormatter
                from matplotlib.dates import num2date
                import seaborn as sns
                from bokeh.io import show
                from bokeh.layouts import column
                from bokeh.models import ColumnDataSource, RangeTool
                from bokeh.plotting import figure, output_file
                
                def plot_observer(strategy,alias='value'):
                    for observer in strategy.getobservers():
                        linealias = observer.lines._getlinealias(0)
                        if linealias ==alias:
                            dates = np.array(strategy.datetime.plot())
                            ydata = np.array(observer.line.plot())
                            dates = num2date(dates)
                            plot_bokeh(dates=dates,values=ydata)
                
                    return
                
                def plot_bokeh(dates,values):
                
                    output_file("equity.html", title="equity line")
                
                    TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
                
                    source = ColumnDataSource(data=dict(date=dates, value=values))
                
                    p = figure(plot_height=300, plot_width=600, tools=TOOLS,
                               x_axis_type="datetime", x_axis_location="above",
                               background_fill_color="#efefef", x_range=(dates[0], dates[-1]))
                
                    p.line('date', 'value', source=source)
                    p.yaxis.axis_label = 'value'
                
                    select = figure(title="Drag the middle and edges of the selection box to change the range above",
                                    plot_height=130, plot_width=600, y_range=p.y_range,
                                    x_axis_type="datetime", y_axis_type=None,
                                    tools="", toolbar_location=None, background_fill_color="#efefef")
                
                    range_rool = RangeTool(x_range=p.x_range)
                    range_rool.overlay.fill_color = "navy"
                    range_rool.overlay.fill_alpha = 0.2
                
                    select.line('date', 'value', source=source)
                    select.ygrid.grid_line_color = None
                    select.add_tools(range_rool)
                    select.toolbar.active_multi = range_rool
                
                    show(column(p, select))
                
                
                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors