File size: 3,693 Bytes
060ac97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
844dcb4
 
060ac97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
This Python code is designed to help investors make informed decisions on how to distribute their investment
across five AI related stocks that are deemed to be potentially undervalued, based on their Price-to-Earnings (P/E) ratios.
The goal is to allocate more funds to stocks with lower P/E ratios, under the premise that they might offer better value.

The approach includes:
1. Listing a predefined set of stock tickers of interest.
2. Fetching P/E ratios for these stocks using the yfinance library.
3. Sorting these stocks by their P/E ratios from lowest to highest.
4. Selecting the top 5 stocks with the lowest P/E ratios.
5. Calculating how much to invest in each stock in inverse proportion to their P/E ratios.
6. Allocating the investment across these stocks based on the calculated proportions.

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.
"""

# install and import the necessary modules
# pip install -q yfinance
# pip install -q gradio
import yfinance as yf
import pandas as pd
import gradio as gr
import logging

# Suppress messages unless there is an error
logging.basicConfig(level=logging.ERROR)

def invest_in_stocks(investment_amount):
    tickers_list = ['AAPL', 'TSM', 'ADBE', 'CRM', 'AMD', 'AMZN', 'GOOG', 'MSFT', 'NVDA']
    data = []

    for ticker in tickers_list:
        stock = yf.Ticker(ticker)
        pe_ratio = stock.info.get('trailingPE', None)
        data.append({'Ticker': ticker, 'P/E Ratio': pe_ratio})

    df = pd.DataFrame(data).copy()
    df_sorted = df.sort_values(by='P/E Ratio')
    df_top5_lowest_pe = df_sorted.head(5).copy()
    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 $
    total_inverse_pe = df_top5_lowest_pe['Inverse PE'].sum()
    df_top5_lowest_pe['Investment Proportion'] = df_top5_lowest_pe['Inverse PE'] / total_inverse_pe
    df_top5_lowest_pe['Investment %'] = (100 * df_top5_lowest_pe['Investment Proportion']).round(0)  # Round to whole number
    df_top5_lowest_pe['Investment $'] = (investment_amount * df_top5_lowest_pe['Investment Proportion']).round(0)  # Round to whole number

    # Preparing the dataframe for output
    output_df = df_top5_lowest_pe[['Ticker', 'Investment %', 'Investment $']]

    return output_df

# now use gradio to create a user interface, show a welcome message explaining the investment approach and ask for input

welcome_message = """
# Welcome to the AI Portfolio Investment Tool! 🚀

This tool helps you **invest** in a portfolio of potentially undervalued **AI stocks** based on their **Price-to-Earnings (P/E) ratios**.

With this interface, you can:
- View a curated list of stocks.
- Specify the amount you'd like to invest.
- See how your investment would be allocated across the **top 5** selected stocks.

### Current Stock List:
- Apple Inc. (AAPL)
- Taiwan Semiconductor Manufacturing Company (TSM)
- Adobe Inc. (ADBE)
- Salesforce (CRM)
- Advanced Micro Devices (AMD)
- Amazon.com, Inc. (AMZN)
- Alphabet Inc. (GOOG)
- Microsoft Corporation (MSFT)
- NVIDIA Corporation (NVDA)

Enter the amount you'd like to invest below and see how it gets allocated. Let's get started!
"""

with gr.Blocks() as app:
    gr.Markdown(welcome_message)
    investment_input = gr.Number(label="Investment Amount ($)", value=100)
    output_table = gr.Dataframe(headers=["Ticker", "Investment %", "Investment $"], label="Investment Allocation")
    investment_input.change(fn=invest_in_stocks, inputs=investment_input, outputs=output_table)

app.launch(share=True)