Options Trading Bot Experiment

Nathan Maton
4 min readAug 1, 2024

--

A short story and my conceptual takeaways:

I’m experimenting with building things right now in my career. The first idea I worked on has kept creeping up for me over the years: the idea that we undervalue risk in the market. I recall in college reading about a heretical trader: Nassim Nicholas Taleb. He has quotes like the best doctor is the ugliest because you know they must actually know what they’re doing vs. relying on good looks to get by. He is famous for popularizing the term black swan: an event that is extremely unlikely to happen but has catastrophic consequences like 9/11.

There’s a lot of intuitive appeal to this idea. It suggests we build for a world that doesn’t exist, much like much of economic rationalist theory. It suggests there’s a way to build an investment portfolio that takes advantage of society’s undervaluing of risk. When I first became a data scientist, this idea intrigued me, and I built a model trying to predict the VIX, a major volatility index, with machine learning. That didn’t work, but this time I wanted to see if I could learn and construct a portfolio like Taleb had.

Apparently, his dream was to lose a little money every day but make way more than he lost in a black swan event. This is a multi-year strategy, which is something I’m also set up well to do as an individual. So I built my first algorithmic trading bot, learned a lot about options and technical indicators commonly used to automate trades, and backtested multiple options strategies (all using fake money).

My portfolio, funded with fake money on Alpaca in mid-july

My bot has been running for a few weeks, and losing money! Success, right? Unfortunately not, but I’ve had a lot of learning along the way. I got $100,000 of fake money on Alpaca and figured out how to use their system to run trades. I learned that a strangle is the option type that is closest to my understanding of Taleb’s strategy. It is a combination of buying 2 options that become profitable if the stock price moves significantly either direction as you can see from this graphic below from Investopedia.

See more about this strategy here

So how do I know this isn’t working? As I go deeper into my learning, I read more about Taleb liking to buy significantly out-of-the-money options (options that are far from the current value of the stock). Strangles typically go a little away from the current stock price, not significantly far away. I also tried backtesting this on a few options backtesting platforms and saw poor results.

Do I keep going on building this strategy? The math and pricing of significantly out-of-the-money options are less clear to me than you figure out, so I’m not sure I want to continue. For now, I’m just writing up my experiment results and learnings and sharing a bit of code for anyone who wants to use Python to build an algorithm on Alpaca.

Before I go into the code for the technical audience here, my takeaways include:

  • Trying to scratch an itch on an idea is a rabbit hole, you have to go from knowing nothing to getting close to expert level. This probably takes more than a month, so you need to really enjoy it.
  • It was really rewarding picking something I could see results from. While it took some time to build my bot, trying different strategies was cool to see and knowing I could keep going was fun.
  • Financial options data is very complex. Backtesting got tricky, and I saw the benefits of paying for a platform to do it vs. building your own code. The more I backtested, the less I saw great value in near the money strangles for a Taleb type system. I ended up finding a backtesting library and learning enough of it to do stock backtesting with open source tools, but finding for options that wasn’t really worth it
  • It took quite a bit of effort to write this post, but gave it a nice lens of finality for myself. Make the effort, it is worth it to reflect and share.

Ok code time!

Accelerating your build of an Alpaca trading bot:

Here’s some code that as of August 1, 2024 you could use to make your first trade using Alpaca, Alpaca-Py and a few other libraries. As anyone building things knows, online code/tutorials vary widely in quality. Much of the Alpaca documentation was out of date, and the Alpaca-py documentation was sparse.

In my repo, there are a few key files:

  • Trade: This is the core engine where I’ve set up functions to make trades and has most of the logic/code.
  • Evaluate: This file is for ongoing evaluation of your portfolio and I import it into a few other scripts.
  • Buy/sell: This is setup to run on a scheduler. I ended up deploying this to Heroku and using their scheduler so I didn’t have to monitor it locally. I won’t go into the Heroku setup but feel free to reach out if you’re interested.

I’ve put more notes in the code itself.

--

--