diff --git a/bot.py b/bot.py index 6d6e3b7..cc4afd8 100644 --- a/bot.py +++ b/bot.py @@ -1,4 +1,5 @@ # Works with Python 3.8 +import datetime import io import logging import os @@ -131,24 +132,18 @@ def search(update, context): update.message.reply_text(text=reply, parse_mode=telegram.ParseMode.MARKDOWN) -def historical(update, context): +def intra(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 - try: - cmd, symbol, period = message.split(" ") - symbol = symbol.replace("$", "") - except TypeError: - update.message.reply_text( - text="example: /historical tsla 5d", parse_mode=telegram.ParseMode.MARKDOWN - ) + symbol = s.find_symbols(message)[0] - h = s.historical_reply(symbol, period) - if h.empty: + df = s.intra_reply(symbol) + if df.empty: update.message.reply_text( - text="Invalid symbol or time period, please see `/help` or `/historical help` for usage details.", + text="Invalid symbol please see `/help` for usage details.", parse_mode=telegram.ParseMode.MARKDOWN, ) @@ -157,13 +152,19 @@ def historical(update, context): ) buf = io.BytesIO() - mpf.plot(h, type="candle", savefig=dict(fname=buf, dpi=400)) + mpf.plot( + df, + type="renko", + title=f"Intraday chart for ${symbol.upper()}\n{datetime.date.today().strftime('%d, %b %Y')}", + volume=True, + style="yahoo", + mav=20, + savefig=dict(fname=buf, dpi=400), + ) buf.seek(0) update.message.reply_photo( - photo=buf, - caption=f"*{period} chart for {symbol}*", - parse_mode=telegram.ParseMode.MARKDOWN, + photo=buf, caption=f"", parse_mode=telegram.ParseMode.MARKDOWN, ) @@ -218,8 +219,7 @@ def main(): dp.add_handler(CommandHandler("news", news)) dp.add_handler(CommandHandler("info", info)) dp.add_handler(CommandHandler("search", search)) - dp.add_handler(CommandHandler("historical", historical)) - dp.add_handler(CommandHandler("h", historical)) + dp.add_handler(CommandHandler("intra", intra)) # on noncommand i.e message - echo the message on Telegram dp.add_handler(MessageHandler(Filters.text, symbol_detect)) diff --git a/functions.py b/functions.py index 4f5e850..b87f133 100644 --- a/functions.py +++ b/functions.py @@ -212,18 +212,15 @@ Market data is provided by [IEX Cloud](https://iexcloud.io) return infoMessages - def historical_reply(self, symbol: str, period: str): + def intra_reply(self, symbol: str): if symbol.upper() not in list(self.symbol_list["symbol"]): return pd.DataFrame() - time_periods = ["max", "5y", "2y", "1y", "ytd", "6m", "3m", "1m", "5d"] - if period not in time_periods: - return pd.DataFrame() - - IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol}/chart/{period}?token={self.IEX_TOKEN}" + IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol}/intraday-prices?token={self.IEX_TOKEN}" response = r.get(IEXurl) if response.status_code == 200: df = pd.DataFrame(response.json()) - df["date"] = pd.to_datetime(df["date"]) - df = df.set_index("date") + df.dropna(inplace=True) + df["DT"] = pd.to_datetime(df["date"] + "T" + df["minute"]) + df = df.set_index("DT") return df