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'
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()
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
Thank you in advance for any help. I have been trying to match the EMA/MACD on Binance for several months off and on. I have tried several methods but the data never lines up with what is showing on the mobile app for the exchange. I read they use TEMA rather then EMA and that the implementation of TA-Lib's MACD matches the exchange. However I have been unable to figure out how to get it to read the Dataframe without giving an error. I have only been programming in Python for a few months but this has stumped me the entire time. If someone could please show me how to fix this. The technic version works with the Dataframe but it does not match the exchange. I have tried a bunch of things and errors range from no index to unable to convert.
Regards, Jeff
import talib
import btalib
import numpy
import pandas as pd
import requests
import time
import json
import technic as ta
from datetime import datetime
from os import system, name
api_key = ''
api_secret = ''
from binance.client import Client
client = Client(api_key, api_secret)
sym = "TKOUSDT"
while 1 == 1:
data = client.get_historical_klines(sym, Client.KLINE_INTERVAL_5MINUTE, "135 mins ago UTC")
data2 = pd.DataFrame(data, columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume', 'close_time', 'quote_av', 'trades', 'tb_base_av', 'tb_quote_av', 'ignore' ])
clse = data2['close']
# print(clse)
clse2 = clse.to_numpy()
# print(clse2)
clse3 = pd.to_numeric(clse2, downcast='float')
# print(clse3)
# This one works... Sorta... The values do not match the Binance exchange! Was told Binance uses TEMA and that the TA-Lib MACD matches the exchange.
macd1 = ta.tmacd(data2['close'], w_slow=26, w_fast=12, w_signal=9)
print(macd1)
# Will not read the Dataframe
macd, signal, hist = talib.MACD(data2['close'], fastperiod=12, slowperiod=26, signalperiod=9)
# print(macd,signal,hist)
# Will not read the Dataframe
macd2 = btalib.macd(data2['close'], pfast=20, pslow=50, psignal=13)
# print(macd2)
time.sleep(10)
Will try and simplify to answer 1) "how to get it to read the Dataframe without giving an error", and 2) why you may see different calculations.
For #1, btalib was not accepting the RangeIndex for some reason (although in the documentation it states it should). When working with this type of data it's typically useful to set a DatetimeIndex or TimedeltaIndex anyways, so adding that and validating the OHLCV data types corrected the errors.
As a side note, btalib will accept an entire dataframe as long as it contains columns like Open/open, etc.
import pandas as pd
import talib
import btalib
import technic
from binance.client import Client # pip install python-binance
api_key = ''
api_secret = ''
client = Client(api_key, api_secret)
sym = "TKOUSDT"
client_columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume',
'close_time', 'quote_av', 'trades', 'tb_base_av', 'tb_quote_av', 'ignore']
data = client.get_historical_klines(sym, Client.KLINE_INTERVAL_1MINUTE, "135 mins ago UTC")
data2 = pd.DataFrame(data, columns=client_columns)
# set time index
data2['timestamp'] = pd.to_timedelta(data2['timestamp'])
data2.set_index('timestamp', inplace=True)
# validate data types for ohlcv columns
ohlcv_columns = ['open', 'high', 'low', 'close', 'volume']
data2[ohlcv_columns] = data2[ohlcv_columns].astype('float')
# calculate
technic_macd = technic.tmacd(data2['close'], w_slow=26, w_fast=12, w_signal=9)
talib_macd = pd.concat(talib.MACD(data2['close'], fastperiod=12, slowperiod=26, signalperiod=9), axis=1)
btalib_macd = btalib.macd(data2, pfast=12, pslow=26, psignal=9).df
For #2, the different calculations are based on methodology, where btalib tries to rectify issues with TA-Lib (see here, where MACD is one of the examples). The differences vs. Binance will depend on what methodology/library Binance is using.
I am trying to download historical intraday data of USD/EURO for the last 6 months from alpha vantage
Here is the code I am trying to execute
import pandas as pd
from alpha_vantage.timeseries import
api = "######"
ts = TimeSeries(key=####,output_format = "pandas")
data,metadata = ts.get_intraday(symbol = "USD/CAD",interval= "1min" , outputsize = "full")
print(data)
It is giving an error
ValueError: Invalid API call. Please retry or visit the documentation (https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=USD/documentation/) for TIME_SERIES_INTRADAY.
What can be the reason for this ?
As per the documentation for TIME_SERIES_INTRADAY and your error message it is somewhat apparent to me that your API is invalid. If you see your command the API is actually missing and as per the documentation it is required.
Try adding your API token/key in the last line (below) and at least the above problem should be solved.
import pandas as pd
from alpha_vantage.timeseries import
api = "######"
ts = TimeSeries(key=####,output_format = "pandas")
data,metadata = ts.get_intraday(function=TIME_SERIES_INTRADAY, symbol = "USD/CAD",interval= "1min" , outputsize = "full", apikey="Please fill your api key here")
Hope it helps.
===============================================================
Edit after going through the source code of alphavantage.
So I went through the code. There is nothing wrong in it as par as apikey is concerned. Since in the line before, where you actually call the api, you have instantiated the TimeSeries class and at that time you have given the api key. So it is not needed again.
I could replicate your error at my end. After some traversal through code, I realised that you are passing the wrong currency perhaps. It should not be USD/CAD but just USD. If you rather wish to get for USD/CAD .. you have to say USDCAD. When you say currency = "USD/CAD" .. most likely your formed API is wrong and due to "/" it is prematurely terminated.
Below is the edited code. I have also edited your original post and in the second line, after import, I have added TimeSeries. I hope that is right. If not please reject the edit.
import pandas as pd
from alpha_vantage.timeseries import TimeSeries
api = "XXXXXXXXXXXXX"
ts = TimeSeries(key=api,output_format = "pandas")
data,metadata = ts.get_intraday(symbol = "USD",interval= "1min" , outputsize = "full")
print(data)
data,metadata = ts.get_intraday(symbol = "USDCAD",interval= "1min" , outputsize = "full")
print(data)
I hope this helps.
Change your code to:
import pandas as pd
from alpha_vantage.timeseries import TimeSeries
api_key = "XXXX"
ts = TimeSeries(key = api_key,output_format = "pandas")
data, metadata = ts.get_intraday(symbol = "USDCAD",interval= "1min" , outputsize = "full")
print(data)
Here were the edits made to solve this.
You had a few syntax errors in your code sample.
"#" strings don't work as a parameter in the API key. For testing, use something like "XXXX"
The symbol has to be "USDCAD" not "USD/CAD"
Optional, but preferred: You should be using the from alpha_vantage.foreignexchange import ForeignExchange package to get currency pairs as opposed to the TimeSeries object.
I am writing code to analyze some data and want to create a data frame. How do I set it up successfully to run?
this is for analysis of data and I would like to create a data frame that can categorize data in different grades such as A
Here is the code I wrote:
import analyze_lc_Feb2update
from imp import reload
analyze_lc_Feb2update = reload(analyze_lc_Feb2update)
df = analyze_lc_Feb2update.create_df()
df.shape
df_new = df[df.grade=='A']
df_new.shape
df.columns
df.int_rate.head(5)
df.int_rate.tail(5)
df.int_rate.dtype
df.term.dtype
df_new = df[df.grade =='A']
df_new.shape
output:
TypeError Traceback (most recent call last)
<ipython-input-3-7079435f776f> in <module>()
2 from imp import reload
3 analyze_lc_Feb2update = reload(analyze_lc_Feb2update)
4 df = analyze_lc_Feb2update.create_df()
5 df.shape
6 df_new = df[df.grade=='A']
TypeError: create_df() missing 1 required positional
argument: 'grade'
Based on what was provided I guess your problem is here:
from imp import reload
analyze_lc_Feb2update = reload(analyze_lc_Feb2update)
df = analyze_lc_Feb2update.create_df()
This looks like some custom library you are trying to use, of which the .create_df() method requires a positional argument "grade" which would require you to do something like:
df = analyze_lc_Feb2update.create_df(grade="blah")
I've written the function (tested and working) below:
import pandas as pd
def ConvertStrDateToWeekId(strDate):
dateformat = '2016-7-15 22:44:09'
aDate = pd.to_datetime(strDate)
wk = aDate.isocalendar()[1]
yr = aDate.isocalendar()[0]
Format_4_5_4_date = str(yr) + str(wk)
return Format_4_5_4_date'
and from what I have seen on line I should be able to use it this way:
ml_poLines = result.value.select('PURCHASEORDERNUMBER', 'ITEMNUMBER', PRODUCTCOLORID', 'RECEIVINGWAREHOUSEID', ConvertStrDateToWeekId('CONFIRMEDDELIVERYDATE'))
However when I "show" my dataframe the "CONFIRMEDDELIVERYDATE" column is the original datetime string! NO errors are given.
I've also tried this:
ml_poLines['WeekId'] = (ConvertStrDateToWeekId(ml_poLines['CONFIRMEDDELIVERYDATE']))
and get the following error:
"ValueError: Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions." which makes no sense to me.
I've also tried this with no success.
x = ml_poLines.toPandas();
x['testDates'] = ConvertStrDateToWeekId(x['CONFIRMEDDELIVERYDATE'])
ml_poLines2 = spark.createDataFrame(x)
ml_poLines2.show()
The above generates the following error:
AttributeError: 'Series' object has no attribute 'isocalendar'
What have I done wrong?
Your function ConvertStrDateToWeekId takes a string. But in the following line the argument of the function call is a series of strings:
x['testDates'] = ConvertStrDateToWeekId(x['CONFIRMEDDELIVERYDATE'])
A possible workaround for this error is to use the apply-function of pandas:
x['testDates'] = x['CONFIRMEDDELIVERYDATE'].apply(ConvertStrDateToWeekId)
But without more information about the kind of data you are processing it is hard to provide further help.
This was the work-around that I got to work:
`# convert the confirimedDeliveryDate to a WeekId
x= ml_poLines.toPandas();
x['WeekId'] = x[['ITEMNUMBER', 'CONFIRMEDDELIVERYDATE']].apply(lambda y:ConvertStrDateToWeekId(y[1]), axis=1)
ml_poLines = spark.createDataFrame(x)
ml_poLines.show()`
Not quite as clean as I would like.
Maybe someone else cam propose a cleaner solution.
I am trying to print unique values of the column ADO_name in my data set. Following is the example data set and code I tried (which gives error):
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
data = {'ADO_name':['car1','car1','car1','car2','car2','car2'],
'Time_sec':[0,1,2,0,1,2],
'Speed.kph':[50,51,52,0,0,52]}
dframe = DataFrame(data)
for ado in dframe.groupby('ADO_name'):
ado_name = ado["ADO_name"]
adoID = ado_name.unique()
print(adoID)
Traceback (most recent call last):
File "C:\Users\Quinton\AppData\Local\Temp\Rtmp88ifpB\chunk-code-188c39fc7de8.txt", line 14, in <module>
ado_name = ado["ADO_name"]
TypeError: tuple indices must be integers or slices, not str
What am I doing wrong and how to fix it? Please help.
You can do: dframe["ADO_name"].unique().
You may want to correct your code or use the correct way.
Here is what you need to correct in your code.
for ado in dframe.groupby('ADO_name'):
ado_name = ado[1]["ADO_name"]
adoID = ado_name.unique()
print(adoID)