mirror of
https://gitlab.com/simple-stock-bots/simple-stock-bot.git
synced 2025-06-16 15:17:28 +00:00
added plotting of historical pricing
This commit is contained in:
parent
fdf338a9c3
commit
463b2f1c3b
45
bot.py
45
bot.py
@ -1,7 +1,9 @@
|
|||||||
# Works with Python 3.8
|
# Works with Python 3.8
|
||||||
|
import io
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
import mplfinance as mpf
|
||||||
import telegram
|
import telegram
|
||||||
from telegram import InlineQueryResultArticle, InputTextMessageContent
|
from telegram import InlineQueryResultArticle, InputTextMessageContent
|
||||||
from telegram.ext import (
|
from telegram.ext import (
|
||||||
@ -117,6 +119,10 @@ def search(update, context):
|
|||||||
message = update.message.text
|
message = update.message.text
|
||||||
chat_id = update.message.chat_id
|
chat_id = update.message.chat_id
|
||||||
|
|
||||||
|
usage = """
|
||||||
|
To use this command
|
||||||
|
"""
|
||||||
|
|
||||||
queries = s.search_symbols(message)[:6]
|
queries = s.search_symbols(message)[:6]
|
||||||
if queries:
|
if queries:
|
||||||
reply = "*Search Results:*\n`$ticker: Company Name`\n"
|
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)
|
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):
|
def inline_query(update, context):
|
||||||
"""
|
"""
|
||||||
Handles inline query.
|
Handles inline query.
|
||||||
@ -176,7 +218,8 @@ def main():
|
|||||||
dp.add_handler(CommandHandler("news", news))
|
dp.add_handler(CommandHandler("news", news))
|
||||||
dp.add_handler(CommandHandler("info", info))
|
dp.add_handler(CommandHandler("info", info))
|
||||||
dp.add_handler(CommandHandler("search", search))
|
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
|
# on noncommand i.e message - echo the message on Telegram
|
||||||
dp.add_handler(MessageHandler(Filters.text, symbol_detect))
|
dp.add_handler(MessageHandler(Filters.text, symbol_detect))
|
||||||
|
|
||||||
|
18
functions.py
18
functions.py
@ -5,8 +5,8 @@ from datetime import datetime, timedelta
|
|||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import requests as r
|
import requests as r
|
||||||
from fuzzywuzzy import fuzz
|
|
||||||
import schedule
|
import schedule
|
||||||
|
from fuzzywuzzy import fuzz
|
||||||
|
|
||||||
|
|
||||||
class Symbol:
|
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."
|
] = f"No information found for: {symbol}\nEither today is boring or the symbol does not exist."
|
||||||
|
|
||||||
return infoMessages
|
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
|
||||||
|
@ -3,4 +3,5 @@ requests==2.23.0
|
|||||||
pandas==1.0.3
|
pandas==1.0.3
|
||||||
fuzzywuzzy==0.18.0
|
fuzzywuzzy==0.18.0
|
||||||
python-Levenshtein==0.12.0
|
python-Levenshtein==0.12.0
|
||||||
schedule==0.6.0
|
schedule==0.6.0
|
||||||
|
mplfinance==0.12.6a3
|
Loading…
x
Reference in New Issue
Block a user