So I've been using ystockquote quite successfully, however I've ran into a small problem.
When I pull historic data for any stock it produces a dictionary with the correct information, however the order of the dictionary dates are wrong?
Here's my code:
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)
and this is the results it produces
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
as you can see the dates from the historic prices dictionary are not in the right order? I understand the gaps are to account for the weekends but the dates that are retrieved are in the wrong order? What am I doing wrong? I've seen plenty of other people on the internet use it the same way and have the dates in the right order!
I'm using Python 3.4!
Thanks for your help!
Ordering in a dictionary is not guaranteed.
Performing list(d.keys()) on a dictionary returns a list of all the keys used in the dictionary, in arbitrary order (if you want it sorted, just use sorted(d.keys()) instead).
From the sorted keys you can display the rest of the entry as well.
Source: https://docs.python.org/3/tutorial/datastructures.html?highlight=dictionary
Sorry for incorrect memory of the syntax first time!