Skip to content

Commit

Permalink
timing example
Browse files Browse the repository at this point in the history
  • Loading branch information
enzbus committed Sep 11, 2023
1 parent 0f4622d commit 3849f18
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 6 deletions.
7 changes: 7 additions & 0 deletions cvxportfolio/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,13 @@ class FredBase(BaseData):
"""Base class for FRED data access."""

URL = "https://fred.stlouisfed.org/graph/fredgraph.csv"

## TODO: implement FRED point-in-time
## example:
## https://alfred.stlouisfed.org/graph/alfredgraph.csv?id=CES0500000003&vintage_date=2023-07-06
## hourly wages time series **as it appeared** on 2023-07-06 (try one day after, there's 1 new datapoint)
## store using pd.Series() of diff'ed values only, in practice they only revise recent 1-2 monthly
## obs.

def _download(self, symbol):
return pd.read_csv(self.URL + f'?id={symbol}', index_col=0, parse_dates=[0])[symbol]
Expand Down
12 changes: 9 additions & 3 deletions cvxportfolio/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,15 +562,16 @@ def _single_backtest(self, policy, start_time, end_time, h, universe=None):

# this is the main loop of a backtest
for t, t_next in zip(backtest_times[:-1], backtest_times[1:]):
# s = time.time()
result.h.loc[t] = h
s = time.time()
h, result.z.loc[t], result.u.loc[t], realized_costs, \
result.policy_times.loc[t] = self._simulate(
t=t, h=h, policy=policy, t_next=t_next)
for cost in realized_costs:
result.costs[cost].loc[t] = realized_costs[cost]
result.simulator_times.loc[t] = time.time(
) - s - result.policy_times.loc[t]
) - self.simulator_timer - result.policy_times.loc[t]
self.simulator_timer = time.time()

result.h.loc[pd.Timestamp(end_time)] = h

Expand All @@ -580,6 +581,7 @@ def _single_backtest(self, policy, start_time, end_time, h, universe=None):
return result

def _concatenated_backtests(self, policy, start_time, end_time, h):
self.simulator_timer = time.time()
constituent_backtests_params = self.market_data._get_limited_backtests(
start_time, end_time)
results = []
Expand Down Expand Up @@ -627,7 +629,11 @@ def _concatenated_backtests(self, policy, start_time, end_time, h):

self.market_data = orig_md

return self._concatenate_backtest_results(results)
result = self._concatenate_backtest_results(results)

# temporary, will refactor these methods
result.simulator_times.iloc[-2] += time.time() - self.simulator_timer
return result

def _concatenate_backtest_results(self, results):

Expand Down
6 changes: 3 additions & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Introduction
Cvxportfolio is an object-oriented library for portfolio optimization and backtesting
which focuses on ease of use. It implements the models described
in the `accompanying book <https://stanford.edu/~boyd/papers/pdf/cvx_portfolio.pdf>`_
and can be extended with user-defined objects and methods to accommodate ingestion
of different data sources, custom cost models (both for simulation and optimization),
and can be extended with user-defined objects and methods to accommodate
different data sources, custom cost models (both for simulation and optimization),
constraints, and so on.

The main abstractions used are the :class:`MarketSimulator`, which faithfully mimics
Expand All @@ -46,7 +46,7 @@ of objects can be customized in many ways, including by deriving or redefining t
Then, we provide the :class:`MarketData` abstraction, which both serves historical
data during a backtest and real-time data in online usage. We implement the interface
to public data sources (`Yahoo finance <https://finance.yahoo.com>`_
and `FRED <https://fred.stlouisfed.org/>`_), in addition to user-provided data (which
and `FRED <https://fred.stlouisfed.org/>`_), as well as user-provided data (which
can also be passed to all other objects).

In addition, we provide logic to easily parallelize backtesting of many different policies,
Expand Down
127 changes: 127 additions & 0 deletions examples/timing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import cvxportfolio as cvx
import matplotlib.pyplot as plt
import time
import pandas as pd

