Design a site like this with WordPress.com
Get started

Posts

Asset Classes

EquitiesDeciding on a universe to trade:If mirroring an index you will need to take the index joiners and leavers into account.Reduce survivorship biasHandling dividendsOption 1 is to use total return time series which assumes dividends are directly reinvested into the same stock (easy to model).Option 2 is to leave the price series alone and just…

Complete Backtest Using Ingested Zipline Data

Import necessary functions in PythonSet target weights for each tickerSet rebalance rules, i.e. monthlySet benchmark, i.e. SPYSet start and end dates – should match trade dates in zipline ingested filesCreate benchmark fileMake sure benchmark dates align with trade datesRun backtest analysisIf there are modules to import – %run analysis.ipynbSave output to csv – results.to_csv(‘result.csv’)

Ingesting A Zipline Data Bundle

Update Users/NAME/.zipline/extension.py (needs to be updated every time you are ingesting a bundle)To locate you will need to unhide files, Command + Shift + Dot (.) on macUpdate the start_session & end_session to match the trading start date and end dateUpdate bundle name and ensure file location is correctUpdate csvdir.py (one time fix)Line 183 -…

Creating A Zipline Data Bundle

Get daily OHLCV stock data using Pythonvia YahooFinance, Polygon.io, etc and save as a CSV fileImport necessary python modulesSet start & end dates manuallyRun pdr.DataReaderspy = pdr.DataReader(‘SPY’, ‘yahoo’, start_date, end_date)Save data to CSVspy.to_csv(‘/Users/bars_adj/spy.csv’)Index dates to Zipline Trading calendarCreate trading_days tag in pythonStart & end dates should use the same dates as step 1Delete any trading…

Exchange Traded Funds (ETFs)

ProsPassive, low cost index trackers – i.e. SPYSuch ETFs track major country, sector, and bond indexesCash instruments make things simple, pay for what you want to buy and have a linear payoffTrading futures, options, swaps, forwards and other derivatives creates complications ConsAnyone can create an ETF and charge whatever they wantAssets under $1B are considered…

Time Series Analytics

Semi-log graph of equity curveExposure over timeHalf year rolling return, annualizedUseful to see what annualized would look like in a shorter time firmAnnualizedReturn = (EndValue/StartValue)^YearDays/ActualDays-1Python:def ann_ret(ts):return np.power((ts[-1]/ts[0]), (year_length/len(ts)))-1Maximum DrawdownPython:def dd(ts):return np.min(ts/np.maximum.accumulate(ts)) – 1Recalculate time series vs benchmarks with same starting pointPython:def rebased(ts):return ts / ts[0] Key python reminders:Remove all N/A calculations:df.dropna(inplace=True)Reset figures when needed:fig.clear(True)Start returns…

Imports & Functions Required For Backtests

Zipline Functionsfrom zipline import run_algorithm #needed every time we run a backtestfrom zipline.api import order_target_percent #ability to place orders in terms of target percentfrom zipline.api import order_target #ability to place orders in terms of sharesfrom zipline.api import symbol #ability to look at stocks based on tickerfrom zipline.api import recordfrom zipline.api import set_benchmarkfrom zipline.api import schedule_functionfrom…

Retrieving Data

Over the recent years, free data has been harder to come byQuandl still provides free equity data so that is a free place to start (Quandl.com)Ingesting the Quandl bundle:set QUANDL_API_KEY=your_own_api_key (on a Mac use export instead of set)zipline ingest -b quandlrepeat this process when you want to refresh dataPython packages needed:matplotlibnb_condaif receive error message on…

Setting Up A Backtest

Data SourcesNorgate Data for stocksCSI Data for futuresQuandl is a FREE data sourceBacktesting EngineZipline by QuantopianMajor downside of zipline is that is only aware of a single world currency. Back testing securities with multiple currency denominations will not work.Works with both equities and futuresShould have its own Python environment as specific versions of libraries are…

Correlation Graph in Python

Must use either log returns or percentage returnsStandard approach is to use log returnsThe following python code will generate a correlation chart of the rolling 50 day window of the returns, showing the last 200 data points: df[‘SP500’].rolling(50).corr(df[‘NDX’])[-200:].plot()The slice function is used to grab specific data points from a data frame[start:stop:step]data[100:200:2] would return every other…

Loading…

Something went wrong. Please refresh the page and/or try again.


%d bloggers like this: