We can get a quote for the latest price of a stock ticker which is close to real-time data using the unofficial Webull API.
The delay compared to real-time streaming data should be 1 to 2 seconds at most, but this should not be used for applications where the recency of data is critically important.
Make sure the module is installed:
$ pip install webull
The script is below. Note that there are two possible fields in the response JSON data for the last price: “close” and “pPrice”. At the time of writing “close” is present, but “pPrice” is present in older data examples. Because the API is not officially supported or documented, this field may change again. If neither of the known fields are present, the script reports an error.
The name of the ticker to search for is taken from the standard input.
from webull import webull
wb = webull()
ticker = input("Ticker symbol: ")
try:
stockData = wb.get_quote(ticker)
except ValueError as e:
print("Exception reading from Webull API: ")
print(e)
if "pPrice" in stockData:
# Current price field in some data.
lastPrice = stockData["pPrice"]
print("Price: " + str(lastPrice))
elif "close" in stockData:
# Current Close of the current candle, i.e. last price.
lastPrice = stockData["close"]
print("Price: " + str(lastPrice))
else:
# May happen if the data format is changed.
print("Price field missing.")
Example input and output:
Ticker symbol: TSLA Price: 490.73
References
https://pypi.org/project/webull
https://github.com/tedchou12/webull
