From 463b2f1c3b168430a8a02a95c1731e1fafa79b74 Mon Sep 17 00:00:00 2001 From: Anson Date: Wed, 1 Jul 2020 20:08:24 -0700 Subject: [PATCH] added plotting of historical pricing --- bot.py | 45 ++++++++++++++++++++++++++++++++++++++++++++- functions.py | 18 +++++++++++++++++- requirements.txt | 3 ++- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/bot.py b/bot.py index 76865da..6d6e3b7 100644 --- a/bot.py +++ b/bot.py @@ -1,7 +1,9 @@ # Works with Python 3.8 +import io import logging import os +import mplfinance as mpf import telegram from telegram import InlineQueryResultArticle, InputTextMessageContent from telegram.ext import ( @@ -117,6 +119,10 @@ def search(update, context): message = update.message.text chat_id = update.message.chat_id + usage = """ + To use this command + """ + queries = s.search_symbols(message)[:6] if queries: reply = "*Search Results:*\n`$ticker: Company Name`\n" @@ -125,6 +131,42 @@ def search(update, context): update.message.reply_text(text=reply, parse_mode=telegram.ParseMode.MARKDOWN) +def historical(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 + ) + + h = s.historical_reply(symbol, period) + if h.empty: + update.message.reply_text( + text="Invalid symbol or time period, please see `/help` or `/historical help` for usage details.", + parse_mode=telegram.ParseMode.MARKDOWN, + ) + + context.bot.send_chat_action( + chat_id=chat_id, action=telegram.ChatAction.UPLOAD_PHOTO + ) + + buf = io.BytesIO() + mpf.plot(h, type="candle", 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, + ) + + def inline_query(update, context): """ Handles inline query. @@ -176,7 +218,8 @@ 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)) # 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 e6b2a98..4f5e850 100644 --- a/functions.py +++ b/functions.py @@ -5,8 +5,8 @@ from datetime import datetime, timedelta import pandas as pd import requests as r -from fuzzywuzzy import fuzz import schedule +from fuzzywuzzy import fuzz class Symbol: @@ -211,3 +211,19 @@ Market data is provided by [IEX Cloud](https://iexcloud.io) ] = f"No information found for: {symbol}\nEither today is boring or the symbol does not exist." return infoMessages + + def historical_reply(self, symbol: str, period: 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}" + 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") + return df diff --git a/requirements.txt b/requirements.txt index 82c907b..da9b845 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ requests==2.23.0 pandas==1.0.3 fuzzywuzzy==0.18.0 python-Levenshtein==0.12.0 -schedule==0.6.0 \ No newline at end of file +schedule==0.6.0 +mplfinance==0.12.6a3 \ No newline at end of file