Advertisement
Guest User

calls.py code

a guest
Aug 12th, 2022
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. ######################################################################
  2. # Program pulls option chain data for calls from yahoo finance API.
  3. # User enters stock ticker then an integer for the dollar spread
  4. # around the current stock price to be displayed.
  5. # E.G. if stock 'ABC' is trading at $10 and you enter '4' as the
  6. # integer for the spread, the option chain will display the calls
  7. # from the $6 strike through $14 strike.
  8. #
  9. # Author: bizraeli
  10. # Date Created: 09/13/21
  11. ######################################################################
  12. from yahoo_fin import options
  13. from yahoo_fin import stock_info as si
  14.  
  15. print("\n")
  16. ticker = input("Stock ticker? (ex: AMZN, AAPL): ")
  17.  
  18. # gets current stock price for ticker and prints the price
  19. sinfo = si.get_live_price(ticker)
  20. print("Stock price: ", sinfo)
  21. print("\n")
  22.  
  23. # gets list of current option expiration dates available and prints them out
  24. ticker_dates = options.get_expiration_dates(ticker)
  25. print(ticker_dates)
  26. print("\n")
  27.  
  28. # user selects which expiration date they wish to see option data for
  29. # input in MM/DD/YY fomrat e.g. 01/19/24
  30. date = input("Select a date from the list (ex: MM/DD/YY): ")
  31.  
  32. # option chain data for inputed stock ticker and expiration date
  33. ticker_calls=options.get_calls(ticker, date)
  34.  
  35. # Prints the total amount of strike prices available on the chosen expiration date
  36. # then asks the user to enter an integer if they want to limit the amount of
  37. # strike prices displayed.
  38. rows_count = len(ticker_calls.index)
  39. print("Option chain size is: ", rows_count)
  40. spread = int(input("To limit range displayed enter integer for +/- $ amount to be displayed (For none enter '0'): "))
  41.  
  42. # When a non-zero spread is entered, the program prints the option chain data
  43. # for the strikes above the current price by the inputed integer, and for the
  44. # strikes below the current price by the inputed integer. A spread of 0
  45. # prints all of the strikes.
  46. if spread != 0:
  47. print(ticker_calls.loc[(ticker_calls["Strike"] >= sinfo - spread) & (ticker_calls["Strike"] <= sinfo + spread)] )
  48. elif spread == 0:
  49. print(ticker_calls)
  50.  
  51. print("Stock price: ", sinfo)
  52. print("\n")
  53.  
  54. # NOTE, the following comparison option does not work, regardless of what the
  55. # user inputs, the program will always prompt the user to enter another
  56. # expiration date to be used as a point of comparison. Modification is needed
  57. # for comparison to trigger properly.
  58. compare = input("Would you like to compare two expiration dates? (Y/N): ")
  59.  
  60. sinfo2 = si.get_live_price(ticker)
  61.  
  62. # This comparison check needs to be tweaked.
  63. # Program runs a second time so you can compare two different expiration dates
  64. # and a different range of strikes or the same range of strikes if you choose.
  65. if compare == "Y" or "y" or 'Y' or 'y':
  66. print(ticker_dates)
  67. print("\n")
  68. date2 = input("Select a date from the list (ex: MM/DD/YY): ")
  69. ticker_calls2=options.get_calls(ticker, date2)
  70.  
  71. rows_count2 = len(ticker_calls2.index)
  72. print("Option chain size is: ", rows_count2)
  73. spread2 = int(input("To limit range displayed enter integer for +/- $ amount to be displayed (For none enter '0'): "))
  74. if spread2 != 0:
  75. print(ticker_calls2.loc[(ticker_calls2["Strike"] >= sinfo2 - spread2) & (ticker_calls2["Strike"] <= sinfo2 + spread2)] )
  76. elif spread2 == 0:
  77. print(ticker_calls2)
  78.  
  79. # Compare check does not work, always prints the stock price for inserted
  80. # ticker. NOTE: when running this program during market hours, the prices will
  81. # differ from the time the first expiration date was entered and the time
  82. # at which the second expiration date was entered.
  83. elif compare != "Y" or "y" or 'Y' or 'y':
  84. print("Stock price: ", sinfo2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement