QuantumLearner commited on
Commit
22ccaa3
·
verified ·
1 Parent(s): b064236

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -2
app.py CHANGED
@@ -12,8 +12,6 @@
12
  # See the License for the specific language governing permissions and
13
  # limitations under the License.
14
  import spaces
15
-
16
-
17
  import streamlit as st
18
  import requests
19
  import pandas as pd
@@ -108,6 +106,7 @@ def page2():
108
  cik_input = st.text_input("Enter CIK", value="0001067983", help="Enter the Central Index Key (CIK) of the institutional investor.")
109
  start_date = st.date_input("Select Start Date", value=datetime(2021, 9, 30), help="Choose the start date for fetching portfolio allocation data.")
110
  top_n = st.number_input("Top N Groups", min_value=1, max_value=100, value=10, step=1, help="Select the number of top groups to display in trend charts.")
 
111
 
112
  run_button = st.sidebar.button("Run")
113
 
@@ -157,6 +156,11 @@ def page2():
157
  fig4 = plot_allocation_trends(allocation_data, "industryTitle", st.session_state.portfolio_allocation_params.get('top_n'))
158
  st.plotly_chart(fig4, use_container_width=True, height=600)
159
 
 
 
 
 
 
160
  # Transpose data at the bottom
161
  st.markdown("---")
162
  st.subheader("Transposed Ticker Data")
@@ -230,6 +234,65 @@ def page2():
230
  transposed_industry_data = transpose_data(allocation_data, "industryTitle")
231
  st.dataframe(transposed_industry_data, use_container_width=True)
232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  # Functions used in Page 2
234
  @st.cache_data
235
  def fetch_dates(cik):
 
12
  # See the License for the specific language governing permissions and
13
  # limitations under the License.
14
  import spaces
 
 
15
  import streamlit as st
16
  import requests
17
  import pandas as pd
 
106
  cik_input = st.text_input("Enter CIK", value="0001067983", help="Enter the Central Index Key (CIK) of the institutional investor.")
107
  start_date = st.date_input("Select Start Date", value=datetime(2021, 9, 30), help="Choose the start date for fetching portfolio allocation data.")
108
  top_n = st.number_input("Top N Groups", min_value=1, max_value=100, value=10, step=1, help="Select the number of top groups to display in trend charts.")
109
+ top_n_changes = st.number_input("Top N Changes", min_value=1, max_value=100, value=10, step=1, help="Select the number of top changes to display.")
110
 
111
  run_button = st.sidebar.button("Run")
112
 
 
156
  fig4 = plot_allocation_trends(allocation_data, "industryTitle", st.session_state.portfolio_allocation_params.get('top_n'))
157
  st.plotly_chart(fig4, use_container_width=True, height=600)
158
 
159
+ st.subheader("Top Changes in Portfolio Allocation")
160
+ fig_top_changes = plot_top_changes_plotly(allocation_data, n=top_n_changes)
161
+ st.plotly_chart(fig_top_changes, use_container_width=True)
162
+
163
+
164
  # Transpose data at the bottom
165
  st.markdown("---")
166
  st.subheader("Transposed Ticker Data")
 
234
  transposed_industry_data = transpose_data(allocation_data, "industryTitle")
235
  st.dataframe(transposed_industry_data, use_container_width=True)
236
 
237
+ def plot_top_changes_plotly(df, n=10):
238
+ # Get unique dates in descending order
239
+ unique_dates = sorted(df["date"].unique(), reverse=True)
240
+ if len(unique_dates) < 2:
241
+ return go.Figure() # Not enough data
242
+
243
+ latest_date = unique_dates[0]
244
+ prev_date = unique_dates[1]
245
+
246
+ # Filter data for the two most recent dates
247
+ df_latest = df[df["date"] == latest_date]
248
+ df_prev = df[df["date"] == prev_date]
249
+
250
+ # Aggregate data by symbol for each date
251
+ df_latest_agg = df_latest.groupby("symbol", as_index=False).agg({
252
+ "weight": "sum",
253
+ "marketValue": "sum"
254
+ })
255
+ df_prev_agg = df_prev.groupby("symbol", as_index=False).agg({
256
+ "weight": "sum",
257
+ "marketValue": "sum"
258
+ })
259
+
260
+ # Merge data on symbol to include new additions or removals
261
+ merged = pd.merge(df_prev_agg, df_latest_agg, on="symbol",
262
+ suffixes=("_prev", "_latest"), how="outer")
263
+ merged.fillna(0, inplace=True)
264
+
265
+ # Compute changes in weight and market value
266
+ merged['delta_weight'] = merged['weight_latest'] - merged['weight_prev']
267
+ merged['delta_marketValue'] = merged['marketValue_latest'] - merged['marketValue_prev']
268
+
269
+ # Select top n positive changes and top n negative changes
270
+ top_up = merged[merged['delta_weight'] > 0].sort_values(
271
+ by='delta_weight', ascending=False).head(n)
272
+ top_down = merged[merged['delta_weight'] < 0].sort_values(
273
+ by='delta_weight', ascending=True).head(n)
274
+
275
+ combined = pd.concat([top_up, top_down])
276
+ combined['color'] = combined['delta_weight'].apply(lambda x: 'green' if x >= 0 else 'red')
277
+ combined['text'] = combined.apply(
278
+ lambda row: f"{row['delta_weight']:.1f}% ({format_market_value(row['delta_marketValue'])})", axis=1)
279
+
280
+ fig = go.Figure(data=[go.Bar(
281
+ x=combined['symbol'],
282
+ y=combined['delta_weight'],
283
+ text=combined['text'],
284
+ textposition='auto',
285
+ marker_color=combined['color']
286
+ )])
287
+ fig.update_layout(
288
+ title=f"Top Up and Down Changes: {prev_date} to {latest_date}",
289
+ xaxis_title="Symbol",
290
+ yaxis_title="Change in Weight (%)",
291
+ template="plotly_white",
292
+ height=500
293
+ )
294
+ return fig
295
+
296
  # Functions used in Page 2
297
  @st.cache_data
298
  def fetch_dates(cik):