In [10]:
# Collect Data
# 1. Ticker 
# 2. Board
# https://pythonprogramming.net/candlestick-ohlc-graph-matplotlib-tutorial/
import Test_bitflyer as bf
import matplotlib.pyplot as plt
import matplotlib.dates as md

from matplotlib.finance import candlestick_ohlc
from datetime import datetime,timedelta
from mongo_db_bitflyer import mongo_db_bitflyer

In [11]:
# for Notebook
%matplotlib notebook

In [12]:
# Draw Charts.
product_code='BTC_JPY' # Bitcoin
start_time=datetime.now()-timedelta(minutes=240) #現在時刻から4時間前
end_time=datetime.now() #現在時刻
bar_range=5 # minutes 5分足

In [13]:
##
## Start Main
##
mdb=mongo_db_bitflyer()
# Show current record number
mdb.ds.count(0)


Out[13]:
488462

In [14]:
# Candlestick cart
# Get data from database...
_ohlc_table=mdb.gather_ohlc_data('BTC_JPY',start_time,end_time,bar_range)

In [15]:
# Draw CandleStick Chart.
# Prepare chart..
fig, ax = plt.subplots()

# set title
plt.title('Candlestick:{0} - {1} minutes bars.'.format(product_code,bar_range))

# setup x-axis for date time values.
dateArray=_ohlc_table[:,0]
plt.xticks(dateArray)
fig.autofmt_xdate(rotation=90)

# setup axis
ax.autoscale(enable=True)
ax.grid(True)
ax.xaxis.set_major_formatter(md.DateFormatter('%Y-%m-%d %H:%M'))

# The candle bar width can be calcurated based on dates and number of bars.
barWidth = ((max(dateArray)-min(dateArray))/dateArray.shape[0])*0.8 # 0.8 is for margin.

# draw it!
candlestick_ohlc(ax, _ohlc_table,width=barWidth,colorup='g', colordown='r')


Out[15]:
([<matplotlib.lines.Line2D at 0x7f18d47e0a20>,
  <matplotlib.lines.Line2D at 0x7f18d47c95c0>,
  <matplotlib.lines.Line2D at 0x7f18d47c1f60>,
  <matplotlib.lines.Line2D at 0x7f18d47d6f98>,
  <matplotlib.lines.Line2D at 0x7f18d482ca58>,
  <matplotlib.lines.Line2D at 0x7f18d4818cf8>,
  <matplotlib.lines.Line2D at 0x7f18d48625f8>,
  <matplotlib.lines.Line2D at 0x7f18d47e5f98>,
  <matplotlib.lines.Line2D at 0x7f18d4778fd0>,
  <matplotlib.lines.Line2D at 0x7f18d47831d0>,
  <matplotlib.lines.Line2D at 0x7f18d47882e8>,
  <matplotlib.lines.Line2D at 0x7f18d478e400>,
  <matplotlib.lines.Line2D at 0x7f18d4794518>,
  <matplotlib.lines.Line2D at 0x7f18d479a630>,
  <matplotlib.lines.Line2D at 0x7f18d479f748>,
  <matplotlib.lines.Line2D at 0x7f18d47a5860>,
  <matplotlib.lines.Line2D at 0x7f18d47cf8d0>,
  <matplotlib.lines.Line2D at 0x7f18d47c1198>,
  <matplotlib.lines.Line2D at 0x7f18d47ae9e8>,
  <matplotlib.lines.Line2D at 0x7f18d4814390>,
  <matplotlib.lines.Line2D at 0x7f18d48209e8>,
  <matplotlib.lines.Line2D at 0x7f18d4860358>,
  <matplotlib.lines.Line2D at 0x7f18d47381d0>,
  <matplotlib.lines.Line2D at 0x7f18d473e2e8>,
  <matplotlib.lines.Line2D at 0x7f18d4745400>,
  <matplotlib.lines.Line2D at 0x7f18d474a518>,
  <matplotlib.lines.Line2D at 0x7f18d4750630>,
  <matplotlib.lines.Line2D at 0x7f18d4755748>,
  <matplotlib.lines.Line2D at 0x7f18d475b860>,
  <matplotlib.lines.Line2D at 0x7f18d4761978>,
  <matplotlib.lines.Line2D at 0x7f18d4767a90>,
  <matplotlib.lines.Line2D at 0x7f18d46eebe0>,
  <matplotlib.lines.Line2D at 0x7f18d46f5cf8>,
  <matplotlib.lines.Line2D at 0x7f18d46f9e10>,
  <matplotlib.lines.Line2D at 0x7f18d46fff28>,
  <matplotlib.lines.Line2D at 0x7f18d4705f60>,
  <matplotlib.lines.Line2D at 0x7f18d470f160>,
  <matplotlib.lines.Line2D at 0x7f18d4716278>,
  <matplotlib.lines.Line2D at 0x7f18d471a390>,
  <matplotlib.lines.Line2D at 0x7f18d47214a8>,
  <matplotlib.lines.Line2D at 0x7f18d47265c0>,
  <matplotlib.lines.Line2D at 0x7f18d472c6d8>,
  <matplotlib.lines.Line2D at 0x7f18d46317f0>,
  <matplotlib.lines.Line2D at 0x7f18d4637908>,
  <matplotlib.lines.Line2D at 0x7f18d463da20>,
  <matplotlib.lines.Line2D at 0x7f18d4643b38>,
  <matplotlib.lines.Line2D at 0x7f18d4649c88>,
  <matplotlib.lines.Line2D at 0x7f18d4650da0>,
  <matplotlib.lines.Line2D at 0x7f18d4656eb8>],
 [<matplotlib.patches.Rectangle at 0x7f18d47dca58>,
  <matplotlib.patches.Rectangle at 0x7f18d47c5be0>,
  <matplotlib.patches.Rectangle at 0x7f18d47dc588>,
  <matplotlib.patches.Rectangle at 0x7f18d47fb5c0>,
  <matplotlib.patches.Rectangle at 0x7f18d4820dd8>,
  <matplotlib.patches.Rectangle at 0x7f18d47fdcc0>,
  <matplotlib.patches.Rectangle at 0x7f18d47e5278>,
  <matplotlib.patches.Rectangle at 0x7f18d4778320>,
  <matplotlib.patches.Rectangle at 0x7f18d477c1d0>,
  <matplotlib.patches.Rectangle at 0x7f18d47832e8>,
  <matplotlib.patches.Rectangle at 0x7f18d4788400>,
  <matplotlib.patches.Rectangle at 0x7f18d478e518>,
  <matplotlib.patches.Rectangle at 0x7f18d4794630>,
  <matplotlib.patches.Rectangle at 0x7f18d479a748>,
  <matplotlib.patches.Rectangle at 0x7f18d479f860>,
  <matplotlib.patches.Rectangle at 0x7f18d47a5978>,
  <matplotlib.patches.Rectangle at 0x7f18d47c9eb8>,
  <matplotlib.patches.Rectangle at 0x7f18d47bb780>,
  <matplotlib.patches.Rectangle at 0x7f18d47fb0f0>,
  <matplotlib.patches.Rectangle at 0x7f18d4829978>,
  <matplotlib.patches.Rectangle at 0x7f18d48187f0>,
  <matplotlib.patches.Rectangle at 0x7f18d47ab438>,
  <matplotlib.patches.Rectangle at 0x7f18d47382e8>,
  <matplotlib.patches.Rectangle at 0x7f18d473e400>,
  <matplotlib.patches.Rectangle at 0x7f18d4745518>,
  <matplotlib.patches.Rectangle at 0x7f18d474a630>,
  <matplotlib.patches.Rectangle at 0x7f18d4750748>,
  <matplotlib.patches.Rectangle at 0x7f18d4755860>,
  <matplotlib.patches.Rectangle at 0x7f18d475b978>,
  <matplotlib.patches.Rectangle at 0x7f18d4761a90>,
  <matplotlib.patches.Rectangle at 0x7f18d4767ba8>,
  <matplotlib.patches.Rectangle at 0x7f18d46eeac8>,
  <matplotlib.patches.Rectangle at 0x7f18d46f90f0>,
  <matplotlib.patches.Rectangle at 0x7f18d46ff208>,
  <matplotlib.patches.Rectangle at 0x7f18d4705358>,
  <matplotlib.patches.Rectangle at 0x7f18d4709160>,
  <matplotlib.patches.Rectangle at 0x7f18d470f278>,
  <matplotlib.patches.Rectangle at 0x7f18d4716390>,
  <matplotlib.patches.Rectangle at 0x7f18d471a4a8>,
  <matplotlib.patches.Rectangle at 0x7f18d47215c0>,
  <matplotlib.patches.Rectangle at 0x7f18d47266d8>,
  <matplotlib.patches.Rectangle at 0x7f18d472c7f0>,
  <matplotlib.patches.Rectangle at 0x7f18d4631908>,
  <matplotlib.patches.Rectangle at 0x7f18d4637a20>,
  <matplotlib.patches.Rectangle at 0x7f18d463db38>,
  <matplotlib.patches.Rectangle at 0x7f18d4643ef0>,
  <matplotlib.patches.Rectangle at 0x7f18d4650160>,
  <matplotlib.patches.Rectangle at 0x7f18d4656198>,
  <matplotlib.patches.Rectangle at 0x7f18d465a2b0>])

