Notebook
In [19]:
import numpy as np
import pandas as pd
import talib
import matplotlib.pyplot as plt
In [20]:
"""This cell defines the plot_candles function"""

def plot_candles(pricing, title=None,
                 volume_bars=False,
                 color_function=None,
                 overlays=None,
                 technicals=None,
                 technicals_titles=None):
    """ Plots a candlestick chart using quantopian pricing data.
    
    Author: Daniel Treiman
    
    Args:
      pricing: A pandas dataframe with columns ['open_price', 'close_price', 'high', 'low', 'volume']
      title: An optional title for the chart
      volume_bars: If True, plots volume bars
      color_function: A function which, given a row index and price series, returns a candle color.
      overlays: A list of additional data series to overlay on top of pricing.  Must be the same length as pricing.
      technicals: A list of additional data series to display as subplots.
      technicals_titles: A list of titles to display for each technical indicator.
    """
    def default_color(index, open_price, close_price, low, high):
        return 'r' if open_price[index] > close_price[index] else 'g'
    color_function = color_function or default_color
    overlays = overlays or []
    technicals = technicals or []
    technicals_titles = technicals_titles or []
    open_price = pricing['open_price']
    close_price = pricing['close_price']
    low = pricing['low']
    high = pricing['high']
    oc_min = pd.concat([open_price, close_price], axis=1).min(axis=1)
    oc_max = pd.concat([open_price, close_price], axis=1).max(axis=1)
    
    subplot_count = 1
    if volume_bars:
        subplot_count = 2
    if technicals:
        subplot_count += len(technicals)
    
    if subplot_count == 1:
        fig, ax1 = plt.subplots(1, 1)
    else:
        ratios = np.insert(np.full(subplot_count - 1, 1), 0, 3)
        fig, subplots = plt.subplots(subplot_count, 1, sharex=True, gridspec_kw={'height_ratios': ratios})
        ax1 = subplots[0]
        
    if title:
        ax1.set_title(title)
    x = np.arange(len(pricing))
    candle_colors = [color_function(i, open_price, close_price, low, high) for i in x]
    candles = ax1.bar(x, oc_max-oc_min, bottom=oc_min, color=candle_colors, linewidth=0)
    lines = ax1.vlines(x + 0.4, low, high, color=candle_colors, linewidth=1)
    ax1.xaxis.grid(False)
    ax1.xaxis.set_tick_params(which='major', length=3.0, direction='in', top='off')
    # Assume minute frequency if first two bars are in the same day.
    frequency = 'minute' if (pricing.index[1] - pricing.index[0]).days == 0 else 'day'
    time_format = '%d-%m-%Y'
    if frequency == 'minute':
        time_format = '%H:%M'
    # Set X axis tick labels.
    plt.xticks(x, [date.strftime(time_format) for date in pricing.index], rotation='vertical')
    for overlay in overlays:
        ax1.plot(x, overlay)
    # Plot volume bars if needed
    if volume_bars:
        ax2 = subplots[1]
        volume = pricing['volume']
        volume_scale = None
        scaled_volume = volume
        if volume.max() > 1000000:
            volume_scale = 'M'
            scaled_volume = volume / 1000000
        elif volume.max() > 1000:
            volume_scale = 'K'
            scaled_volume = volume / 1000
        ax2.bar(x, scaled_volume, color=candle_colors)
        volume_title = 'Volume'
        if volume_scale:
            volume_title = 'Volume (%s)' % volume_scale
        ax2.set_title(volume_title)
        ax2.xaxis.grid(False)
    # Plot additional technical indicators
    for (i, technical) in enumerate(technicals):
        ax = subplots[i - len(technicals)] # Technical indicator plots are shown last
        ax.plot(x, technical)
        if i < len(technicals_titles):
            ax.set_title(technicals_titles[i])
            
        
