简体   繁体   中英

Python: How to use the sum function with variables defined from a user input with yfinance?

I am learning python and would like to practice some basic financial analysis. How can I sum the values of the PEratio variables that come from a list built from input?

import yfinance as yf

#user input
list = input("Enter ticker(s): ")

#building a list
ticker_list = list.split(", ")
all_symbols  = " ".join(list)
all_symbols  = " ".join(ticker_list)
tickers = yf.Tickers(all_symbols)

#calling data
for ticker in ticker_list:
    price = tickers.tickers[ticker].info["currentPrice"]
    market_cap = tickers.tickers[ticker].info["marketCap"]
    PEratio = tickers.tickers[ticker].info["trailingPE"]
    FWPEratio = tickers.tickers[ticker].info["forwardPE"]
    print(ticker,"\nMarket cap:", market_cap,"\nShare Price:", price, "\nTrailingPE:", PEratio, "\nForward PE:", FWPEratio)

    
   #analysis: I would like to sum all the values for the PEratios and divide them by the list size to compute the average
  

    print(sum(PEratio["trailingPE"])/float(len(ticker_list))) #This isnt correct but is my thought process

EDIT: To clarify, the error I am receiving is the final line of code:

 Traceback (most recent call last):
  File "C:\Users\Sean\OneDrive\Programming\yfinance\multiples\test.py", line 24, in <module>
    print(sum(PEratio["trailingPE"])/float(len(ticker_list))) #This isnt correct but is my thought process
TypeError: 'float' object is not subscriptable

Additionally, it tries to print the average underneath each ticker individually, and not as a sum but as the individual PE ratio divided by the list size

The error that you are receiving is because you are trying to subscript an item that doesn't have the item that you are looking for in your sum()

This is because you are doing ["trailingPE"] twice, first when you are setting PEratio and then a second time in the sum() calculation. Therefore to fix the issue that you have you should instead just do.

print(sum(PEratio)/float(len(ticker_list)))

Assuming that PEratio only points toward a single ratio and not the full list:

However, you want to sum() up all of the PEratios for the entire group of stock tickers which you would likely not be accomplished with this statement. If you were looking to average out all of the PEratios you would likely have to do something like this:

PEratios = [] # to store the PEratios for each company
for ticker in ticker_list:
    price = tickers.tickers[ticker].info["currentPrice"]
    market_cap = tickers.tickers[ticker].info["marketCap"]
    PEratio = tickers.tickers[ticker].info["trailingPE"]
    PEratios.append(PEratio) # adding the PEratios to the list
    FWPEratio = tickers.tickers[ticker].info["forwardPE"]
    print(ticker,"\nMarket cap:", market_cap,"\nShare Price:", price, "\nTrailingPE:", PEratio, "\nForward PE:", FWPEratio)      

print(sum(PEratios)/float(len(ticker_list))) # using the same formula with the list object `PEratios` just outside of the for loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM