我设计一个工具,将帮助我管理我的股票投资组合的风险。我有一些代码可以收集来自雅虎财经的4个股票,2个多头和2个空头的当前价格数据。这是使用雅虎金融工具。如何处理来自雅虎财经报价阅读器的打印输出?
我可以收集数据,但我不知道如何相互分割的价格来回报的传播价值(stockA/stockB为相对值贸易)
#always helpful to show the version:
(env) LaurencsonsiMac:~ admin$ python
Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
from yahoo_finance import Share
>>> L1 = Share('YHOO')
>>> L2 = Share('GOOG')
>>> S1 = Share('AAPL')
>>> S2 = Share('MSFT')
>>> print L1.get_price()
50.55
>>> print S1.get_price()
154.45
小胜利!我可以获取价格:)但 我不知道如何将这种输出操作作为一个对象,并定义为Long1“无论是打印L1.get_price()返回” 最后的输出会是这样一个表,用扩散值作为单个(超级重要!)数字到两个或三个(不管)小数位数。
Position, Spread
YHOO/AAPL, 0.327 #value of Yahoo stock price/Apple stock price
GOOG/MSF, 4.55
ticker/ticker, 10.14
ticker/ticker, 0.567
我试图定义Long1和Short1作为L1.get_price()打印数量:
>>> Long1/Short1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'str' and 'str'
:
>>> Long1 = "L1.get_price()"
>>> Short1 = "S2.get_price()"
然后希望我应该跳水这两个得到一个数字
所以我试图将这些数字转换为浮动[因为它可能工作为什么不]但我很明显误解了一些东西:
>>> float(Long1)/float(Short1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: L1.get_price()
另外我也设法让使用这段代码利用大熊猫模块的输出:(谢谢你,布拉德·所罗门这个:)
import pandas_datareader.data as web
def get_quotes(symbols, type='dict'):
quotes = web.get_quote_yahoo(symbols)['last']
if type == 'dict':
quotes = quotes.to_dict() # otherwise, Series
return quotes
quotes = get_quotes(symbols=['RAD', 'MSFT']); quotes
Out[16]: {'MSFT': 70.409999999999997, 'RAD': 3.46}
但你会如何实现MSFT/RAD,得到蟒蛇“读取字符串”?
我真的很坚持,任何人都可以提示我如何才能让我的报价为真实物体可以使用吗? 谢谢!