SP500 = ['MMM', 'AOS', 'ABT', 'ABBV', 'ACN', 'ATVI', 'ADM', 'ADBE', 'ADP',
'AAP', 'AES', 'AFL', 'A', 'APD', 'AKAM', 'ALK', 'ALB', 'ARE',
'ALGN', 'ALLE', 'LNT', 'ALL', 'GOOGL', 'GOOG', 'MO', 'AMZN',
'AMCR', 'AMD', 'AEE', 'AAL', 'AEP', 'AXP', 'AIG', 'AMT', 'AWK',
'AMP', 'ABC', 'AME', 'AMGN', 'APH', 'ADI', 'ANSS', 'AON', 'APA',
'AAPL', 'AMAT', 'APTV', 'ACGL', 'ANET', 'AJG', 'AIZ', 'T', 'ATO',
'ADSK', 'AZO', 'AVB', 'AVY', 'AXON', 'BKR', 'BALL', 'BAC', 'BBWI',
'BAX', 'BDX', 'WRB', 'BRK-B',
'BBY', 'BIO', 'TECH', 'BIIB', 'BLK',
'BK', 'BA', 'BKNG', 'BWA', 'BXP', 'BSX', 'BMY', 'AVGO', 'BR',
'BRO', 'BF-B',
'BG', 'CHRW', 'CDNS', 'CZR', 'CPT', 'CPB', 'COF',
'CAH', 'KMX', 'CCL', 'CARR', 'CTLT', 'CAT', 'CBOE', 'CBRE', 'CDW',
'CE', 'CNC', 'CNP', 'CDAY', 'CF', 'CRL', 'SCHW', 'CHTR', 'CVX',
'CMG', 'CB', 'CHD', 'CI', 'CINF', 'CTAS', 'CSCO', 'C', 'CFG',
'CLX', 'CME', 'CMS', 'KO', 'CTSH', 'CL', 'CMCSA', 'CMA', 'CAG',
'COP', 'ED', 'STZ', 'CEG', 'COO', 'CPRT', 'CTVA', 'CSGP',
'COST', 'CTRA', 'CCI', 'CSX', 'CMI', 'CVS', 'DHI', 'DHR', 'DRI',
'DVA', 'DE', 'DAL', 'XRAY', 'DVN', 'DXCM', 'FANG', 'DLR', 'DFS',
'DISH', 'DIS', 'DG', 'DLTR', 'D', 'DPZ', 'DOV', 'DOW', 'DTE',
'DUK', 'DD', 'DXC', 'EMN', 'ETN', 'EBAY', 'ECL', 'EIX', 'EW', 'EA',
'ELV', 'LLY', 'EMR', 'ENPH', 'ETR', 'EOG', 'EPAM', 'EQT', 'EFX',
'EQIX', 'EQR', 'ESS', 'EL', 'ETSY', 'RE', 'EVRG', 'ES', 'EXC',
'EXPE', 'EXPD', 'EXR', 'XOM', 'FFIV', 'FDS', 'FICO', 'FAST', 'FRT',
'FDX', 'FITB', 'FSLR', 'FE', 'FIS', 'FLT', 'FMC', 'F',
'FTNT', 'FTV', 'FOXA', 'FOX', 'BEN', 'FCX', 'GRMN', 'IT', 'GEHC',
'GEN', 'GNRC', 'GD', 'GE', 'GIS', 'GM', 'GPC', 'GILD', 'GL', 'GPN',
'GS', 'HAL', 'HIG', 'HAS', 'HCA', 'PEAK', 'HSIC', 'HSY', 'HES',
'HPE', 'HLT', 'HOLX', 'HD', 'HON', 'HRL', 'HST', 'HWM', 'HPQ',
'HUM', 'HBAN', 'HII', 'IBM', 'IEX', 'IDXX', 'ITW', 'ILMN', 'INCY',
'IR', 'PODD', 'INTC', 'ICE', 'IFF', 'IP', 'IPG', 'INTU', 'ISRG',
'IVZ', 'INVH', 'IQV', 'IRM', 'JBHT', 'JKHY', 'J', 'JNJ', 'JCI',
'JPM', 'JNPR', 'K', 'KDP', 'KEY', 'KEYS', 'KMB', 'KIM', 'KMI',
'KLAC', 'KHC', 'KR', 'LHX', 'LH', 'LRCX', 'LW', 'LVS', 'LDOS',
'LEN', 'LNC', 'LIN', 'LYV', 'LKQ', 'LMT', 'L', 'LOW', 'LYB', 'MTB',
'MRO', 'MPC', 'MKTX', 'MAR', 'MMC', 'MLM', 'MAS', 'MA', 'MTCH',
'MKC', 'MCD', 'MCK', 'MDT', 'MRK', 'META', 'MET', 'MTD', 'MGM',
'MCHP', 'MU', 'MSFT', 'MAA', 'MRNA', 'MHK', 'MOH', 'TAP', 'MDLZ',
'MPWR', 'MNST', 'MCO', 'MS', 'MOS', 'MSI', 'MSCI', 'NDAQ', 'NTAP',
'NFLX', 'NWL', 'NEM', 'NWSA', 'NWS', 'NEE', 'NKE', 'NI', 'NDSN',
'NSC', 'NTRS', 'NOC', 'NCLH', 'NRG', 'NUE', 'NVDA', 'NVR', 'NXPI',
'ORLY', 'OXY', 'ODFL', 'OMC', 'ON', 'OKE', 'ORCL', 'OGN', 'OTIS',
'PCAR', 'PKG', 'PARA', 'PH', 'PAYX', 'PAYC', 'PYPL', 'PNR', 'PEP',
'PFE', 'PCG', 'PM', 'PSX', 'PNW', 'PXD', 'PNC', 'POOL', 'PPG',
'PPL', 'PFG', 'PG', 'PGR', 'PLD', 'PRU', 'PEG', 'PTC', 'PSA',
'PHM', 'QRVO', 'PWR', 'QCOM', 'DGX', 'RL', 'RJF', 'RTX', 'O',
'REG', 'REGN', 'RF', 'RSG', 'RMD', 'RVTY', 'RHI', 'ROK', 'ROL',
'ROP', 'ROST', 'RCL', 'SPGI', 'CRM', 'SBAC', 'SLB', 'STX', 'SEE',
'SRE', 'NOW', 'SHW', 'SPG', 'SWKS', 'SJM', 'SNA', #'SEDG',
'SO',
'LUV', 'SWK', 'SBUX', 'STT', 'STLD', 'STE', 'SYK', 'SYF', 'SNPS',
'SYY', 'TMUS', 'TROW', 'TTWO', 'TPR', 'TRGP', 'TGT', 'TEL', 'TDY',
'TFX', 'TER', 'TSLA', 'TXN', 'TXT', 'TMO', 'TJX', 'TSCO', 'TT',
'TDG', 'TRV', 'TRMB', 'TFC', 'TYL', 'TSN', 'USB', 'UDR', 'ULTA',
'UNP', 'UAL', 'UPS', 'URI', 'UNH', 'UHS', 'VLO', 'VTR', 'VRSN',
'VRSK', 'VZ', 'VRTX', 'VFC', 'VTRS', 'VICI', 'V', 'VMC', 'WAB',
'WBA', 'WMT', 'WBD', 'WM', 'WAT', 'WEC', 'WFC', 'WELL', 'WST',
'WDC', 'WRK', 'WY', 'WHR', 'WMB', #'WTW',
'GWW', 'WYNN', 'XEL',
'XYL', 'YUM', 'ZBRA', 'ZBH', 'ZION', 'ZTS']

