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



  • 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 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'



  • 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.



  • 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
    


  • 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/



  • Thanks for your answer.
    Where do you get data ?



  • @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/



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



  • @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.


Log in to reply
 

});