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
PROJECT_ID = 'cloud-project-examples'
Need bucket name set
bucket_name = "cloud-storage-exam"
Need yfinance installed
$ pip install openbb-yfinance
Need mathplotlib installed
$ pip install matplotlib
Need pandas library installed
$ pip3 install pandas
pip install cachetools
pip install --upgrade google-cloud-storage google-auth
amzn_vol.py
====================================================
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
#Google Cloud project ID
# use your project ID from Google cloud
PROJECT_ID = 'cloud-project-examples'
# The ID of your GCS bucket (bucket name)
bucket_name = "cloud-storage-exam"
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, auto_adjust=True)['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.savefig('amzn_vol.png')
plt.show()
# 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}.')
==================================================
Results
john_iacovacci1@cloudshell:~/yf (cloud-project-examples)$ python3 amzn_vol.py
[*********************100%***********************] 1 of 1 completed
File amzn_vol.png uploaded to stocks/amzn_vol.png.
john_iacovacci1@cloudshell:~/yf (cloud-project-examples)$
Check for the file in storage Bucket
Click into the stocks folder
Click the created file
Click the Public URL link
Copy the file
The next program will visualize AMZN stock prices
amzn_pr.py
====================================================
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 = 'cloud-project-examples'
# The ID of your GCS bucket
bucket_name = "cloud-storage-exam"
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, auto_adjust=True)['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.savefig('amzn_prc.png')
plt.show()
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}.')
====================================================
john_iacovacci1@cloudshell:~/yf (cloud-project-examples)$ python3 amzn_pr.py
[*********************100%***********************] 1 of 1 completed
File amzn_prc.png uploaded to stocks/amzn_prc.png.
john_iacovacci1@cloudshell:~/yf (cloud-project-examples)$
No comments:
Post a Comment