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/

    Alpha Vantage

    General Code/Help
    4
    9
    729
    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.
    • Aill3urs 2019
      Aill3urs 2019 last edited by

      Hello
      In this code i try to import data from alpha vantage but look like doesn't work

      Thanks for your help

      from alpha_vantage.timeseries import TimeSeries
      import backtrader as bt

      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=10, # period for the fast moving average
      pslow=30 # 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

      Create a data feed

      #data = bt.feeds.YahooFinanceData(dataname='MSFT', fromdate=datetime(2011, 1, 1), todate=datetime(2012, 12, 31))
      ts = TimeSeries(key='MYKEY', output_format='pandas')
      data, meta_data = ts.get_intraday(symbol='MSF',interval='1min', outputsize='full')

      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

      Aill3urs 2019 1 Reply Last reply Reply Quote 0
      • Aill3urs 2019
        Aill3urs 2019 @Aill3urs 2019 last edited by

        @Aill3urs-2019 My errors

        File "/xxxxxx/Trading001/Entrainement_Data_Ok.py", line 64, in <module>
        cerebro.adddata(data) # Add the data feed

        File "/xxxxxx/site-packages/backtrader/cerebro.py", line 758, in adddata
        data.setenvironment(self)

        File "/xxxxx/site-packages/pandas/core/generic.py", line 5274, in getattr
        return object.getattribute(self, name)

        AttributeError: 'DataFrame' object has no attribute 'setenvironment'

        1 Reply Last reply Reply Quote 0
        • A
          ab_trader last edited by

          Please read the statement about using backticks on top of the page and repost your code. Hard to understand your code right now. Seems you are not providing data in the format of the backtrader.

          • If my answer helped, hit reputation up arrow at lower right corner of the post.
          • Python Debugging With Pdb
          • New to python and bt - check this out
          1 Reply Last reply Reply Quote 0
          • Aill3urs 2019
            Aill3urs 2019 last edited by

            oops sorry, here is the code

            from alpha_vantage.timeseries import TimeSeries
            import backtrader as bt
            
            # 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=10,  # period for the fast moving average
                    pslow=30   # 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() 
            
            ts = TimeSeries(key='MYKEY', output_format='pandas')
            data = ts.get_intraday(symbol='BTCUSD',interval='1min', outputsize='full')
            print(data)
            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
            
            1 Reply Last reply Reply Quote 0
            • vladisld
              vladisld last edited by

              Not familiar with alpha_vantage API, but it seems that ts.get_intraday method just returns the Panda's DataFrame which can't be directly consumed by cerebro.adddata.

              You probably need to wrap it using bt.PandasData. Please take a look at the following blog post for more info: https://www.backtrader.com/blog/posts/2015-08-21-pandas-datafeed/pandas-datafeed/

              1 Reply Last reply Reply Quote 1
              • Aill3urs 2019
                Aill3urs 2019 last edited by

                Thanks for your answer.
                Where do you get data ?

                T 1 Reply Last reply Reply Quote 0
                • T
                  ThatBlokeDave @Aill3urs 2019 last edited by

                  @Aill3urs-2019

                  You might find this post useful. A template for working with Alpha Vantage

                  https://backtest-rookies.com/2019/01/04/backtrader-alpha-vantage-data-direct-ingest/

                  1 Reply Last reply Reply Quote 1
                  • Aill3urs 2019
                    Aill3urs 2019 last edited by

                    Thanks i try it but no result at all :-)
                    Ouput nothing :-)

                    T 1 Reply Last reply Reply Quote 0
                    • T
                      ThatBlokeDave @Aill3urs 2019 last edited by

                      @Aill3urs-2019

                      Did you follow the article? I.e. add your api keys etc? Did you change anything e.g. tickers?

                      Whatever, it did output you could post here along with details of anything you changed.

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