Code Monkey home page Code Monkey logo

trading-bot-1's Introduction

Trading Bot

Headless algorithmic trading bot using market data from Alpaca API for real time data subscription/a paper trading account, and implemented using a bespoke Django wrapper for Alpaca's Python client.

Trades are placed with Alpaca Paper trading platform, with market data periodically sourced using Docker containers running Celery and Redis, and stored using a Postgresql database.

Current supported strategies:

  • 14 day moving average
  • 7 day moving average
  • // TODO

Author

Built with

  • Django - Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.
  • Django-Rest-Framework - Django REST framework is a powerful and flexible toolkit for building Web APIs.
  • Docker - Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers.
  • Celery - Celery is a simple, flexible, and reliable distributed system to process vast amounts of messages, while providing operations with the tools required to maintain such a system. It’s a task queue with focus on real-time processing, while also supporting task scheduling.
  • Redis - Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker
  • Visual Studio Code - IDE

Instructions

  1. Install Docker Desktop. Create your own Alpaca API Account.

  2. Navigate to your server directory, and create a .env file. Copy the included .env.example in this repo for a starter file;

$ cd server
$ cp .env.example .env

Note: setting POSTGRES_HOST_AUTH_METHOD=trust means Postgresql does not require a password. This is used only in development mode, being run on localhost

  1. Activate your virtual environment, and navigate back into your project folder if necessary
$ pipenv shell
  1. Run docker compose up to initialise django server, postgres for databasing, and celery/celery-beat/redis for handling of background tasks.
$ (server) docker compose up

Note: you may have to give wait-for-it.sh and docker-entrypoint.sh executable permissions

$ (server) chmod +x ./wait-for-it.sh && chmod +x ./docker-entrypoint.sh
  1. To run the server for the first time, you will have to run database migrations. and create a superuser, and test data (refer core.management.bootstrap.py for detail on these commands). To do this you can use docker compose and the web container
# Run migrations
$ (server) docker compose exec web python manage.py migrate
# Create superuser account
$ (server) docker compose exec web python manage.py bootstrap -cs
# Create example data
$ (server) docker compose exec web python manage.py bootstrap -ced
  1. Create a strategy via POST to http://localhost:8000/v1/strategies/ or through your browser using Django's admin at http://localhost:8000/admin/core/strategy/

  2. Keep docker and your local server running for periodic tasks to perform the given strategy created in 6.

Testing

$ (server) docker compose exec web python manage.py test
  • Optionally add --keepdb to persist database between tests, and --verbosity=2 to receive verbose output of tests
$ (server) docker compose exec web python manage.py test --keepdb --verbosity=2

Dependencies

[packages]
django
djangorestframework
django-environ
alpaca-trade-api
celery
django-celery-beat
psycopg2
psycopg2-binary

[dev-packages]
django-debug-toolbar
freezegun

Note

To make running docker compose commands on your local machine easier, use alias' in .bashrc/.zshrc configuration files eg.

# ~/.zshrc file
[...]
alias pm="docker compose exec web python manage.py"

Then the command docker compose exec web python manage.py test server becomes pm test server

Licence

This project is licensed under the terms of the MIT license

trading-bot-1's People

Contributors

jdalton92 avatar

Stargazers

WebClinic avatar

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.