Skip to content

Instantly share code, notes, and snippets.

@chriddyp
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriddyp/54d8a69ff6f01e60381a to your computer and use it in GitHub Desktop.
Save chriddyp/54d8a69ff6f01e60381a to your computer and use it in GitHub Desktop.
{"nbformat_minor": 0, "cells": [{"execution_count": 1, "cell_type": "code", "source": "import copy\nimport plotly.plotly as py\nimport Quandl\nimport datetime\nimport numpy as np\nfrom plotly.graph_objs import *\npy.sign_in('chriddyp', 'o1do5o15p8')", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}, {"execution_count": 31, "cell_type": "code", "source": "basefig = py.get_figure('jackp', 4771)\nbasefig['data'].remove(basefig['data'][-1])\nbasefig['data'].remove(basefig['data'][5])\nbasefig['data'].remove(basefig['data'][5])\nbasefig['data'][1]['x'][0] = 0\n\nadjusted = basefig['data'].pop(5) # move adjusted_close to the end, to be drawn on top\nbasefig['data'].append(adjusted)\n\nbasefig['layout']['margin']['l'] = 2\n\nbasefig['layout']['annotations'] = Annotations([\n # 0\n Annotation(\n x=1,\n y=1.4071023494463948,\n xref=u'x',\n yref=u'y',\n text=u'VIX Index',\n showarrow=False\n ),\n # 1\n Annotation(\n x=1.5066964285714284,\n y=1.4071023494463948,\n xref=u'x',\n yref=u'y',\n text=u'Static',\n showarrow=False\n ),\n # 2\n Annotation(\n x=2,\n y=1.4071023494463948,\n xref=u'x',\n yref=u'y',\n text=u'Daily',\n showarrow=False\n ),\n # 3\n Annotation(\n x=22,\n y=1.4,\n xref=u'x3',\n yref=u'y3',\n text=u'22',\n showarrow=False\n ),\n # 4\n Annotation(\n x=40,\n y=1.4,\n xref=u'x3',\n yref=u'y3',\n text=u'40',\n showarrow=False\n ),\n # 5\n Annotation(\n x=56,\n y=1.4,\n xref=u'x3',\n yref=u'y3',\n text=u'56',\n showarrow=False\n ),\n # 6\n Annotation(\n x=0.29166666666666674,\n y=1.4071428571428573,\n xref=u'paper',\n yref=u'paper',\n text=u'<b>Periodicity</b>',\n showarrow=False\n ),\n # 7\n Annotation(\n x=0.2011904761904762,\n y=1.40016611295681,\n xref=u'paper',\n yref=u'paper',\n text=u'<b>Threshold</b>',\n showarrow=False\n ),\n # 8\n Annotation(\n x=0.4214285714285715,\n y=1.4071428571428573,\n xref=u'paper',\n yref=u'paper',\n text=u'<b>Chart</b>',\n showarrow=False\n ),\n # 9\n Annotation(\n x=0.016666666666666663,\n y=1.392857142857143,\n xref=u'paper',\n yref=u'paper',\n text=u'<b>Status</b>',\n showarrow=False\n ),\n # 10 \n Annotation(\n x=0.40595238095238095,\n y=-0.2785714285714286,\n xref=u'paper',\n yref=u'paper',\n text=u'30-Mar',\n showarrow=False\n ), \n # 11\n Annotation(\n x=0.5785714285714285,\n y=-0.28571428571428575,\n xref=u'paper',\n yref=u'paper',\n text=u'9-Apr',\n showarrow=False,\n arrowhead=0,\n ax=-1,\n ay=23\n ),\n # 12\n Annotation(\n x=0.09999999999999998,\n y=1.392857142857143,\n xref='paper',\n yref='paper',\n text='<b>Indicator</b>',\n showarrow=False\n ) \n])\n\nbasefig['layout']['xaxis3']['domain'] = [0.6, 0.92]\nbasefig['layout']['legend']['x'] = 0.92", "outputs": [], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 32, "cell_type": "code", "source": "py.iplot(basefig, filename='EWI/basefig')", "outputs": [{"execution_count": 32, "output_type": "execute_result", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/120.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 33, "cell_type": "code", "source": "def create_chart(caution_val, level1_val, level2_val, adjusted_close):\n prior_val = adjusted_close['y'][-2]\n current_val = adjusted_close['y'][-1]\n\n fig = copy.deepcopy(basefig)\n \n layout = fig['layout']\n layout['yaxis2']['autorange'] = True\n\n \n data = fig['data']\n status = data[0]\n aok = data[1]\n caution = data[2]\n level1= data[3]\n level2 = data[4]\n \n adjusted_close_trace = data[-1]\n caution_lines = data[5]\n level1_lines = data[6]\n level2_lines = data[7]\n \n caution_anno = fig['layout']['annotations'][3]\n level1_anno = fig['layout']['annotations'][4]\n level2_anno = fig['layout']['annotations'][5]\n \n date_anno_start = fig['layout']['annotations'][10]\n date_anno_end = fig['layout']['annotations'][11]\n date_start = adjusted_close['x'][0]\n date_end = adjusted_close['x'][-1]\n if isinstance(date_start, basestring):\n date_start = datetime.datetime.strptime(date_start, '%Y-%m-%d')\n if isinstance(date_end, basestring):\n date_end = datetime.datetime.strptime(date_end, '%Y-%m-%d') \n date_anno_start['text'] = date_start.strftime('%d-%b')\n date_anno_end['text'] = date_end.strftime('%d-%b') \n \n layout['xaxis2']['range'] = [date_start, date_end+datetime.timedelta(days=1)] \n \n # Status\n colors = {'aok': 'rgb(44, 160, 44)', 'caution': 'rgb(241, 194, 50)', \n 'level1': 'rgb(148, 103, 189)', 'level2': 'rgb(214, 39, 40)'}\n labels = {'aok': 'Normal', 'caution': 'Caution', 'level1': 'Level 1 Stress',\n 'level2': 'Level 2 Stress'}\n \n arrow_trace = {\n 'x': [prior_val, current_val],\n 'y': [2.7, 2.7],\n 'xaxis': 'x3',\n 'yaxis': 'y3',\n 'mode': 'lines+markers+text',\n 'text': ['Prior ({})'.format(prior_val), 'Current ({})'.format(current_val)],\n 'textposition': ['top left', 'bottom right'],\n 'marker': {\n 'symbol': ['line-ns-open', 'triangle-right'], \n 'color': 'rgb(40,40,40)'\n },\n 'showlegend': False\n }\n\n # Text positioning logic\n\n if prior_val < current_val:\n arrow_trace['marker']['symbol'] = ['line-ns-open', 'triangle-right']\n else:\n arrow_trace['marker']['symbol'] = ['line-ns-open', 'triangle-left']\n\n # Change range to accomadate text\n spacing = 0.3*level2_val\n layout['xaxis3']['range'] = [-1*spacing, level2_val + spacing]\n layout['xaxis3']['autorange'] = False\n\n spacing = abs(current_val - prior_val)/(1.0*level2_val)\n print spacing\n if spacing > 0.3:\n arrow_trace['textposition'] = ['top', 'top']\n else:\n if prior_val < current_val:\n arrow_trace['textposition'] = ['top left', 'top right']\n else:\n arrow_trace['textposition'] = ['top right', 'top left']\n\n data.append(arrow_trace)\n\n layout['yaxis3']['range'] = [0, 5]\n \n if current_val < caution:\n status['text'][0] = labels['aok']\n status['marker']['color'] = colors['aok']\n elif current_val < level1_val:\n status['text'][0] = labels['caution']\n status['marker']['color'] = colors['caution']\n elif current_val < level2_val:\n status['text'][0] = labels['level1']\n status['marker']['color'] = colors['level1']\n else:\n status['text'][0] = labels['level2']\n status['marker']['color'] = colors['level2']\n \n # prior_value['x'][0] = prior_val\n # current_value['x'][0] = current_val\n\n aok['x'][0] = layout['xaxis3']['range'][0]\n aok['x'][1] = caution['x'][0] = caution_anno['x'] = caution_val\n caution['x'][1] = level1['x'][0] = level1_anno['x'] = level1_val\n level1['x'][1] = level2['x'][0] = level2_anno['x'] = level2_val\n level2['x'][1] = 10+level2['x'][0]\n\n caution_anno['text'] = str(caution_anno['x'])\n level1_anno['text'] = str(level1_anno['x'])\n level2_anno['text'] = str(level2_anno['x'])\n caution_anno['y'] = level1_anno['y'] = level2_anno['y'] = 0\n \n adjusted_close_trace['x'] = caution_lines['x'] = level1_lines['x'] = level2_lines['x'] = adjusted_close['x']\n adjusted_close_trace['y'] = adjusted_close['y']\n caution_lines['y'] = [caution_val]*len(caution_lines['x'])\n level1_lines['y'] = [level1_val]*len(level1_lines['x'])\n level2_lines['y'] = [level2_val]*len(level2_lines['x'])\n \n return fig", "outputs": [], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 34, "cell_type": "code", "source": "# df = Quandl.get(\"YAHOO/INDEX_VIX\")", "outputs": [], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 74, "cell_type": "code", "source": "from IPython.display import display\ndef gen_chart(days_ago, interval=20):\n start = datetime.datetime.now() - datetime.timedelta(days=days_ago)\n end = start + datetime.timedelta(days=interval)\n dfi = df[(df.index > start) & (df.index < end)]\n display(py.iplot(create_chart(22, 40, 56, \n {'x': dfi.index, 'y': dfi['Adjusted Close']}), \n filename='EWI/INDEX_VIX/{} to {}'.format(start.strftime('%d-%b'),\n end.strftime('%d-%b')), validate=False))", "outputs": [], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 76, "cell_type": "code", "source": "gen_chart(20)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "0.0242857142857\n"}, {"output_type": "display_data", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/147.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 77, "cell_type": "code", "source": "gen_chart(40)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "0.00375\n"}, {"output_type": "display_data", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/148.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 78, "cell_type": "code", "source": "gen_chart(60)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "0.00660714285714\n"}, {"output_type": "display_data", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/149.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 79, "cell_type": "code", "source": "gen_chart(80)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "0.0289285714286\n"}, {"output_type": "display_data", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/150.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 80, "cell_type": "code", "source": "gen_chart(100)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "0.00464285714286\n"}, {"output_type": "display_data", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/151.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 81, "cell_type": "code", "source": "gen_chart(120)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "0.0251785714286\n"}, {"output_type": "display_data", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/152.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 82, "cell_type": "code", "source": "gen_chart(140)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "0.0178571428571\n"}, {"output_type": "display_data", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/153.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 83, "cell_type": "code", "source": "gen_chart(160)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "0.005\n"}, {"output_type": "display_data", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/154.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 84, "cell_type": "code", "source": "gen_chart(180)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "0.00285714285714\n"}, {"output_type": "display_data", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/155.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 85, "cell_type": "code", "source": "gen_chart(200)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "0.0442857142857\n"}, {"output_type": "display_data", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/156.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 86, "cell_type": "code", "source": "gen_chart(220)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "0.0423214285714\n"}, {"output_type": "display_data", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/157.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 87, "cell_type": "code", "source": "gen_chart(240)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "0.00982142857143\n"}, {"output_type": "display_data", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/158.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 88, "cell_type": "code", "source": "gen_chart(260)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "0.0130357142857\n"}, {"output_type": "display_data", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/159.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 89, "cell_type": "code", "source": "gen_chart(280)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "0.0151785714286\n"}, {"output_type": "display_data", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/160.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 90, "cell_type": "code", "source": "gen_chart(300)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "0.0180357142857\n"}, {"output_type": "display_data", "data": {"text/plain": "<plotly.tools.PlotlyDisplay object>", "text/html": "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chriddyp/161.embed\" height=\"525\" width=\"100%\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": null, "cell_type": "code", "source": "", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}, {"execution_count": null, "cell_type": "code", "source": "", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}, {"execution_count": null, "cell_type": "code", "source": "", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}, {"execution_count": null, "cell_type": "code", "source": "", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}, {"execution_count": null, "cell_type": "code", "source": "", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}, {"execution_count": null, "cell_type": "code", "source": "", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}, {"execution_count": null, "cell_type": "code", "source": "", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}, {"execution_count": null, "cell_type": "code", "source": "", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "Python 2", "name": "python2", "language": "python"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "2.7.9", "name": "python", "file_extension": ".py", "pygments_lexer": "ipython2", "codemirror_mode": {"version": 2, "name": "ipython"}}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment