def getPertinentDataForModels(allModels): tickersRequired = [] for mod in allModels: print(mod.describe()) for ticker in mod.returnAllTickersInvolved(): if ticker not in tickersRequired: tickersRequired.append(ticker) pulledData, validTickers = dataAck.downloadTickerData(tickersRequired) joinedData = dataAck.joinDatasets( [pulledData[ticker] for ticker in pulledData]) return joinedData
def getPertinentDataForModels(allModels): tickersRequired = [] tickersTraded = [] for mod in allModels: print(mod.describe()) if mod.inputSeries.targetTicker not in tickersRequired: tickersRequired.append(mod.inputSeries.targetTicker) if mod.inputSeries.series.ticker not in tickersRequired: tickersRequired.append(mod.inputSeries.series.ticker) if mod.inputSeries.targetTicker not in tickersTraded: tickersTraded.append(mod.inputSeries.targetTicker) pulledData, validTickers = dataAck.downloadTickerData(tickersRequired) joinedData = dataAck.joinDatasets( [pulledData[ticker] for ticker in pulledData]) return joinedData
def computeReturnsForUniqueModelsCache(uniqueModels, factorToTrade): tickersRequired = [] for mod in uniqueModels: print(mod.describe()) if mod.inputSeries.targetTicker not in tickersRequired: tickersRequired.append(mod.inputSeries.targetTicker) if mod.inputSeries.series.ticker not in tickersRequired: tickersRequired.append(mod.inputSeries.series.ticker) if factorToTrade not in tickersRequired: tickersRequired.append(factorToTrade) pulledData, validTickers = dataAck.downloadTickerData(tickersRequired) joinedData = dataAck.joinDatasets([pulledData[ticker] for ticker in pulledData]) modelReturns, modelPredictions, modelSlippageReturns, cleanedModels = generateAllReturnsFromCache(uniqueModels) return cleanedModels, modelReturns, modelPredictions, modelSlippageReturns, modelReturns.join(dataAck.getDailyFactorReturn(factorToTrade, joinedData)).dropna(), joinedData
"startedTrading": downloadedPortfolioInfo[portfolioHash] } print(portfolioInfo) portfolioInfos.append(portfolioInfo) ##GET ALL BENCHMARKS benchmarksNeeded = [] print(joinedData.columns.values) for info in portfolioInfos: print(info["benchmark"]) if info["benchmark"] not in benchmarksNeeded and ( "Adj_Close_" + info["benchmark"] not in joinedData.columns.values): benchmarksNeeded.append(info["benchmark"]) print("BENCHMARKS NEEDED", benchmarksNeeded) pulledData, unused_ = dataAck.downloadTickerData(benchmarksNeeded) thisJoinedData = joinedData for ticker in pulledData: thisJoinedData = joinedData.join(pulledData[ticker], how='outer') for info in portfolioInfos: portfolioHash = info["key"] print(portfolioHash) portfolioData = autoPortfolioTree.getDataForPortfolio( portfolioHash, portfolioInfo["benchmark"], thisJoinedData, portfolioInfo["startedTrading"]) portfolioGeneration.cachePortfolio(info, portfolioData, mode) # # CALCULATE FUND PERFORMANCE METRICS # In[43]:
joinedData = None validTickers = None if tData is None: dataAck.logModel("Cache", { "type":"miss", "ticker":tickerToTrade, "day":str(portfolio.getToday()) }) tickersToPull = dataAck.getDataSourcesForTicker(tickerToTrade) print(tickersToPull) pulledData, validTickers = dataAck.downloadTickerData(tickersToPull) joinedData = dataAck.joinDatasets([pulledData[ticker] for ticker in pulledData]) dataAck.storeTrainingData(tickerToTrade, (joinedData, validTickers)) else: joinedData = tData[0] validTickers = tData[1] dataAck.logModel("Cache", { "type":"hit", "ticker":tickerToTrade, "day":str(portfolio.getToday()) }) sManager = dataAck.seriesManager(validTickers)
def getDataForPortfolio(portfolioKey): models = portfolio.getModelsByKey(portfolio.getPortfolioModels(portfolioKey)) ##DOWNLOAD REQUIRED DATA FOR TARGET TICKERS tickersRequired = [] for mod in models: print(mod.describe()) if mod.inputSeries.targetTicker not in tickersRequired: tickersRequired.append(mod.inputSeries.targetTicker) pulledData, validTickers = dataAck.downloadTickerData(tickersRequired) joinedData = dataAck.joinDatasets([pulledData[ticker] for ticker in pulledData]) ##GENERATE RETURNS FOR PORTFOLIO portfolioAllocations = portfolio.getPortfolioAllocations(portfolioKey) predsTable = pd.DataFrame([]) weightsTable = pd.DataFrame([]) tickerAllocationsTable = pd.DataFrame([]) scaledTickerAllocationsTable = pd.DataFrame([]) for allocation in portfolioAllocations: colsAlgo = [] valsAlgo = [] colsAlgoWeight = [] valsAlgoWeight = [] colsTicker = [] valsTicker = [] colsTickerScaled = [] valsTickerScaled = [] for key in allocation: if key.startswith("ticker_"): colsTicker.append(key[len("ticker_"):]) valsTicker.append(allocation[key]) if key.startswith("scaled_ticker_"): colsTickerScaled.append(key[len("scaled_ticker_"):]) valsTickerScaled.append(allocation[key]) if key.startswith("algo_") and not key.startswith("algo_weight_"): colsAlgo.append(key[len("algo_"):]) valsAlgo.append(allocation[key]) if key.startswith("algo_weight_"): colsAlgoWeight.append(key[len("algo_weight_"):]) valsAlgoWeight.append(allocation[key]) predsTable = pd.concat([predsTable, pd.DataFrame([valsAlgo], index = [allocation["predictionDay"]], columns=colsAlgo).tz_localize(None)]) weightsTable = pd.concat([weightsTable, pd.DataFrame([valsAlgoWeight], index = [allocation["predictionDay"]], columns=colsAlgoWeight).tz_localize(None)]) tickerAllocationsTable = pd.concat([tickerAllocationsTable, pd.DataFrame([valsTicker], index = [allocation["predictionDay"]], columns=colsTicker).tz_localize(None)]) scaledTickerAllocationsTable = pd.concat([scaledTickerAllocationsTable, pd.DataFrame([valsTickerScaled], index = [allocation["predictionDay"]], columns=colsTickerScaled).tz_localize(None)]) predsTable = predsTable.sort_index() weightsTable = weightsTable.sort_index() tickerAllocationsTable = tickerAllocationsTable.sort_index() scaledTickerAllocationsTable = scaledTickerAllocationsTable.sort_index() tickerPerformance = calculatePerformanceForTable(tickerAllocationsTable, tickerAllocationsTable.columns, joinedData) algoPerformance = pd.DataFrame(tickerPerformance.apply(lambda x:sum(x), axis=1), columns=["Algo Return"]) benchmark = portfolio.getPortfolioByKey(portfolioKey)["benchmark"] factorReturn = dataAck.getDailyFactorReturn(benchmark, joinedData) factorReturn.columns = ["Factor Return (" + benchmark + ")"] algoVsBenchmark = algoPerformance.join(factorReturn).dropna() ##FORM HASH TO TICKER hashToTicker = {} for model in models: hashToTicker[portfolio.getModelHash(model)] = model.inputSeries.targetTicker individualAlgoPerformance = calculatePerformanceForTable(predsTable,[hashToTicker[modelHash] for modelHash in predsTable.columns], joinedData) return json.dumps(convertTableToJSON(empyrical.cum_returns(tickerPerformance))),\ json.dumps(convertTableToJSON(empyrical.cum_returns(algoPerformance))),\ json.dumps(convertTableToJSON(empyrical.cum_returns(algoVsBenchmark))),\ json.dumps(convertTableToJSON(empyrical.cum_returns(individualAlgoPerformance))),\
def getFundData(): historicalAllocations, realizedAllocations = getNetAllocationAcrossPortfolios( ) if historicalAllocations is None: return None, None pulledData, unused_ = dataAck.downloadTickerData( historicalAllocations.columns.values) allocationJoinedData = dataAck.joinDatasets( [pulledData[ticker] for ticker in pulledData]) dataToCache = [] for allocationForm in [historicalAllocations, realizedAllocations]: performanceByTicker, fundPerformance, fundTransactionCost = portfolioGeneration.calculatePerformanceForAllocations( allocationForm, allocationJoinedData) if len(fundPerformance) == 0: dataToCache.append({}) continue ##CALCULATE BETAS FOR ALL TICKERS TO FUND PERFORMANCE tickerAlphaBetas = [] for ticker in allocationForm.columns.values: factorReturn = dataAck.getDailyFactorReturn( ticker, allocationJoinedData) alpha, beta = empyrical.alpha_beta(fundPerformance, factorReturn) tickerAlphaBetas.append({ "ticker": ticker, "alpha": alpha * 100, "beta": beta }) tickerCols, tickerRows = portfolioGeneration.convertTableToJSON( empyrical.cum_returns(performanceByTicker)) tickerAllocationsCols, tickerAllocationsRows = portfolioGeneration.convertTableToJSON( allocationForm) fundCols, fundRows = portfolioGeneration.convertTableToJSON( empyrical.cum_returns(fundPerformance)) sharpe = empyrical.sharpe_ratio(fundPerformance) annualReturn = empyrical.annual_return(fundPerformance)[0] annualVol = empyrical.annual_volatility(fundPerformance) commissionCols, commissionRows = portfolioGeneration.convertTableToJSON( fundTransactionCost) dataToCache.append({ "tickerAlphaBetas": tickerAlphaBetas, "tickerCols": json.dumps(tickerCols), "tickerRows": json.dumps(tickerRows), "tickerAllocationsCols": json.dumps(tickerAllocationsCols), "tickerAllocationsRows": json.dumps(tickerAllocationsRows), "fundCols": json.dumps(fundCols), "fundRows": json.dumps(fundRows), "sharpe": sharpe, "annualReturn": annualReturn * 100, "annualVol": annualVol * 100, "commissionCols": json.dumps(commissionCols), "commissionRows": json.dumps(commissionRows) }) historicalData = dataToCache[0] realizedData = dataToCache[1] ##GET TODAY ALLOCATION if realizedData != {}: newRows = [] tARows = json.loads(realizedData["tickerAllocationsRows"]) tACols = json.loads(realizedData["tickerAllocationsCols"]) print(tARows[-1]) for i in range(len(tACols)): newRows.append([tACols[i], abs(tARows[-1][i + 1])]) ##i+1 because date realizedData["todayAllocation"] = json.dumps(newRows) print(realizedData["todayAllocation"]) return historicalData, realizedData