-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
100 additions
and
33 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from datetime import date, timedelta | ||
|
||
def from_to(): | ||
to = date.today() | ||
from_ = to - timedelta(days=7) | ||
return from_, to |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from from_to import from_to | ||
|
||
|
||
def growth_rate(data): | ||
previousClose, lastClose = data[1].close, data[0].close | ||
return (lastClose - previousClose) / previousClose | ||
|
||
def close_price(data): | ||
return data[0].close | ||
|
||
def fetch_stock_price_data(stockApiClient, symbol): | ||
from_, to = from_to() | ||
data = stockApiClient.get_aggs( | ||
ticker=symbol, | ||
multiplier=1, | ||
timespan="day", | ||
from_=from_, | ||
to=to, | ||
adjusted=True, | ||
sort="desc", | ||
limit=50, | ||
) | ||
info = stockApiClient.get_ticker_details(symbol) | ||
|
||
price = close_price(data) | ||
growth = growth_rate(data) | ||
name = info.name | ||
return price, growth, name |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,53 @@ | ||
import yfinance as yf | ||
|
||
from from_to import from_to | ||
from number_formatter import format_growth_rate, format_price | ||
import poly | ||
import yahoo | ||
|
||
HELP_CONTENT = """ | ||
$stonk [SYMBOL] | ||
Examples: | ||
- $stonk $help: show this help message | ||
- $stonk AAPL: show the latest stock price of AAPL | ||
""" | ||
|
||
def growth_rate(data): | ||
previouseClose, lastClose = data['Close'][-2], data['Close'][-1] | ||
return (lastClose - previouseClose) / previouseClose | ||
def use_polygon(client, symbol): | ||
try: | ||
price, growth, name = poly.fetch_stock_price_data(client, symbol) | ||
except Exception as error: | ||
print("Failed to fetch data from Polygon {}".format(error)) | ||
return None, False | ||
return (price, growth, name), True | ||
|
||
def close_price(data): | ||
return data['Close'][-1] | ||
def use_yahoo(symbol): | ||
try: | ||
price, growth, name = yahoo.fetch_stock_price_data(symbol) | ||
except Exception as error: | ||
print("Failed to fetch data from Yahoo! Finance {}".format(error)) | ||
return None, False | ||
return (price, growth, name), True | ||
|
||
def fetch_price_data(symbol): | ||
ticker = yf.Ticker(symbol) | ||
data = ticker.history(period='7d') | ||
info = ticker.info | ||
return data, info | ||
async def reply(message, results, symbol): | ||
price, growth, name = results | ||
await message.reply("{} ({}): {} {}".format(symbol, name, format_price(price), format_growth_rate(growth))) | ||
|
||
async def handle_stonk(symbol, message): | ||
async def handle_stonk(stockApiClient, symbol, message): | ||
if symbol is None: | ||
await message.reply("Ticker symbol cannot be empty") | ||
return | ||
|
||
try: | ||
data, info = fetch_price_data(symbol) | ||
price = close_price(data) | ||
growth = growth_rate(data) | ||
name = info["shortName"] | ||
await message.reply("{} ({}): {} {}".format(symbol, name, format_price(price), format_growth_rate(growth))) | ||
except Exception as error: | ||
print(error) | ||
await message.reply("Cannot get data for {}".format(symbol)) | ||
results, success = use_polygon(stockApiClient, symbol) | ||
if success: | ||
await reply(message, results, symbol) | ||
return | ||
|
||
results, success = use_yahoo(symbol) | ||
if success: | ||
await reply(message, results, symbol) | ||
return | ||
|
||
await message.reply("Cannot get data for {}".format(symbol)) | ||
|
||
|
||
async def handle_help(message): | ||
content = """ | ||
$stonk [SYMBOL] | ||
Examples: | ||
- $stonk $help: show this help message | ||
- $stonk AAPL: show the latest stock price of AAPL | ||
""" | ||
await message.reply(content) | ||
await message.reply(HELP_CONTENT) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import yfinance as yf | ||
|
||
def growth_rate(data): | ||
previouseClose, lastClose = data['Close'][-2], data['Close'][-1] | ||
return (lastClose - previouseClose) / previouseClose | ||
|
||
def close_price(data): | ||
return data['Close'][-1] | ||
|
||
def fetch_stock_price_data(symbol): | ||
ticker = yf.Ticker(symbol) | ||
data = ticker.history(period='7d') | ||
info = ticker.info | ||
|
||
price = close_price(data) | ||
growth = growth_rate(data) | ||
name = info.name | ||
return price, growth, name |