• Stars
    star
    615
  • Rank 71,405 (Top 2 %)
  • Language
    Python
  • Created over 5 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Example Order Book Imbalance Algorithm

Example HFT-ish Algorithm for Alpaca Trading API

The aim of this algorithm is to capture slight moves in the bid/ask spread as they happen. It is only intended to work for high-volume stocks where there are frequent moves of 1 cent exactly. It is one of the trading strategies based on order book imbalance. For more details about it, please refer to Darryl Shen, 2015 or other online articles.

This algorithm will make many trades on the same security each day, so any account running it will quickly encounter PDT rules. Please make sure your account balance is well above $25,000 before running this script in a live environment.

This script also presents a basic framework for streaming-based algorithms. You can learn how to write your algorithm based on the real-time price updates.

Setup

This algorithm runs with Python 3.6 or above. It uses Alpaca Python SDK so make sure install it beforehand, or if you have pipenv, you can install it by

$ pipenv install

in this directory.

API Key

In order to run this algorithm, you have to have Alpaca Trading API key. Please obtain it from the dashboard and set it in enviroment variables.

export APCA_API_KEY_ID=<your key id>
export APCA_API_SECRET_KEY=<your secret key>

Run

$ python ./tick_taker.py

The parameters are following.

  • --symbol: the stock to trade (defaults to "SNAP")
  • --quantity: the maximum number of shares to hold at once. Note that this does not account for any existing position; the algorithm only tracks what is bought as part of its execution. (Default 500, minimum 100.)
  • --key-id: your API key ID. (Can also be set via the APCA_API_KEY_ID environment variable.)
  • --secret-key: your API key secret. (Can also be set via the APCA_API_SECRET_KEY environment variable.)
  • --base-url: the URL to connect to. (Can also be set via the APCA_API_BASE_URL environment variable. Defaults to "https://paper-api.alpaca.markets" if using a paper account key, "https://api.alpaca.markets" otherwise.)

The algorithm can be stopped at any time by sending a keyboard interrupt CTRL+C to the console. (You may need to send two CTRL+C commands to kill the process depending where in the execution you catch it.)

Note

Please also note that this algorithm uses the Polygon streaming API with Alpaca API key, so you have to have a live trading account setup. For more details about the data requirements, please see Alpaca documentation.

More Repositories

1

marketstore

DataFrame Server for Financial Timeseries Data
Go
1,814
star
2

alpaca-trade-api-python

Python client for Alpaca's trade API
Python
1,552
star
3

pylivetrader

Python live trade execution library with zipline interface.
Python
622
star
4

example-scalping

A working example algorithm for scalping strategy trading multiple stocks concurrently using python asyncio
Python
619
star
5

Momentum-Trading-Example

An example algorithm for a momentum-based day trading strategy.
Python
568
star
6

alpaca-backtrader-api

Alpaca Trading API integrated with backtrader
Python
524
star
7

alpaca-trade-api-js

Node.js library for Alpaca Trade API.
JavaScript
428
star
8

alpaca-trade-api-go

Go client for Alpaca's trade API
Go
253
star
9

alpaca-py

The Official Python SDK for Alpaca API
Python
249
star
10

alpaca-trade-api-csharp

C# SDK for Alpaca Trade API https://docs.alpaca.markets/
C#
208
star
11

pipeline-live

Pipeline Extension for Live Trading
Python
198
star
12

alpaca-ts

A TypeScript Node.js library for the https://alpaca.markets REST API and WebSocket streams.
TypeScript
144
star
13

Alpaca-API

The Alpaca API is a developer interface for trading operations and market data reception through the Alpaca platform.
136
star
14

pymarketstore

Python driver for MarketStore
Python
103
star
15

user-docs

Documentation for the Alpaca platform.
HTML
72
star
16

samplealgo01

Sample algo
Python
69
star
17

slait

A time-series data cache
Go
47
star
18

roboadvisor

Python
43
star
19

example-portfolio-manager

Simple portfolio management script in python
Python
42
star
20

insomnia-workspace

An Insomnia Workspace for Alpaca API
34
star
21

ribbit-backend

Backend for Ribbit, Broker API Reference App
Go
33
star
22

blogmaterials

Jupyter Notebook
30
star
23

alpaca-postman

Postman collections for Alpaca's APIs
29
star
24

alpaca-zipline

Alpaca riding on a zipline
Python
28
star
25

ribbit-ios

iOS app for Ribbit, Broker API Reference App
Swift
28
star
26

plug-and-play-strategies

Jupyter Notebook
27
star
27

sp100algo

Follow S&P100
Python
16
star
28

alpaca-erasure

Example script to generate Erasure data
Python
15
star
29

slackbot-trader

A Slackbot that can access the Alpaca API
Python
14
star
30

ribbit-android

Android app for Ribbit, Broker API Reference App
Java
12
star
31

alpaca-docs

Developer documentation for Alpaca API's
HTML
10
star
32

broker-fastapi-backend

A reference backend for Alpaca's Broker API utilizing Python & FastAPI
Python
8
star
33

bkdocs

Broker API Documentation
HTML
8
star
34

notebooks

A collection of Jupyter notebooks for getting started with Alpaca
Jupyter Notebook
6
star
35

Alpaca-Flutter-Demo-App

A boilerplate trading application to trade crypto on Alpaca. Made with Flutter and Dart.
C++
5
star
36

alpacadecimal

Similar and compatible with decimal.Decimal, but optimized for Alpaca's data sets.
Go
5
star
37

supertrend_crypto_bot

An Alpaca Live Crypto Trading Bot built with Python!
Python
4
star
38

Triangular-Arbitrage-with-Alpaca-API-s

Python
3
star
39

quantopian-fundamentals-examples

Implementations of algorithms from https://blog.quantopian.com/fundamentals-contest-winners/ using Alpaca's API.
Python
3
star
40

plprofiler

Python
2
star
41

alpaca-discord-bot

Python
1
star
42

go-onfido

Wrapper around the go-onfido-openapi package
1
star
43

alexa-trader-skill

An Alexa skill that can access the Alpaca API
1
star
44

go-onfido-openapi

Golang sdk for onfido. Generated from openapi
Shell
1
star