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/

    Help With YahooFinance Data

    General Code/Help
    data feed yahoofinancedat
    4
    10
    5760
    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.
    • R
      rishabmah5 last edited by

      I am trying to get some YahooFinance Data to backtest my strategy. Below is the code that I am running.

      import backtrader as bt
      import backtrader.feeds as btfeeds
      from datetime import datetime
      
      cerebro = bt.Cerebro()
      cerebro.broker.setcash(100000)
      data = btfeeds.YahooFinanceData(dataname="MSFT", fromdate=datetime(2016, 6, 25), todate=datetime(2021, 6, 25))
      cerebro.adddata(data)
      cerebro.run()
      

      This is the error that I am getting:

      Traceback (most recent call last):
        File "c:\Users\risha\PycharmProjects\PythonDataScience\BacktraderBacktesting\TestingData.py", line 10, in <module>
          cerebro.run()
        File "C:\Users\risha\anaconda3\lib\site-packages\backtrader\cerebro.py", line 1127, in run
          runstrat = self.runstrategies(iterstrat)
        File "C:\Users\risha\anaconda3\lib\site-packages\backtrader\cerebro.py", line 1210, in runstrategies
          data._start()
        File "C:\Users\risha\anaconda3\lib\site-packages\backtrader\feed.py", line 203, in _start
          self.start()
        File "C:\Users\risha\anaconda3\lib\site-packages\backtrader\feeds\yahoo.py", line 355, in start
          super(YahooFinanceData, self).start()
        File "C:\Users\risha\anaconda3\lib\site-packages\backtrader\feeds\yahoo.py", line 94, in start
          super(YahooFinanceCSVData, self).start()
        File "C:\Users\risha\anaconda3\lib\site-packages\backtrader\feed.py", line 674, in start
          self.f = io.open(self.p.dataname, 'r')
      FileNotFoundError: [Errno 2] No such file or directory: 'MSFT'
      

      Could someone please help me solve this?

      D run-out 2 Replies Last reply Reply Quote 0
      • D
        Dim Trader @rishabmah5 last edited by

        @rishabmah5 Please use yfinance to download data from yahoo finance.

        pip install yfinance
        

        And use this code

        import yfinance as yf
        import backtrader as bt
        
        data = bt.feeds.PandasData(dataname=yf.download('TSLA', '2018-01-01', '2019-01-01'))
        
        cerebro = bt.Cerebro()
        cerebro.adddata(data)
        
        R M 2 Replies Last reply Reply Quote 2
        • R
          rishabmah5 @Dim Trader last edited by

          @dim-trader
          Thank you soo much

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

            @rishabmah5 This is a known error that was just fixed on Backtrader2 today. See here for the fix. You can install backtrader two, or just add the one line in to your backtrader library in feeds/yahoo.py

            RunBacktest.com

            1 Reply Last reply Reply Quote 0
            • M
              Madhoon @Dim Trader last edited by

              @dim-trader

              from datetime import datetime
              import backtrader as bt
              from backtrader import plot
              import yfinance as yf
              # Create a subclass of Strategy to define the indicators and logic
              
              class SmaCross(bt.Strategy):
                  # list of parameters which are configurable for the strategy
                  params = dict(
                      pfast=50,  # period for the fast moving average
                      pslow=200   # period for the slow moving average
                  )
              
                  def __init__(self):
                      sma1 = bt.ind.SMA(period=self.p.pfast)  # fast moving average
                      sma2 = bt.ind.SMA(period=self.p.pslow)  # slow moving average
                      self.crossover = bt.ind.CrossOver(sma1, sma2)  # crossover signal
              
                  def next(self):
                      if not self.position:  # not in the market
                          if self.crossover > 0:  # if fast crosses slow to the upside
                              self.buy()  # enter long
              
                      elif self.crossover < 0:  # in the market & cross to the downside
                          self.close()  # close long position
              
              
              cerebro = bt.Cerebro()  # create a "Cerebro" engine instance
              cerebro.broker.setcash(100000)
              # Create a data feed
              data = bt.feeds.PandasData(dataname=yf.download('KPITTECH.NS', '2019-01-01', '2021-07-06'))
              
              cerebro.adddata(data)  # Add the data feed
              
              cerebro.addstrategy(SmaCross)  # Add the trading strategy
              cerebro.run()  # run it all
              cerebro.plot()  # and plot it with a single command
              
              
              
              
              this is my code⬆️ and this is the error⬇️
              
              
              Traceback (most recent call last):
                File "/Users/madhurjodhwani/Desktop/python/hey.py", line 37, in <module>
                  cerebro.plot()  # and plot it with a single command
                File "/opt/homebrew/lib/python3.9/site-packages/backtrader/cerebro.py", line 974, in plot
                  from . import plot
                File "/opt/homebrew/lib/python3.9/site-packages/backtrader/plot/__init__.py", line 42, in <module>
                  from .plot import Plot, Plot_OldSync
                File "/opt/homebrew/lib/python3.9/site-packages/backtrader/plot/plot.py", line 44, in <module>
                  from . import locator as loc
                File "/opt/homebrew/lib/python3.9/site-packages/backtrader/plot/locator.py", line 35, in <module>
                  from matplotlib.dates import (HOURS_PER_DAY, MIN_PER_HOUR, SEC_PER_MIN,
              ImportError: cannot import name 'warnings' from 'matplotlib.dates' (/opt/homebrew/lib/python3.9/site-packages/matplotlib/dates.py)
              

              I am trying to run cerebro.plot() and it is giving this error, also I am new to finance and wnant to learn this bactrader,is there any course or any videos to help me with this?

              run-out R 2 Replies Last reply Reply Quote 0
              • run-out
                run-out @Madhoon last edited by

                @madhoon Google: backtrader https://stackoverflow.com/questions/63471764/importerror-cannot-import-name-warnings-from-matplotlib-dates

                First response: Stack Overflow

                RunBacktest.com

                run-out M 2 Replies Last reply Reply Quote 0
                • run-out
                  run-out @run-out last edited by

                  @run-out Correction:

                  Google: ImportError: cannot import name 'warnings' from 'matplotlib.dates'
                  

                  RunBacktest.com

                  1 Reply Last reply Reply Quote 0
                  • R
                    rishabmah5 @Madhoon last edited by

                    @madhoon said in Help With YahooFinance Data:

                    I actually had the same issue myself. For some reason, the cerebro.plot() only works with a specific version of matplotlib. Go to your terminal and run

                    pip install matplotlib==3.2.2.

                    That will allow you to plot the graphs.

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

                      @run-out Got it,thank you, hey, I am looking for a detailed course on studying the backtrader, any help?

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

                        @madhoon I haven't heard of any courses. Docs, community and our old pal google. Good luck!

                        RunBacktest.com

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