This is a manual as well as autonomous trading bot in Typescript with web access! It allows users to run algorithms and create and deploy new algorithms quickly and easily. It allows paper trading as well as live trading (live pending approval).
It is just the beginning but my vision for this repo is as follows:
See it in action - https://tradingbotx.com
- Build a community driven strong open source algorithm base in Typescript for trading efficiently
- Make these available to users for free forever so anyone can benefit from these algorithms
- Take a list of ticker symbols and algorithm(s), and find the best deals in the market from within that list.
- Able to schedule algorithms based on user's preference
- Provide a rating mechanism for Algorithms, so users can see the popular/highly rated algorithms
- Seamlessly integrate with all popular brokerages and crypto exchanges
Let's see how it goes! It's only the start.
- π§Ή Rewrite the project in typescript
- π β¨ OAuth Support (https://alpaca.markets/docs/build-apps_services-with-alpaca/oauth-guide/)
- π β¨ Trade with watchlists
- π β¨ Watchlist
- π β¨ SSL
- π§Ή Transaction Type in the Trade Table
- π β¨ Premium API support
- π§Ή Skip orders that are not tradable fractionally
- π β¨ Responsive Trade table for mobile
- π§Ή Fix list selector styling on the trading page
- π β¨ Support for live account (https://alpaca.markets/docs/api-documentation/api-v2/#paper-trading) - not exposed to public yet
- π§Ή Error handling layer for both sync & async
- π Timezone fix (changed to EST)
- π β¨ Intraday price support for stocks (confidence is now realtime)
- π§Ή Generic Algo Execution interfaces
- π β¨ Multiple Algo Support - Buy & Sell basic
- π§Ή README documentation for open sources, code clean up etc.
- π β¨ Basic Backtest feature - allows you to look up algorithm's output b/w any timeframe
- π β¨ Dark Mode Toggle π
- π Fix algo selection based on query params on Quick Analysis page
- π β¨ Algorithms page to describe what each algorithm does
- π β¨ Account's page
- π β¨ Account's positions page
- π β¨ List Orders page
- π β¨ Cloud based trade schedule
- π β¨ Advanced Backtest feature - allows you to simulate the trading using multiple algorithms on a list of stocks
- Window based backtest - now we have 100% accurate backtest
- Expose API based live money trading for people who are looking to live trade (since OAuth isn't available for live trading yet)
- Live money trading approval by Alpaca
- Portfolio Chart Support - Account Page
- Strong Validations (https://wanago.io/2018/12/17/typescript-express-error-handling-validation/)
- Generic algorithm parameters support
- Expose a mechanism to configure algorithms from the interface
- Delete watchlist support
- Watchlist detail page
- Trade link on watchlist page
- Cancel Order
- Update Order
After you fork and checkout the repo in your local box:
npm install
npm run compile
To start the website, you'll need a couple of APIs:
- Get AlphaVantage Free API here. AlphaVantage is used to pull historical Stock Prices data. We use OAuth, so you'll need to get the clientId and secret from the Alpaca's account dashboard.
- (optional) Get Alpaca Free API here. Alpaca is used as a brokerage to make trades, manage watchlists etc for Stocks.
- (optional) Get Free Crypto Compare API here. Crypto Compare API is used to pull price information for Cryptos.
Once you have these, you can follow next steps to get up and running locally:
You need to set following environment variables. You can do so by creating a .env
file in the root folder of this project locally and copy pasting the following in it:
# AlphaVantage - API KEY (need for Stock's historical prices)
AV_API_KEY=<your-alphavantage-key>
# (optional) Crypto Compare Key (only needed for Crypto history prices)
CRYPTO_API_KEY=<your-crypto-compare-key>
# (optional) Alpaca Configuration
# Note: You only need these if you're planning to
# 1. Make Trades from local
# 2. Manage/view watchlists
# OAuth Configuration
# Guide: https://alpaca.markets/docs/build-apps_services-with-alpaca/oauth-guide/
ALP_CLIENT_ID=<your-alpaca-client-id>
ALP_CLIENT_SECRET=<your-alpaca-client-secret>
# for oauth redirect, you can use the staging end point
ALP_REDIRECT_URI=<redirect-uri>
# (optional) You can also hardcode your personal access token for local development
ALP_HARDCODED_ACCESS_TOKEN=...
# Server Configuration
ENV=dev
PORT=3000
and then, simply:
npm start
Hit http://localhost:3000/ from your browser.
First we need to understand the how algorithms are represented in this repo and their interfaces.
An Algorithm in this repo has an input and an output. The input takes an object that contains prices as timeseries. You don't need to worry about pulling prices from an API yourself, it comes out of the box for you!
export interface AlgoInput {
open: number[]; // open price
close: number[]; // close price
adjustedClose: number[]; // close price, split adjusted
high: number[]; // high price
low: number[]; // low price
volume: number[]; // volume
splitCoefficient: number[]; // split coefficient for the interval
}
The output of an Algo is a timeseries list of indicator values predicted/calculated by the algo. This is because some algorithms might output more than one indicators.
export interface AlgoOutput {
indicators: number[][]; // [0][1] means 0th indicator's 1st timestamp value
}
And lastly, algorithms have an actionType
i.e. whether it is predicting a buy
action or a sell
action for the users. Algorithms also have a name and unique id. Name will be visible on the website. Here's how to implement a very basic algorithm:
Let's create a Diamond Hands algorithm which returns 100% buy confidence always. :) Create diamond-hands.ts
file in the ./src/algo
folder and then copy paste the code below:
import {Algo, AlgoInput, AlgoOutput, AlgoActionType} from './algo';
export class DiamondHandsAlgo implements Algo {
async run(algoInput: AlgoInput): Promise<AlgoOutput> {
// This algo always recommends 100% buy confidence :)
const indicatorA = algoInput.prices.map(p => 100);
return {
indicators: [indicatorA],
};
}
actionType(): AlgoActionType {
return 'buy';
}
name() {
return 'Diamond Hands';
}
id() {
return 'DiamondHands';
}
}
Last thing we need to do is to add this algo in the registry ./src/algo/algoresitry.ts
:
...
import {DiamondHandsAlgo} from './diamond-hands';
export const ALGO_REGISTRY = [
new BuyLowAlgo(),
new SellHighAlgo(),
new DiamondHandsAlgo(), // add your new algo here
/**
* Add your new Algo here
*/
];
And, that's it! π―
npm run compile && npm start
Go to http://localhost:3000 and see it in action:
Note: Do not forget to create a documentation of your algorithm before submitting the pull request so community can understand how to use it. You can find documentation README at ./src/algo/README.md
.
After you are ready to deploy, feel free to submit a pull request.
Check out the current algorithms here.
Copyright Kevindra Singh Β© 2021. All rights reserved.