Spaces:
Build error
Build error
Baisub Lee
commited on
Commit
·
b651d9b
1
Parent(s):
10c1e20
first app
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import yfinance as yf
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
def fetch_and_rebalance(etfs, period, rebalance_threshold_percentage):
|
| 6 |
+
etf_data = {etf: yf.Ticker(etf).history(period=period) for etf in etfs}
|
| 7 |
+
for etf in etfs:
|
| 8 |
+
etf_data[etf].index = pd.to_datetime(etf_data[etf].index)
|
| 9 |
+
etf_dividends = {etf: yf.Ticker(etf).dividends for etf in etfs}
|
| 10 |
+
common_dates = etf_data[etfs[0]].index
|
| 11 |
+
for etf in etfs[1:]:
|
| 12 |
+
common_dates = common_dates.intersection(etf_data[etf].index)
|
| 13 |
+
for etf in etfs:
|
| 14 |
+
etf_data[etf] = etf_data[etf].reindex(common_dates)
|
| 15 |
+
etf_dividends[etf] = etf_dividends[etf].reindex(common_dates, fill_value=0)
|
| 16 |
+
initial_investment = 10000
|
| 17 |
+
investments = {etf: initial_investment / len(etfs) for etf in etfs}
|
| 18 |
+
shares = {etf: investments[etf] / etf_data[etf]['Close'].iloc[0] for etf in etfs}
|
| 19 |
+
df_rebalance = pd.DataFrame(columns=["Date"] + [f"{etf} Value" for etf in etfs] + ["Total Value"])
|
| 20 |
+
for date in common_dates[1:]:
|
| 21 |
+
total_value = 0
|
| 22 |
+
for etf in etfs:
|
| 23 |
+
previous_date = etf_data[etf].index[etf_data[etf].index.get_loc(date) - 1]
|
| 24 |
+
investments[etf] *= (etf_data[etf]['Close'].loc[date] / etf_data[etf]['Close'].loc[previous_date])
|
| 25 |
+
investments[etf] += etf_dividends[etf].loc[date] * shares[etf]
|
| 26 |
+
total_value += investments[etf]
|
| 27 |
+
value_difference = abs(investments[etfs[0]] - investments[etfs[1]])
|
| 28 |
+
percentage_difference = value_difference / total_value
|
| 29 |
+
rebalance_needed = percentage_difference > rebalance_threshold_percentage
|
| 30 |
+
if rebalance_needed:
|
| 31 |
+
investments = {etf: total_value / len(etfs) for etf in etfs}
|
| 32 |
+
shares = {etf: investments[etf] / etf_data[etf]['Close'].loc[date] for etf in etfs}
|
| 33 |
+
df_rebalance = pd.concat([
|
| 34 |
+
df_rebalance,
|
| 35 |
+
pd.DataFrame({
|
| 36 |
+
"Date": [date],
|
| 37 |
+
**{f"{etf} Value": [investments[etf]] for etf in etfs},
|
| 38 |
+
"Total Value": [total_value]
|
| 39 |
+
})
|
| 40 |
+
], ignore_index=True)
|
| 41 |
+
if df_rebalance.empty:
|
| 42 |
+
return "No data was appended."
|
| 43 |
+
final_value = df_rebalance["Total Value"].iloc[-1]
|
| 44 |
+
total_return = (final_value - initial_investment) / initial_investment * 100
|
| 45 |
+
return f"Final Value: {final_value}, Total Return: {total_return}%"
|
| 46 |
+
|
| 47 |
+
interface = gr.Interface(
|
| 48 |
+
fn=fetch_and_rebalance,
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.inputs.Textbox(lines=1, placeholder="ETFs (comma separated)", default="TQQQ,JEPI"),
|
| 51 |
+
gr.inputs.Textbox(lines=1, placeholder="Period", default="ytd"),
|
| 52 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.10, label="Rebalance Threshold Percentage")
|
| 53 |
+
],
|
| 54 |
+
outputs="text"
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
interface.launch()
|
| 58 |
+
|