Please note that this article is for educational and informational purposes only. All screenshots are for illustrative purposes only. The views and opinions expressed are those of the author and do not reflect or represent the views and opinions of Alpaca. Alpaca does not recommend any specific securities or investment strategies.
Author: Mike Wright is the founder of RoboBull, an open-source initiative for algorithmic trading and stock analysis. He is also Head of Engineering at Titanbay, a private equity investment platform.
Disclaimer: We strongly advise that you thoroughly test this project, its settings, and algorithms with a paper trading account. RoboBull, or the author, accepts no liability whatsoever in the usage of this project in accordance with the source code’s provided license.
The RoboBull Trading Bot is a fully automated trading bot which analyzes stock market data with a variety of technical indicators. The idea for the creation of RoboBull came about in response to finding a simple, yet extendable, way to to work with market data providers (such as Alpaca) and test out the effectiveness of technical indicators (and combining of them).
RoboBull works by mapping Technical indicators to specified weightings which determine whether a signal (BUY/SELL) is generated. If an overall weighting threshold is met, based on generated signals, an order is created. The bot currently supports both live trading (paper account recommended!) and historical back testing, both via Alpaca's API as an integrated trading provider. We also plan to support other providers as well as Crypto trading in the future.
We've integrated our Stock Screener into this project which provides market analyses and top performing stocks. Our top 150 stocks (ranked in order) should be automatically loaded into your bot every 15 minutes to keep your signals and market data fresh. We aim to provide this data for FREE as long as we can. Donations or sponsorship welcome for upkeep! Of course, please feel free to disable our screener by setting useStockScreener to false in config/settings.json. You can also change any of the default stocks in the config/stocks.json file.
Let’s get started and running RoboBull
First, you will need to pull down (download) our code via Github.
- You’ll need to use Docker Compose which requires you to first install Docker.
- Go to Alpaca and sign up for a free account. We recommend paper trading only for using this project. You don't need to spend real dollars to test this project. Once you have a paper trading account, you can create your API key and secret which will be needed in the next step.
- Copy .env-example file and rename it to .env and then add your Alpaca key (API_KEY) and secret (SECRET_API_KEY). Important: keep your key/secret on your local machine only, don't commit it or share with anyone.
- Ensure you have edited your settings.json for live/paper trading or back testing
- Run docker-compose up
- Create a new user by going here: (http://localhost:3000/create-user). You'll then be redirected to the trading terminal. You only need to create a new user once.
- Once you've created a new user, you can re-run the trading bot in the future by visiting (http://localhost:3000/run). However, you may need to stop the docker container and start it again to rerun the trading bot (this is due to only 1 connection allowed per trading account on Alpaca).
- You can also visit (http://localhost:8081) to view and manage MongoDb data
Tip: If the US markets are closed, then you can use back testing instead. More on this later.
Basic Settings
It's worth familiarizing yourself with the basic settings before you run docker-compose up during the installation. You can find all settings and their explanations later in this repo’s readme.
Tip: You may need to restart your Docker container each time you make a change to your settings or code. Just Control-C out of the current process and then docker-compose up again.
settings.json
- provider alpaca - trading provider to use (currently only Alpaca is available)
- isPaper true/false - to use paper trading account (default: true)
- isBacktest true/false - to use back testing functionality (default: false)
Tip: All positions will automatically close (sell) 15 mins before market close. It may be beneficial to keep your positions, especially if you're at a loss, but it's completely up to you. If you wish to do so, set useClosePositionsBeforeMarketClose to false (then restart the docker process).
stocks.json
Edit your stock symbols/tickers in this file. The stocks will be used as your default stocks for live trading as well as the stocks used for back testing (if enabled).
algos.json
The algorithms / technical indicators are massively customizable and have been tested for optimal performance. It may be better to retain the current setup and use the basic properties below until you have spent time analysing your trading ROI (both live/paper and back testing).
- thresholdToBuy 35 - overall weightings threshold from signals to trigger a BUY order
- thresholdToSell 25 - overall weightings threshold from signals to trigger a SELL order
- useStandardAlgos true/false - to use standard technical indicators like bullish/bearish patterns (default: false)
Feel free to tweak the settings in algo.json file as much as you like. A more detailed explanation of algo settings is coming soon. Tip: You may want to increase the thresholdToBuy setting to 40 which will tell the bot to be stricter when buying stocks.
Run in "Console Only" mode
This feature allows you to run both live/paper trading and back testing without the need of a web browser. In order to do this open a terminal tab:
- Run docker-compose up
Then open another terminal tab:
- Run docker exec -it $(docker ps -aqf "ancestor=trading-bot_app") npm run console
Back Testing
Change the following in settings.json, then re-run docker-compose up and go to http://localhost:3000/run
- provider make sure this is set to alpaca
- isPaper make sure this is set to true
- isBacktest make sure this is set to true
- startingCapital set this to the amount you wish to start back testing with (currency is always set to USD)
- startDate set your start date for back testing
- endDate set your end date for back testing
Settings / Configuration
- provider alpaca - trading provider to use (currently only Alpaca is available)
- isPaper true/false - to use paper trading account (default: true)
- isBacktest true/false - to use back testing functionality (default: false)
- resetSignals true/false - to reset signals after calculating signals (default: true)
- startingCapital 100000 - starting capital (used during back testing)
- thresholdCapitalAllowance 10 - how much capital allowance usage to calculate portfolio percentage (reduces percentage)
- thresholdRiskAllocation 2 - how much risk allocation to apply to portfolio percentage (multiplies percentage)
- thresholdCapitalRetention 10000 - how much capital to retain during trading
- thresholdBuyCap 0.12 - how much capital any given trade can amount to
- thresholdBuyTimeRestriction -60 - how long in minutes a position must be held before being sold
- thresholdStockCap 15 - how many positions can be held at once
- orderStopLoss -0.006 - how much a position can lose in return on investment before selling
- orderTakeProfit 0.008 - how much a positions can gain in return on investment before selling
- orderHoldUntilProfit false - whether to stop selling a position at a loss (holding until profit)
- roiToClosePositions 0.3 - how much the overall ROI must be over to close all current positions and end trading
- roiToResetPositions -0.5 - how much the overall ROI must be below to gradually close poor performing positions (currently disabled / needs refactoring)
- startDate "2021-02-01 00:00:00" - start date used for back testing
- endDate "2021-02-01 00:00:00" - end date used for back testing
- useClosePositionsBeforeMarketClose true/false - closes/sells all open positions before market is closed (default: true)
- usePreviousUserSession true/false - to use previous user's session (no longer exists, default: false)
- useDefaultStocks true/false - to use a list of stocks by default and also required for back testing (default: true)
- useStockScreener true/false - to use a list of high performing stocks from RoboBull Stock Screener every 15mins to ensure that best stocks are used during live/paper trading (default: true, fallback if empty is to use default stocks)
- useStockQuotePercentage true/false - to use Financial Modeling Prep API for latest stock percentage change before ordering. Requires third-party API key. (default: false)
- stockQuotePercentageChangeRangeTo 15 - how much of a percentage that a stock's change can be increased up to when processing order. This is to help against volatile stocks along with pump and dumps (only used during live/paper trading)
- stockQuotePercentageChangeRangeFrom 1 - currently not used
We hope you’ve enjoyed this article. For more on this project, please visit RoboBull or head over to our GitHub repo for the most up-to-date documentation.
Alpaca does not prepare, edit, or endorse Third Party Content. Alpaca does not guarantee the accuracy, timeliness, completeness or usefulness of Third Party Content, and is not responsible or liable for any content, advertising, products, or other materials on or available from third-party sites.
Brokerage services are provided by Alpaca Securities LLC ("Alpaca"), member FINRA/SIPC, a wholly-owned subsidiary of AlpacaDB, Inc. Technology and services are offered by AlpacaDB, Inc.