import ts_charting as charting
import pandas.io.data as pdd
import pandas as pd
charting.figsize(13, 10)
df = pdd.get_data_yahoo('AAPL')
spy = pdd.get_data_yahoo('SPY')
df = df.tail(100)
spy = spy.tail(100)
returns = df.Close.pct_change()
spy_returns = spy.Close.pct_change()
importing ts_charting
will install the ohlc_plot
for pd.DataFrame
. Also we plot markers for every day that ends up higher than yesterdays close.
fig = charting.figure(1)
df.ohlc_plot()
(df.Close.pct_change() > 0).fplot_markers('close up', yvalues=(df.Open + df.Close) / 2, color='green')
fig.line(460)
overnight = np.log(df.Open / df.Close.shift(1))
intraday = np.log(df.Close / df.Open)
overnight_roll = pd.rolling_sum(overnight, 10)
intraday_roll = pd.rolling_sum(intraday, 10)
fplot supports a yax
parameters which creates a named axis. This allows you to have an infinite number of y-axes. It also supports secondary_y
which is the same as yax="right"
A right side spline will be added for each y-axis.
fig = charting.figure(1)
df.ohlc_plot()
(df.Close.pct_change() > 0).fplot_markers('close up', yvalues=(df.Open + df.Close) / 2, color='green')
(returns + 1).cumprod().fplot('equity', secondary_y=True)
overnight_roll.fplot('overnight returns', yax='10day')
intraday_roll.fplot('intraday returns', yax='10day')
Note that due to misunderstanding matplotlib, subplots are selected via a set_ax. Probably need to change that, and the datetime formatter looks wonky for plots other than the first.
fig = charting.figure(2)
fig.set_ax(1)
df.ohlc_plot()
(df.Close.pct_change() > 0).fplot_markers('close up', yvalues=(df.Open + df.Close) / 2, color='green')
overnight_roll.fplot('overnight returns', yax='10day')
intraday_roll.fplot('intraday returns', yax='10day')
fig.set_ax(2)
(returns + 1).cumprod().fplot('equity')
(spy_returns + 1).cumprod().fplot('spy')
fig.set_xticks('MS')
Note the difference bewteen subplot 2 and 3. Both only plot the last 10 days.
fig = charting.figure(3)
fig.set_ax(1)
df.ohlc_plot()
(df.Close.pct_change() > 0).fplot_markers('close up', yvalues=(df.Open + df.Close) / 2, color='green')
overnight_roll.fplot('overnight returns', yax='10day')
intraday_roll.fplot('intraday returns', yax='10day')
fig.set_ax(2)
(returns + 1).cumprod().tail(10).fplot('equity')
(spy_returns + 1).cumprod().tail(10).fplot('spy')
fig.set_ax(3, sharex=1)
(returns + 1).cumprod().tail(10).fplot('equity')
(spy_returns + 1).cumprod().tail(10).fplot('spy')
fig.hl_span(start, end)
fig = charting.figure(2)
fig.set_ax(1)
df.ohlc_plot()
(df.Close.pct_change() > 0).fplot_markers('close up', yvalues=(df.Open + df.Close) / 2, color='green')
overnight_roll.fplot('overnight returns', yax='10day')
intraday_roll.fplot('intraday returns', yax='10day')
fig.hl_span("4/29/2013", "5/20/2013", color='red')
fig.set_ax(2, sharex=1)
(returns + 1).cumprod().fplot('equity')
(spy_returns + 1).cumprod().fplot('spy')
fig.hl_span("3/18/2013", "4/08/2013", color='red')
plot
takes a method
parameters which gets passed along to an internal reindex
call. This can be used to find values for missing or mis-aligned datasets
week_max = df.High.resample('W', 'max', label='left').tshift(-1, '1h')
week_min = df.Low.resample('W', 'min', label='left').tshift(-3, '1h')
# note these are both sparse (anchored on saturday) and misaligned (happens at 11 or 9 PM)
week_max
Date 2013-02-09 23:00:00 484.94 2013-02-16 23:00:00 462.73 2013-02-23 23:00:00 455.12 2013-03-02 23:00:00 435.43 2013-03-09 23:00:00 444.23 2013-03-16 23:00:00 462.10 2013-03-23 23:00:00 469.95 2013-03-30 23:00:00 443.70 2013-04-06 23:00:00 437.99 2013-04-13 23:00:00 427.89 2013-04-20 23:00:00 418.77 2013-04-27 23:00:00 453.23 2013-05-04 23:00:00 465.75 2013-05-11 23:00:00 457.90 2013-05-18 23:00:00 448.35 2013-05-25 23:00:00 457.10 2013-06-01 23:00:00 454.43 2013-06-08 23:00:00 449.08 2013-06-15 23:00:00 435.70 2013-06-22 23:00:00 408.66 2013-06-29 23:00:00 422.98 dtype: float64
fig = charting.figure(1)
fig.set_ax(1)
df.ohlc_plot()
week_max.fplot('week_max', method='ffill', color='green')
week_min.fplot('week_min', method='ffill', color='red')
By default the TimestampLocator
will make a best guess how where to place ticks. This is done by grabbing the lowest freq that can support a min_tick of 5.
However, you can explicitly set the tick frequency and also pass in custom dates
fig = charting.figure(3)
fig.set_ax(1)
(returns + 1).cumprod().fplot('equity')
fig.set_xticks('MS') # pandas freq
fig.set_ax(2, sharex=1)
(returns + 1).cumprod().fplot('equity')
fig.set_xticks([pd.Timestamp("2013-03-01"), pd.Timestamp("2013-05-14")]) # explicit dates
fig.set_ax(3, sharex=1)
(returns + 1).cumprod().fplot('equity')
fig.set_xticks(returns > 0.02) # set tick whenver we are above .02