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

#31 just need to fix bugs from Main

This commit is contained in:
Anson Biggs 2021-03-27 17:31:23 -07:00
parent e1ed5fbf57
commit 6ab7f2c25d
5 changed files with 19 additions and 20 deletions

View File

@ -10,7 +10,7 @@ import schedule
from fuzzywuzzy import fuzz
import os
from symbol_router import Stock
from Symbol import Stock
class IEX_Symbol:

View File

@ -1,5 +1,4 @@
import requests as r
from cg_Crypto import cg_Crypto
class Symbol:
@ -28,10 +27,11 @@ class Stock(Symbol):
def __init__(self, symbol: str) -> None:
self.symbol = symbol
self.id = symbol
self.name = "$" + symbol.upper()
# This is so every Coin instance doesnt have to download entire list of coin symbols and id's
cg = cg_Crypto()
# Used by Coin to change symbols for ids
coins = r.get("https://api.coingecko.com/api/v3/coins/list").json()
class Coin(Symbol):
@ -40,7 +40,9 @@ class Coin(Symbol):
self.get_data()
def get_data(self) -> None:
self.id = cg.symbol_id(self.symbol)
self.id = list(filter(lambda coin: coin["symbol"] == self.symbol, coins))[0][
"id"
]
data = r.get("https://api.coingecko.com/api/v3/coins/" + self.id).json()
self.data = data

11
bot.py
View File

@ -260,7 +260,7 @@ def intra(update: Update, context: CallbackContext):
mpf.plot(
df,
type="renko",
title=f"\n${symbol.name}",
title=f"\n{symbol.name}",
volume="volume" in df.keys(),
style="yahoo",
mav=20,
@ -270,7 +270,7 @@ def intra(update: Update, context: CallbackContext):
update.message.reply_photo(
photo=buf,
caption=f"\nIntraday chart for ${symbol.name} from {df.first_valid_index().strftime('%I:%M')} to"
caption=f"\nIntraday chart for {symbol.name} from {df.first_valid_index().strftime('%I:%M')} to"
+ f" {df.last_valid_index().strftime('%I:%M')} ET on"
+ f" {datetime.date.today().strftime('%d, %b %Y')}\n\n{s.price_reply([symbol])[0]}",
parse_mode=telegram.ParseMode.MARKDOWN,
@ -298,7 +298,6 @@ def chart(update: Update, context: CallbackContext):
parse_mode=telegram.ParseMode.MARKDOWN,
)
return
context.bot.send_chat_action(
chat_id=chat_id, action=telegram.ChatAction.UPLOAD_PHOTO
)
@ -307,7 +306,7 @@ def chart(update: Update, context: CallbackContext):
mpf.plot(
df,
type="candle",
title=f"\n${symbol.name}",
title=f"\n{symbol.name}",
volume="volume" in df.keys(),
style="yahoo",
savefig=dict(fname=buf, dpi=400, bbox_inches="tight"),
@ -316,7 +315,7 @@ def chart(update: Update, context: CallbackContext):
update.message.reply_photo(
photo=buf,
caption=f"\n1 Month chart for ${symbol.name} from {df.first_valid_index().strftime('%d, %b %Y')}"
caption=f"\n1 Month chart for {symbol.name} from {df.first_valid_index().strftime('%d, %b %Y')}"
+ f" to {df.last_valid_index().strftime('%d, %b %Y')}\n\n{s.price_reply([symbol])[0]}",
parse_mode=telegram.ParseMode.MARKDOWN,
)
@ -406,8 +405,6 @@ def error(update: Update, context: CallbackContext):
# Finally, send the message
update.message.reply_text(text=message, parse_mode=telegram.ParseMode.HTML)
update.message.reply_text(text="Please inform the bot admin of this issue.")
print("-" * 50)
print(tb_string)
def main():

View File

@ -9,7 +9,7 @@ import requests as r
import schedule
from fuzzywuzzy import fuzz
from markdownify import markdownify
from symbol_router import Coin
from Symbol import Coin
class cg_Crypto:
@ -163,7 +163,7 @@ class cg_Crypto:
Returns a timeseries dataframe with high, low, and volume data if its available. Otherwise returns empty pd.DataFrame.
"""
response = r.get(
"https://api.coingecko.com/api/v3/coins/{symbol}/ohlc?vs_currency=usd&days=1"
f"https://api.coingecko.com/api/v3/coins/{symbol.id}/ohlc?vs_currency=usd&days=1"
)
if response.status_code == 200:
df = pd.DataFrame(
@ -190,9 +190,9 @@ class cg_Crypto:
Returns a timeseries dataframe with high, low, and volume data if its available. Otherwise returns empty pd.DataFrame.
"""
response = r.get(
"https://api.coingecko.com/api/v3/coins/{symbol}/ohlc?vs_currency=usd&days=30"
f"https://api.coingecko.com/api/v3/coins/{symbol.id}/ohlc?vs_currency=usd&days=30"
)
print(response.status_code)
if response.status_code == 200:
df = pd.DataFrame(
response.json(), columns=["Date", "Open", "High", "Low", "Close"]
@ -217,7 +217,7 @@ class cg_Crypto:
Each symbol passed in is a key with its value being a human readable formatted string of the symbols statistics.
"""
response = r.get(
f"https://api.coingecko.com/api/v3/coins/{symbol}?localization=false"
f"https://api.coingecko.com/api/v3/coins/{symbol.id}?localization=false"
)
if response.status_code == 200:
data = response.json()
@ -249,12 +249,12 @@ class cg_Crypto:
"""
response = r.get(
f"https://api.coingecko.com/api/v3/coins/{symbol}?localization=false"
f"https://api.coingecko.com/api/v3/coins/{symbol.id}?localization=false"
)
if response.status_code == 200:
data = response.json()
try:
return markdownify(data["description"])
return markdownify(data["description"]["en"])
except KeyError:
return f"{symbol} does not have a description available."

View File

@ -51,7 +51,7 @@ class Router:
symbols.append(Coin(coin.lower()))
else:
print(f"{coin} is not in list of coins")
print(symbols)
return symbols
def status(self) -> str: