cryman38 commited on
Commit
5e2fce2
ยท
verified ยท
1 Parent(s): cef28d7

Upload 16 files

Browse files
interface/rebalancing_interface.py CHANGED
@@ -3,8 +3,7 @@ from modules.rebalancing import rebalancing_tool
3
  from modules.utils import currency_codes, current_time
4
 
5
  examples = [
6
- ["KRW", "458730 KRW 580 [8],\n368590 KRW 80 [2],", 507977],
7
- ["KRW", "SCHD USD 500 [8],\nQQQ USD 200 [2],\n458730 KRW 580 [8],\n368590 KRW 80 [2],", 10000000]
8
  ]
9
 
10
  # Define Gradio components
@@ -20,12 +19,12 @@ holdings = gr.Textbox(
20
  lines=2,
21
  # info="Format: Ticker Currency Quantity [Target_Weight], ...",
22
  placeholder="Format: Ticker Currency Quantity [Target_Ratio], ...",
23
- value="SCHD USD 8.160864 [3], \n458730 KRW 580 [4], \n368590 KRW 80 [1],"
24
  )
25
  title_cash = gr.Markdown("<h3 class='h3_title'>Contribution or withdrawal</h3>")
26
  cash_amount = gr.Number(
27
  label="Contribution or withdrawal amount",
28
- value=507977
29
  )
30
  allow_sales = gr.Checkbox(
31
  value=True,
 
3
  from modules.utils import currency_codes, current_time
4
 
5
  examples = [
6
+ ["KRW", "458730 KRW 596 [8],\n368590 KRW 95 [2],", 0],
 
7
  ]
8
 
9
  # Define Gradio components
 
19
  lines=2,
20
  # info="Format: Ticker Currency Quantity [Target_Weight], ...",
21
  placeholder="Format: Ticker Currency Quantity [Target_Ratio], ...",
22
+ value="AAPL USD 500 [20], \nMSFT USD 300 [15], \nGOOGL USD 400 [10], \nAMZN USD 50 [5], \nTSLA USD 200 [10], \nNVDA USD 250 [10], \nNFLX USD 150 [10], \nQQQ USD 450 [5], \nADBE USD 100 [10], \nPYPL USD 50 [5]"
23
  )
24
  title_cash = gr.Markdown("<h3 class='h3_title'>Contribution or withdrawal</h3>")
25
  cash_amount = gr.Number(
26
  label="Contribution or withdrawal amount",
27
+ value=6693188
28
  )
