I am working on a project where I’m trying to create an algorithmic trading module with python. I intend to experiment with real-time market data obtained from the Alpha Vantage website via a free API Key/Token however, even though I’m able to make the initial API call & parse the API call, I am unable to add the stocks data to a pandas dataframe via append method.
My code:
# Import necessary libraries
import pandas as pd
import requests
# Unused libraries so far
"""import numpy as np
import pandas as pd
import xlsxwriter
import math"""
# Acquiring an API Token
# Free API Token/Key can be easily acquired here >>> https://www.alphavantage.co/support/#api-key
API_key = ''
# S&P 500 stocks List in CSV Format >>> for later use in the project
# ***stocks = pd.read_csv('sp_500_stocks.csv')
# Making the API call
symbol = 'AAPL'
api_url = f'https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={symbol}&apikey={API_key}'
data = requests.get(api_url).json()
# print(data['Global Quote']['06. volume'])
# Parsing the API call
# symbol = data['Global Quote']['01. symbol']
price = data['Global Quote']['05. price']
volume = data['Global Quote']['06. volume']
# Adding the stocks data to a pandas DataFrame
my_columns = ['Ticker', 'Stock Price', 'Market Volume', 'Number of Shares to Buy'] # Number of Shares to Buy is to be calculated later in the project
final_dataFrame = pd.DataFrame(columns=my_columns)
print(final_dataFrame)
# Append method
final_dataFrame._append( # *** START HERE<<<<<<<<
pd.Series(
[
symbol,
price,
volume,
'N/A' # To be calculated later in the project
],
index=my_columns
),
ignore_index=True
)
I tried the above code & it returns the following:
Empty DataFrame
Columns: [Ticker, Stock Price, Market Volume, Number of Shares to Buy]
Index: []
The output should be a table showing the my_columns list and the corresponding data for each coliumn on the rows.
Samson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.