Create a bot to sell covered calls every month in 23 lines of JavaScript

April 23, 2017

Covered Call is a common option strategy that is used to enhance a long stock position. In simple terms, it allows an investor to sell a portion of the unlimited upside in a stock position for cash each month. I almost always prefer covered calls to naked stock because it provides some downside protection (via cost basis reduction) and it allows me to profit even when the share price stays the same. Covered calls is easy to learn and doesn’t introduce any additional risk.

Setup
- Buy 100 shares of stock.
- Sell 1 call for every 100 shares.

Max Profit
Difference between stock purchase price & short call strike + premium received from selling the call.

Max Loss
Stock purchase price – premium received from selling the call.

Commitment

Unlike a simple buy and hold strategy, selling covered calls requires an additional time commitment for each individual long stock position. Each month I have to watch for price swings, crunch the numbers and make a trade to roll to the next month at the appropriate time. With 5 or 10 stock positions, I have to invest a considerable amount time and mental effort monthly to supervise and manage all of them.

Bot trading can help

Alta5 is the first platform designed specifically to automate strategies like the one described above. In this article, I will walk you through the process I used to create a simple bot, Stronghold, that trades a covered call strategy based on the research by Tom Sosnoff and Tony Battista of tastyTrade in this video.

  • Buys the selected stock
  • Sells calls from the first standard monthly option series with at least 30 days days to expiration at a configurable delta value ($delta). The default is .30 as suggested in the video.
  • Monitors calls in real-time and automatically rolls to the next month when the call’s time value falls below a configurable value ($timeValue). The video suggests .25 but I made the default .10 since I tend to sell covered calls on stocks instead of SPY.

New Strategy

Once logged into Alta5, I selected Build in the top menu and then clicked New Strategy. Next, I filled in some basic info about the strategy and clicked Save.

Opportunity Limit

Then I clicked on the More Options button in the header, then Settings and then I selected  “Allow 2 open opportunities” to allow a bot to open 2 opportunities at once (the default is 1).

Watchlist

Next I added a watchlist variable for the stock named $stock. A bot’s watchlist is similar to a watchlist in your favorite trading program except instead of displaying data about stocks in rows and columns for your eyes to scan, each stock is defined as a variable with the data as properties your bot can scan (e.g. $stock.bid or $stock.volume).

Because I selected to Pick symbol for each bot later, I am presented with a form field to select the symbol for the $stock variable when I start each bot that uses the Stronghold strategy:

Inputs

The strategy sells covered calls based on their delta greek value (instead of strike price) and I want that value to be configurable for each individual bot/stock. Adjusting the delta value allows me to control the distance the covered call strike price is from the current price (based how bullish I am about the stock). The strategy also rolls the sold calls each month when the calls’ time value falls below a certain value. Adjusting the time value gives me the ability to control how early/late the bot takes a profit or loss on the calls and rolls to the next month.

I added two number inputs$delta and $timeValue. To add a number input, click the + button on the Inputs list and then select Number.

When starting a Stronghold bot, I’m now presented with a step for me to provide the value for the variables for that bot:

Events

Next I added scripts for 2 events, scan and monitor.

scan

The scan event is triggered multiple times per second when the market is open, all data is loaded and the bot has not hit the opportunity limit (set to 2 above). My script for the scan event makes a simple decision based on the number of opportunities the bot currently has open. If the bot doesn’t have any open, it opens an equity opportunity as the cover position. If it hasn’t sold the covered calls yet, it opens a shortcall opportunity.

var count = $bot.opps.length;
if(count === 0){
    $bot.open({
        type: 'equity',
        symbol: $stock.symbol
    });
}else if(count === 1){
    $bot.open({
        type: 'shortcall',
        symbol: $stock.symbol,
        days: 30,
        monthlyOnly: true,
        delta: $delta
    });
}

Here’s a breakdown of the options used to open the covered calls:

  • type - The type of opportunity.
  • symbol - The symbol of the option chain.
  • days - The minimum days to expiration for the option series.
  • monthlyOnly - Only standard monthly option series that expire on the 3rd Friday of each month.
  • delta - Select the call with the delta value nearest to $delta.

The rest of the available options, including price and quantity, are filled in by the Alta5 automation layer based on my settings.  Smart Pricing uses timed limit orders between the bid/ask spread to get my order filled at the best price and Alta5′s risk management layer will figure out the maximum quantity the bot can open based on capital draw limits.

monitor

The monitor event is triggered multiple times per second when the market is open, all data is loaded and the bot has an open opportunity to monitor. It’s generally used to check if any open opportunities should be closed.

The script for the monitor event is also very simple. First, I get the shortcall opportunity from the $bot.opps collection using the first(type) lookup function. If the shortcall opportunity has been open more than a day and the option time value is less than $timeValue, it rolls to calls in the next monthly expiration at the set $delta.

var calls = $bot.opps.first('shortcall');
if(calls && calls.daysOpen > 1 && calls.timeValue < $timeValue){
    calls.roll({
        delta: $delta,
        expiration: calls.expiration.nextMonthlyDate
    });
}

Conclusion

This is a very simple but effective implementation of a covered call strategy to reduce cost basis for long stock positions over time. Depending on investment goals, it could be customized to require a minimum return on sold calls, roll calls up/down in the same expiration and/or use strike price instead of delta to select calls.

The strategy is available for you to use and/or customize under Build -> Sample Strategies in Alta5.