The Manual Alpaca — Manual Trading with Alpaca and Python

Matt Haines
Automation Generation
7 min readFeb 26, 2019
This trading strategy was profitable enough to prompt me to write the script! (July 2018 through February 2019) Past performance is not indicative of future results.

My promise to the Alpaca trading community

A few weeks ago, I announced to the Alpaca trading community that I had written some code that allowed me to make manual trades using the Alpaca brokerage, and that I had also opened up the code for others to use and share. Alpaca asked me to write about it for Medium.

Manually entering trading orders with Alpaca is, on the face of it, somewhat absurd. So why did I bother?

I’ve been actively trading since late 2014. And by “actively” I mean frequently. In that time, I’ve executed over 1700 trades. That’s more than one round-trip trade every two trading days. I’ve learned a few things over the last few years. Made a little money, lost a little money…and I’ve learned what works and what doesn’t work with short-term “swing” trading.

For a long time, I used Robinhood as my broker. It was tough to argue with zero commissions, and that allowed me to use trading styles that would have drained my account quickly with a traditional commission-based brokerage. This worked just fine when I only needed to enter a few trades at a time.

Stressful but profitable strategy

Then I developed a trading strategy that made manual order entry problematic. This strategy required that I buy up to ten stocks per day, hold them overnight and sell them at the opening bell of the next day. However, my strategy often presented me with up to twenty candidates for each trading day. This meant I had to juggle the buys and sells at the open, waiting for my funds to clear from the sell orders before I could buy the new ones. I also had to watch throughout the day for price movements.

This strategy made for some stressful morning before breakfast (I’m in California, so the market opens for me at 6:30am). It also meant missed or erroneous trades made in my haste to get orders entered.

The strategy, however, was profitable (you can see the equity curve in the lead image since I started trading it in July 2018). Even with all my fumble-fingered mistakes, swiping left when I should swipe right or whatever, it was making money. I needed to make improvements on my pathetic ordering attempts.

I began collaborating with a coder to automate the strategy on Robinhood. He had already written code to trade other strategies, so it seemed like a reachable goal. We began work on it. Or, to be fair, he began work on it and I simply divulged the details of my profitable trading system to him.

The problem is, Robinhood does not want you to use its services via bots or automated trading. As my collaborator found out, they send you emails if they suspect you’re using an algo to trade. Also, because they seem to have no interest in supporting automated trading, they would often change aspects of their API without announcing any changes. My coding friend would often have to reverse-engineer the new changes that would appear, which resulted in a lot of online cursing.

Birth of the Manual Alpaca

One day in passing, my friend mentioned Alpaca (thanks TW!). This sounded too good to be true: another commission-free brokerage, but one aimed squarely at the algo/automated crowd? This seemed perfect for me! All I needed to do was learn how to code!

Ok that’s not quite true. I’d written some code before in Python, and even posted a handy little data visualizer on my financial blog (here). I had not, however, ever written something of this magnitude.

One beautiful aspect of Alpaca is that you can set up a paper-trading account. This means you can test your code and make paper trades, without putting real money at risk. I was free to make stupid coding errors as much as I wanted, and could hit “reset” at any time and start over.

It turns out that teaching yourself how to write a trading execution script takes a long time. I started in November of 2018, and only recently went live with my automated system (as I write this, it’s mid-February 2019).

As I wrote my code, I realized it was often helpful to set up my “paper” account with some positions already active, so I could test the sell side as well as the buy side. Furthermore, my algorithm would sometimes — ok, frequently — make bad trades. Rather than wipe my results clean and start again, I just wanted to delete those trades and move forward. Alpaca didn’t have any way to enter an order (at the time). The need was clear: I needed to write a manual-trading system as well as an automated system.

That was how my script, Manual Alpaca, was born. Catchy title, right?

Advantages of the Manual Alpaca

Once I’d written this little bit of code, I soon realized I could use it to trade “live” as well. It certainly lacks the slick interface of a phone app. It does however have some advantages.

First off, it’s faster. Like a musical instrument, as I became more adept at the proper key strokes, I was able to execute trades much faster than with Robinhood’s app. I could also set things up like default trading position sizes, so I didn’t have to calculate on the fly the number of shares I needed. I could enter user-defined IDs for my orders, so I could track which trade belonged to which strategy.

The development of my automated system dragged on longer than I thought, but I decided to move my funds over from RH and start trading manually with Alpaca while I continued to develop my algo.

I’m excited that a week ago I started live trading with my automated script. So far, things are running pretty smoothly. Does this mean I no longer need Manual Alpaca? Far from it.

I still use it to set up or remove test trades in my paper account. I use it to track down order details if I have questions about particular trades. There are also strategies that I only trade occasionally, and it’s not worth the effort (yet) to implement them in code. I use Manual Alpaca for those trades as well. It’s not pretty, but it gets the job done.

What the Manual Alpaca can do for you

Manual Alpaca is free, and you can edit as you see fit. Use at your own risk, as they say. If you’re just starting out with Python or need to see the nuts and bolts of trading with Alpaca, the code might be useful to you. If you share it, please give attribution. Here are examples of what it can do for you:

  • Show you your current positions, and sell part or all of your position if you want.
  • Look up the current price of a ticker symbol, and set a market or limit order.
  • View all recent orders, or view only the closed or open orders.
  • You can specify a prefix for your orders’ custom user ID field, and MA will combine that with the current timestamp to uniquely identify your orders. This is helpful if you trade multiple strategies at once. (see above, circled)
  • You can set a maximum position size in dollars. When you place buy orders, MA will calculate the maximum number of shares you can buy with that position size, and will default to that amount (which is easily overridden).
  • It very clearly tells you if you’re trading from your live account.
  • There is also a handy help text that is accessible from the script.

It costs you ZERO dollars to set up a paper-trading account with Alpaca. With my script (and there are others), you can get your feet wet and play around with coding and trading for Python. Why not give it a try?

Feel free to hit me up with questions via my website, or just read my blog posts on trading. http://www.throwinggoodmoney.com

Disclaimer: I do not work for, nor do I have any financial affiliation with Alpaca Markets. However, they did say I might get a pair of Alpaca-branded socks if I write this article.

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.

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

--

--