Skip to content

Commit

Permalink
added sector snapshot
Browse files Browse the repository at this point in the history
close #485
  • Loading branch information
StephanAkkerman committed Jul 15, 2024
1 parent 0b00813 commit 9814a76
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 1 deletion.
4 changes: 4 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ LOOPS:
ENABLED: True
CHANNEL: 🚥┃rsi-heatmap

SECTOR_SNAPSHOT:
ENABLED: True
CHANNEL: 📸┃spy-sectors

STOCK_HALTS:
ENABLED: True
CHANNEL: 🛑┃halted
Expand Down
9 changes: 9 additions & 0 deletions data/bitcoin_data.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file added img/icons/barchart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion src/cogs/loops/funding_heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
188 changes: 188 additions & 0 deletions src/cogs/loops/sector_snapshot.py
Original file line number Diff line number Diff line change
@@ -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))
1 change: 1 addition & 0 deletions src/util/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9814a76

Please sign in to comment.