#!/usr/bin/env python # coding: utf-8 # In[1]: get_ipython().run_line_magic('matplotlib', 'inline') # In[3]: 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) # In[2]: 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) # In[4]: df = panel['AAPL'] # In[5]: 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') # ## ID3-lab # # Using the same plot commands, generate a dataset for the id3lab. # # http://rawgithub.com/dalejung/id3/master/demo/lab_demo/lab_demo.html # In[6]: # 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) # In[7]: with open('lab.json', 'w') as f: pass #f.write(lab.to_json()) # ## standalone pages # # `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. # # [Example link](https://www.evernote.com/shard/s9/sh/89b66ac6-3327-45c1-a051-bd969da7c0df/796f6fe46d3007bd4abecff788d24667/deep/0/ts-chart-id3lab-8673298.png) # # [Example output:](https://www.evernote.com/shard/s9/sh/35f6322c-3360-4c57-8e68-234938f4a8ce/44a8d1db9f0d3650c145fb799987ab63/deep/0/idale.local-8888-standalone-4ac720f8-8097-51d8-9285-ac1bad518b5a-id3lab-to_html-and-1.-tmux-(ssh).png) # In[7]: from id3lab import ID3Lab lab = ID3Lab() # In[8]: for symbol in panel.keys(): plot(lab, symbol) lab # In[27]: from ts_charting import json # In[ ]: