Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ######################################################################
- # Program pulls option chain data for calls from yahoo finance API.
- # User enters stock ticker then an integer for the dollar spread
- # around the current stock price to be displayed.
- # E.G. if stock 'ABC' is trading at $10 and you enter '4' as the
- # integer for the spread, the option chain will display the calls
- # from the $6 strike through $14 strike.
- #
- # Author: bizraeli
- # Date Created: 09/13/21
- ######################################################################
- from yahoo_fin import options
- from yahoo_fin import stock_info as si
- print("\n")
- ticker = input("Stock ticker? (ex: AMZN, AAPL): ")
- # gets current stock price for ticker and prints the price
- sinfo = si.get_live_price(ticker)
- print("Stock price: ", sinfo)
- print("\n")
- # gets list of current option expiration dates available and prints them out
- ticker_dates = options.get_expiration_dates(ticker)
- print(ticker_dates)
- print("\n")
- # user selects which expiration date they wish to see option data for
- # input in MM/DD/YY fomrat e.g. 01/19/24
- date = input("Select a date from the list (ex: MM/DD/YY): ")
- # option chain data for inputed stock ticker and expiration date
- ticker_calls=options.get_calls(ticker, date)
- # Prints the total amount of strike prices available on the chosen expiration date
- # then asks the user to enter an integer if they want to limit the amount of
- # strike prices displayed.
- rows_count = len(ticker_calls.index)
- print("Option chain size is: ", rows_count)
- spread = int(input("To limit range displayed enter integer for +/- $ amount to be displayed (For none enter '0'): "))
- # When a non-zero spread is entered, the program prints the option chain data
- # for the strikes above the current price by the inputed integer, and for the
- # strikes below the current price by the inputed integer. A spread of 0
- # prints all of the strikes.
- if spread != 0:
- print(ticker_calls.loc[(ticker_calls["Strike"] >= sinfo - spread) & (ticker_calls["Strike"] <= sinfo + spread)] )
- elif spread == 0:
- print(ticker_calls)
- print("Stock price: ", sinfo)
- print("\n")
- # NOTE, the following comparison option does not work, regardless of what the
- # user inputs, the program will always prompt the user to enter another
- # expiration date to be used as a point of comparison. Modification is needed
- # for comparison to trigger properly.
- compare = input("Would you like to compare two expiration dates? (Y/N): ")
- sinfo2 = si.get_live_price(ticker)
- # This comparison check needs to be tweaked.
- # Program runs a second time so you can compare two different expiration dates
- # and a different range of strikes or the same range of strikes if you choose.
- if compare == "Y" or "y" or 'Y' or 'y':
- print(ticker_dates)
- print("\n")
- date2 = input("Select a date from the list (ex: MM/DD/YY): ")
- ticker_calls2=options.get_calls(ticker, date2)
- rows_count2 = len(ticker_calls2.index)
- print("Option chain size is: ", rows_count2)
- spread2 = int(input("To limit range displayed enter integer for +/- $ amount to be displayed (For none enter '0'): "))
- if spread2 != 0:
- print(ticker_calls2.loc[(ticker_calls2["Strike"] >= sinfo2 - spread2) & (ticker_calls2["Strike"] <= sinfo2 + spread2)] )
- elif spread2 == 0:
- print(ticker_calls2)
- # Compare check does not work, always prints the stock price for inserted
- # ticker. NOTE: when running this program during market hours, the prices will
- # differ from the time the first expiration date was entered and the time
- # at which the second expiration date was entered.
- elif compare != "Y" or "y" or 'Y' or 'y':
- print("Stock price: ", sinfo2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement