【问题标题】:ystockquote historic data wrong order?ystockquote 历史数据顺序错误?
【发布时间】:2014-10-31 00:46:22
【问题描述】:

所以我一直非常成功地使用 ystockquote,但是我遇到了一个小问题。

当我提取任何股票的历史数据时,它会生成一个包含正确信息的字典,但是字典日期的顺序是错误的吗?

这是我的代码:

import ystockquote
import matplotlib.pyplot as plt

ticker = "YHOO"
stock_exchange = ystockquote.get_stock_exchange(ticker)
change = ystockquote.get_change(ticker)
price = ystockquote.get_price(ticker)
market_cap = ystockquote.get_market_cap(ticker)
_52_week_high = ystockquote.get_52_week_high(ticker)
_52_week_low = ystockquote.get_52_week_low(ticker)
avg_volume = ystockquote.get_avg_daily_volume(ticker)
volume = ystockquote.get_volume(ticker)


print (ticker + " (" + change + ") ")
print (stock_exchange.strip('"'))
print ("Share Price: " + price)
print ("Market Cap: " + market_cap)
print ("52 Week High/Low: " + _52_week_high + "/" + _52_week_low)
print ("Trading Volume: " + volume)
print ("Average Trading Volume(3m): " + avg_volume)

historic_prices = ystockquote.get_historical_prices(ticker, '2014-10-01', '2014-10-15')

for x in historic_prices:
    print(x)

这就是它产生的结果

YHOO (+0.20) 
NasdaqNM
Share Price: 45.63
Market Cap: 44.672B
52 Week High/Low: 46.15/32.06
Trading Volume: 16209593
Average Trading Volume(3m): 34564400
2014-10-01
2014-10-14
2014-10-02
2014-10-13
2014-10-10
2014-10-09
2014-10-08
2014-10-15
2014-10-03
2014-10-07
2014-10-06

您可以看到历史价格字典中的日期顺序不正确?我知道差距是为了说明周末,但检索到的日期顺序错误?我究竟做错了什么?我在互联网上看到很多其他人以同样的方式使用它,并且日期的顺序正确!

我正在使用 Python 3.4!

感谢您的帮助!

【问题讨论】:

    标签: python finance stocks


    【解决方案1】:

    不保证在字典中的排序。

    对字典执行 list(d.keys()) 以任意顺序返回字典中使用的所有键的列表(如果要对其进行排序,只需使用 sorted(d.keys()) 代替)。

    从排序后的键中,您也可以显示条目的其余部分。

    来源:https://docs.python.org/3/tutorial/datastructures.html?highlight=dictionary

    抱歉第一次记错了语法!

    【讨论】:

    • 无法对字典进行排序
    • 您不能对字典进行排序,但是,它首先应该产生正确的顺序,互联网上的所有其他示例都会产生相同的顺序!
    猜你喜欢
    • 2016-01-14
    • 2023-03-10
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多