29
  allow_sales = gr.Checkbox(
30
  value=True,
modules/rebalancing.py CHANGED
@@ -82,56 +82,43 @@ def build_portfolio(stock_inputs, main_currency):
82
 
83
  return portfolio, target_ratios, total_value
84
 
85
- # def generate_portfolio_info(portfolio, total_value, main_currency):
86
- # css = load_css()
87
- # currency_symbol = get_currency_symbol(main_currency)
88
 
89
- # # ๋ณด์œ  ์ข…๋ชฉ๋ณ„ ์ด์•ก์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
90
- # holdings_totals = {
91
- # stock_code: {
92
- # 'value': stock['price'] * stock['quantity'],
93
- # 'weight': (stock['price'] * stock['quantity'] / total_value)
94
- # }
95
- # for stock_code, stock in portfolio.items()
96
- # }
97
 
98
- # # ํ†ตํ™”๋ณ„ ์ด์•ก์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
99
- # currency_totals = defaultdict(lambda: {'value': 0, 'weight': 0})
100
- # for stock in portfolio.values():
101
- # currency = stock['currency']
102
- # value = stock['price'] * stock['quantity']
103
- # currency_totals[currency]['value'] += value
104
- # currency_totals[currency]['weight'] += value / total_value
105
 
106
- # # ํ˜„์žฌ ๋น„์ค‘์„ ์‚ฌ์šฉํ•˜์—ฌ ํฌํŠธํด๋ฆฌ์˜ค ํŠธ๋ฆฌ๋งต ์ฐจํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
107
- # currunt_weights = {stock_code: details['weight'] for stock_code, details in holdings_totals.items()}
108
- # currunt_weights_chart = plot_donut_chart(currunt_weights)
109
 
110
- # currency_weights = {currency: details['weight'] for currency, details in currency_totals.items()}
111
- # currency_weights_chart = plot_donut_chart(currency_weights)
112
 
113
- # # HTML ์ƒ์„ฑ
114
- # portfolio_info = css + f"""
115
- # <div class="wrap-text">
116
- # <h3>Your Portfolio by Currency</h3>
117
- # {currency_weights_chart}
118
- # <br>
119
- # <div class='table-container wrap-text'>
120
- # <table>
121
- # <thead>
122
- # <tr><th>Currency</th><th>Total Weight (%)</th><th>Total Value</th></tr>
123
- # </thead>
124
- # <tbody>
125
- # {''.join(
126
- # f"<tr><td>{currency.upper()}</td><td>{details['weight']* 100:.1f}%</td><td>{currency_symbol}{format_value(details['value'])}</td></tr>"
127
- # for currency, details in currency_totals.items()
128
- # )}
129
- # </tbody>
130
- # </table>
131
- # </div>
132
- # <br>
133
- # </div>
134
- # """
135
  # <div class='table-container wrap-text'>
136
  # <table>
137
  # <thead>
@@ -145,8 +132,25 @@ def build_portfolio(stock_inputs, main_currency):
145
  # </tbody>
146
  # </table>
147
  # </div>
148
- # <br>
149
- # return portfolio_info
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
  def generate_rebalancing_analysis(portfolio, target_ratios, total_value, main_currency, cash_amount, allow_selling):
152
  css = load_css()
@@ -210,26 +214,12 @@ def generate_rebalancing_analysis(portfolio, target_ratios, total_value, main_cu
210
  'new_value_pct': new_value / new_total_value
211
  })
212
 
213
- # # ์‹ ๊ทœ ํ†ตํ™”๋ณ„ ์ด์•ก์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
214
- # currency_new_totals = defaultdict(lambda: {'new_value': 0, 'new_value_pct': 0})
215
- # for adj in adjustments:
216
- # currency = adj['currency']
217
- # value = adj['new_value']
218
- # currency_new_totals[currency]['new_value'] += value
219
- # currency_new_totals[currency]['new_value_pct'] += value / new_total_value
220
-
221
- # # currency_new_totals๋ฅผ new_value์— ๋”ฐ๋ผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
222
- # sorted_currency_new_totals = sorted(currency_new_totals.items(), key=lambda item: item[1]['new_value'], reverse=True)
223
-
224
- # # ์‹ ๊ทœ ๋„๋„› ์ฐจํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
225
- # currency_new_weights_chart = plot_donut_chart({currency: details['new_value_pct'] for currency, details in sorted_currency_new_totals})
226
-
227
  # HTML ์ƒ์„ฑ
228
  rebalancing_analysis = css + f"""
229
  <div class="wrap-text">
230
  <div style="margin-bottom: 1.5rem;">
231
  <div style="font-size: 1.5rem; margin-top: 1.5rem; margin-bottom: 1.5rem;">Re-Balancing Analysis | Your Portfolio Holdings as of {current_time}</div>
232
- <span style='font-size: 1.5rem; font-weight: bold; color: #1678fb'>{currency_symbol}{format_value(sum(adj['new_value'] for adj in adjustments))}</span>
233
  (After Trades)
234
  </div>
235
  <div class='table-container wrap-text'>
@@ -251,7 +241,7 @@ def generate_rebalancing_analysis(portfolio, target_ratios, total_value, main_cu
251
  <th>Trade Amount - {main_currency} {currency_symbol}</th>
252
  <th>Current Price per Share - {main_currency} {currency_symbol}</th>
253
  <th>Estimated # of Shares to Buy or Sell</th>
254
- <th>Quantity</th>
255
  <th class="header-bg-after">Total Value - {main_currency} {currency_symbol}</th>
256
  <th class="header-bg-after">% Asset Allocation</th>
257
  </tr>
@@ -293,32 +283,16 @@ def generate_rebalancing_analysis(portfolio, target_ratios, total_value, main_cu
293
 
294
  </div>
295
  """
296
- # <h3>Your Portfolio by Currency (After Trades)</h3>
297
- # {currency_new_weights_chart}
298
- # <br>
299
- # <div class='table-container wrap-text'>
300
- # <table>
301
- # <thead>
302
- # <tr><th>Currency</th><th>Total Weight (%)</th><th>Total Value</th></tr>
303
- # </thead>
304
- # <tbody>
305
- # {''.join(
306
- # f"<tr><td>{currency.upper()}</td><td>{details['new_value_pct'] * 100:.1f}%</td><td>{currency_symbol}{format_value(details['new_value'])}</td></tr>"
307
- # for currency, details in sorted_currency_new_totals
308
- # )}
309
- # </tbody>
310
- # </table>
311
- # </div>
312
- # <br>
313
  return rebalancing_analysis
314
 
315
  def rebalancing_tool(main_currency, holdings, cash_amount, allow_selling):
316
  try:
317
  stock_inputs, cash_amount = parse_input(holdings, cash_amount)
318
  portfolio, target_ratios, total_value = build_portfolio(stock_inputs, main_currency)
319
- # portfolio_info = generate_portfolio_info(portfolio, total_value, main_currency)
320
  rebalancing_analysis = generate_rebalancing_analysis(portfolio, target_ratios, total_value, main_currency, cash_amount, allow_selling)
321
 
322
- return rebalancing_analysis
323
  except Exception as e:
324
  return f"<p style='color: red;'>An error occurred: {e}</p>"
 
82
 
83
  return portfolio, target_ratios, total_value
84
 
85
+ def generate_portfolio_info(portfolio, total_value, main_currency):
86
+ css = load_css()
87
+ currency_symbol = get_currency_symbol(main_currency)
88
 
89
+ # ๋ณด์œ  ์ข…๋ชฉ๋ณ„ ์ด์•ก์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
90
+ holdings_totals = {
91
+ stock_code: {
92
+ 'value': stock['price'] * stock['quantity'],
93
+ 'weight': (stock['price'] * stock['quantity'] / total_value)
94
+ }
95
+ for stock_code, stock in portfolio.items()
96
+ }
97
 
98
+ # ํ†ตํ™”๋ณ„ ์ด์•ก์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
99
+ currency_totals = defaultdict(lambda: {'value': 0, 'weight': 0})
100
+ for stock in portfolio.values():
101
+ currency = stock['currency']
102
+ value = stock['price'] * stock['quantity']
103
+ currency_totals[currency]['value'] += value
104
+ currency_totals[currency]['weight'] += value / total_value
105
 
106
+ # ํ˜„์žฌ ๋น„์ค‘์„ ์‚ฌ์šฉํ•˜์—ฌ ํฌํŠธํด๋ฆฌ์˜ค ํŠธ๋ฆฌ๋งต ์ฐจํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
107
+ currunt_weights = {stock_code: details['weight'] for stock_code, details in holdings_totals.items()}
108
+ currunt_weights_chart = plot_donut_chart(currunt_weights)
109
 
110
+ currency_weights = {currency: details['weight'] for currency, details in currency_totals.items()}
111
+ currency_weights_chart = plot_donut_chart(currency_weights)
112
 
113
+ # HTML ์ƒ์„ฑ
114
+ portfolio_info = css + f"""
115
+ <div class="wrap-text">
116
+ <h3>Your Portfolio Holdings</h3>
117
+ {currunt_weights_chart}
118
+ <br>
119
+ </div>
120
+ """
121
+ # <br>
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  # <div class='table-container wrap-text'>
123
  # <table>
124
  # <thead>
 
132
  # </tbody>
133
  # </table>
134
  # </div>
135
+
136
+ # <br>
137
+ # <h3>Your Portfolio by Currency</h3>
138
+ # {currency_weights_chart}
139
+ # <br>
140
+ # <div class='table-container wrap-text'>
141
+ # <table>
142
+ # <thead>
143
+ # <tr><th>Currency</th><th>Total Weight (%)</th><th>Total Value</th></tr>
144
+ # </thead>
145
+ # <tbody>
146
+ # {''.join(
147
+ # f"<tr><td>{currency.upper()}</td><td>{details['weight']* 100:.1f}%</td><td>{currency_symbol}{format_value(details['value'])}</td></tr>"
148
+ # for currency, details in currency_totals.items()
149
+ # )}
150
+ # </tbody>
151
+ # </table>
152
+ # </div>
153
+ return portfolio_info
154
 
155
  def generate_rebalancing_analysis(portfolio, target_ratios, total_value, main_currency, cash_amount, allow_selling):
156
  css = load_css()
 
214
  'new_value_pct': new_value / new_total_value
215
  })
216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  # HTML ์ƒ์„ฑ
218
  rebalancing_analysis = css + f"""
219
  <div class="wrap-text">
220
  <div style="margin-bottom: 1.5rem;">
221
  <div style="font-size: 1.5rem; margin-top: 1.5rem; margin-bottom: 1.5rem;">Re-Balancing Analysis | Your Portfolio Holdings as of {current_time}</div>
222
+ <span style='font-size: 1.5rem; font-weight: bold; color: #1678fb'>{currency_symbol}{format_value(sum(adj['new_value'] for adj in adjustments))} </span>
223
  (After Trades)
224
  </div>
225
  <div class='table-container wrap-text'>
 
241
  <th>Trade Amount - {main_currency} {currency_symbol}</th>
242
  <th>Current Price per Share - {main_currency} {currency_symbol}</th>
243
  <th>Estimated # of Shares to Buy or Sell</th>
244
+ <th>Shares Before and After</th>
245
  <th class="header-bg-after">Total Value - {main_currency} {currency_symbol}</th>
246
  <th class="header-bg-after">% Asset Allocation</th>
247
  </tr>
 
283
 
284
  </div>
285
  """
286
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
  return rebalancing_analysis
288
 
289
  def rebalancing_tool(main_currency, holdings, cash_amount, allow_selling):
290
  try:
291
  stock_inputs, cash_amount = parse_input(holdings, cash_amount)
292
  portfolio, target_ratios, total_value = build_portfolio(stock_inputs, main_currency)
293
+ portfolio_info = generate_portfolio_info(portfolio, total_value, main_currency)
294
  rebalancing_analysis = generate_rebalancing_analysis(portfolio, target_ratios, total_value, main_currency, cash_amount, allow_selling)
295
 
296
+ return portfolio_info + rebalancing_analysis
297
  except Exception as e:
298
  return f"<p style='color: red;'>An error occurred: {e}</p>"
modules/utils.py CHANGED
@@ -98,7 +98,7 @@ def create_tab(tab_name, inputs, outputs, update_fn, examples, component_rows):
98
  render_components(component_rows)
99
  clear_buttons(inputs)
100
  submit_buttons(inputs, update_fn, outputs)
101
- # gr.Examples(examples=examples, cache_examples=False, inputs=inputs)
102
  with gr.Column():
103
  outputs.render()
104
  on_change(inputs, update_fn, outputs)
 
98
  render_components(component_rows)
99
  clear_buttons(inputs)
100
  submit_buttons(inputs, update_fn, outputs)
101
+ gr.Examples(examples=examples, cache_examples=False, inputs=inputs)
102
  with gr.Column():
103
  outputs.render()
104
  on_change(inputs, update_fn, outputs)