1
0
mirror of https://gitlab.com/simple-stock-bots/simple-stock-bot.git synced 2025-06-16 15:17:28 +00:00

changed historical to instraday plotting

This commit is contained in:
Anson 2020-07-09 00:00:31 -07:00
parent 68d66b833f
commit 7ec1c02a8b
2 changed files with 22 additions and 25 deletions

34
bot.py
View File

@ -1,4 +1,5 @@
# Works with Python 3.8 # Works with Python 3.8
import datetime
import io import io
import logging import logging
import os import os
@ -131,24 +132,18 @@ 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): def intra(update, context):
# TODO: Document usage of this command. https://iexcloud.io/docs/api/#historical-prices # TODO: Document usage of this command. https://iexcloud.io/docs/api/#historical-prices
message = update.message.text message = update.message.text
chat_id = update.message.chat_id chat_id = update.message.chat_id
try: symbol = s.find_symbols(message)[0]
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) df = s.intra_reply(symbol)
if h.empty: if df.empty:
update.message.reply_text( 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, parse_mode=telegram.ParseMode.MARKDOWN,
) )
@ -157,13 +152,19 @@ def historical(update, context):
) )
buf = io.BytesIO() 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) buf.seek(0)
update.message.reply_photo( update.message.reply_photo(
photo=buf, photo=buf, caption=f"", parse_mode=telegram.ParseMode.MARKDOWN,
caption=f"*{period} chart for {symbol}*",
parse_mode=telegram.ParseMode.MARKDOWN,
) )
@ -218,8 +219,7 @@ 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("intra", intra))
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))

View File

@ -212,18 +212,15 @@ Market data is provided by [IEX Cloud](https://iexcloud.io)
return infoMessages 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"]): if symbol.upper() not in list(self.symbol_list["symbol"]):
return pd.DataFrame() return pd.DataFrame()
time_periods = ["max", "5y", "2y", "1y", "ytd", "6m", "3m", "1m", "5d"] IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol}/intraday-prices?token={self.IEX_TOKEN}"
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) response = r.get(IEXurl)
if response.status_code == 200: if response.status_code == 200:
df = pd.DataFrame(response.json()) df = pd.DataFrame(response.json())
df["date"] = pd.to_datetime(df["date"]) df.dropna(inplace=True)
df = df.set_index("date") df["DT"] = pd.to_datetime(df["date"] + "T" + df["minute"])
df = df.set_index("DT")
return df return df