Python code for monthly returns | Stock Options

Shyam BV
Code Sprout
Published in
6 min readNov 26, 2021

--

Intuition

No! It is not an article that sells something or a shortcut to money! To execute the code, it needs a deeper understanding of stocks and options. Code is everywhere in our lives. What if there is a code for some basic monthly returns based on your investment? Or at least help you in making decisions to make those returns? Of course, the risk is embedded in it.

Stock options are one of the best ways to make monthly returns until you understand the complete picture. In this article, I will explain how I use code for trading stock options for regular returns.

Photo by Michael Longmire on Unsplash

Disclaimer

Options can be inherently risky, and everything mentioned here is just one way of doing it. Please use it at your own risk. Understand options entirely before starting any trades.

Prerequisites

  1. You might need sufficient capital or sufficient stocks
  2. Also, you might need a brokerage account with options
  3. Some basic math skills
  4. Knowledge about options
  5. Code is written for Robinhood. But can be used with any brokerage in any country.

Different Options contract

There are a ton of others ways like poor man's covered call, but those are a little advanced. Let's deal with something simple! The below image illustrates different options trades which can be performed.

Call/Put options: This is straightforward. If you think the stock is going up or down, buy a call/put according to your prediction. Here the risk is the premium you paid, but the profit is unlimited.

But there are inherent risks associated with it. Of course, the more chance you make more you will benefit from it.

However, I wanted to play in defensive mode. It means I want the trades to be handled automatically with very little intervention to get monthly returns.

Covered Call

Covered call means obligated to sell the underlying at the strike price. In this, you should have a minimum of 100 shares(1 contract) of underlying stock or asset.

The buyer of the contract will pay you a premium. If the stock price reaches the specific value, you are obligated to sell the underlying asset at a price mentioned in the contract.

For example: To trade Apple(AAPL), you need to have at least 100 shares. Let us assume we have 100 shares of AAPL.

Stock is traded at $157.46. You can buy the Out the money strike price. You will choose $160 as the strike price. If you sell the call contract, you will receive a credit of $4.63 per share. For 100 shares, you will receive $463.

If the stock reaches $160 by the end of December 31, the initial buyer of the contract will exercise our shares. This means you have to sell the $100 shares.

You needed a number that is not reachable realistically. If the stock does not reach the strike price, you keep the premium paid to you. However, if you choose $200, then you might receive a lower premium.

I know it is a little hard to wrap around it. However, for traders/option traders, these are simple.

I will show a way to automate the entire process every month so you can get the premium by holding the stock.

Cash secured puts

Similar to a covered call, we can sell a put contract. It is called cash-secured puts. The only difference is we needed to buy the stock with the cash we had.

Let's write some code.

I am going to show the process using Robinhood. I will also point out ways for other brokerages.

Also, we will be using the robin-stocks python package for the trades.

We needed the below information for covered call

  1. Ticker
  2. Option type — call or put
  3. Expiration date
  4. Strike price

Ticker

As a first step, we needed to list all the tickers(you can have many) which are required and get their corresponding prices. The last line of the code will provide us the all tradable option contracts using Robinhood.

Option type

If you hold 100 shares or more, then perform a call. Else if you have cash in the account, then go with puts.

Expiration date

Getting the correct expiration is a little tricky. There is no correct or wrong date. I have a preference to choose a date between 30–90 days. So we can have a reasonable premium for the contract, and it will have sufficient time.

The above code says to take the contract between 30 to 90 are the days.

Strike price

This is one of the more extensive discussions about how to get a fair price. You needed to understand delta, implied volatility, and other greek metrics. To keep it simple, we go and find all the contracts traded and get an optimal one based on our preference.

It will take a separate article to go over the above numbers. But basically, we are looping through the entire set of contracts to find the right one which works for us.

Here I have used airtable for storing the information. But it can be stored anywhere in a database. Storing the records helps us to see the trades which are happening.

So the final output is a set of stocks that is tradable with the optimum strike price, expiration date, percentage difference, and the chance of profit.

Output

Placing trades

Now placing trades is the only last step that is pending. If you are making a covered call, make sure you have 100 shares of the asset. If you have it, then the below code will place the trade. If you don't have 100 shares, the order will not go through. Also, these parameters are parametrized for each ticker.

Excellent, now we have the code to place the trades depending on the stocks which we own. It can be a covered call or cash-secured puts. We need to sell the contract for a profit.

It is another tricky area on how to sell the option with a bunch of metrics. So keep tuned! It will be discussed in my next article.

Final thoughts

Options are risky; however, there is a standard contract or procedure for using it. If you obey those rules, you are always in good hands. As humans, we get greedy and wait for more profits. Program all your logic and code to avoid that, so you don't need to worry about it.

In the above article, we have seen how to place the trades in Robinhood and focus on the US market. However, you can use the same approach universally in any region which has options trading.

Also, there are other APIs like EODhistorical, which provide data internationally. All you need to do is to remove the Robinhood API and use your brokerage API.

In the comments, would you please let me know if you want a more basic version of the article or an advanced version?

Subscribe

Please subscribe to my newsletter to get updates on my articles and free code for my articles.

I write on different data science-related products and try out new tools. Connect with me on LinkedIn and Twitter.

Get Code

Please subscribe to my newsletter to get the complete working code for my articles and other updates.

--

--