基金比较上机题
- 现有两只基金:A. 某医药基金 vs B. 某医疗基金
(1)将它们包含的各支股票的信息存储起来。
(2)按市盈率从小到大分别打印出A和B的各个股票。
(3)按持仓金额占比从大到小分别打印出A和B的各个股票。
(4)分别计算出A和B的平均市盈率。
医药基金A
股票代码, 股票名称, 股价(元), 持股数(万股), 市盈率
002399, 海普瑞, 17.23, 10.79, 1442.22
600085, 同仁堂, 33.35, 8.26, 38.13
601607, 上海医药, 25.13, 7.04, 17.55
医疗基金B
股票代码, 股票名称, 股价(元), 持股数(万股), 市盈率
002044, 美年健康, 18.52, 290.00, 786.19
300003, 乐普医疗, 22.58, 184.53, 40.61
002223, 鱼跃医疗, 21.79, 86.53, 31.06
市盈率(P/E)是一个公司的股价与每股净利润的比值,是评估股价水平是否合理的指标之一。一般认为,如果一家公司股票的市盈率过高,那么该股票的价格具有“泡沫” ,价值被高估。一只基金是一揽子股票。在选择基金时,基金的平均市盈率是考虑因素之一。
股价=公司的当日总市值/发行股票数
每股净利润=公司在该年度的净利润/发行股票数。
市盈率的倒数反映了投资回报率(类比存款利息)。-
平均市盈率
某基金里有n只股票,第i只股票的持仓金额占比为q,对应市盈率为e,则平均市盈率为:
funda1={}
funda2={}
funda3={}
funda1["code"] = "002399"
funda1["name"] = "海普瑞"
funda1["price"] = 17.23
funda1["number"] = 10.79
funda1["PE"] = 1442.22
funda2["code"] = "600085"
funda2["name"] = "同仁堂"
funda2["price"] = 33.35
funda2["number"] = 8.26
funda2["PE"] = 38.13
funda3["code"] = "601607"
funda3["name"] = "上海医药"
funda3["price"] = 25.13
funda3["number"] = 7.04
funda3["PE"] = 17.55
funda = [funda1,funda2,funda3]
fundb1={}
fundb2={}
fundb3={}
fundb1["code"] = "002044"
fundb1["name"] = "美年健康"
fundb1["price"] = 18.52
fundb1["number"] = 290.00
fundb1["PE"] = 786.19
fundb2["code"] = "300003"
fundb2["name"] = "乐普医疗"
fundb2["price"] = 22.58
fundb2["number"] = 184.53
fundb2["PE"] = 40.61
fundb3["code"] = "002223"
fundb3["name"] = "鱼跃医疗"
fundb3["price"] = 21.79
fundb3["number"] = 86.53
fundb3["PE"] = 31.06
fundb = [fundb1,fundb2,fundb3]
print(sorted(funda,key = lambda e:e["PE"]))
print(sorted(fundb,key = lambda e:e["PE"]))
market_value_total_a = 0
for fund in funda:
market_value = fund["number"]*fund["price"]
market_value_total_a = market_value_total_a+market_value
for fund in funda:
market_value = fund["number"]*fund["price"]
percentage = market_value/market_value_total_a
fund["percentage"] = percentage
print(sorted(funda,key = lambda e:e["percentage"]))
market_value_total_b = 0
for fund in fundb:
market_value = fund["number"]*fund["price"]
market_value_total_b = market_value_total_b+market_value
for fund in fundb:
market_value = fund["number"]*fund["price"]
percentage = market_value/market_value_total_b
fund["percentage"] = percentage
print(sorted(fundb,key = lambda e:e["percentage"]))
sum1 = 0
for fund in funda:
sum1 = sum1+fund["percentage"]/fund["PE"]
PEratio_a = 1/sum1
print(PEratio_a)
sum2 = 0
for fund in fundb:
sum2 = sum2+fund["percentage"]/fund["PE"]
PEratio_b = 1/sum2
print(PEratio_b)