def execute(tick: str): ticker = Ticker(tick) optionManager = OptionManager(ticker) output = pd.DataFrame for now in range(date1, date2, increment): # Compute day by day try: # Exclude bad results and weekends if optionManager.setNow(now) is not False and datetime.fromtimestamp(now).weekday() not in [5, 6]: exp = optionManager.getExpirationByMaturity(maturity_min, method='greater') optionChain = optionManager.getOptionChain(exp=exp) deepOptionChain = DeepOptionChain(optionChain, computeIV=computeIV, progressBar=False) summary = {'Date': unixtimestamp_to_date(optionChain.getChainDate()), 'Exp': unixtimestamp_to_date(optionChain.getChainExpiration()), 'Maturity': optionChain.getTimeToMaturity().days, 'ForwardPrice': optionChain.getForwardPrice(), 'SpotPrice': optionChain.getSpotPrice(), } # compute signals chainWeights = ChainWeights(optionChain) for mode, weights in {'EW': chainWeights.computeEquallyWeights(), 'Beta': chainWeights.computeBetaWeights(), 'ATM': chainWeights.computeATMWeights(), 'OI': chainWeights.computeOpenInterestsWeights(), 'Moneyness': chainWeights.computeMoneynessWeights()}.items(): synopsis = OptionChainSinopsys(deepOptionChain, weights=weights) OPS = synopsis.computeOptionPriceSpread() IVS = synopsis.computeImpliedVolatilitySpread() NAP = synopsis.computeNoArbitragePrice() OIR = synopsis.computeOpenInterestRatio() summary['OPS_[{}]'.format(mode)] = OPS.mean summary['IVS_[{}]'.format(mode)] = IVS.mean summary['NAP_[{}]'.format(mode)] = NAP.value summary['NAP_ret_[{}]'.format(mode)] = NAP.ret summary['OIR_[{}]'.format(mode)] = OIR.mean PCD = synopsis.computePutCallDelta() summary['PCD'] = PCD # Compute volatility metrics VIX = EquityVIX(deepOptionChain) summary['VIX_[hist]'] = VIX.getHistoricalVolatility() summary['VIX_[mean]'] = VIX.getMeanVIX().value summary['VIX_[beta]'] = VIX.getBetaVIX().value summary['VIX_[CBOE]'] = VIX.getCBOEVIX() if output.empty: output = pd.DataFrame(columns=list(summary.keys()), index=[]) output = output.append(summary, ignore_index=True) except Exception as e: logs.append('Error with [{}] - [{}]: {}'.format(ticker.ticker, now, e)) pass # ----------------------[EXPORT BLOCK]-------------------------------- path = '../Export/BackTest/{}'.format(ticker.ticker) if not os.path.exists(path): os.makedirs(path) try: with pd.ExcelWriter('{}/backTest_[{}].xlsx'.format(path, maturity_min)) as writer: output.to_excel(writer, sheet_name='Results', index=False) except: pass
# -------------------------[Set-up]------------------------- ticker = Ticker('MSFT') optionManager = OptionManager(ticker) # Custom increment = 86400 # 1 day date1 = 1483228800 # Sunday 01 January 2017 date2 = date1 + increment * 365 * 3 # ---------------------------------------------------------- ticker_info = ticker.getInfo() output = [] for now in tqdm(range(date1, date2, increment), desc='Compute history'): # Compute day by day if optionManager.setNow(now) is not False: exp = optionManager.getExpirationByMaturity(5, method='greater') optionChain = optionManager.getOptionChain(exp=exp) deepOptionChain = DeepOptionChain(optionChain, computeIV=True, progressBar=False) VIX = EquityVIX(deepOptionChain) summary = { 'Date': unixtimestamp_to_date(optionChain.getChainDate()), 'SpotPrice': optionChain.getSpotPrice(), 'CBOE_VIX': VIX.getCBOEVIX(), 'Mean': VIX.getMeanVIX().value, 'Beta': VIX.getBetaVIX().value, 'ATM': VIX.getATMVIX().value,
This file is part of the EcoFin-Library (https://github.com/LucaCamerani/EcoFin-Library), and is released under the "BSD Open Source License". """ from EcoFin.dataDownload.optionsManager import OptionManager from EcoFin.dataDownload.ticker import Ticker # -------------------------[Set-up]------------------------- ticker = Ticker('MSFT') # 1Y increment = 86400 maturity = 30 date1 = 1546300800 date2 = 1577664000 # ---------------------------------------------------------- optionManager = OptionManager(ticker) ticker_info = ticker.getInfo() for now in range(date1, date2, increment): # Compute day by day try: optionManager.setNow(now) exp = optionManager.getExpirationByMaturity(maturity, 'greater') optionChain = optionManager.getOptionChain(exp=exp) print('Maturity: {} days'.format(optionChain.getTimeToMaturity().days)) except: pass