Git Product home page Git Product logo

alpaca-zipline's Introduction

Zipline Docker Image for Alpaca API

This is the Alpaca official docker container packages zipline. With this container image, you can easily run your Quantopian algorithm with live trading. This is to run your algorithm in your computer, or server by yourself, and Alpaca is also desiging a "one-click" solution to run your algorithm without having your server.

Prerequisite

All you need is docker installed in your system.

Usage

There are two usages for this container. One is a Jupyter notebook enivronment for the exploration purpose, and the other is a CLI environment to launch your algorithm for real money trading.

In either case, you should set Alpaca API Key environment variables first.

$ export APCA_API_KEY_ID=xxxxx
$ export APCA_API_SECRET_KEY=yyyy

The container installs Alpaca's official python SDK.

Jupyter notebook environment

By default, the container launches a Jupyter notebook with no password on the port 8888. You need to forward the port so you can access the notebook from your browser.

$ docker run -it --rm \
    -e APCA_API_KEY_ID -e APCA_API_SECRET_KEY -p 8888:8888 \
    alpacamarkets/alpaca-zipline

Then, open http://localhost:8888/ . You can start accessing Alpaca API immediately on this environment.

CLI environment

You can start a CLI environment using the same image as below.

# some data directory to download the bundle data
$ DATADIR=/data
# the directory where your algorithm file is located
$ ALGODIR=/algorithm
$ docker run -it --rm -e APCA_API_KEY_ID -e APCA_API_SECRET_KEY \
    -v $DATADIR:/root/.zipline/data \
    -v $ALGODIR:/work -w /work \
    alpacamarkets/alpaca-zipline bash

Modify DATADIR and ALGODIR for your needs. The data directory is used to store the bundle data and better to be mounted from the host directory. The algorithm directory is where our algorithm file is located.

Download alpaca-bundle

In order to run the live trading, you still need to have a bundle data. Since zipline's default quantopian-quandl bundle covers very limited data (3k stocks with daily only), Alpaca offers more realistic data bundle called alpaca-bundle (8k stocks with daily/minute last 3000 bars). From inside the container, simply run:

$ zipline ingest -b alpaca-bundle

It may take a couple of minutes to complete.

Run algorithm

Once the bundle is saved, you are ready to run your algorithm.

$ zipline run -f /work/myalgo.py \
    --broker=alpaca --broker-uri=dummy \
    --state-file /work/state \
    --realtime-bar-target /work/realtime-bars/ \
    --bundle alpaca-bundle --data-frequency minute

Note the --broker-uri parameter is ignored with alpaca broker, but you have to specify something. The --state-file is the file location where the zipline context is serialized.

Compatibility

Quantopian runs each algorithm in Python 2, but this container is packaged in Python 3. You may need to adjust some of your code for Python 3. You will also need to insert this code at the beginning of your algorithm file that fills Quantopian's implicit import modules.

from zipline.api import *
import logbook
log = logbook.Logger('algo')

Support

While we have verified a few algorithms runnable with this image, we are aware of many different use cases, and we are happy to hear your issues, and please let us know. Alpaca constantly keeps improving the image so that it covers more use cases.

alpaca-zipline's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

alpaca-zipline's Issues

Docker Build Failing on Git

When attempting to install on my Ubuntu 18.04 machine i receive the following error:

Collecting git+https://github.com/alpacahq/zipline@98e860b55c9fc4a8a825fe6c0f558b0fb2866bf8
  Cloning https://github.com/alpacahq/zipline (to revision 98e860b55c9fc4a8a825fe6c0f558b0fb2866bf8) to /tmp/pip-req-build-dptyknir
fatal: reference is not a tree: 98e860b55c9fc4a8a825fe6c0f558b0fb2866bf8
Command "git checkout -q 98e860b55c9fc4a8a825fe6c0f558b0fb2866bf8" failed with error code 128 in /tmp/pip-req-build-dptyknir
The command '/bin/sh -c pip install git+https://github.com/alpacahq/zipline@98e860b55c9fc4a8a825fe6c0f558b0fb2866bf8' returned a non-zero code: 1

Please let me know if additional information is needed.

Thank You

Alpaca-Bundle Not working

In Jupyter, via my conda env, I run:

!zipline ingest -b alpaca-bundle

and receive the following:

Error: No bundle registered with the name 'alpaca-bundle'

Any ideas?

zipline example algo's fail on "JSONDecodeError" using docker image

Hi,

I successfully built the docker image and ingested the alpaca-bundle, as described in the documentation.

zipline ingest -b alpaca-bundle
Downloading Bundle: alpaca-bundle [####################################] 100%
Writing data to /root/.zipline/data/alpaca-bundle/2019-07-08T07;05;52.204324.

When running an example algorithm, however, I always get a json.decoder error (see below). Is this error coming from the ingested bundle or something different with regards to recent zipline issues? Please let me know if you are able to reproduce the error.

(using bash shell in alpaca-zipline docker image):

zipline run -f /work/examples/dual_moving_average.py --broker=alpaca --broker-uri=dummy --state-file /work/state --realtime-bar-target /work/realtime-bars --bundle alpaca-bundle --data-frequency daily --start 2015-1-1 --end 2016-1-1

2019-07-08 07:30:21.428332] INFO: Loader: Cache at /root/.zipline/data/SPY_benchmark.csv does not have data from 1990-01-02 00:00:00+00:00 to 2019-07-03 00:00:00+00:00.

[2019-07-08 07:30:21.428517] INFO: Loader: Downloading benchmark data for 'SPY'.
Traceback (most recent call last):
File "/usr/local/bin/zipline", line 10, in
sys.exit(main())
File "/usr/local/lib/python3.5/site-packages/click/core.py", line 764, in call
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.5/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.5/site-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.5/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/zipline/main.py", line 102, in _
return f(*args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/click/decorators.py", line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/zipline/main.py", line 316, in run
realtime_bar_target=realtime_bar_target
File "/usr/local/lib/python3.5/site-packages/zipline/utils/run_algo.py", line 142, in _run
env = TradingEnvironment(asset_db_path=connstr, environ=environ)
File "/usr/local/lib/python3.5/site-packages/zipline/finance/trading.py", line 99, in init
self.bm_symbol,
File "/usr/local/lib/python3.5/site-packages/zipline/data/loader.py", line 165, in load_market_data
environ,
File "/usr/local/lib/python3.5/site-packages/zipline/data/loader.py", line 215, in ensure_benchmark_data
data = get_benchmark_returns(symbol)
File "/usr/local/lib/python3.5/site-packages/zipline/data/benchmarks.py", line 36, in get_benchmark_returns
data = json.loads(r.text)
File "/usr/local/lib/python3.5/json/init.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Thanks very much for your attention to this. I would very much like to get zipline properly setup to continue porting my algorithms into Alpaca's trading platform.

Regards,

Joseph Oravetz

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.