Buying and Selling Stocks from the Command Line

David Aaron Suddjian
Automation Generation
4 min readDec 6, 2018

(This is 12/6 post for Trading API Advent Calendar 2018)

If you’re like me, you always have half a dozen different things going on in your terminal: system monitoring, compiling, code editing, little background utilities, the list goes on. Sometimes it feels like I live on my command line.

I also manage my stock portfolio zealously, checking and making trade decisions many times a day. It seemed natural to combine the two, so I built my own little Node.js-powered CLI using the recently released Alpaca Trading API. This lets me manage my portfolio with command line arguments, like so:

> cowsay whaaaaaat

Is this a necessary tool? Absolutely not, there are loads of regular stock brokerages, with fancy charts and tables and buttons. But does this make every trade feel a hundred times more badass? Oh yes.

TL;DR

This CLI is publicly available on npm as alpaca-cli, and the Alpaca platform is free to use. So if you like, you can install it and try it out, or fork the code and change things up. Or if you want you can make your own stock trading programs with the same Javascript API that I’m using here.

If you’d like to learn about how it works, read on!

Trading Stocks with Javascript is Easy

Writing stock trading code sounds complicated, and it used to be. APIs in the past have either been prohibitively expensive, or unofficial hacked-together affairs, or just poorly designed. If you weren’t a professional trader, it wasn’t worth it. But these days Alpaca’s API makes development easy. This is all the code needed to implement fully functional alpaca buy and alpaca sell commands, minus a bunch of boring user input validation code.

Buying and selling is a good first step to any set of trading tools, but I also need to be able to see how my portfolio is doing. The API makes that easy too, with a call to getPositions.

The majority of this code is just string manipulation to make the output look acceptable. Only one function call is required to actually get the data! This command has significant room for enhancement, but here’s what it looks like right now.

My real portfolio is better, I promise.

The API makes a lot more data available, so there are a lot of different directions I could take this. It could show charts, pending orders, intraday performance, live streamed trade data, and more. I’m excited to incorporate more functionality as I keep improving this little program. For now, I’ll leave those as an exercise for the reader.

What’s the Point?

I’m doing this because I’m a programmer, and I think it’s fun to manage my portfolio with the command line. I’ve always wanted features from my financial apps that they didn’t offer, and now I can implement those features on my own, and make them open source.

Trading through an API lets you connect any code you want to the financial markets, and that’s a really powerful ability. Someday I might even build algorithms that manage my stocks for me automatically. The possibilities are endless!

Or if you don’t care about that, you can use it to trade at work without your boss knowing.

Technology and services are offered by AlpacaDB, Inc. Brokerage services are provided by Alpaca Securities LLC (alpaca.markets), member FINRA/SIPC. Alpaca Securities LLC is a wholly-owned subsidiary of AlpacaDB, Inc.

You can find us @AlpacaHQ, if you use twitter.

Follow Automation Generation, a Medium’s publication created for developers/makers in trading and fintech.

--

--