Algotrader
Simple algorithmic stock and option trading for Node.js.
Install / Use
/learn @torreyleonard/AlgotraderREADME

Simple algorithmic stock and option trading for Node.js.
Features
- Extensive broker library
- Easily place orders
- Retrieve past orders
- Query a users portfolio
- Supported brokers:
- Robinhood
- TDAmeritrade (in progress)
- Oanda (in progress)
- If you'd like to have another broker supported, submit an issue or a pull request
- Data library
- Real time quote data streaming for cryptocurrency, forex, equities
- Get data on bids, asks, last price, and more from the Yahoo Finance API
- Stream news headlines along with quotes
- Up-to-date options data
- Easily find stocks for various queries
- Retrieve the day's top gainers
- Retrieve the day's top losers
- Get stocks by highest (abnormal) volume
- Get options contracts by highest open interest
- And more
- Get up to the minute breaking headlines
- Get technical indicators from AlphaVantage
- SMA, EMA, RSI, etc.
- Get fundamentals and balance sheet data (in progress)
- Assets, debt, liabilities, revenue, earnings, etc.
- Real time quote data streaming for cryptocurrency, forex, equities
- Algorithm library (in progress)
- Create algorithms that will automatically place trades based on your criteria
- Backtest algorithms on past market data
- Paper (simulated) trade your algorithm to see how it performs in real time
- Support for many third-party APIs
Table of Contents
- Getting Started
- Broker Library
- Algorithm Library
- Data Library
- Further Notes
Support
<img align="center" src="https://i.imgur.com/nPCnzon.png">
- Join our Discord: https://discord.gg/DWFWBPn
- Get answers to questions or help us deal with open issues.
- Tell us about how you're using Algotrader!
Getting started
Using NPM, you can install Algotrader using the following console command: npm i algotrader --save
Once Algotrader is installed, you can import it into your Node.js project.
const algotrader = require('algotrader');
After, you can instantiate any Algotrader library like so:
const Robinhood = algotrader.Robinhood;
const Data = algotrader.Data;
const Algorithm = algotrader.Algorithm; // in progress
Robinhood
First, you'll need to create a new User instance and authenticate them.
const robinhood = require('algotrader').Robinhood;
const User = robinhood.User;
const options = {
doNotSaveToDisk: false, // If the `save` method should not store the user login info to disk (file)
serializedUserFile: null // File to where the serialized user login info can be saved
};
const myUser = new User("username", "password", options);
myUser.authenticate()
.then(() => {
// User was authenticated
})
.catch(error => {
// Either the request failed, or Robinhood responded with an error.
// (Ex: you don't have internet access or your user credentials were incorrect)
})
Personally, I either store user data as an array in a .json file, then require it into the class, (insecure) or ask for the user's credentials in the console. You should handle this sensitive data in a way that you're comfortable with.
Note: providing a password in the User constructor is optional. You can also pass it to User.authenticate() as the first parameter like so:
const myUser = new User("username");
// Or with options:
// const myUser = new User("username", null, options);
myUser.authenticate("password")
.then(() => {
// User was authenticated
});
If it is not provided at all, you will be prompted via CLI.
MFA
Algotrader now supports multi-factor authentication. So, if you have this enabled on your account (which is a good idea by the way), you'll be prompted to enter the six-digit code after login. If you run a trading script with this library automatically and have MFA enabled, it may be worth your while to utilize a telecom API (possible through Twilio?) to have the code programmatically retrieved (see below).
The MFA prompt will appear like so:

To enter the code programmatically, you can pass a function to User.authenticate() that returns a promise containing the MFA code in a six-character string. For example:
function getMFA() {
return new Promise((resolve, reject) => {
// Get the code here
const mfa = "123456"
resolve(mfa);
})
}
// Note: the first parameter here is 'password' and is only required if you are re-authenticating
// an expired user or if you did not provide a password in the User constructor.
myUser.authenticate(null, getMFA)
.then(() => {
// User was authenticated
})
Saving & loading a user
In order to reduce time logging in, you can save an authenticated user to disk and load it into memory for subsequent requests. If you're using multi-factor authentication, I definitely recommend this- it really saves a lot of time and energy.
After you've logged in (see above), you can run the following:
const authenticatedUser;
authenticatedUser.save()
.then(() => {
// The user data was saved to:
// project/node_modules/algotrader/objects/broker/robinhood/User.json
// This filepath can be configured in the options parameter in `User` constructor
});
Note that your password will never be saved to disk. Keep this in mind when having to re-authenticate.
Once saved, you can easily login like so:
const robinhood = require('algotrader').Robinhood;
const User = robinhood.User;
User.load()
.then(myUser => {
myUser.isAuthenticated(); // Boolean - see below
})
.catch(error => {
// Make sure to always catch possible errors. You'll need to re-authenticate here.
// - Possible errors: user session has expired, a saved user does not exist.
if (error) {
// Re-auth here and save if desired.
}
});
However, authentication tokens issued by Robinhood expire after 24 hours. Version 1.4.5 takes this into account and User.isAuthenticated() will return false if the token has expired. Make sure to check for this and re-authenticate if necessary. When re-authenticating, you will need to provide a password either through CLI or when calling User.authenticate() as the first parameter.
If you need to save and retrieve the user login info to somewhere else (like a Database, specially useful to keep your service stateless), you can use:
const options = { doNotSaveToDisk: true };
const authenticatedUser = new User("username", "password", options);
// ...
authenticatedUser.save()
.then((serializedUser) => {
// You can store `serializedUser` to where you want, like a Database
});
// ...
const serializedUser = ''; // Get this value from your storage
User.load(serializedUser)
.then(myUser => {
myUser.isAuthenticated(); // Boolean - see below
})
.catch(error => {
// Make sure to always catch possible errors. You'll need to re-authenticate here.
// - Possible errors: user session has expired, a saved user does not exist.
if (error) {
// Re-auth here and save if desired.
}
});
Get a user's portfolio
There are a good amount

