How to trade a covered call with Alpaca (the easy way)

June 22, 2024
Facebook logo.
Twitter logo.
LinkedIn logo.
Get this code in Google Colab

How to trade a covered call with Alpaca (the easy way)

Bullish markets create great opportunities for a covered call.

Covered calls allow traders to profit from the potential appreciation of their underlying stock while collecting premium income from selling call options.

However, traditional trading platforms often present challenges. Many make it hard to automate these strategies with Python.

I have always struggled to automate covered call trades that worked with my investment strategy and risk tolerance.

I wish I had Alpaca.

In today's newsletter, we'll execute a covered call using Alpaca’s Trading API with Python.

Let's go!

How to trade a covered call with Alpaca (the easy way)

A covered call involves owning a stock and selling a call option on that same stock. The call option gives the buyer the right, but not the obligation, to purchase the stock at a specified strike price within a certain time frame.

Covered calls generate income through the premium received from selling the call option.

However, the upside is capped. If the stock price rises significantly above the strike price, the stock may be called away. You’ll then sell the stock at the strike price.

Covered calls are often used by traders seeking additional income or to hedge against minor declines in the stock’s price.

Let’s see how it works.

Create an Alpaca Trading API account and get your API keys

The first step is to create a developer account with Alpaca and get your API keys.

  1. Go to ​Alpaca​ and hover over “Sign Up” on the top-right
  2. Click “Trading API” and create an Alpaca account
  3. Head to your dashboard and fine the API keys on the right side

Set up your Python environment for the covered calls

Install the Alpha Python library using PIP.

1pip install alpaca-py

Now, import the library into Python.

1from alpaca.trading.client import TradingClient
2from alpaca.trading.requests import LimitOrderRequest
3from alpaca.trading.enums import OrderSide, TimeInForce
4api_key = "YOUR API KEY"
5secret_key = "YOUR SECRET API KEY"

We import the specific modules we’ll need for the covered call strategy. Make sure you add your API key and secret.

Use the API to execute trades and manage the position

A covered call has two steps. First, going long the underlying stock. We’ll use SPY. Then, selling call options on that same underlying security.

1trading_client = TradingClient(api_key, secret_key, paper=True)

We start by authenticating our access to the API and creating an instance of the TradingClient class. We set paper=True to indicate that this is Paper trading, not live trading.

limit_order_data = LimitOrderRequest(
    symbol="SPY",
    limit_price=535,
    qty=100,
    side=OrderSide.BUY,
    time_in_force=TimeInForce.DAY
)
limit_order = trading_client.submit_order(order_data = limit_order_data)

After authentication, we define the trade details using the LimitOrderRequest class to place a trade using a limit order. Alpaca supports other order types, such as market orders, which can be explored ​here​.

The LimitOrderRequest class has the following parameters:

  • symbol: Specifies the security symbol to trade
  • limit_price: The price at which you want to enter the market
  • qty: Specifies the number of shares you want to buy or sell
  • side: Indicates the type of order
  • time_in_force: Specifies how long the order will remain active

Finally, we place the order using the submit_order function with the defined trading details.

Note that since one options contract represents 100 shares, we need to buy 100 shares of the underlying for every options contract.

Selling call options follows a similar procedure to placing a long trade. Note the qty parameter represents the number of contracts. Since we bought 100 shares of SPY, we can sell one contract. Also, note that TIF must be set to DAY for options.

call_order_data = LimitOrderRequest(
    symbol="SPY240705C00540000", 
    limit_price=4.11,
    qty=1, 
    side=OrderSide.SELL, 
    time_in_force=TimeInForce.DAY
)
call_order = trading_client.submit_order(order_data = call_order_data)

We use the same class LimitOrderRequest for the option.

  • The symbol is “SPY240705C00540000,” representing an SPY contract with a strike price of 540 expiring on July 5, 2024
  • The limit_price is set to 4.11, meaning we enter the market when the contract price reaches $4.11
  • The side is OrderSide.SELL, indicating we’re selling the call options

You can retrieve your existing positions with just a single line of code.

trading_client.get_all_positions()

Here, we use the get_all_positions() function, which retrieves all the current positions held in the account. In our case, it will display the details of both the long and short trades we’ve executed, along with the P&L of each trade and current price information.

Next steps

Open up a free Alpaca account and set up an API key. Once you have it set up, you can start paper trading (risk-free) to experiment with the Trading API.

-

Alpaca and PyQuant News, LLC ("PyQuant News") are not affiliated, and neither are responsible for the liabilities of the other.

Options trading is not suitable for all investors due to its inherent high risk, which can potentially result in significant losses. Please read Characteristics and Risks of Standardized Options before investing in options.

All investments involve risk and the past performance of a security, or financial product does not guarantee future results or returns. There is no guarantee that any investment strategy will achieve its objectives. Please note that diversification does not assure a profit, or protect against loss. There is always the potential of losing money when you invest in securities, or other financial products. Investors should consider their investment objectives and risks carefully before investing.

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

This is not an offer, solicitation of an offer, or advice to buy or sell securities or cryptocurrencies, or open a brokerage account or cryptocurrency account in any jurisdiction where Alpaca Securities or Alpaca Crypto respectively, are not registered or licensed, as applicable.

The content of this article is for general informational purposes only and is believed to be accurate as of the posting date but may be subject to change. All examples are for illustrative purposes only.

Man with glasses and a wristwatch, wearing a white shirt, looking thoughtfully at a laptop with a data screen in the background.