From 5e50ea24885c2bcc5a5a37fa3b6ccad277ec5cab Mon Sep 17 00:00:00 2001 From: Chris Vanderloo Date: Thu, 9 Aug 2018 23:06:34 -0400 Subject: [PATCH] Added inline algorithm execution and made Robinhood library wrap --- examples/example.js | 35 +++++++++---- src/index.js | 119 +++++++++++++++++++++++--------------------- 2 files changed, 87 insertions(+), 67 deletions(-) diff --git a/examples/example.js b/examples/example.js index ac8ea1c..af823f2 100644 --- a/examples/example.js +++ b/examples/example.js @@ -1,7 +1,7 @@ /* eslint-disable no-console */ require('dotenv').config(); -const Robinhood = require('../src'); +const { Robinhood } = require('../src'); const client = new Robinhood(); // pass username, password, and trade function @@ -16,18 +16,35 @@ function trade(error) { console.log(error); return; } - console.log('Ready to trade!'); - client.addAlgorithm('ema', emaAlgorithm); + console.log('Ready to trade!'); - client.execute('ema'); + // Adds an algorithm to run later + // + // These algorithms should be designed to take data from ONE + // source, aka AAPL or BTC, and not try to balance multiple. + // + // Any algorithm function should return a Promise - console.log(client); - } + client.addAlgorithm('ema', emaAlgorithm); -// An algorithm that will take in data and reply with buy or sell signals -function emaAlgorithm() { - console.log("started") + // Execute a saved algorithm + client.execute({ + name: 'ema', + symbol: 'BTC', + max_usd: '134.32' + }); + + // or execute one in command by omitting 'name' from options + client.execute({ + symbol: 'BTC', + max_usd: '134.32' + }, emaAlgorithm); +} + +// An algorithm that will take in data and reply with performance data or +function emaAlgorithm(buy,sell) { + console.log("started"); } \ No newline at end of file diff --git a/src/index.js b/src/index.js index 5dbb920..e8cbabc 100644 --- a/src/index.js +++ b/src/index.js @@ -45,70 +45,73 @@ function getPaginated(token, url, callback, err) { }).catch(error => err(error)); } -module.exports = function transaction() { - this.portfolio = []; - this.init = (opts, callback) => { - // For error handling - const err = (error) => { - callback(error); - }; - - // Logs in with client credentials listed in options - login(opts.username, opts.password, (res) => { - // Creates requests object to track multiple requests to the API - const requests = []; - - // Saves the authorization token - this.token = res.body.token; - - // Gathers the current account's past and present positions - getPaginated(this.token, 'https://api.robinhood.com/positions/', (pos) => { - this.positions = pos; - - // Finds just current positions (with purchased shares) - // Then this grabs the instrument of each live position, and saves each symbol - pos.forEach((obj) => { - if (obj.quantity > 0) { - requests.push(getWithAuth(this.token, obj.instrument, (ticker) => { - this.portfolio.push(ticker.symbol); - }, err)); - } - }); - - // Gets user information, saves it to object - requests.push(getWithAuth(this.token, 'https://api.robinhood.com/user/', (info) => { - this.user = info; - }, err)); - - // Gets account information, saves it to object - requests.push(getWithAuth(this.token, 'https://api.robinhood.com/accounts/', (accounts) => { - [this.account] = accounts.results; - }, err)); - - // Awaits all requests to complete, and then replies to the callback - Promise.all(requests).then(() => { - callback(); +module.exports = { + Robinhood: function transaction() { + this.portfolio = []; + this.init = (opts, callback) => { + // For error handling + const err = (error) => { + callback(error); + }; + + // Logs in with client credentials listed in options + login(opts.username, opts.password, (res) => { + // Creates requests object to track multiple requests to the API + const requests = []; + + // Saves the authorization token + this.token = res.body.token; + + // Gathers the current account's past and present positions + getPaginated(this.token, 'https://api.robinhood.com/positions/', (pos) => { + this.positions = pos; + + // Finds just current positions (with purchased shares) + // Then this grabs the instrument of each live position, and saves each symbol + pos.forEach((obj) => { + if (obj.quantity > 0) { + requests.push(getWithAuth(this.token, obj.instrument, (ticker) => { + this.portfolio.push(ticker.symbol); + }, err)); + } + }); + + // Gets user information, saves it to object + requests.push(getWithAuth(this.token, 'https://api.robinhood.com/user/', (info) => { + this.user = info; + }, err)); + + // Gets account information, saves it to object + requests.push(getWithAuth(this.token, 'https://api.robinhood.com/accounts/', (accounts) => { + [this.account] = accounts.results; + }, err)); + + // Awaits all requests to complete, and then replies to the callback + Promise.all(requests).then(() => { + callback(); + }); }); }); - }); - }; + }; - this.get = (url, callback, err) => getWithAuth(this.token, url, (res) => { - if (res.results) callback(res.results); - else callback(res); - }, err); + this.get = (url, callback, err) => getWithAuth(this.token, url, (res) => { + if (res.results) callback(res.results); + else callback(res); + }, err); - this.getPaginated = (url, callback, err) => getPaginated(this.token, url, callback, err); + this.getPaginated = (url, callback, err) => getPaginated(this.token, url, callback, err); - // ALL ABOUT ALGORITHMS + // ALL ABOUT ALGORITHMS - this.algorithms = []; + this.algorithms = []; - this.addAlgorithm = (name, callback) => { - this.algorithms.push({ name, func: callback }); - }; + this.addAlgorithm = (name, callback) => { + this.algorithms.push({ name, func: callback }); + }; - this.execute = (name) => { - this.algorithms.find(x => x.name === name).func(); - }; + this.execute = (opts, func) => { + if (opts.name) return this.algorithms.find(x => x.name === opts.name).func(); + else return func(); + }; + }, }; -- 2.51.2