Navigation

    Backtrader Community

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

    Posts made by Russell Turner

    • Need help getting started, trying to graph draw down

      I have tried to write a couple programs but haven't really been able to fully grasp how all the pieces fit together. I have tried reading a lot of the documentation and examples online etc... But I quickly get lost... It seems they start with super simple examples but quickly get complicated. Also, I see lots of little differences with how they go about programming something that is basically the same thing. I understand there are lots of ways to skin a cat... But this causes me to just get confused. As far as I am concerned, I don't really care at this point what the "best" way is. Even the "worst" way is better than "no" way at this point which is where I am currently at.

      I am trying to keep things simple to start, as I learn and progress, I can "improve" and learn why I should do things one way vs the other.

      Ok... So I simply want to make a graph of the "drawdown" for Tesla. At this point, I would like to simply hard code "Tesla" and if i want to see drawdown for apple... Then I can backspace a couple times and type in Apple.

      As I understand, I may need to place an order to actually produce a graph of drawdown. If this is the case then I would like to simply say I buy 1 share on X date. For example, I buy 1 share on July 2nd, 2019. Then If I want to change the date... I can simply backspace a few times and type in a new date.

      One problem I keep running into is that I find only a piece of the code I need. For example just the piece to show drawdown but I can't never figure out how to add it to my code.

      Here is what I have been able to successfully accomplish. Which I am really proud of although it took me a ridiculous number of hours to get this far. Many of the examples use data from a file, I like that this can pull some of the latest data from online by simply changing the dates.

      from __future__ import (absolute_import, division, print_function, unicode_literals)
      
      from datetime import datetime
      
      import backtrader as bt
      
      # Create a cerebro enity (cerebro engine)
      cerebro = bt.Cerebro()
      
      # Get TSLA data
      # ** Easy to change Tesla to another stock like Apple
      # **** Just backspace and enter new ticker symbol and hit run
      # ** Easy to change Date
      # **** Just backspace and enter new date and hit run
      data = bt.feeds.YahooFinanceData(dataname='TSLA', fromdate=datetime(2019, 1, 1), todate=datetime(2019, 12, 31))
      
      # Add TSLA data to cerebro engine (inject a datafeed)
      cerebro.adddata(data)
      
      # Run over everything
      cerebro.run()
      
      # Plot TSLA data
      cerebro.plot()
      

      Can someone please help me to add drawdown to this? I know if someone provided the solution, I could study it and begin to understand how all this works. Again, keep it simple for me, just the basic minimum I need to add a plot of drawdown to the code above. Thank you.

      posted in General Code/Help
      Russell Turner
      Russell Turner
    • 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,
                          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.

      posted in General Code/Help
      Russell Turner
      Russell Turner
    • How to use VWR

      I am new to backtrader and a novice at python. I was trying to figure out how to use the "VWR" analyzer.

      from __future__ import (absolute_import, division, print_function,
                          unicode_literals)
      
      from datetime import datetime
      import backtrader as bt
      import backtrader.analyzers as btanalyzers
      
      cerebro = bt.Cerebro()
      
      data = bt.feeds.YahooFinanceData(dataname='SPY', fromdate=datetime(2019, 1, 1),
                                    todate=datetime(2019, 12, 31))
      cerebro.adddata(data)
      
      # Analyzer
      cerebro.addanalyzer(btanalyzers.VWR, _name='myvwr')
      
      thestrats = cerebro.run()
      thestrat = thestrats[0]
      
      print('VWR: ', thestrat.analyzers.myvwr.get_analysis())
      

      I get this as a result:

      VWR: OrderedDict([('vwr', 0.0)])

      I think the result shouldn't be 0.0 so i think i did something wrong. Or maybe I need to show more decimal places? But i couldn't figure out how to do that?

      posted in General Discussion
      Russell Turner
      Russell Turner
    • 1 / 1