from EcoFin.stat.equityVolatility import EquityVolatility # -------------------------[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) # ---------------------------------------------------------- strikeList = optionChain.getStrikeList() volatility_back = 260 series = ticker.getHistory( end=optionChain.getChainDate()).tail(volatility_back) price = optionChain.getSpotPrice() r = optionChain.getRiskFreeRate() sigma = EquityVolatility(series).meanSigma() maturity = optionChain.getTimeToMaturity().days forwardPrice = optionChain.getForwardPrice() fig, ax = plt.subplots() chain = optionChain.getChain() theoreticalPrices = {'call': [], 'put': []} for strike in strikeList: option = BSM(price, strike, r, sigma, maturity)
and is released under the "BSD Open Source License". """ import matplotlib.pyplot as plt import numpy as np import pandas as pd from EcoFin.assetAllocation.markowitzModel import MarkowitzModel from EcoFin.dataDownload.ticker import Ticker market = ['AAPL', 'AMZN', 'CSCO', 'MSFT', 'GOOGL'] marketDataFrame = pd.DataFrame(columns=market) for company in market: ticker = Ticker(company) df = ticker.getHistory().tail(260) marketDataFrame[company] = df['Close'] model = MarkowitzModel(marketDataFrame) randW = model.randomWeights(10000) rets = [] stds = [] for r in randW: performance = model.portfolioPerformance(r) rets.append(performance.ret) stds.append(performance.std) plt.scatter(stds, rets,
optionManager = OptionManager(ticker, now=1492646400) exp = optionManager.getExpirationByMaturity(30, method='greater') optionChain = optionManager.getOptionChain(exp=exp) # ---------------------------------------------------------- ticker_info = ticker.getInfo() forwardPrice = optionChain.getForwardPrice() deepOptionChain = DeepOptionChain(optionChain) data = deepOptionChain.getDeepOptionChain() data['weights'] = data.loc[:, ['openInterest_call', 'openInterest_put']].sum(axis=1) / \ np.nansum(data.loc[:, ['openInterest_call', 'openInterest_put']].to_numpy()) history_back = 100 history = ticker.getHistory(end=optionChain.getChainDate()).tail(history_back) data['S_mean'] = np.mean(data[['S_U', 'S_L']], axis=1) forecast = np.mean(data[['S_U', 'S_L', 'S_mean']]) fig, axs = plt.subplots(3, figsize=(15, 8)) fig.suptitle('No-arbitrage price bounds ({})'.format(ticker_info.longName), fontsize=16) # chart 1 axs[0].set_title('No arbitrage price bounds') axs[0].plot(data.strike, data['S_U'], label='Upper bound ($S^{U})_{now}$)', color='green') axs[0].plot(data.strike,
forwardP.index, linestyle="dashed", color='white', alpha=1, label='Forward Price') ax.legend() plt.colorbar(c) # Plot underlying price ax = fig.add_subplot(224) ax.set_title('Underlying prices') # Get underlying price history history_back = 30 history = ticker.getHistory(end=now).tail(history_back) ax.plot(history.Close) plt.show() # Save frame # ----------------------[EXPORT BLOCK]-------------------------------- path = r'../../../Export/{}'.format(ticker.ticker) if not os.path.exists(path): os.makedirs(path) plt.savefig(r'{}/{}_{}.png'.format(path, now, ticker.ticker)) # ----------------------[EXPORT BLOCK]-------------------------------- except: print('Error: {}'.format(now)) pass
""" 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())