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

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.
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")
Create the alpaca REST object.
alpaca = tradeapi.REST(
alpaca_api_key,
alpaca_secret_key,
api_version="v2"
)
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()
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.

Related

Plotting a graph of the top 15 highest values

I am working on a dataset which shows the budget spent on movies. I want make a plot which contains the top 15 highest budget movies.
#sort the 'budget' column in decending order and store it in the new dataframe.
info = pd.DataFrame(dp['budget'].sort_values(ascending = False))
info['original_title'] = dp['original_title']
data = list(map(str,(info['original_title'])))
#extract the top 10 budget movies data from the list and dataframe.
x = list(data[:10])
y = list(info['budget'][:10])
This was the ouput i got
C:\Users\Phillip\AppData\Local\Temp\ipykernel_7692\1681814737.py:2: FutureWarning: The behavior of `series[i:j]` with an integer-dtype index is deprecated. In a future version, this will be treated as *label-based* indexing, consistent with e.g. `series[i]` lookups. To retain the old behavior, use `series.iloc[i:j]`. To get the future behavior, use `series.loc[i:j]`.
y = list(info['budget'][:5])
I'm new to the data analysis scene so i'm confused on how else to go about the problem
A simple example using a movie dataset I found online:
import pandas as pd
url = "https://raw.githubusercontent.com/erajabi/Python_examples/master/movie_sample_dataset.csv"
df = pd.read_csv(url)
# Bar plot of 15 highest budgets:
df.nlargest(n=15, columns="budget").plot.bar(x="movie_title", y="budget")
You can customize your plot in various ways by adding arguments to the .bar(...) call.

Is there any better way for me to find the (Mean & Standard Deviation) for Spreads Price

I have this set of data (e.g):
data =
How can i get the ( standard deviation) for spreads (1x2,1x3, 1x4,.....etc) in matrix form by using pandas please ?
I manage to get the results by doing the following:
# numpy is import as py ; #panda is import as pd
DM=data.apply(py.mean).values
SprdMean = DM \* 100 - DM \* 100)\[:, py.newaxis\]
SprdMean = pd.DataFrame(SprdMean, columns='1y','2y'...etc, index=1y','2y'...etc)
SprdMean.T.round(decimals=0)
However for Standard Deviation, the above 'MEAN' method can't be applied,
What can i do to obtain / modify to obtain the below results in matrix format please,
#Beginner here, thanks

Alpha Vantage API: How do I get stock market indexes?

I'm trying to get data for these indexes SPX,COMP,DJIA,DJT,RUT,DJU using this Python library
I'm doing this
data, meta_data = ts.get_intraday(symbol='DJIA',interval='60min', outputsize='full')
but I get
ValueError: Error getting data from the api, no return was given.
If I change DJIA to AAPL I get a response.How do I get data for the indexes mentioned above? Is there another API that I can use?
I tried Yahoo Finance API but could only retrieve data for DJIA,DJT, DJU
You need to provide the API key each time you access the Alpha Vantage library.
from alpha_vantage.timeseries import TimeSeries
import pandas as pd
import time
import datetime as dt
ts = TimeSeries (key=keys, output_format = "pandas")
### STOCK TIME SERIES > DAILY ADJUSTED ###
# Date / Open / High / Low / Close / Adjusted Close / Volume / Dividend / Split
data_daily, meta_data = ts.get_daily_adjusted(symbol=stock_ticker, outputsize ='compact')
# data_daily['column name'][row number]
data_daily_lastOpenPrice = data_daily['1. open'][0]
data_daily_lastHighPrice = data_daily['2. high'][0]
data_daily_lastLowPrice = data_daily['3. low'][0]
data_daily_lastAdjustedClosingPrice = data_daily['5. adjusted close'][0]
data_daily_lastTradingVolume = data_daily['6. volume'][0]
data_daily_lastDividendAmount = data_daily['7. dividend amount'][0]
If you put this inside a for loop you can cycle through a list of your stocks and print out the results for the metrics you are looking for.

Is there a better way of finding summary statistics in Python?

The following is my code for finding the 5 point summary statistics. I keep getting this error:
list indices must be integers or slices, not str
It seems like the way i'm using the describe function that i created is wrong.
from statistics import stdev,median,mean
def describe(key):
a=[]
for i in scripts:
a.append(i[key])
a=scripts[key]
total = sum(script[key] for script in scripts)
avg = total/len(a)
avg=mean(a)
s = stdev(a)
q25 = min(a)+(max(a)-min(a))*25
med = min(a)+(max(a)-min(a))*50
med=median(a)
q75 = min(a)+(max(a)-min(a))*75
return (total, avg, s, q25, med, q75)`enter code here`
summary = [('items', describe('items')),
('quantity', describe('quantity')),
('nic', describe('nic')),
('act_cost', describe('act_cost'))]
I keep getting this error:
TypeError Traceback (most recent call last)
<ipython-input-8-ba78d5218ead> in <module>()
----> 1 summary = [('items', describe('items')),
2 ('quantity', describe('quantity')),
3 ('nic', describe('nic')),
4 ('act_cost', describe('act_cost'))]
<ipython-input-1-bcf37f98eb7d> in describe(key)
4 for i in scripts:
5 a.append(i[key])
----> 6 a=scripts[key]
7 total = sum(script[key] for script in scripts)
8 avg = total/len(a)
TypeError: list indices must be integers or slices, not str
It is hard to understand your problem, since we don't know how scripts looks like. It is a global variable which is not defined in your script. The error states that scripts is of type list, but it looks like you assume it is a dataframe in your code. So please check the type of scripts.
Also, did you know that there is an easy way to calculate a Five-number summary with numpy like this:
import numpy as np
minimum, q25, med, q75, maximum = np.percentile(a, [0, 25, 50, 75, 100], interpolation='midpoint')
For description, see:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.percentile.html
As per your question, you are accessing list of dictionaries.
Directly accessing with its key value is not yielding the result here.
So you must do,
getValues = lambda key,inputData: [subVal[key] for subVal in inputData if key in subVal]
in this case,
getValues('key', scripts) will give the corresponding list, then its easy to compute the statistics of that list.

Pandas DataReader work-around for Google finance

I pull historical data for a large universe of stocks and ETFs daily. Quandl has pretty good free coverage of US Equities, but they do not have historical data for ETFs so I use the Google API as a backup for Quandl.
The recent Google finance "renovation" hasn't left me with a great alternative, so I am trying to apply Brad Solomon's work (thanks Brad, link below) to a list of symbols. Assume it is unlikely without a loop given that he is creating URLs. Any clever ideas welcome.
Related question: How come pandas_datareader for google doesn't work?
Thanks.
Under the hood, pandas-datareader is looping through each symbol that you pass and making http requests one by one.
Here's the function that does that in the base class, from which the google- and yahoo-related classes inherit: base._DailyBaseReader._dl_mult_symbols.
The magic is that these are appended to a list and then aggregated into a pandas Panel.
I would note, however, that Panel is deprecated and you can get the same functionality in a DataFrame with a MultiIndex, a structure that's technically 2-dimenionsal but replicates higher dimensionalities in practice.
So, here's the barebones of what you could do, below. Please note I'm skipping a lot of the functionality embedded within the package itself, such as parsing string dates to datetime.
import datetime
from io import StringIO
import requests
from pandas.io.common import urlencode
import pandas as pd
BASE = 'http://finance.google.com/finance/historical'
def get_params(sym, start, end):
params = {
'q': sym,
'startdate': start.strftime('%Y/%m/%d'),
'enddate': end.strftime('%Y/%m/%d'),
'output': "csv"
}
return params
def build_url(sym, start, end):
params = get_params(sym, start, end)
return BASE + '?' + urlencode(params)
def get_one_data(sym, start=None, end=None):
if not start:
start = datetime.datetime(2010, 1, 1)
if not end:
end = datetime.datetime.today()
url = build_url(sym, start, end)
data = requests.get(url).text
return pd.read_csv(StringIO(data), index_col='Date',
parse_dates=True).sort_index()
def get_multiple(sym, start=None, end=None, return_type='Panel'):
if isinstance(sym, str):
return get_one_data(sym, start=start, end=end)
elif isinstance(sym, (list, tuple, set)):
res = {}
for s in sym:
res[s] = get_one_data(s, start, end)
# The actual module also implements a 'passed' and 'failed'
# check here and also using chunking to get around
# data retreival limits (I believe)
if return_type.lower() == 'panel':
return pd.Panel(res).swapaxes('items', 'minor')
elif return_type.lower() == 'mi': # MultiIndex DataFrame
return pd.concat((res), axis=1)
An example:
syms = ['AAPL', 'GE']
data = get_multiple(syms, return_type='mi')
# Here's how you would filter down to Close prices
# on MultiIndex columns
data.xs('Close', axis=1, level=1)
AAPL GE
Date
2010-01-04 30.57 15.45
2010-01-05 30.63 15.53
2010-01-06 30.14 15.45
2010-01-07 30.08 16.25
2010-01-08 30.28 16.60
...