尝试在相同函数 运行 时更改不同证券的变量值
Attempting to change variable values for different securities when same function is run
下面的列表定义了要在下面的 PVGO 屏幕中评估的证券
Securities = [ 'AAPL' , 'TSLA' , 'AMZN' ]
for 'AAPL':
PV = 120
EPS = 8.31
r = 0.0519
K=.86
D=.88
for 'TSLA':
PV = 244.73
EPS = -5
r = 0.2
K=.83
D=.90
for 'AMZN':
PV = 808.33
EPS = 4.46
r = 0.087
K=.86
D=.90
以下函数定义了基本筛选器将如何评估每种证券
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 ))
我得到的输出如下所示,并且只是 AAPL 的预期输出。
0.829059829059829
volatile security
Technical Buy
如何获得 TSLA 和 AMZN 的输出;这就是问,我如何更改证券列表中每个证券的变量值,并为每个证券更改 PVGOscreen 输出,而不是只获取 AAPL?
首先,正如一些人在评论中提到的,for 'AAPL':
语法无效 Python 语法。
其次,Python 中有很多更好的方法来跟踪您上面的数据。使用 built-in 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
是另一个非常适合 well-suited 解决此类问题的软件包,但它不包含基本 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
下面的列表定义了要在下面的 PVGO 屏幕中评估的证券
Securities = [ 'AAPL' , 'TSLA' , 'AMZN' ]
for 'AAPL':
PV = 120
EPS = 8.31
r = 0.0519
K=.86
D=.88
for 'TSLA':
PV = 244.73
EPS = -5
r = 0.2
K=.83
D=.90
for 'AMZN':
PV = 808.33
EPS = 4.46
r = 0.087
K=.86
D=.90
以下函数定义了基本筛选器将如何评估每种证券
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 ))
我得到的输出如下所示,并且只是 AAPL 的预期输出。
0.829059829059829
volatile security
Technical Buy
如何获得 TSLA 和 AMZN 的输出;这就是问,我如何更改证券列表中每个证券的变量值,并为每个证券更改 PVGOscreen 输出,而不是只获取 AAPL?
首先,正如一些人在评论中提到的,for 'AAPL':
语法无效 Python 语法。
其次,Python 中有很多更好的方法来跟踪您上面的数据。使用 built-in 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
是另一个非常适合 well-suited 解决此类问题的软件包,但它不包含基本 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