Home > Blockchain >  Whata is the problem in my python apı code?
Whata is the problem in my python apı code?

Time:01-26

What is the problem here I writed the code but I got problems?

import pandas as pd
import matplotlib.pyplot as plt
import requests
from alpha_vantage.timeseries import TimeSeries

key: "https://api.polygon.io/v2/aggs/ticker/AAPL/range/1/day/2023-01-09/2023-01-09?adjusted=true&sort=asc&limit=120&apiKey=<my_key>".read()

ts = TimeSeries(key, output_format='pandas')
data, meta = ts.get_intraday('', interval='1min', outputsize='full')

meta
data.info()
data.head()
plt.plot(data['4. close'])

columns = ['open', 'high', 'low', 'close', 'volume']
data.columns = columns
data['TradeDate'] = data.index.date
data['time'] = data.index.time
data.loc['2020-12-31']

market = data.between_time('09:30:00', '16:00:00').copy()
market.sort_index(inplace=True)
market.info()

market.groupby('TradeDate').agg({'low':min, 'high':max})

Error:

> C:\Users\yaray\PycharmProjects\pythonProject6\venv\Scripts\python.exe
> C:/Users/yaray/PycharmProjects/pythonProject6/main.py Traceback (most
> recent call last):   File
> "C:\Users\yaray\PycharmProjects\pythonProject6\main.py", line 6, in
> <module>
>     key: "https://api.polygon.io/v2/aggs/ticker/AAPL/range/1/day/2023-01-09/2023-01-09?adjusted=true&sort=asc&limit=120&apiKey=adxXwhD9disXeBHHaifFLOX9BxbDIDHD".read()
> AttributeError: 'str' object has no attribute 'read'

CodePudding user response:

You are trying to read a string. You must first make the API call, you can than read the response of that API call into a variable on which you can call the read() method.

Ps. for readability, don't put any functionality past the page line. If you really have to assign a long string, save the string to a variable and call a method on it later.

Example:

key = "long_string"
read_key = key.read()

CodePudding user response:

Your code is a little wonky. Does this get you started? It is a little more than I can put in a comment and potentially less than I would put in an answer.

Based on https://pypi.org/project/alpha-vantage/ I think you want to get started with something more like this.

from alpha_vantage.timeseries import TimeSeries

my_api_key = "ad...HD"
ts = TimeSeries(key=my_api_key, output_format='pandas')
data, meta = ts.get_intraday('GOOGL')

print(meta)
print(data)

Let me know if that helps or not so I can remove this partial answer.

  • Related