Posts

Asset Classes

Equities Deciding on a universe to trade: If mirroring an index you will need to take the index joiners and leavers into account. Reduce survivorship bias Handling dividends Option 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 leaveContinue reading “Asset Classes”

Complete Backtest Using Ingested Zipline Data

Import necessary functions in Python Set target weights for each ticker Set rebalance rules, i.e. monthly Set benchmark, i.e. SPY Set start and end dates – should match trade dates in zipline ingested files Create benchmark file Make sure benchmark dates align with trade dates Run backtest analysis If there are modules to import –Continue reading “Complete Backtest Using Ingested Zipline Data”

Creating A Zipline Data Bundle

Get daily OHLCV stock data using Python via YahooFinance, Polygon.io, etc and save as a CSV file Import necessary python modules Set start & end dates manually Run pdr.DataReader spy = pdr.DataReader(‘SPY’, ‘yahoo’, start_date, end_date) Save data to CSV spy.to_csv(‘/Users/bars_adj/spy.csv’) Index dates to Zipline Trading calendar Create trading_days tag in python Start & end datesContinue reading “Creating A Zipline Data Bundle”

Time Series Analytics

Semi-log graph of equity curve Exposure over time Half year rolling return, annualized Useful to see what annualized would look like in a shorter time firm AnnualizedReturn = (EndValue/StartValue)^YearDays/ActualDays-1 Python: def ann_ret(ts): return np.power((ts[-1]/ts[0]), (year_length/len(ts)))-1 Maximum Drawdown Python: def dd(ts): return np.min(ts/np.maximum.accumulate(ts)) – 1 Recalculate time series vs benchmarks with same starting point Python: defContinue reading “Time Series Analytics”

Imports & Functions Required For Backtests

Zipline Functions from zipline import run_algorithm #needed every time we run a backtest from zipline.api import order_target_percent #ability to place orders in terms of target percent from zipline.api import order_target #ability to place orders in terms of shares from zipline.api import symbol #ability to look at stocks based on ticker from zipline.api import record fromContinue reading “Imports & Functions Required For Backtests”

Retrieving Data

Over the recent years, free data has been harder to come by Quandl 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 quandl repeat this process when you want to refresh data PythonContinue reading “Retrieving Data”

Setting Up A Backtest

Data Sources Norgate Data for stocks CSI Data for futures Quandl is a FREE data source Backtesting Engine Zipline by Quantopian Major 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 futures Should have its ownContinue reading “Setting Up A Backtest”

Correlation Graph in Python

Must use either log returns or percentage returns Standard approach is to use log returns The 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]Continue reading “Correlation Graph in Python”

Loading…

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


Create your website with WordPress.com
Get started
%d bloggers like this: