%matplotlib inline
startDate = datetime(2008, 1, 1)
endDate = datetime(2012, 8, 1)
def getQuotes(symbol, start, end):
quotes = fin.quotes_historical_yahoo(symbol, start, end)
dates, open, close, high, low, volume = zip(*quotes)
data = {
'open': open,
'close': close,
'high': high,
'low': low,
'volume': volume
}
dates = pd.Index([datetime.fromordinal(int(d)) for d in dates])
return pd.DataFrame(data, index=dates)
data = {}
for s in ['AAPL', 'GOOG', 'INTC', 'MSFT', 'FB']:
data[s] = getQuotes(s, startDate, endDate)
panel = pd.Panel(data)
from datetime import datetime
import pandas as pd
import numpy as np
import matplotlib.finance as fin
import ts_charting as charting
import ts_charting.lab.lab as tslab
charting.figsize(20,10)
WARNING: ip.register_post_execute is deprecated, use ip.events.register('post_run_cell', func) instead.
df = panel['AAPL']
lab = tslab.Lab(draw=True)
fig = lab.station('AAPL')
df.tail(500).ohlc_plot()
pd.rolling_mean(df.close, 30).fplot('30 day ma', color='blue')
pd.rolling_mean(df.close, 90).fplot('90 day ma', color='red')
fig.plot_markers('gap up > 1%', (df.open / df.high.shift(1)) > 1.01, yvalues=df.open, color='lime')
fig.plot_markers('gap down > 1%', (df.open / df.low.shift(1)) < .99, yvalues=df.open, color='pink')
Using the same plot commands, generate a dataset for the id3lab.
http://rawgithub.com/dalejung/id3/master/demo/lab_demo/lab_demo.html
#
lab = tslab.Lab(draw=False)
def plot(lab, symbol):
fig = lab.station(symbol)
df = panel[symbol]
df.ohlc_plot()
pd.rolling_mean(df.close, 30).fplot('30 day ma', color='blue')
pd.rolling_mean(df.close, 90).fplot('90 day ma', color='red')
fig.plot_markers('gap up > 1%', (df.open / df.high.shift(1)) > 1.01, yvalues=df.open, color='lime')
fig.plot_markers('gap down > 1%', (df.open / df.low.shift(1)) < .99, yvalues=df.open, color='pink')
for symbol in panel.keys():
plot(lab, symbol)
with open('lab.json', 'w') as f:
pass
#f.write(lab.to_json())
ipycli
standalone with id3lab
Note to nbviewers
The actual standalone link is generated via javascript (in order to get kernel/host information available only in browser). The pics below show functionality when running on live ipython.
from id3lab import ID3Lab
lab = ID3Lab()
for symbol in panel.keys():
plot(lab, symbol)
lab
from ts_charting import json