Python code for monthly returns | Stock Options | Part 2

Shyam BV
Investor’s Handbook
5 min readDec 8, 2021

Background

Trading stock options in a safe manner is a specific set of procedures. Why not automate it when it is possible? In this article, I will show how to trade options using Robinhood. I have written an article on how to get options data in the below article.

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

Below are the different options contracts which can be traded. Off-course there are complex types such as credit spread put options, iron condor, etc. But let's focus our goal on autonomous trading in a safe fashion.

Sell an options contract.

Selling an option can be a covered call or cash-secured puts as a starting point for opening a position. Please refer to part one of the articles on how to place trade automatically.

Opening a position involves various analyses, including delta, probability of success, and other greek parameters. It also depends on your risk parameters which I will let you decide on it.

Below is the code to place a sell option contract (Open a position)

Excellent! Once we sell a contract, we will be credited with the premium amount.

Buyback the contract

If you have traded options before, you know that it is mostly best to buy back the contract rather than exercise it. But we also need to know when to buy it depending on our risk parameters.

For example, we are trading covered call of a stock NIO. You get credited with $103, and the contract price is $1.03

For 50% returns, the buyback price is $0.515

For 73% returns, the buyback price is $0.27

So on a timeframe of 13 days, this option contract is repurchased. We can also let the contract expire, and it is up to the individual risk tolerance. So approximately 2% of returns. This is not a return similar to wallstreetbets. However, when we do it in an automated fashion, this is almost the free minimum monthly income for the stocks we already hold.

If we have a 100K account, we will approximately receive 2K-3K of the amount on a bi-weekly or monthly basis. If we reinvest back the amount, we will be getting higher returns.

Code for buying back

Now we know how the contract works, let us see the code to buy back the contract.

Get all the option trades.

We needed two pieces of information from Robinhood to perform these trades, and we needed to calculate the returns of each contract we held. The below code will achieve it.

We need to buy the contracts to close the contract once we get the contracts. To create a simple formula, we have three conditions. Of course, we can add more logic and requirements on top of it.

Conditions to code

Buy to close when the current contract value.

  1. Has a profit of 70% from our price irrespective of the contract expiry date
  2. If the contract return is between 0–70% and the days of expiry is less than a day
  3. Returns are negative and decide when to buy back depending on risk level

Now let us see how to convert these conditions into code.

Placing sell orders

Once buy to close order satisfies all our conditions, we need to place our sell order. Below is the code for buy to close an option.

We can modify the conditions, order timeline, quantity, and other details according to the risk factors.

Great! Now we have completely automated the process of sell to open and buy to close. It has potential returns up to 70% on a given options trade.

Alternatively, you can put a limit order; however, we need to calculate manually, missing out on the rules, manual intervention. This is all about automation.

Closing thoughts

In the two-part article, we have discussed how to find the optimal options and perform covered call or cash-secured puts. Create a sell to open and monitor for optimal time-window to perform buy to close in an automated fashion.

This works for different options contracts at other expiry dates. So you will have peace of mind and let the code handle your trades!

Related article

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.

--

--