# we test the typical time it takes
# to solve and simulate an SPO policy,
# in the same way as we did in Figure
# 7.8 of the book

# NOTE: the first time you run this, it will
# compute and cache the risk model terms (for
# each day). The second time you run you should
# see faster runtime.

# changing these may have some effect
# on the solver time, but small
GAMMA_RISK = 1.
GAMMA_TRADE = 1.
GAMMA_HOLD = 1.

# the solve time grows linearly
# with this. 15 is the same number
# we had in the book examples
NUM_RISK_FACTORS = 15

# if you change this to 2 (quadratic model)
# the resulting problem is a QP and can be
# solved faster
TCOST_EXPONENT = 1.5

# you can add any constraint or objective
# term to see how it affects execution time
policy = cvx.SinglePeriodOptimization(
objective = cvx.ReturnsForecast()
- GAMMA_RISK * cvx.FactorModelCovariance(num_factors=NUM_RISK_FACTORS)
- GAMMA_TRADE * cvx.StocksTransactionCost(exponent=TCOST_EXPONENT)
- GAMMA_HOLD * cvx.StocksHoldingCost(),
constraints = [
cvx.LeverageLimit(3),
],
# You can select any cvxpy
# solver here to see how it
# affects performance of your
# particular problem. This one
# is the default for this type
# of problems.
solver='ECOS'
)

# this downloads data for all the sp500
simulator = cvx.StockMarketSimulator(SP500)

# execution and timing, 5 years backtest
s = time.time()
result = simulator.backtest(policy, start_time=pd.Timestamp.today() - pd.Timedelta(f'{365*5}d'))
print('BACKTEST TOOK', time.time() - s)
print('SIMULATOR + POLICY TIMES', result.simulator_times.sum() + result.policy_times.sum())
print('AVERAGE TIME PER ITERATION', result.simulator_times.mean() + result.policy_times.mean())

# plot
result.policy_times.plot(label='policy times')
result.simulator_times.plot(label='simulator times')
plt.legend()
plt.show()

0 comments on commit 3849f18

Please sign in to comment.