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/

    Analyzer (pyfolio) for intraday trades

    Indicators/Strategies/Analyzers
    1
    3
    86
    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.
    • Pierre Cilliers 0
      Pierre Cilliers 0 last edited by

      Hi all,

      BACKGROUND:

      I have a strategy that ingests tick data (on a microsecond interval). I resample these data to daily bars to calculate my technical indicators (e.g. moving average). See the code below which indicates my two data sources (self.data/self.data0 represents tick level data and self.data1 represents daily bar data):

          datapath = os.path.join(os.getcwd(), 'input_data/EURUSD_tick_data.csv')    
          datac = bt.feeds.GenericCSVData(dataname = datapath, 
                                          fromdate = datetime.datetime(2021, 1, 3), 
                                          todate = datetime.datetime(2021, 1, 8),
                                          dtformat=('%Y%m%d'),
                                          tmformat=('%H:%M:%S:%f'),
                                          timeframe=bt.TimeFrame.MicroSeconds, 
                                          compression=1,
                                          date=0,
                                          time=1,
                                          open=2,
                                          high=3,
                                          low=2,
                                          close=3,
                                          volume=4,
                                          openinterest=-1
                                          )
         
      
          cerebro = bt.Cerebro(exactbars = False)
      
          cerebro.replaydata(datac, timeframe=bt.TimeFrame.MicroSeconds, compression=1, name='data')
          datac.plotinfo.plotmaster = datac
          cerebro.replaydata(datac, timeframe=bt.TimeFrame.Days, compression=1, name='data1')
      

      The logic of my strategy is not that important for this inquiry, however, note that self.data is used to make trades (therefore def next() is triggered each tick and trades are executed at a tick level). Even though the technical indicators are calculated on daily bar data, the trades are executed every tick.

      MY QUESTION:

      Although my strategy runs end-to-end and I can get a plot after my execution, I want to calculate additional performance measures.

      I have tried the following to get pyfolio tear sheets, however, it seems like it struggles with intraday trades. Is there a analyzer (or pyfolio if im using it wrong) to output performance measures for intraday trades.

          results = cerebro.run()
          strat = results[0]
          pyfoliozer = strat.analyzers.getbyname('pyfolio')
          returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items()
      
          import pyfolio as pf
      
          pf.create_simple_tear_sheet(returns)
          pf.create_returns_tear_sheet(returns)
      
      Pierre Cilliers 0 2 Replies Last reply Reply Quote 0
      • Pierre Cilliers 0
        Pierre Cilliers 0 @Pierre Cilliers 0 last edited by

        I am investigating quantstats a bit. Seems to fulfill the needs for intra-day trades (on a tick level).

        1 Reply Last reply Reply Quote 0
        • Pierre Cilliers 0
          Pierre Cilliers 0 @Pierre Cilliers 0 last edited by

          Some code I am experimenting with

          results = cerebro.run()
          strat = results[0]
          pyfoliozer = strat.analyzers.getbyname('pyfolio')
          returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items()
          returns.index = returns.index.tz_convert(None)
          
          # Plot the result
          cerebro.plot(iplot=False)
          
          import pandas as pd
          import quantstats
          
          quantstats.reports.html(returns, output='stats.html', title='Strategy naive')
          
          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors