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

Polygon data



  • Hello
    I am trying to get Polygon data feed, I assigned polygon data feed to data
    and when I print the data, I can see the data before "cerebro.adddata(data)"
    However, I received an error with "cerebro.adddata(data)"

    ec8ed198-c6b6-4e00-b60d-1a7d562f138a-image.png

    just could not figured out the issue, appreciate your help

    my code is below:
    from matplotlib.dates import (HOURS_PER_DAY, MIN_PER_HOUR, SEC_PER_MIN,
    MONTHS_PER_YEAR, DAYS_PER_WEEK,
    SEC_PER_HOUR, SEC_PER_DAY,
    num2date, rrulewrapper, YearLocator,
    MicrosecondLocator)

    import math
    import alpaca_backtrader_api
    import backtrader as bt
    import alpaca_trade_api as tradeapi
    #from utils.pinescript import line2arr, barssince, valuewhen, na, nz
    import backtrader.indicators as btind
    from datetime import datetime
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import pytz

    NY = 'America/New_York'

    ALPACA_API_KEY = ""
    ALPACA_SECRET_KEY = ""
    ALPACA_API_BASE_URL= "https://paper-api.alpaca.markets" ##https://api.alpaca.markets (for live)
    ALPACA_PAPER = True
    api = tradeapi.REST(ALPACA_API_KEY, ALPACA_SECRET_KEY, base_url=ALPACA_API_BASE_URL)

    symbol = "AAPL"

    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):
    self.dataclose = self.datas[0].close

    cerebro = bt.Cerebro()
    cerebro.addstrategy(TestStrategy)

    store = alpaca_backtrader_api.AlpacaStore(
    key_id=ALPACA_API_KEY,
    secret_key=ALPACA_SECRET_KEY,
    paper=ALPACA_PAPER
    )

    if not ALPACA_PAPER:
    broker = store.getbroker() # or just alpaca_backtrader_api.AlpacaBroker()
    cerebro.setbroker(broker)

    #DataFactory = store.getdata # or use alpaca_backtrader_api.AlpacaData
    #data = DataFactory(dataname=symbol, historical=True, fromdate=datetime( 2020, 7, 1), timeframe=bt.TimeFrame.Days)
    d_start=pd.Timestamp('2020-08-01', tz=NY).isoformat() ## day data
    d_end=pd.Timestamp('2020-08-30', tz=NY).isoformat()

    start=pytz.timezone(NY).localize(datetime(2020,10,1,9,30)).timestamp()*1000 # for minutes, timestamp in micro seconds
    end=pytz.timezone(NY).localize(datetime(2020,10,14,16,0)).timestamp()*1000

    #data=api.polygon.historic_agg_v2('AAPL', 1, 'minute', _from=start, to=end).df
    data = api.polygon.historic_agg_v2('AAPL', 1, 'day', _from=d_start, to=d_end).df
    print(data)
    cerebro.adddata(data)

    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
    cerebro.run()
    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
    cerebro.plot(style='candlestick')



  • All these codes are complicated.



  • Only LineSeries inherited objects (data feeds) could be added to the Cerebro engine using adddata method.

    AFAIU Alpaca API provides the backtrader store AlpacaStore, so you should use the AlpacaStore.getdata method to get the backtrader compatible data feed to be added to Cerebro.

    On a different matter:

    • Please use the backtick or grave accent to wrap you code - this way it will be easier for everybody to read you posts ( see the header on this page )

    • Please include the backtrace / error info as a text and not images.


Log in to reply
 

});