def PVGOscreen( ):
PVGO = PV - EPS/r
ratio = PVGO/PV
print (ratio)
if (ratio < 0.6) :
print("stable security")
else:
print("volatile security")
if (K < 0.2) and (K > D) :
print ("Technical Buy")
elif (K > 0.8) and (K < D) :
print ("Technical Sell")
else:
print ("Not Actionable")
print (PVGOscreen( Securities ))
0.829059829059829
volatile security
Technical Buy
我得到的输出是下面的,只是AAPL的预期输出。
<div>
如何获得TSLA和AMZN的输出;请问,如何更改证券列表中每个证券的变量值,并为每个证券的PVGOscreen输出变化,而不是仅获得AAPL?
答案 0 :(得分:1)
首先,正如几位人士在评论中提到的那样,语法for 'AAPL':
不是有效的Python语法。
其次,Python有更好的方法来跟踪上面的数据。使用内置Python,嵌套的dict
对象可以很好地工作:
securities = ['AAPL', 'TSLA' , 'AMZN']
# Initialize "empty" nested `dict`s to better store financial data
securities_data = {sec: {} for sec in securities}
for sec in securities_data:
if sec == 'AAPL':
PV = 120
EPS = 8.31
r = 0.0519
K=.86
D=.88
elif sec == 'TSLA':
PV = 244.73
EPS = -5
r = 0.2
K=.83
D=.90
elif sec == 'AMZN':
PV = 808.33
EPS = 4.46
r = 0.087
K=.86
D=.90
else:
PV = None
EPS = None
r = None
K = None
D = None
# Nested dictionary to better keep track of each security's data
securities_data[sec]['PV'] = PV
securities_data[sec]['EPS'] = EPS
securities_data[sec]['r'] = r
securities_data[sec]['K'] = K
securities_data[sec]['D'] = D
有更好的方法来完成这项工作,但我希望这个例子能说明你的代码可以轻松地从程序员的角度来调整你的生活。
pandas
是另一个非常适合此类问题的软件包,但是不附带基础Python。
第三,将代码名称传递给PVGOscreen
函数将使您的代码更加灵活,因为您可以使用上面的嵌套字典动态引用每个故障单的不同值,例如: PV
和EPS
:
def PVGOscreen(sec):
PV = securities_data[sec]['PV']
EPS = securities_data[sec]['EPS']
r = securities_data[sec]['r']
PVGO = PV - EPS/r
ratio = PVGO/PV
print(ratio)
if ratio < 0.6:
print("stable security")
else:
print("volatile security")
if K < 0.2 and K > D:
print("Technical Buy")
elif K > 0.8 and K < D:
print("Technical Sell")
else:
print("Not Actionable")
立即测试你的功能:
>>> PVGOscreen('AAPL')
-0.33429672447013487
stable security
Technical Sell
如果你想要每个代码的输出,你可以试试这个:
>>> for s in securities:
... print(s)
... PVGOscreen(s)
...
AAPL
-0.33429672447013487
stable security
Technical Sell
TSLA
1.1021533935357333
volatile security
Technical Sell
AMZN
0.9365799020003068
volatile security
Technical Sell