Trading options spread using python

Shyam BV
Code Sprout
Published in
6 min readJan 8, 2022

--

Introduction

Trading options with python is not something new. Please take time to understand the below articles for better understanding.

I have been requested a lot for spread trading. Credit spreads trading is an advanced topic and needs understanding on different fronts. So gear up; in this article, I will explain how to trade credit spreads using python and Robinhood.

Photo by Maxim Hopman 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. Credit spreads take it to the next level, and you need a deeper understanding to trade credit spreads.

What are spreads?

An option spreads a strategy that involves the simultaneous buying and selling of options on the same underlying asset.

Credit spread

A credit spread involves selling a high-premium option while purchasing a low-premium option in the same class or of the same security, resulting in a credit to the trader's account.

At the trade open, you will get a credit for the trade. Here is a summarized way to think about credit spread.

Put credit spread — Bullish
Call debit spread — Bearish

Debit spread

A debit spread involves purchasing a high-premium option while selling a low-premium option in the same class or of the same security, resulting in a debit from the trader's account.

At the trade open, you will get be debited for the trade. Here is a summarized way to think about credit spread.

Put credit spread — Bearish
Call debit spread —Bullish

Mixed spreads

This is where all the mixed strategies such as long/short strangle, iron condor, etc. come to the picture.

There are so many strategies so that we will focus on one area. Put credit spreads for the sake of simplicity. Similar concepts can be applied to other strategies.

Things required for spread trading

  1. Trading account (I use Robinhood)
  2. Robin-stocks package
  3. Level-3 options enabled in the account

I know robinhood has a latency in option contract execution. However, I don’t see any alternative platform with no fees and with API enabled.

Steps involved

Our code needs to do the below ones.

  1. Login to your account — I will skip the login part covered in the previous articles.
  2. Screen the tickers — SPY, TSLA, AMZN, etc.
  3. Filter according to specific parameters — Such as delta, RSI, etc.
  4. Buy the credit spread
  5. Monitor the credit spread and price
  6. If it hits the target, buy back the sell contract. Remember it is put credit spread.
  7. Then sell the open put contract.

Although it is a lot of work, it is achievable with our code.

1. Screen the tickers

We all know there are a ton of tickers, and however, not all the tickers have an excellent options volume. If we wanted to trade credit spreads, we needed good liquidity and sufficient contracts in float.

So for the sake of this article, we will choose the tickers which have enough liquidity. Check the article listed to see how to automate it.

Tickers: SPY, TSLA and AMD. We are creating a config like the below one for all the stocks.

Expiry lag: How many days of lag or later expiry date do we need
Two_lags: For SPY example

390 — Open — sell put option — 8 strike prices lagging
391 — Open — buy long put option — 9 strike prices lagging

Rule: Don't open new contracts for that ticker if you have an open one. So to get it we need to retrieve all the current open options positions. We can add additional rules according to our needs.

2. Filter according to specific parameters

Once we filter out the contracts, we need to validate if it meets all your needs. Here we can include all our fancy technical indicators, deltas, etc. But for now, I will say if the stock price has fallen below our config percentage, we will buy it.

Great! Now we have verified all the option positions. Of course, we can add a lot of conditions here, depending on our strict verification process.

3. Buy the credit spreads

Buying credit spreads is robin-stocks API is a little tricky. We need to combine all our option contracts in a list and execute it.

As we are doing put credit spread, we needed to sell a put contract and buy a put contract at a lower strike price.

Fantastic! Above code will place buy a put credit spread on your account at the price value which you mention. I can also detail the option_value, but the calculation is complicated.

5. Monitor the credit spread and price

We needed to calculate the overall returns. If we have access to real-time feeds or webhooks, we can calculate the profit.

We can calculate our profit from the below code.

Let's say if we wanted 70% returns on every contract we need to have a condition for the total_returns value and execute our trade accordingly.

6. Buyback the sell contract

If the condition is met, we will buy back the contract we sold before.

It is really important to buy back the contract at the correct time. Option contract decays over the period of time. So delay as much as possible and buy the contract.

Here I have shown the logic for-profit scenario, and I have not shown the logic for the loss scenario as it depends on each person's loss threshold of when to sell a contract and how much is the loss appetite. But the logic remains the same.

Sell the open long put contract

Once you buy back the sell contract, we will have a long put contract. We can leave the contract as it is and let it expire. Or do we need to leave our money on the table? We can sell the long contract and squeeze in some more dollars on the contract.

Fantastic. Now we have successfully closed our entire credit spread with a profit.

If the entire process goes correctly, this is how you will see your trades.

For SPY — put credit spread.

Profit calculation for one single short term SPY contract:

Sell put credit price(C)— $26
Long put credit price (D)— $23

Buyback credit spread(D) — $18
Sell long credit spread(C) — $36

Total profit: $21

This is just a simple calculation for a single contract, and it can be done for any number of stocks with any number of contracts.

Final thoughts

We have seen how to screen the tickers and place the credit option trades using python. Also, monitor these option prices and sell them using python at the correct timeframe.

Options trading is a set of predefined rules. However, it has risks if it does not go as you planned. I hope this gave you a starting point.

DM me if you have expertise in credit options and python to collaborate more.

Subscribe

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

I write on different data science, trading, NFT, 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.

--

--