From 81c42000d8ce27b9fb37e7bf18bf7dbd4c812f3d Mon Sep 17 00:00:00 2001 From: Anson Biggs Date: Wed, 27 Jan 2021 12:50:12 -0700 Subject: [PATCH] Close #32 --- bot.py | 39 +++++++++++++++++++++++++++++++++++++++ functions.py | 14 ++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/bot.py b/bot.py index 8049c68..e01165e 100644 --- a/bot.py +++ b/bot.py @@ -173,6 +173,44 @@ def intra(update, context): ) +def chart(update, context): + # TODO: Document usage of this command. https://iexcloud.io/docs/api/#historical-prices + + message = update.message.text + chat_id = update.message.chat_id + + symbol = s.find_symbols(message)[0] + + df = s.chart_reply(symbol) + if df.empty: + update.message.reply_text( + text="Invalid symbol please see `/help` for usage details.", + parse_mode=telegram.ParseMode.MARKDOWN, + ) + + context.bot.send_chat_action( + chat_id=chat_id, action=telegram.ChatAction.UPLOAD_PHOTO + ) + + price = s.price_reply([symbol]) + buf = io.BytesIO() + mpf.plot( + df, + type="candle", + title=f"\n${symbol.upper()}", + volume=True, + style="yahoo", + savefig=dict(fname=buf, dpi=400), + ) + buf.seek(0) + + update.message.reply_photo( + photo=buf, + caption=f"\n1 Month chart for ${symbol.upper()} on {datetime.date.today().strftime('%d, %b %Y')}\n\n{price[symbol]}", + parse_mode=telegram.ParseMode.MARKDOWN, + ) + + def stat(update, context): """ https://iexcloud.io/docs/api/#key-stats @@ -278,6 +316,7 @@ def main(): dp.add_handler(CommandHandler("search", search)) dp.add_handler(CommandHandler("intraday", intra)) dp.add_handler(CommandHandler("intra", intra)) + dp.add_handler(CommandHandler("chart", chart)) dp.add_handler(CommandHandler("crypto", crypto)) dp.add_handler(CommandHandler("random", rand_pick)) diff --git a/functions.py b/functions.py index be02862..510dbb2 100644 --- a/functions.py +++ b/functions.py @@ -235,6 +235,20 @@ Market data is provided by [IEX Cloud](https://iexcloud.io) df = df.set_index("DT") return df + def chart_reply(self, symbol: str): + if symbol.upper() not in list(self.symbol_list["symbol"]): + return pd.DataFrame() + + IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol}/chart/1mm?token={self.IEX_TOKEN}&chartInterval=3&includeToday=true" + print(IEXurl) + response = r.get(IEXurl) + if response.status_code == 200: + df = pd.DataFrame(response.json()) + df.dropna(inplace=True, subset=["date", "minute", "high", "low", "volume"]) + df["DT"] = pd.to_datetime(df["date"] + "T" + df["minute"]) + df = df.set_index("DT") + return df + def stat_reply(self, symbols: list): infoMessages = {}