# -*- coding: utf-8 -*-
import numpy as np
import pandas_datareader.data as web
import datetime
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick2_ohlc
def make_tick_input(symbol, start, end, tick_unit = 0.01):
hist = web.DataReader(symbol,'yahoo',start,end)
ticks = 1 / tick_unit
s_open = np.round(hist.ix[0]['Open'] * ticks)
hist['open_tick'] = np.round(hist['Open'] * ticks) - s_open
hist['body'] = np.round((hist['Close']-hist['Open']) * ticks)
hist['upper'] = np.round((hist['High']-np.maximum(hist['Open'], hist['Close'])) * ticks)
hist['lower'] = np.round((np.minimum(hist['Open'], hist['Close'])-hist['Low']) * ticks)
return hist
start = datetime.datetime(2016,9,1)
end = datetime.datetime(2016,9,30)
hist = make_tick_input('^DJI', start, end)
print(hist)
fid, ax = plt.subplots(figsize=(16,6))
ax.set_xticks(range(0, len(hist)))
ax.set_xticklabels(hist.index.strftime('%Y-%m-%d'), rotation=70)
candlestick2_ohlc(ax, hist['Open'], hist['High'], hist['Low'], hist['Close'],
width=1, colorup='r', colordown='b')
plt.show()
Open High Low Close Volume \ Date 2016-09-01 18396.570312 18430.050781 18295.480469 18419.300781 74780000 2016-09-02 18466.009766 18544.759766 18439.099609 18491.960938 74350000 2016-09-06 18493.400391 18551.539062 18450.320312 18538.119141 83710000 2016-09-07 18527.710938 18536.890625 18474.769531 18526.140625 67570000 2016-09-08 18486.689453 18506.240234 18446.689453 18479.910156 79060000 2016-09-09 18404.169922 18404.169922 18085.449219 18085.449219 120670000 2016-09-12 18028.949219 18358.689453 17994.839844 18325.070312 103260000 2016-09-13 18262.990234 18262.990234 18028.060547 18066.750000 102830000 2016-09-14 18073.390625 18163.480469 17992.210938 18034.769531 103360000 2016-09-15 18024.910156 18250.109375 18015.490234 18212.480469 99550000 2016-09-16 18217.210938 18016.929688 18217.210938 18123.800781 278510000 2016-09-19 18154.820312 18254.880859 18093.050781 18120.169922 78090000 2016-09-20 18175.359375 18227.210938 18128.800781 18129.960938 72450000 2016-09-21 18164.960938 18307.429688 18121.570312 18293.699219 94090000 2016-09-22 18343.759766 18449.880859 18343.759766 18392.460938 73030000 2016-09-23 18377.359375 18383.759766 18254.839844 18261.449219 87170000 2016-09-26 18217.759766 18217.759766 18083.320312 18094.830078 95510000 2016-09-27 18099.210938 18238.099609 18052.160156 18228.300781 84130000 2016-09-28 18240.220703 18349.859375 18179.339844 18339.240234 97650000 2016-09-29 18322.880859 18366.230469 18091.640625 18143.449219 95880000 2016-09-30 18181.800781 18369.619141 18181.800781 18308.150391 135470000 Adj Close open_tick body upper lower Date 2016-09-01 18419.300781 0.0 2273.0 1075.0 10109.0 2016-09-02 18491.960938 6944.0 2595.0 5280.0 2691.0 2016-09-06 18538.119141 9683.0 4472.0 1342.0 4308.0 2016-09-07 18526.140625 13114.0 -157.0 918.0 5137.0 2016-09-08 18479.910156 9012.0 -678.0 1955.0 3322.0 2016-09-09 18085.449219 760.0 -31872.0 0.0 0.0 2016-09-12 18325.070312 -36762.0 29612.0 3362.0 3411.0 2016-09-13 18066.750000 -13358.0 -19624.0 0.0 3869.0 2016-09-14 18034.769531 -32318.0 -3862.0 9009.0 4256.0 2016-09-15 18212.480469 -37166.0 18757.0 3763.0 942.0 2016-09-16 18123.800781 -17936.0 -9341.0 -20028.0 -9341.0 2016-09-19 18120.169922 -24175.0 -3465.0 10006.0 2712.0 2016-09-20 18129.960938 -22121.0 -4540.0 5185.0 116.0 2016-09-21 18293.699219 -23161.0 12874.0 1373.0 4339.0 2016-09-22 18392.460938 -5281.0 4870.0 5742.0 0.0 2016-09-23 18261.449219 -1921.0 -11591.0 640.0 661.0 2016-09-26 18094.830078 -17881.0 -12293.0 0.0 1151.0 2016-09-27 18228.300781 -29736.0 12909.0 980.0 4705.0 2016-09-28 18339.240234 -15635.0 9902.0 1062.0 6088.0 2016-09-29 18143.449219 -7369.0 -17943.0 4335.0 5181.0 2016-09-30 18308.150391 -21477.0 12635.0 6147.0 0.0