Last active
July 26, 2023 05:46
-
-
Save jarvisx17/3138fa2ef9259e59df46367669ef3622 to your computer and use it in GitHub Desktop.
Free Realtime Live Stock Data Python Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import time | |
import pandas as pd | |
import datetime | |
def fetch_data(ticker): | |
SYMB = ticker # Symbol of stock | |
# Yahoo url | |
url = f'https://query1.finance.yahoo.com/v8/finance/chart/{SYMB}?region=US&lang=en-US&includePrePost=false&interval=1mo&useYfid=true&range=1d&corsDomain=finance.yahoo.com&.tsrc=finance' | |
# Browser Headers | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' | |
} | |
# store response | |
response = requests.get(url, headers=headers) | |
if response.status_code == 200: | |
data = response.json() | |
print(f"Current Time : {datetime.datetime.fromtimestamp(data['chart']['result'][0]['meta']['regularMarketTime'])}") | |
print(f"Stock : { data['chart']['result'][0]['meta']['symbol'] }") | |
time = { 'Time' : [str(datetime.datetime.fromtimestamp(data['chart']['result'][0]['meta']['regularMarketTime']))] } | |
chart = data['chart']['result'][0]['indicators']['quote'][0] | |
chart.update(time) | |
chart = pd.DataFrame(chart) | |
chart.set_index('Time', inplace=True) | |
chart = chart[['open','close','high','low','volume']] | |
print(pd.DataFrame(chart)) | |
else: | |
print(f"Error: Request failed with status code {response.status_code}") | |
# Infinite loop with 5-second interval ( You can seconds change as per your requirements) | |
while True: | |
ticker = 'GOOG' # ( symbol of stock from yahoo finance website ) | |
fetch_data(ticker) | |
time.sleep(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment