简体   繁体   中英

Alpaca trade api error: 'REST' object has no attribute 'get_barset'

I use ALPACA paper markets. I'm trying to get stock data from ALPACA markets to put into a dataframe, and running into an error.

AttributeError Traceback (most recent call last) in 11 # Get 1 year's worth of historical data for Tesla and Coca-Cola 12 # YOUR CODE HERE. ---> 13 df_ticker = alpaca,get_barset( 14 ticker, 15 timeframe: AttributeError: 'REST' object has no attribute 'get_barset'

Imports

import os
import pandas as pd
import alpaca_trade_api as tradeapi
from dotenv import load_dotenv
load_dotenv('.env')    # loading my environment variables.
  1. Import my API keys from the loaded environment variables.
alpaca_api_key = os.getenv("ALPACA_API_KEY")
alpaca_secret_key = os.getenv("ALPACA_SECRET_KEY")
  1. Create the alpaca REST object.
alpaca = tradeapi.REST(
    alpaca_api_key,
    alpaca_secret_key,
    api_version="v2"
)
  1. Define stock data variables to use to fetch historic data. I am getting the past year of closing prices for each day.
ticker = [list of stocks]
timeframe = "1D"    # 1-days worth of closing prices.
start_date = pd.Timestamp("2021-07-26", tz="America/New_York").isoformat()
end_date = pd.Timestamp("2022-07-26", tz="America/New_York").isoformat()
  1. Create a dataframe with the fetched stock data. This is where it fails.
df_ticker = alpaca.get_barset(
    ticker,                           
    timeframe,                   # 1-day closing prices.
    start = start_date,   
    end = end_date,
    limit = 1000                 # put a limit that way there's not too mucb data returned and screws up program.
).df                             # format as a dataframe

It looks like get_barset() is part of the V1 API for alpaca, you need to use the get_bars() method with V2, or else specify the API V1 when creating the REST object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM