Quandl is a platform that offers free and premium access to financial and economic data. On top of this the data export is supported by many languages and softwares such as R, C#, Matlab. You can find here an exhaustive list of environments.
In the following you will find an illustration of how you can retrieve data from Quandl, using the Quandl python package and then plot this data (here, Debt expressed as % of GDP for several countries).
To make this sample running, you need your own Quandl token, which can be recovered from the Quandl website. Once you have it, you should replace “yourcode” in the code below by your token.
And of course, you should install the Quandl package (using pip or conda).
The code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import matplotlib.pyplot as plt from matplotlib.pyplot import * import Quandl from matplotlib.font_manager import FontProperties print "Defining instruments ..." #print plt.style.available plt.style.use('ggplot') fig=plt.figure() ax1=fig.add_subplot(111) debtarray = [] # Countries ISO codes: https://s3.amazonaws.com/quandl-static-content/API+Descriptions/WHO/ccodes.txt debtarray.append("ODA/USA_GGXWDN_NGDP") debtarray.append("ODA/JPN_GGXWDN_NGDP") debtarray.append("ODA/ITA_GGXWDN_NGDP") debtarray.append("ODA/FRA_GGXWDN_NGDP") debtarray.append("ODA/ESP_GGXWDN_NGDP") debtarray.append("ODA/GBR_GGXWDN_NGDP") debtarray.append("ODA/GRC_GGXWDN_NGDP") debtarray.append("ODA/IRL_GGXWDN_NGDP") debtarray.append("ODA/DEU_GGXWDN_NGDP") debtarray.append("ODA/CHE_GGXWDN_NGDP") debtarray.append("ODA/CAN_GGXWDN_NGDP") debtarray.append("ODA/BEL_GGXWDN_NGDP") #debtarray.append("ODA/NOR_GGXWDN_NGDP") debtarray.append("ODA/SWE_GGXWDN_NGDP") debtarray.append("ODA/DEN_GGXWDN_NGDP") print "Retrieving data ..." series = Quandl.get(debtarray,authtoken="yourcode",trim_start="2000-01-01", trim_end="2016-11-01",collapse="annual") print "Plotting ..." # https://www.quandl.com/collections/economics/net-debt-as-share-of-gdp-oda-by-country for i in range(len(debtarray)): try: dateaxis = series.reset_index()['Date'] plt.plot(dateaxis, series.ix[:,i], label=debtarray[i][4:-12], marker='.') ax1.annotate(debtarray[i][4:-12], xy=(dateaxis[i], series.ix[:,i][i]), rotation=0) except Exception as e: print "error at " + debtarray[i] pass plt.xlabel("Year") plt.ylabel("% of GDP") myFont = FontProperties() myFont.set_size('xx-small') plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=2, mode="expand", borderaxespad=0., numpoints = 1) legend = ax1.legend(bbox_to_anchor=(0, 0, 1, 1), loc=0, ncol=1, prop = myFont,fancybox=True,shadow=False,title='Debt as % of GDP') plt.setp(legend.get_title(),fontsize='xx-small') plt.show() |
The chart
peter
it is a good tutorial , and I is passionated to run the code.
however it is not allowed to copy 🙁