API Price and Volume graphs
The following programs leverage the finance API to retrieve price and volume for 1 year period and build graphs from the data.
Need project name set (your project
PROJECT_ID = 'consummate-tine-433617-f0'
Need bucket name set (note need your bucket and needs to be public)
bucket_name = "yahoostocks"
Need yfinance installed
pip install yfinance --upgrade --no-cache-dir
Need mathplotlib installed
pip install matplotlib
Need pandas library installed
pip3 install pandas
Send me links to the graphs from the buckets
====================================================
import pandas as pd # Data frame
import yfinance as yf # yfinance API
from datetime import date, timedelta # Date / time modules
from matplotlib import pyplot as plt # Plotting Library
from google.cloud import storage # Leverage Google Storage Buckets
#Google Cloud project ID
# use your project ID from Google cloud
PROJECT_ID = 'consummate-tine-433617-f0'
# The ID of your GCS bucket (bucket name)
bucket_name = "yahoostocks"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
#calculate starting and ending date of range you wish to retrieve
Start = date.today() - timedelta(365)
Start.strftime('%Y-%m-%d')
End = date.today() + timedelta(2)
End.strftime('%Y-%m-%d')
# function to get daily stock volume of shares traded
def closing_volume(ticker):
Asset = pd.DataFrame(yf.download(ticker, start=Start,
end=End)['Volume'])
return Asset
# call function for the symbol AMZN
AMAZON = closing_volume('AMZN')
# plot AMZN volume graph
plt.plot(AMAZON, color='purple', linewidth=2)
plt.title('AMAZON Shares Traded Daily')
plt.ylabel('Volume (#)')
plt.xlabel('Date')
plt.show()
plt.savefig('amzn_vol.png')
# send plot file to your bucket
plot_filename = 'amzn_vol.png'
destination_blob_name = f'stocks/{plot_filename}'
source_file_name = 'amzn_vol.png'
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name) #upload file to specified destination
print(f'File {source_file_name} uploaded to {destination_blob_name}.')
==================================================
import pandas as pd
import yfinance as yf
from datetime import date, timedelta
from matplotlib import pyplot as plt
from google.cloud import storage
#Google Cloud project ID
PROJECT_ID = 'consummate-tine-433617-f0'
# The ID of your GCS bucket
bucket_name = "yahoostocks"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
Start = date.today() - timedelta(365)
Start.strftime('%Y-%m-%d')
End = date.today() + timedelta(2)
End.strftime('%Y-%m-%d')
def closing_price(ticker):
Asset = pd.DataFrame(yf.download(ticker, start=Start,
end=End)['Adj Close'])
return Asset
AMAZON = closing_price('AMZN')
plt.plot(AMAZON, color='purple', linewidth=2)
plt.title('AMAZON Performance')
plt.ylabel('Price ($)')
plt.xlabel('Date')
plt.show()
plt.savefig('amzn_prc.png')
plot_filename = 'amzn_prc.png'
destination_blob_name = f'stocks/{plot_filename}'
source_file_name = 'amzn_prc.png'
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name) #upload file to specified destination
print(f'File {source_file_name} uploaded to {destination_blob_name}.')
No comments:
Post a Comment