For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Having trouble understanding how to get VWR analyzer to work

  • 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,
    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]
            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))
        # Add a strategy
        # Analyzer
        cerebro.addanalyzer(btanalyzers.VWR, _name='myvwr')
        thestrats =
        thestrat = thestrats[0]
        print('VWR: ', thestrat.analyzers.myvwr.get_analysis())
        # Plot the result

    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.

  • You have no trades, your VWR will be 0.

Log in to reply