Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SPY % heatmap #565

Closed
StephanAkkerman opened this issue May 25, 2024 · 4 comments
Closed

Add SPY % heatmap #565

StephanAkkerman opened this issue May 25, 2024 · 4 comments
Labels
Difficulty: Medium 😐 This issue can be solved, but a decent amount of lines need to be changed New feature ⭐ New feature or request Priority: Medium 🥈 Assign this label if this issue is used around once a day

Comments

@StephanAkkerman
Copy link
Owner

image
https://phx.unusualwhales.com/api/etf/SPY/heatmap?date_range=one_day can get the data to create a heatmap of the SPY

Also make this a separate repo

@StephanAkkerman StephanAkkerman added Priority: Medium 🥈 Assign this label if this issue is used around once a day New feature ⭐ New feature or request Difficulty: Medium 😐 This issue can be solved, but a decent amount of lines need to be changed labels May 25, 2024
@StephanAkkerman
Copy link
Owner Author

This is also interesting to add as a separate repo

@StephanAkkerman
Copy link
Owner Author

@StephanAkkerman
Copy link
Owner Author

Code is almost ready:

import pandas as pd
import plotly.express as px
import requests


def get_df():
    data = requests.get(
        "https://phx.unusualwhales.com/api/etf/SPY/heatmap?date_range=one_day",
        headers={
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36"
        },
    ).json()

    # Create DataFrame
    df = pd.DataFrame(data["data"])

    # Convert relevant columns to numeric types
    df["call_premium"] = pd.to_numeric(df["call_premium"])
    df["close"] = pd.to_numeric(df["close"])
    df["high"] = pd.to_numeric(df["high"])
    df["low"] = pd.to_numeric(df["low"])
    df["marketcap"] = pd.to_numeric(df["marketcap"])
    df["open"] = pd.to_numeric(df["open"])
    df["prev_close"] = pd.to_numeric(df["prev_close"])
    df["put_premium"] = pd.to_numeric(df["put_premium"])

    # Add change column
    df["percentage_change"] = (df["close"] - df["prev_close"]) / df["prev_close"] * 100

    # Drop rows where the marketcap == 0
    df = df[df["marketcap"] > 0]

    return df


def create_treemap(df):

    # Custom color scale
    color_scale = [
        (0, "red"),  # Bright red at -5%
        (0.5, "grey"),  # Grey around 0%
        (1, "green"),  # Bright green at 5%
    ]

    # Generate the treemap
    fig = px.treemap(
        df,
        path=[px.Constant("Sectors"), "sector", "industry", "ticker"],
        values="marketcap",
        color="percentage_change",
        hover_data=["percentage_change", "ticker", "marketcap"],
        color_continuous_scale=color_scale,
        range_color=(-5, 5),
        color_continuous_midpoint=0,
    )

    fig.update_layout(
        margin=dict(t=30, l=10, r=10, b=10), font_size=20, coloraxis_colorbar=None
    )

    fig.data[0].texttemplate = "%{customdata[1]}<br>%{customdata[0]:.2f}%"
    fig.update_traces(textposition="middle center")

    # Disable the color bar
    fig.update(layout_coloraxis_showscale=False)

    # Save the figure as an image
    fig.write_image(file="image.png", format="png", width=1920, height=1080)

    # Plot the figure
    fig.show()


create_treemap(get_df())

@StephanAkkerman
Copy link
Owner Author

StephanAkkerman added a commit that referenced this issue Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Medium 😐 This issue can be solved, but a decent amount of lines need to be changed New feature ⭐ New feature or request Priority: Medium 🥈 Assign this label if this issue is used around once a day
Projects
None yet
Development

No branches or pull requests

1 participant