In [1]:
from os import sys
sys.version
Out[1]:
'3.5.1 |Anaconda 2.4.1 (x86_64)| (default, Dec  7 2015, 11:24:55) \n[GCC 4.2.1 (Apple Inc. build 5577)]'

获取比特币价格数据:

In [2]:
import pandas as pd
import io
import requests

url = 'http://api.bitcoincharts.com/v1/trades.csv?symbol=coinbaseUSD'

raw = requests.get(url).content

data = pd.read_csv(io.StringIO(raw.decode('utf-8')), names=['price', 'amount'], index_col=0)
data.index = pd.to_datetime((data.index.values * 1e9).astype(int))

data.head()
Out[2]:
price amount
2016-01-20 03:47:01 377.35 0.95571
2016-01-20 03:46:32 377.35 0.05540
2016-01-20 03:46:02 377.27 0.04510
2016-01-20 03:45:31 377.45 0.92573
2016-01-20 03:45:31 377.44 0.07600

由于比特币是24小时,没有开市闭市的概念,所以这里使用每十分钟作为一个周期,计算开盘,最高,最低和收盘:

In [3]:
dt = data['price'].resample('10Min',how='ohlc')
dt.head()
Out[3]:
open high low close
2016-01-18 23:10:00 380.60 383.79 379.42 382.94
2016-01-18 23:20:00 382.51 384.87 381.11 383.14
2016-01-18 23:30:00 383.26 384.74 383.09 384.37
2016-01-18 23:40:00 384.19 384.49 383.94 384.49
2016-01-18 23:50:00 384.48 385.00 384.48 384.89

载入talib,导入TRANGE, ATR, NATR

  • real = TRANGE(high, low, close)
In [4]:
from talib import ATR, NATR, TRANGE
tr = pd.DataFrame({'True Range':TRANGE(dt['high'].values,dt['low'].values,dt['close'].values)},index=dt.index)
tr.head()
Out[4]:
True Range
2016-01-18 23:10:00 NaN
2016-01-18 23:20:00 3.76
2016-01-18 23:30:00 1.65
2016-01-18 23:40:00 0.55
2016-01-18 23:50:00 0.52
  • real = ATR(high, low, close, timeperiod=14)
In [5]:
atr = pd.DataFrame({'Average True Range':ATR(dt['high'].values,dt['low'].values,dt['close'].values)},index=dt.index)
atr.tail()
Out[5]:
Average True Range
2016-01-20 03:00:00 1.625621
2016-01-20 03:10:00 1.553077
2016-01-20 03:20:00 1.538571
2016-01-20 03:30:00 1.541531
2016-01-20 03:40:00 1.469993
  • real = NATR(high, low, close, timeperiod=14)
In [6]:
natr = pd.DataFrame({'Normalized Average True Range':NATR(dt['high'].values,dt['low'].values,dt['close'].values)},index=dt.index)
natr.tail()
Out[6]:
Normalized Average True Range
2016-01-20 03:00:00 0.431669
2016-01-20 03:10:00 0.412482
2016-01-20 03:20:00 0.409260
2016-01-20 03:30:00 0.408688
2016-01-20 03:40:00 0.389557
In [7]:
from plotly import tools
import cufflinks as cf
from plotly.graph_objs import *
import plotly.plotly as py

cf.set_config_file(world_readable=True,offline=False, theme='ggplot')
ggplot=cf.colors.get_scales('ggplot')

trLine = Bar(
    x = tr.index,
    y = tr['True Range'],
    name = 'True Range',
    marker= dict(
        color = ggplot[1]
    )
)

atrLine = Scatter(
    x = atr.index,
    y = atr['Average True Range'],
    name = 'Average True Range',
    marker= dict(
        color= ggplot[4]
    )
)

natrLine = Scatter(
    x = natr.index,
    y = natr['Normalized Average True Range'],
    name = 'Normalized Average True Range',
    marker= dict(
        color= ggplot[5]
    )
)

candle = dt.iplot(kind='candle', asFigure = True )

fig = tools.make_subplots(rows=2, cols=1, shared_xaxes=True, shared_yaxes=True, specs=[[{}], [{}]],  
                          vertical_spacing=0.05)
fig['data'].extend(candle['data'])

fig.append_trace(trLine,2,1)
fig.append_trace(atrLine,2,1)
fig.append_trace(natrLine,2,1)

fig['layout'].update({'title':'Volatility Indicator functions'})
_ = py.iplot(fig, filename='talib/VolatilityIndicators')
This is the format of your plot grid:
[ (1,1) x1,y1 ]
[ (2,1) x1,y2 ]