Skip to content

Instantly share code, notes, and snippets.

@andrewkim316
Last active August 23, 2019 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewkim316/c98256b34ba55632155960b5ad0c55ba to your computer and use it in GitHub Desktop.
Save andrewkim316/c98256b34ba55632155960b5ad0c55ba to your computer and use it in GitHub Desktop.
// Get percent changes of the stock prices over the past 10 minutes.
getPercentChanges(allStocks){
var length = 10;
var promStocks = [];
allStocks.forEach((stock) => {
promStocks.push(new Promise(async (resolve, reject) => {
await this.alpaca.getBars('minute', stock.name, {limit: length}).then((resp) => {
stock.pc = (resp[stock.name][length - 1].c - resp[stock.name][0].o) / resp[stock.name][0].o;
}).catch((err) => {console.log(err.error);});
resolve();
}));
});
return promStocks;
}
// Mechanism used to rank the stocks, the basis of the Long-Short Equity Strategy.
async rank(){
// Ranks all stocks by percent change over the past 10 minutes (higher is better).
var promStocks = this.getPercentChanges(this.allStocks);
await Promise.all(promStocks);
// Sort the stocks in place by the percent change field (marked by pc).
this.allStocks.sort((a, b) => {return a.pc - b.pc;});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment