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/

    Having trouble understanding how to get VWR analyzer to work

    General Code/Help
    2
    2
    54
    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.
    • Russell Turner
      Russell Turner last edited by

      I am new to this and the examples for the VWR analyzer in the help documentation are too complicated for me to understand. I have been able to run this code:

      from __future__ import (absolute_import, division, print_function,
                          unicode_literals)
      
      from datetime import datetime
      
      import backtrader as bt
      import backtrader.analyzers as btanalyzers
      
      # Create a Strategy
      class TestStrategy(bt.Strategy):
      
          def log(self, txt, dt=None):
              ''' Logging function for this strategy'''
              dt = dt or self.datas[0].datetime.date(0)
              print('%s, %s' % (dt.isoformat(), txt))
      
          def __init__(self):
              # Keep a reference to the "close" line in the data[0] dataseries
              self.dataclose = self.datas[0].close
      
          def next(self):
              # Simply log the closing price of the series from the reference
              self.log('Close, %.2f' % self.dataclose[0])
      
      if __name__ == '__main__':
          # Create a cerebro enity
          cerebro = bt.Cerebro()
      
          data = bt.feeds.YahooFinanceData(dataname='SPY', fromdate=datetime(2019, 1, 1), todate=datetime(2019, 12, 31))
          cerebro.adddata(data)
      
          # Add a strategy
          cerebro.addstrategy(TestStrategy)
      
          # Analyzer
          cerebro.addanalyzer(btanalyzers.VWR, _name='myvwr')
      
          thestrats = cerebro.run()
          thestrat = thestrats[0]
      
          print('VWR: ', thestrat.analyzers.myvwr.get_analysis())
      
          # Plot the result
          cerebro.plot()
      

      I can change 'SPY' to any other ticker symbol... For example Apple... or Microsoft... or whatever. I can change the date range easy enough too. But I can't figure out how to get a value for VWR other than "0." At first I didnt realize I needed a strategy in order to get VWR but then someone told me I needed a strategy to get VWR. So I made a strategy as you can see above... but it still shows 0 for VWR.

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        You have no trades, your VWR will be 0.

        RunBacktest.com

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