In [7]:
# MACD chart
fillcolor = 'darkslategrey' #
nslow = 26
nfast = 12
nema = 9
start_time=datetime.now()-timedelta(minutes=240+nslow*bar_range) #現在時刻から4時間+(MACD slow x 足) 前
end_time=datetime.now() #現在時刻

In [8]:
# Get data from database...
_macd_t,_ema_s,_ema_f,_macd,_signal = mdb.gather_macd_data(product_code,start_time,end_time,bar_range,nslow,nfast,nema)

# _macd_t : タイムスタンプ
# _ema_s : 対数移動平均(slow)
# _ema_f : 対数移動平均(fast)
# _macd : Moving Average Convergence/Divergence 
# _signal: Signal

# このコードでは、グラフに表示するのは、_macd と signal。
# _macd_ma_s,_macd_ma_f は使用しない。一応 None にする。
_ema_s=_ema_f = None

# nslow 項までのデータはグラフの形状を考慮して切り詰める
_macd_t,_macd,_signal=_macd_t[nslow:],_macd[nslow:],_signal[nslow:]

In [9]:
# Prepare chart..
fig2, ax2 = plt.subplots()

# set title
plt.title('MACD ({1},{2},{3}):{0} - {4} minutes bars.'.format(product_code,nslow,nfast,nema,bar_range))

# setup x-axis for date time values.
dateArray=_macd_t
plt.xticks(dateArray)
fig2.autofmt_xdate(rotation=90)

# setup axis
ax2.autoscale(enable=True)
ax2.grid(True)
ax2.xaxis.set_major_formatter(md.DateFormatter('%Y-%m-%d %H:%M'))
#ax.yaxis.

# draw it!
ax2.plot(_macd_t,_macd,label='MACD:')
ax2.plot(_macd_t,_signal,label='Signal:')
ax2.legend(loc=1)

#MACD Histogram
ax2_second=ax2.twinx()
ax2_second.grid(True,color='r',alpha=0.1)
ax2_second.fill_between(_macd_t, _macd - _signal, 0, alpha=0.2, facecolor=fillcolor, edgecolor=fillcolor,label='MACD Histogram')
ax2_second.legend(loc=4)


Out[9]:
<matplotlib.legend.Legend at 0x7f18d46bd5c0>

In [ ]: