You've successfully subscribed to Alpaca Resources
Great! Next, complete checkout for full access to Alpaca Resources
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.
  • Home
  • We're hiring
  • Forum
  • Docs
  • Log in
  • Sign up
Use Cases

Using the Command Line to Buy/Sell Stocks

Alpaca Team
Alpaca Team

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:

@alpacahq/alpaca-trade-api
Javascript library for the Alpaca Trade API
> 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.

alpacahq/alpaca-trade-api-js
Contribute to alpacahq/alpaca-trade-api-js development by creating an account on GitHub.

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.

Use Cases

Alpaca Team

API-first stock brokerage. *Securities are offered through Alpaca Securities LLC* http://alpaca.markets/#disclosures