Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. JuanM09
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    J
    • Profile
    • Following 0
    • Followers 0
    • Topics 0
    • Posts 1
    • Best 1
    • Groups 0

    JuanM09

    @JuanM09

    1
    Reputation
    1
    Profile views
    1
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    JuanM09 Unfollow Follow

    Best posts made by JuanM09

    • RE: ERROR Pyfolio and Resample

      Hello, after further investigating the subject, I asked in a telegram group called "Python for trading"

      in this group, I managed to find the answer, many thanks to @ultra1971 , he is the one who generated the code and after my question he quickly answered with a valid answer which allows to obtain the quantstats sheet without having to use pyfolio adding modifications in the timeframe in which the backtrader runs. The solution is the next.

      First a custom analyzer is created.

      class QuantStats(bts.Analyzer):
          """
          Analyzer returning market and bechmark values for report returns to quantstats
          """
          params = (('benchmark', 0),)
      
          def start(self):
              super().start()
      
          def create_analysis(self):
              self.rets = {}
              #self.vals = 0.0
      
          def notify_cashvalue(self, cash, value):
              #self.vals = (value)
              benchmark_value = self.datas[self.p.benchmark].close[0]
              self.rets[self.strategy.datetime.datetime()] = (value, benchmark_value)
      
          def get_analysis(self):
              return self.rets
      

      Then added to Cerebro.

      cerebro.addanalyzer(QuantStats, _name='qs')
      
      thestrats = cerebro.run()
      thestrat = thestrats[0]
      
       returns_analyzer = thestrat.analyzers.getbyname("qs").get_analysis()
      
       df_qs = pd.DataFrame(returns_analyzer).T
       returns = qs.utils.to_returns(df_qs.iloc[:, 0])
       qs.reports.html(returns,output='test_qs.html', )
      

      Finally, the analyzer data is obtained and the modified returns are added to the sheet.

      I hope this helps the community, I had seen many people with these questions and could not find an answer.

      Thanks to @ultra1971, who managed to solve the problem and clear up all my doubts, thank you very much!

      posted in Indicators/Strategies/Analyzers
      J
      JuanM09

    Latest posts made by JuanM09

    • RE: ERROR Pyfolio and Resample

      Hello, after further investigating the subject, I asked in a telegram group called "Python for trading"

      in this group, I managed to find the answer, many thanks to @ultra1971 , he is the one who generated the code and after my question he quickly answered with a valid answer which allows to obtain the quantstats sheet without having to use pyfolio adding modifications in the timeframe in which the backtrader runs. The solution is the next.

      First a custom analyzer is created.

      class QuantStats(bts.Analyzer):
          """
          Analyzer returning market and bechmark values for report returns to quantstats
          """
          params = (('benchmark', 0),)
      
          def start(self):
              super().start()
      
          def create_analysis(self):
              self.rets = {}
              #self.vals = 0.0
      
          def notify_cashvalue(self, cash, value):
              #self.vals = (value)
              benchmark_value = self.datas[self.p.benchmark].close[0]
              self.rets[self.strategy.datetime.datetime()] = (value, benchmark_value)
      
          def get_analysis(self):
              return self.rets
      

      Then added to Cerebro.

      cerebro.addanalyzer(QuantStats, _name='qs')
      
      thestrats = cerebro.run()
      thestrat = thestrats[0]
      
       returns_analyzer = thestrat.analyzers.getbyname("qs").get_analysis()
      
       df_qs = pd.DataFrame(returns_analyzer).T
       returns = qs.utils.to_returns(df_qs.iloc[:, 0])
       qs.reports.html(returns,output='test_qs.html', )
      

      Finally, the analyzer data is obtained and the modified returns are added to the sheet.

      I hope this helps the community, I had seen many people with these questions and could not find an answer.

      Thanks to @ultra1971, who managed to solve the problem and clear up all my doubts, thank you very much!

      posted in Indicators/Strategies/Analyzers
      J
      JuanM09