Skip to main content
https://img.shields.io/travis/ChrisPappalardo/stockbot.svg https://img.shields.io/pypi/v/stockbot.svg

Stock market analysis library written in Python.

Features

  • Market data sourcing from Yahoo!, CNBC, and zipline bundles

  • S&P500 stock listing scraper

  • ADX, DI, and Stochastic technical indicators implemented using TA-lib

  • Average Directional Movement Index (ADX) ranking for portfolios

  • Trending and oscillating instrument trading algorithms for zipline

Installation

Install the latest package with:

$ pip install stockbot

The dependencies are not trivial and may not install properly on your system through pip. We recommend developing and deploying your projects that use stockbot in containers with the necessary packages pre-installed.

One way to do this is to first build the quantopian/zipline Docker image with the following command:

$ docker build -t quantopian/zipline https://github.com/quantopian/zipline.git#1.0.2

Using a docker-compose development configuration similar to the one contained in stockbot, you could then create a development container with:

$ docker-compose -f docker-compose-dev.yml up

Note that our docker configuration installs the latest zipline quantopian-quandl bundle in the project root. This is necessary for the default stockbot configuration when using functions such as get_zipline_dp and get_zipline_hist.

Usage

Stockbot can provide you with a list of S&P500 stocks from wikipedia:

>>> from stockbot.core import get_sp500_list
>>> get_sp500_list()
[u'MMM', u'ABT', u'ABBV', u'ACN', u'ATVI', u'AYI', u'ADBE', ... u'ZTS']

To get a delayed quote from Yahoo! use get_yahoo_quote:

>>> from stockbot.sources import get_yahoo_quote
>>> get_yahoo_quote('YHOO')
{'volume': 3405057, 'last': 41.0, 'symbol': 'YHOO', 'datetime': datetime.datetime(2016, 11, 22, 18, 0, tzinfo=<UTC>), 'high': 41.4, 'low': 40.83, 'open': 41.2, 'change': -0.11}

Or a real-time quote from CNBC using get_cnbc_quote:

>>> from stockbot.sources import get_cnbc_quote
>>> next(get_cnbc_quote('YHOO'))
{'volume': 3528566, 'last': 41.04, 'symbol': u'YHOO', 'datetime': datetime.datetime(2016, 11, 22, 21, 0, tzinfo=<UTC>), 'high': 41.395, 'low': 40.83, 'open': 41.2, 'change': -0.07}

Note:: get_cnbc_quote returns a generator

Stockbot returns quote data using a dict like object stockbot.marketdata.MarketData that performs certain data and datetime processing.

Historical data can be obtained from Yahoo! using get_yahoo_hist:

>>> from stockbot.sources import get_yahoo_hist
>>> get_yahoo_hist('YHOO')
{'high': 41.48, 'last': 41.110001, 'datetime': datetime.datetime(2016, 11, 21, 21, 0, tzinfo=<UTC>), 'volume': 11338000, 'low': 40.939999, 'close': 41.110001, 'open': 41.439999}

Historical data can also be obtained from zipline bundles using the get_zipline_hist function:

>>> from stockbot.sources import get_zipline_hist
>>> get_zipline_hist('YHOO', 'close',
2016-01-04 00:00:00+00:00    31.41
Freq: C, Name: Equity(3177 [YHOO]), dtype: float64

Look up symbols with stockbot.sources.get_symbol which searches Yahoo! finance for the passed term.

Zipline trading algorithms that utilize the Directional Movement technical indicator system are provided in stockbot.algo. For example, the following zipline trading algorithm would use ADX and DI to trade the top trending stocks and Stochastic Oscillators to trade the top oscillating stocks in the S&P 500 index:

from logbook import Logger
from stockbot.algo.core import (
    adx_init,
    trade_di,
    trade_so,
)
from stockbot.core import get_sp500_list

def initialize(context):
    return adx_init(
        context,
        name='adx_di_so',
        top_rank=5,
        bot_rank=5,
        di_window=14,
        symbols=get_sp500_list(),
        log=Logger('Stockbot'),
    )

def handle_data(context, data):
    # increment counter and log datetime
    context.i += 1
    context.adx['log'].info('processing %s' % context.get_datetime())

    # trade trending S&P500 stocks using the DI system
    trade_di(
        context,
        data,
        window=context.adx['di_window'],
        portfolio=[i for (i, adx) in context.adx['top']],
        capital_ppi=1.0/(len(context.adx['top'])+len(context.adx['bot'])),
        log=context.adx['log'],
    )

    # trade oscillating S&P500 stocks using the SO system
    trade_so(
        context,
        data,
        window=context.adx['di_window'],
        portfolio=[i for (i, adx) in context.adx['bot']],
        capital_ppi=1.0/(len(context.adx['top'])+len(context.adx['bot'])),
        log=context.adx['log'],
    )

To run this algorithm in a docker container, copy the code above into a file and issue the following:

$ zipline run -f <file> --start <date> --end <date>

Use the the <YYYY-M-D> format for dates. Use -o /path/file.pickle to capture pickled results that can be used in python.

History

0.1.0 (2015-09-23)

  • created basic market data sourcing from Yahoo! and CNBC

0.2.0 (2016-11-22)

  • created S&P500 stock listing scraper

  • implemented TA-lib for technical analysis (ADX, DI, STOCH)

  • added zipline bundles to data sourcing

  • created zipline trading algorithms for trending and oscillating instruments

Metadata

Release files for stockbot 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stockbot 0.2.0
File Size Uploaded
stockbot-0.2.0.tar.gz 94.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for stockbot 0.2.0
File Interpreter ABI Platform
stockbot-0.2.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 108.2 kB

Release files / stockbot-0.2.0.tar.gz

Download URL stockbot-0.2.0.tar.gz
Size 94.5 kB
Tags Source
SHA-256 checksum
How to use checksums
fb6ed254f28f06de8be054b74a8e8c9dc224c9b658b39402c112ffa637dec27a
BLAKE2b-256 checksum
How to use checksums
2e141435092093d3bd1b36a341d832e12c9f6f5661b8a760836adeb970b99dd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / stockbot-0.2.0-py2.py3-none-any.whl

Download URL stockbot-0.2.0-py2.py3-none-any.whl
Size 13.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
35584c1add67aab4c5133ebf0fe1558586a2c30cc39b4f27b36e93622f107974
BLAKE2b-256 checksum
How to use checksums
d206ae78da1d82499c92688dbafec0c6f4baaf671cebfba92a2805ff443cd35d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page