{"id":122,"date":"2019-09-19T22:27:33","date_gmt":"2019-09-19T20:27:33","guid":{"rendered":"https:\/\/blog.wofuer.com\/?p=122"},"modified":"2019-09-20T10:29:15","modified_gmt":"2019-09-20T08:29:15","slug":"analyzing-timeseries-with-python","status":"publish","type":"post","link":"https:\/\/blog.wofuer.com\/?p=122","title":{"rendered":"Analyzing Timeseries with Python"},"content":{"rendered":"\n<p>Small demo of the needed elements to load, analyze and print a time series in Python. Format of the data-source:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>...\n2019-08-28,11701.019531,EUR\n2019-08-29,11838.879883,EUR\n2019-08-30,11939.280273,EUR\n2019-09-02,11953.780273,EUR\n2019-09-03,11910.860352,EUR\n2019-09-04,12025.040039,EUR\n2019-09-05,12126.780273,EUR\n2019-09-06,12191.730469,EUR\n2019-09-09,12226.099609,EUR\n2019-09-10,12268.709961,EUR<\/code><\/pre>\n\n\n\n<p>Generates based on the complete data, the annual average performance and volatility<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Annual Performance:  8.026639312445006\nAnnual Vola:  19.100050116208784<\/code><\/pre>\n\n\n\n<p>&#8230; and the plot of the analyzed data:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"https:\/\/blog.wofuer.com\/wp-content\/uploads\/2019\/09\/plot.png\" alt=\"\" class=\"wp-image-123\" srcset=\"https:\/\/blog.wofuer.com\/wp-content\/uploads\/2019\/09\/plot.png 640w, https:\/\/blog.wofuer.com\/wp-content\/uploads\/2019\/09\/plot-300x225.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<p>&#8230; based on the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/python3 \n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom datetime import datetime, date\n\n# some variables\nnow = datetime.now() # date\/time of software execution\nendDate = date(year = now.year, month = now.month, day = now.day) # date of software execution (end of analytics period)\nstartDate = date(year = now.year-10, month = now.month, day = now.day) # 1 year earlies (begin of analytics period)\ndeltaYears = (endDate-startDate).days\/365.2425 # difference of startDate and endDate in years\n\n# read csv file &amp; build timeseries\n# raw = pd.read_csv(\".\/Reference\/stocks_2\/JP3942600002.EUR.csv\", header=None)\nraw = pd.read_csv(\".\/DAX.EUR.csv\", header=None)\n\nts = pd.DataFrame(columns=['datetime','value']) # generate timeframe\nts['datetime'] = pd.to_datetime(raw[0]) # load column-datetime with the raw-timestamps\nts['value'] = raw[1] # load column-value with the values\nts = ts.set_index('datetime') # index based on datetime\n# print(ts)\n\n# reduction of timeseries to the choosen period and cleaning for weekdays\nts = ts.resample('D').ffill() # generate sample size one-day and fill missing elements\nselection = pd.date_range(startDate, endDate, freq='B') # generat selection from startDate to endDate with weekdays\nts = ts.asof(selection) # get subset of ts according selection and interpolate remaining ()\n# print(ts)\n\n# some calculation\nval = np.array(ts['value'])\nres = np.log(val[1:]\/val[:(len(val)-1)])\n# r = (np.power(1+np.mean(res),len(res))-1)*100 # performance\nr = (np.power(np.power(1+np.mean(res),len(res)),1\/deltaYears)-1)*100 # performance (annual)\nv = np.std(res)*np.sqrt(len(res))*100\/np.sqrt(deltaYears) # vola (annual)\nprint('Annual Performance: ',r)\nprint('Annual Vola: ',v)\n\n# check\nts.head()\n\n# plot \nts.plot()\nplt.show()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Small demo of the needed elements to load, analyze and print a time series in Python. Format of the data-source: Generates based on the complete data, the annual average performance and volatility &#8230; and the plot of the analyzed data: &#8230; based on the following code:<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,7],"tags":[3,15,16],"class_list":["post-122","post","type-post","status-publish","format-standard","hentry","category-investment","category-software","tag-data","tag-investment","tag-python"],"_links":{"self":[{"href":"https:\/\/blog.wofuer.com\/index.php?rest_route=\/wp\/v2\/posts\/122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.wofuer.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wofuer.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wofuer.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wofuer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=122"}],"version-history":[{"count":1,"href":"https:\/\/blog.wofuer.com\/index.php?rest_route=\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":124,"href":"https:\/\/blog.wofuer.com\/index.php?rest_route=\/wp\/v2\/posts\/122\/revisions\/124"}],"wp:attachment":[{"href":"https:\/\/blog.wofuer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wofuer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wofuer.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}