Error: %s
\" % e )\n make_table = (\"CREATE TABLE \" + ticker.replace('.', '_') + \" (date datetime, high float, low float, close float, volume bigint)\")\n cursor.execute(make_table)\n cnx.commit()\n add_data = (\"INSERT INTO \" + ticker.replace('.', '_') + \" (date, high, low, close, volume) VALUES (%s, %s, %s, %s, %s)\")\n cursor.execute(add_data, (datetime.strptime('1024-01-01', '%Y-%m-%d'), 0, 0, 0, 1))\n cnx.commit()\n except:\n try:\n query_valid.value = \"Can't access Yahoo Finance. Connecting to database.\"\n get_data = (\"SELECT * FROM \" + ticker.replace('.', '_') + \" WHERE date BETWEEN %s AND %s\")\n cursor.execute(get_data, (start, end))\n data = pandas.DataFrame(columns=('Date', 'High', 'Low', 'Close', 'Volume'))\n index = 0\n for s in cursor.fetchall():\n data.loc[index] = (s[0].strftime('%Y-%m-%d'), s[1], s[2], s[3], s[4])\n index = index + 1\n data = data.reindex(index = data.index[::1]).reset_index()\n except:\n query_valid.value = 'Invalid Query'\n return False\n data[data.columns[1]] = pandas.to_datetime(data[data.columns[1]])\n data['Cumulative'] = data['Volume'].cumsum() - data['Volume']\n \n # Create weekly data\n weeks = pandas.DataFrame(columns=['High', 'Low', 'Volume', 'Prev', 'Close', 'Days', 'Start', 'End'])\n index = 0\n weeks.loc[0] = (0, 0, 0, 0, 0, 0, 0, 0)\n yesterday = 6\n for i, row in data.iterrows():\n if row[1].weekday() < yesterday:\n index = index + 1\n weeks.loc[index] = (row['High'],\n row['Low'],\n 0,\n weeks.loc[index - 1][3] + weeks.loc[index - 1][2],\n 0,\n 0,\n row[1],\n row[1])\n yesterday = row[1].weekday()\n weeks.loc[index] = (max(row['High'], weeks.loc[index][0]),\n min(row['Low'], weeks.loc[index][1]),\n row['Volume'] + weeks.loc[index][2],\n weeks.loc[index][3],\n row['Close'],\n weeks.loc[index][5] + 1,\n weeks.loc[index][6],\n row[1])\n\n weeks = weeks[1:]\n\ndef draw_graph():\n # refresh the graph\n plt.close()\n dis.clear_output()\n fig, ax = plt.subplots()\n \n # draw bar graph\n ax.set_title(ticker + \": \" + start.strftime(\"%b %d, %Y\") + \" - \" + end.strftime(\"%b %d, %Y\"), fontsize=15)\n bars = ax.bar(weeks['Prev']/1000000.0,\n weeks['High'] - weeks['Low'],\n width=weeks['Volume']/1000000.0,\n bottom=weeks['Low'])\n ax.plot((weeks['Prev'] + weeks['Volume'])/1000000.0, weeks['Close'], '_', mew = 2, ms = 4, color='r')\n \n ylims = ax.get_ylim()\n \n # draw markups\n for mk in marks:\n x1 = weeks.loc[mk[0]][3]/1000000.0\n y = max(weeks.loc[mk[0]][0], weeks.loc[mk[1]][0])\n x2 = weeks.loc[mk[1]][3]/1000000.0\n ax.plot([x1, x1], [0, y], linestyle = '-', linewidth = 2, color = 'k')\n ax.plot([x2, x2], [0, y], linestyle = '-', linewidth = 2, color = 'k')\n \n x3 = max(x1, x2) + abs(x1 - x2)\n ax.plot([x3, x3], [0, y], linestyle = '-', linewidth = 2, color = 'k')\n ax.plot([min(x1, x2), x3], [y, y], linestyle = '-', linewidth = 2, color = 'k')\n \n xlims = ax.get_xlim()\n \n # draw lines\n for ln in lines:\n x1 = weeks.loc[ln[0]][3]/1000000.0\n x2 = weeks.loc[ln[2]][3]/1000000.0\n if ln[1] == 'High':\n y1 = weeks.loc[ln[0]][0]\n y2 = weeks.loc[ln[2]][0]\n else:\n y1 = weeks.loc[ln[0]][1]\n y2 = weeks.loc[ln[2]][1]\n minx = min(x1, x2)\n if x1 < x2:\n miny = y1\n else:\n miny = y2\n ax.plot([minx, xlims[1]], [miny, (xlims[1] - minx)*(y2-y1)/(x2-x1)+miny], linestyle = '-', linewidth = 2, color = 'm')\n \n ax.set_xlim(xlims)\n ax.set_ylim(ylims)\n ax.set_ylabel(\"Price\", fontsize=14,labelpad = 10)\n ax.set_xlabel(\"Volume (millions)\", fontsize=14,labelpad = 10)\n plt.locator_params(axis='x',nbins=4)\n ax.grid(True, alpha=0.3)\n \n \n # add annotations\n for j, bar in enumerate(bars.get_children()):\n popup=str('| Week # | %d | \\\n
|---|---|
| Start | %s | \\\n
| End | %s | \\\n
| High | %.2f | \\\n
| Low | %.2f | \\\n
| Close | %.2f | \\\n
| Volume | %s | \\\n