diff --git a/config.yaml b/config.yaml index 6202bc2b..5ce0564a 100644 --- a/config.yaml +++ b/config.yaml @@ -182,6 +182,10 @@ LOOPS: ENABLED: True CHANNEL: 🚥┃rsi-heatmap + SECTOR_SNAPSHOT: + ENABLED: True + CHANNEL: 📸┃spy-sectors + STOCK_HALTS: ENABLED: True CHANNEL: 🛑┃halted diff --git a/data/bitcoin_data.csv b/data/bitcoin_data.csv index 49fe6cc8..87e6fd9f 100644 --- a/data/bitcoin_data.csv +++ b/data/bitcoin_data.csv @@ -5671,3 +5671,12 @@ Date,Value 2024-07-04,57050.01 2024-07-05,56628.79 2024-07-06,58230.13 +2024-07-06,58230.13 +2024-07-07,55857.81 +2024-07-08,56714.62 +2024-07-09,58050.0 +2024-07-10,57725.85 +2024-07-11,57339.89 +2024-07-12,57889.1 +2024-07-13,59204.02 +2024-07-14,60797.91 diff --git a/img/icons/barchart.png b/img/icons/barchart.png new file mode 100644 index 00000000..c7868e01 Binary files /dev/null and b/img/icons/barchart.png differ diff --git a/src/cogs/loops/funding_heatmap.py b/src/cogs/loops/funding_heatmap.py index 68735a9d..ef9c13ed 100644 --- a/src/cogs/loops/funding_heatmap.py +++ b/src/cogs/loops/funding_heatmap.py @@ -217,7 +217,6 @@ def plot_heatmap(data: pd.DataFrame): ax.set_facecolor(BACKGROUND_COLOR) # Dark background color for the axes # Plot the heatmap - print(data) heatmap = sns.heatmap( data, cmap="viridis", diff --git a/src/cogs/loops/sector_snapshot.py b/src/cogs/loops/sector_snapshot.py new file mode 100644 index 00000000..e4eb9317 --- /dev/null +++ b/src/cogs/loops/sector_snapshot.py @@ -0,0 +1,188 @@ +import datetime +import os +from io import StringIO + +import discord +import matplotlib.colors as mcolors +import matplotlib.pyplot as plt +import numpy as np +import pandas as pd +from discord.ext import commands +from discord.ext.tasks import loop + +from util.disc_util import get_channel +from util.vars import config, data_sources, get_json_data + + +class Sector_snapshot(commands.Cog): + """ + This class contains the cog for posting the Liquidations chart. + It can be enabled / disabled in the config under ["LOOPS"]["LIQUIDATIONS"]. + """ + + def __init__(self, bot: commands.Bot) -> None: + self.bot = bot + self.channel = None + + if config["LOOPS"]["SECTOR_SNAPSHOT"]["ENABLED"]: + self.post_snapshot.start() + + @loop(hours=12) + async def post_snapshot(self): + if self.channel is None: + self.channel = await get_channel( + self.bot, + config["LOOPS"]["SECTOR_SNAPSHOT"]["CHANNEL"], + config["CATEGORIES"]["STOCKS"], + ) + + df = await get_data() + plot_data(df) + + # Save plot + file_name = "sector_snap.png" + file_path = os.path.join("temp", file_name) + plt.savefig(file_path, bbox_inches="tight", dpi=300) + plt.cla() + plt.close() + + e = discord.Embed( + title="Percentage Of Large Cap Stocks Above Their Moving Averages", + description="", + color=data_sources["barchart"]["color"], + timestamp=datetime.datetime.now(datetime.timezone.utc), + url="https://www.barchart.com/stocks/market-performance", + ) + file = discord.File(file_path, filename=file_name) + e.set_image(url=f"attachment://{file_name}") + e.set_footer( + text="\u200b", + icon_url=data_sources["barchart"]["icon"], + ) + + await self.channel.purge(limit=1) + await self.channel.send(file=file, embed=e) + + # Delete temp file + os.remove(file_path) + + +async def get_data(): + headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" + } + r = await get_json_data( + url="https://www.barchart.com/stocks/market-performance", + headers=headers, + text=True, + ) + df = pd.read_html(StringIO(r))[0] + + # Remove % from all rows + df = df.replace("%", "", regex=True) + + # convert columns to numeric + num_cols = [ + "5 Day Mov Avg", + "20 Day Mov Avg", + "50 Day Mov Avg", + "100 Day Mov Avg", + "150 Day Mov Avg", + "200 Day Mov Avg", + ] + df[num_cols] = df[num_cols].apply(pd.to_numeric, errors="coerce") + + return df + + +def plot_data(df): + + # Define custom colormap for each 10% increment + colors = [ + (0, "#620101"), # 0% + (0.1, "#76030f"), # 10% + (0.2, "#801533"), # 20% + (0.3, "#620000"), # 30% + (0.4, "#74274b"), # 40% + (0.5, "#062f5b"), # 50% + (0.6, "#174f52"), # 60% + (0.7, "#113b35"), # 70% + (0.8, "#074510"), # 80% + (0.9, "#03360c"), # 90% + (1.0, "#012909"), # 100% + ] + + # Create the custom colormap + cmap = mcolors.LinearSegmentedColormap.from_list("custom_cmap", colors) + + # Normalize data to [0, 1] for color mapping + norm = plt.Normalize(0, 100) + + # Apply custom colormap + values = df.drop(columns="Name").values + colors = cmap(norm(values)) + + # Create a white color array for the 'Name' column + name_colors = np.ones((df.shape[0], 1, 4)) + + # Concatenate the name colors with the data colors + colors = np.concatenate((name_colors, colors), axis=1) + + # Create the table + fig, ax = plt.subplots(figsize=(14, 6)) + + # Set the background color of the figure + fig.patch.set_facecolor("#2e2e2e") + + ax.axis("off") + + # Add % to all values in the DF except Name column + df = df.map(lambda x: f"{x}%" if isinstance(x, (int, float)) else x) + + # Create the table + table = ax.table( + cellText=df.values, + colLabels=df.columns, + cellColours=colors, + cellLoc="center", + loc="center", + ) + + # Adjust column widths + table.auto_set_column_width(list(range(df.shape[1]))) + table.scale(1, 2) # Make the first column wider + + # Style the header + for key, cell in table.get_celld().items(): + cell.set_text_props(color="w") + cell.set_edgecolor("#2b2f30") # Set the grid color to white + # First row + if key[0] == 0: + cell.set_fontsize(10) + cell.set_text_props(weight="bold") + cell.set_facecolor("#181a1b") + elif key[1] == 0: + cell.set_fontsize(10) + cell.set_facecolor("#181a1b") + cell.set_text_props(ha="left") + else: + cell.set_fontsize(12) + + table.auto_set_font_size(False) + + # Add the title in the top left corner + plt.text( + -0.05, + 1.05, + "Percentage Of Large Cap Stocks Above Their Moving Averages", + transform=ax.transAxes, + fontsize=12, + verticalalignment="top", + horizontalalignment="left", + color="white", + weight="bold", + ) + + +def setup(bot: commands.Bot) -> None: + bot.add_cog(Sector_snapshot(bot)) diff --git a/src/util/vars.py b/src/util/vars.py index d6384be1..80f081af 100644 --- a/src/util/vars.py +++ b/src/util/vars.py @@ -55,6 +55,7 @@ "nasdaqtrader": {"color": 0x0996C7, "icon": icon_url + "nasdaqtrader.png"}, "stocktwits": {"color": 0xFFFFFF, "icon": icon_url + "stocktwits.png"}, "cryptocraft": {"color": 0x634C7B, "icon": icon_url + "cryptocraft.png"}, + "barchart": {"color": 0x84C8C, "icon": icon_url + "barchart.png"}, } # Stable coins