How To Use Python To Buy Options From Robinhood | by Melvynn Fernandez | Towards Data Science

Just really want to test out my programming skills to mess with stocks.
So what are options in trading? They are contracts which give you the right to buy or sell that asset at a price for a period of time. Some people view options as some sort of lottery ticket but I view it like blackjack, where you can do certain strategies to reduce your risk.
In this article I will create Python code that will look at stock and purchase it. In order to do that let’s look at how an option looks like. Here is an example of how a standard quote of an option looks.
ABC FEBRUARY 14, 2020 $69 CALL AT $4.20
Let’s break this down. ABC is the stock that the option is based on and it usually represents 100 shares of said stock. FEBRUARY 14, 2020 represents the month when the option is going to expire. 69 is the strike price for the stock. CALL would be one the option types, puts can also be filled here. $4.20 is the premium of the stock.
So Microsoft stock has been really good to me so far in options trading. So far I have gained a significant amount. You can see my gains and losses here. We will use Microsoft to purchase an option on Robinhood.
What I am using to pull data from Robinhood is called robin_stocks. I have an article showing how to set it up below.
towardsdatascience.com
So for my trading strategy for Microsoft stock is I usually buy an option call 30 days ahead of the current date and then I select a strike price that is above the current stock price.
robin_stocks makes it simple where we can already pull the available stock options that we can send to them using find_tradable_options_for_stock. I have set it up where it already pulls just the calls below.
r.options.find_tradable_options_for_stock(‘MSFT’, optionType=’call’)
I like viewing the results in a DataFrame so I will transfer all this data over. Results will look like this:
Now that we know what expiration dates are available we can now send an option order with the correct information. We will use this line of code to send orders in robin_stocks.orders.order_buy_option_limit(price, symbol, quantity, expirationDate, strike, optionType='both', timeInForce='gfd')
For price, we can just fill in with the current option price. For symbol, we are going to be using MSFT . Quantity we will just be purchasing 1 contract order. For date, we will use 30 days from today (2/13/2020). So we will try to pick a date close to 30 days(3/13/2020). Strike we will choose a price that is closest and above the current stock price. Option type we want a call. Time in force is good for day (gfd). So our code will look like this and its translation in Robinhood:
robin_stocks.orders.order_buy_option_limit(4.20, 'MSFT', 1, 2020-03-13, 185.00, optionType='call', timeInForce='gfd')
MSFT MARCH 13, 2020 185 CALL AT $4.20
If we run this code in Python the order will be sent. However, it does not necessarily that the order executed. Here’s the full code below:
Access to my Microsoft gains and losses here!
I also have tutoring and career guidance available here!
Don’t forget to connect with me on LinkedIn if you guys have any questions, comments or concerns!