0

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()

1 Answers1

0
RuntimeWarning: invalid value encountered in log2 entropy = -p_buy * np.log2(p_buy) - p_sell * np.log2(p_sell)

You are getting this error because values passed to log scale are in negative. p_buy = -552.6374.

Place a check to modify values to be greater than 0 before passing them to log scale.

  • How would I do that exactly? Thank you for answering my question! – Tayrohn Jackson Dec 17 '22 at 04:07
  • @TayrohnJackson, while debugging I got p_buy value in negative. So first rule of thumb, log scales don't accept negative values, it should be > 0. To avoid this consider `abs(p_buy)` and `abs(p_sell)`, I can't be sure if it satisfies your requirement. – Immature trader Dec 17 '22 at 04:11
  • I believe you solved my negative value problem, however, I think it highlighted another problem. I am getting a syntax error for the entropy dataframe – Tayrohn Jackson Dec 17 '22 at 04:17
  • Can you post the error? – Immature trader Dec 17 '22 at 04:18
  • Input In [14] df["entropy"] = entropy ^ SyntaxError: invalid syntax – Tayrohn Jackson Dec 17 '22 at 04:21
  • df is a dataframe, you are assigning a float value to df column. Logic is not correct, check this for reference: https://stackoverflow.com/questions/49685591/how-to-find-the-entropy-of-each-column-of-data-set-by-python – Immature trader Dec 17 '22 at 05:32