There is a runtime warning for my entropy function in my script. I don't understand why it is running this error.
I thought I forgot to import the numpy library. However, when I run the script, it says that the issue is with the log base 2. I don't really know how to fix this. Any tips?
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# Retrieve the stock data
ticker = yf.Ticker("ES=F")
df = ticker.history(interval="2m", period="60d")
# Calculate the order flow imbalance
df["order_flow_imbalance"] = df["Volume"]
df.loc[df["Close"] > df["Open"], "order_flow_imbalance"] *= -1
# Calculate the entropy
total_orders = df["order_flow_imbalance"].sum()
p_buy = df.loc[df["order_flow_imbalance"] > 0,
"order_flow_imbalance"].sum() / total_orders
p_sell = df.loc[df["order_flow_imbalance"] < 0,
"order_flow_imbalance"].sum() / total_orders
entropy = -p_buy * np.log2(p_buy) - p_sell * np.log2(p_sell)
df["entropy"] = entropy
# Calculate the returns
df["returns"] = (df["Close"] - df["Open"]) / df["Open"]
# Filter the data
df = df[df["order_flow_imbalance"] > 100000]
# Plot the data
sns.scatterplot(x="returns", y="entropy", data=df)
sns.regplot(x="returns", y="entropy", data=df)
plt.show()