In [21]:
contract = symbols('QQQ')
ym_minute_pricing = get_pricing(contract, start_date = '2020-06-18 15:00:00', end_date = '2020-06-18 20:00:00', frequency = "minute")
last_hour = ym_minute_pricing[-60:]
ym_minute_pricing
Out[21]:
open_price high low close_price volume price
2020-06-18 15:00:00+00:00 244.060 244.255 244.060 244.220 122394.0 244.220
2020-06-18 15:01:00+00:00 244.210 244.300 244.160 244.200 73732.0 244.200
2020-06-18 15:02:00+00:00 244.190 244.230 244.090 244.130 59891.0 244.130
2020-06-18 15:03:00+00:00 244.145 244.160 244.030 244.030 55529.0 244.030
2020-06-18 15:04:00+00:00 244.030 244.088 244.000 244.020 39237.0 244.020
2020-06-18 15:05:00+00:00 244.030 244.130 244.028 244.050 60626.0 244.050
2020-06-18 15:06:00+00:00 244.050 244.130 244.030 244.125 34195.0 244.125
2020-06-18 15:07:00+00:00 244.140 244.270 244.120 244.180 54386.0 244.180
2020-06-18 15:08:00+00:00 244.180 244.280 244.180 244.220 31822.0 244.220
2020-06-18 15:09:00+00:00 244.225 244.290 244.172 244.220 36983.0 244.220
2020-06-18 15:10:00+00:00 244.210 244.245 244.170 244.230 28698.0 244.230
2020-06-18 15:11:00+00:00 244.269 244.300 244.200 244.235 29095.0 244.235
2020-06-18 15:12:00+00:00 244.230 244.240 244.130 244.140 39761.0 244.140
2020-06-18 15:13:00+00:00 244.150 244.280 244.140 244.190 27077.0 244.190
2020-06-18 15:14:00+00:00 244.200 244.250 244.161 244.210 21715.0 244.210
2020-06-18 15:15:00+00:00 244.200 244.440 244.190 244.400 75273.0 244.400
2020-06-18 15:16:00+00:00 244.390 244.530 244.390 244.420 120526.0 244.420
2020-06-18 15:17:00+00:00 244.420 244.420 244.320 244.390 40679.0 244.390
2020-06-18 15:18:00+00:00 244.400 244.465 244.370 244.465 25747.0 244.465
2020-06-18 15:19:00+00:00 244.441 244.510 244.440 244.440 43930.0 244.440
2020-06-18 15:20:00+00:00 244.450 244.470 244.370 244.380 32876.0 244.380
2020-06-18 15:21:00+00:00 244.377 244.420 244.340 244.400 30949.0 244.400
2020-06-18 15:22:00+00:00 244.400 244.400 244.270 244.320 18996.0 244.320
2020-06-18 15:23:00+00:00 244.306 244.330 244.220 244.265 84135.0 244.265
2020-06-18 15:24:00+00:00 244.280 244.290 244.210 244.250 61407.0 244.250
2020-06-18 15:25:00+00:00 244.250 244.270 244.135 244.170 91183.0 244.170
2020-06-18 15:26:00+00:00 244.160 244.240 244.110 244.230 99159.0 244.230
2020-06-18 15:27:00+00:00 244.240 244.350 244.220 244.310 50164.0 244.310
2020-06-18 15:28:00+00:00 244.305 244.310 244.140 244.150 43886.0 244.150
2020-06-18 15:29:00+00:00 244.150 244.230 244.060 244.205 49008.0 244.205
... ... ... ... ... ... ...
2020-06-18 19:31:00+00:00 243.415 243.498 243.260 243.409 72603.0 243.409
2020-06-18 19:32:00+00:00 243.390 243.509 243.380 243.490 59980.0 243.490
2020-06-18 19:33:00+00:00 243.490 243.630 243.460 243.620 106325.0 243.620
2020-06-18 19:34:00+00:00 243.615 243.850 243.590 243.620 234539.0 243.620
2020-06-18 19:35:00+00:00 243.615 243.710 243.580 243.660 104713.0 243.660
2020-06-18 19:36:00+00:00 243.650 243.795 243.620 243.780 112644.0 243.780
2020-06-18 19:37:00+00:00 243.780 243.830 243.700 243.780 83612.0 243.780
2020-06-18 19:38:00+00:00 243.770 243.820 243.730 243.810 67209.0 243.810
2020-06-18 19:39:00+00:00 243.805 243.945 243.805 243.910 87254.0 243.910
2020-06-18 19:40:00+00:00 243.930 244.020 243.930 244.000 78976.0 244.000
2020-06-18 19:41:00+00:00 244.000 244.025 243.871 243.910 80067.0 243.910
2020-06-18 19:42:00+00:00 243.920 243.935 243.870 243.910 120544.0 243.910
2020-06-18 19:43:00+00:00 243.915 243.915 243.790 243.795 51111.0 243.795
2020-06-18 19:44:00+00:00 243.789 243.850 243.760 243.820 51788.0 243.820
2020-06-18 19:45:00+00:00 243.820 243.850 243.755 243.825 41104.0 243.825
2020-06-18 19:46:00+00:00 243.825 243.920 243.790 243.865 52759.0 243.865
2020-06-18 19:47:00+00:00 243.866 243.899 243.760 243.770 66485.0 243.770
2020-06-18 19:48:00+00:00 243.775 243.810 243.760 243.780 48519.0 243.780
2020-06-18 19:49:00+00:00 243.780 243.865 243.740 243.830 71139.0 243.830
2020-06-18 19:50:00+00:00 243.830 243.900 243.770 243.810 56617.0 243.810
2020-06-18 19:51:00+00:00 243.820 244.120 243.620 243.670 176406.0 243.670
2020-06-18 19:52:00+00:00 243.670 243.800 243.590 243.790 55334.0 243.790
2020-06-18 19:53:00+00:00 243.800 243.985 243.800 243.980 57200.0 243.980
2020-06-18 19:54:00+00:00 243.980 244.140 243.955 244.110 167115.0 244.110
2020-06-18 19:55:00+00:00 244.100 244.240 244.030 244.225 116204.0 244.225
2020-06-18 19:56:00+00:00 244.220 244.260 244.160 244.260 65679.0 244.260
2020-06-18 19:57:00+00:00 244.265 244.270 244.040 244.170 99083.0 244.170
2020-06-18 19:58:00+00:00 244.170 244.290 244.170 244.280 118390.0 244.280
2020-06-18 19:59:00+00:00 244.275 244.275 244.200 244.235 155459.0 244.235
2020-06-18 20:00:00+00:00 244.240 244.460 244.210 244.290 456449.0 244.290

301 rows × 6 columns

In [ ]:
 
In [24]:
upper, middle, lower = talib.BBANDS(last_hour['close_price'].as_matrix())

RSI = talib.RSI(last_hour['close_price'].as_matrix())
plot_candles(last_hour,
             title='1 minute candles + Bollinger Bands + RSI',
             overlays=[upper, middle, lower],
             technicals=[RSI],
             technicals_titles=['RSI'])
In [ ]: