def main(): # -------------------------[Set-up]------------------------- ticker = Ticker('MSFT') optionManager = OptionManager(ticker, now=None) i = 0 # <- Time To Maturity curve exp = optionManager.getExpirations()[i] optionChain = optionManager.getOptionChain(exp=exp) # ---------------------------------------------------------- start_time = time.time() impliedVolatility = ImpliedVolatility(optionChain) curve = impliedVolatility.getImpliedVolatility() print('chrono: {}'.format(time.time() - start_time)) fig, ax = plt.subplots(figsize=(8, 5)) ax.set(xlabel='Strike', ylabel='Volatility', title='Option implied volatility ({})'.format( ticker.getInfo().ticker)) ax.plot(curve.calls.strike, curve.calls.IV, label='Calls', color='green', alpha=.5) ax.plot(curve.puts.strike, curve.puts.IV, label='Puts', color='red', alpha=.5) ax.plot(curve.calls.strike, curve.calls.naturalIV, linestyle='dotted', label='Original Calls', color='green') ax.plot(curve.puts.strike, curve.puts.naturalIV, linestyle='dotted', label='Original Puts', color='red') plt.legend() plt.show()
import os import pandas as pd from EcoFin.dataDownload.optionsManager import OptionManager from EcoFin.dataDownload.ticker import Ticker from EcoFin.options.deepOptionChain import DeepOptionChain # -------------------------[Set-up]------------------------- ticker = Ticker('MSFT') optionManager = OptionManager(ticker) exp = optionManager.getExpirationByMaturity(30, method='greater') optionChain = optionManager.getOptionChain(exp=exp) # ---------------------------------------------------------- ticker_info = ticker.getInfo() forwardPrice = optionChain.getForwardPrice() deepOptionChain = DeepOptionChain(optionChain, computeIV=False) data = deepOptionChain.getDeepOptionChain() # ----------------------[EXPORT BLOCK]-------------------------------- path = '../Export/[{}]'.format(ticker.ticker) if not os.path.exists(path): os.makedirs(path) with pd.ExcelWriter('{}/deepOptionChain_[{}].xlsx'.format(path, exp)) as writer: data.to_excel(writer, sheet_name='Chain', index=False) print('\nFile saved!') # ----------------------[EXPORT BLOCK]--------------------------------
volatilitySmile = VolatilitySmile(impliedVolatility) smile = volatilitySmile.getVolatilitySmile() fig, ax = plt.subplots(figsize=(8, 5)) ax.plot(smile.strike, smile.smile, label='Smile', alpha=.5) ax.plot(smile.strike, smile.naturalSmile, linestyle='dotted', label='Original Smile') ax.plot(forwardPrice, np.amin(smile.smile) * 0.5, markersize=8, marker="^", color='violet') ax.vlines(forwardPrice, np.amin(smile.smile) * 0.5, np.amax(smile.smile), linestyles="dashed", color='violet', alpha=.6, label='Forward Price') ax.set(xlabel='Strike', ylabel='Volatility', title='Option volatility smile ({})'.format(ticker.getInfo().ticker)) ax.legend() ax.grid() plt.show()
from EcoFin.options.chainWeights import ChainWeights from EcoFin.options.impliedVolatility import ImpliedVolatility from EcoFin.options.volatilitySmile import VolatilitySmile # -------------------------[Set-up]------------------------- ticker = Ticker('MSFT') optionManager = OptionManager(ticker, now=None) i = 0 # <- Time To Maturity curve exp = optionManager.getExpirations()[i] optionChain = optionManager.getOptionChain(exp=exp) # ---------------------------------------------------------- fig, axs = plt.subplots(2, figsize=(15, 8), sharex=True) fig.suptitle('Weigh implied volatility ({})'.format(ticker.getInfo().ticker), fontsize=16) forwardPrice = optionChain.getForwardPrice() impliedVolatility = ImpliedVolatility(optionChain) volatilitySmile = VolatilitySmile(impliedVolatility) smile = volatilitySmile.getVolatilitySmile() chainWeights = ChainWeights(optionChain) weights = chainWeights.computeMoneynessWeights() # chart 1 axs[0].set_title('Volatility smile (VS)') axs[0].plot(smile.strike, smile.smile, label='Smile') axs[0].plot(forwardPrice,
""" tickerHistory.py Created by Luca Camerani at 06/10/2020, University of Milano-Bicocca. ([email protected]) All rights reserved. This file is part of the EcoFin-Library (https://github.com/LucaCamerani/EcoFin-Library), and is released under the "BSD Open Source License". """ import matplotlib.pyplot as plt from EcoFin.dataDownload.ticker import Ticker ticker = Ticker('MSFT') history = ticker.getHistory().tail(100) plt.plot(history.index, history.Close, label='Price ($S_t$)') plt.show() print(ticker.getInfo())