Huggingfacelearner123 commited on
Commit
060ac97
·
verified ·
1 Parent(s): e4caf4d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This Python code is designed to help investors make informed decisions on how to distribute their investment
3
+ across five AI related stocks that are deemed to be potentially undervalued, based on their Price-to-Earnings (P/E) ratios.
4
+ The goal is to allocate more funds to stocks with lower P/E ratios, under the premise that they might offer better value.
5
+
6
+ The approach includes:
7
+ 1. Listing a predefined set of stock tickers of interest.
8
+ 2. Fetching P/E ratios for these stocks using the yfinance library.
9
+ 3. Sorting these stocks by their P/E ratios from lowest to highest.
10
+ 4. Selecting the top 5 stocks with the lowest P/E ratios.
11
+ 5. Calculating how much to invest in each stock in inverse proportion to their P/E ratios.
12
+ 6. Allocating the investment across these stocks based on the calculated proportions.
13
+
14
+ The method prioritizes stocks with lower P/E ratios, intending to maximize the value of the investment by focusing on stocks that are potentially undervalued.
15
+ """
16
+
17
+ # install and import the necessary modules
18
+ !pip install -q yfinance
19
+ !pip install -q gradio
20
+ import yfinance as yf
21
+ import pandas as pd
22
+ import gradio as gr
23
+ import logging
24
+
25
+ # Suppress messages unless there is an error
26
+ logging.basicConfig(level=logging.ERROR)
27
+
28
+ def invest_in_stocks(investment_amount):
29
+ tickers_list = ['AAPL', 'TSM', 'ADBE', 'CRM', 'AMD', 'AMZN', 'GOOG', 'MSFT', 'NVDA']
30
+ data = []
31
+
32
+ for ticker in tickers_list:
33
+ stock = yf.Ticker(ticker)
34
+ pe_ratio = stock.info.get('trailingPE', None)
35
+ data.append({'Ticker': ticker, 'P/E Ratio': pe_ratio})
36
+
37
+ df = pd.DataFrame(data).copy()
38
+ df_sorted = df.sort_values(by='P/E Ratio')
39
+ df_top5_lowest_pe = df_sorted.head(5).copy()
40
+ df_top5_lowest_pe['Inverse PE'] = 1 / df_top5_lowest_pe['P/E Ratio'] # invest in inverse proportion to the individual stock p/e; the cheaper stock gets more $
41
+ total_inverse_pe = df_top5_lowest_pe['Inverse PE'].sum()
42
+ df_top5_lowest_pe['Investment Proportion'] = df_top5_lowest_pe['Inverse PE'] / total_inverse_pe
43
+ df_top5_lowest_pe['Investment %'] = (100 * df_top5_lowest_pe['Investment Proportion']).round(0) # Round to whole number
44
+ df_top5_lowest_pe['Investment $'] = (investment_amount * df_top5_lowest_pe['Investment Proportion']).round(0) # Round to whole number
45
+
46
+ # Preparing the dataframe for output
47
+ output_df = df_top5_lowest_pe[['Ticker', 'Investment %', 'Investment $']]
48
+
49
+ return output_df
50
+
51
+ # now use gradio to create a user interface, show a welcome message explaining the investment approach and ask for input
52
+
53
+ welcome_message = """
54
+ # Welcome to the AI Portfolio Investment Tool! 🚀
55
+
56
+ This tool helps you **invest** in a portfolio of potentially undervalued **AI stocks** based on their **Price-to-Earnings (P/E) ratios**.
57
+
58
+ With this interface, you can:
59
+ - View a curated list of stocks.
60
+ - Specify the amount you'd like to invest.
61
+ - See how your investment would be allocated across the **top 5** selected stocks.
62
+
63
+ ### Current Stock List:
64
+ - Apple Inc. (AAPL)
65
+ - Taiwan Semiconductor Manufacturing Company (TSM)
66
+ - Adobe Inc. (ADBE)
67
+ - Salesforce (CRM)
68
+ - Advanced Micro Devices (AMD)
69
+ - Amazon.com, Inc. (AMZN)
70
+ - Alphabet Inc. (GOOG)
71
+ - Microsoft Corporation (MSFT)
72
+ - NVIDIA Corporation (NVDA)
73
+
74
+ Enter the amount you'd like to invest below and see how it gets allocated. Let's get started!
75
+ """
76
+
77
+ with gr.Blocks() as app:
78
+ gr.Markdown(welcome_message)
79
+ investment_input = gr.Number(label="Investment Amount ($)", value=100)
80
+ output_table = gr.Dataframe(headers=["Ticker", "Investment %", "Investment $"], label="Investment Allocation")
81
+ investment_input.change(fn=invest_in_stocks, inputs=investment_input, outputs=output_table)
82
+
83
+ app.launch(share=True)