This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Backtest of ETF Replay Ranking Algorithm | |
# clear the workspace | |
rm(list=ls()) | |
# step-by-step example of ETF Replay Ranking Algorithm | |
##### etfReplayRank Function ##### | |
# we can use the same step-by-step approach outlined above to write a function | |
# to rank instruments based on ETF Replay |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ranking Algorithm for ETF Replay | |
# Rank based on 3 factors | |
# 1. ReturnA (ret1) | |
# 2. ReturnB (ret2) | |
# 3. Volatility (vol) | |
# Returns are ranked from high to low | |
# Volatility is ranked from low to high |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# orthogonalization of correlated factors in a multifactor model | |
def orthogonalize_factors(factors_df, output_format='df'): | |
""" | |
As described by Klein and Chow (2013) in Orthogonalized Factors and Systematic Risk Decompositions: | |
https://www.sciencedirect.com/science/article/abs/pii/S1062976913000185 | |
They propose an optimal simultaneous orthogonal transformation of factors, following the so-called symmetric procedure | |
of Schweinler and Wigner (1970) and Löwdin (1970). The data transformation allows the identification of the underlying uncorrelated | |
components of common factors without changing their correlation with the original factors. It also facilitates the systematic risk | |
decomposition by disentangling the coefficient of determination (R²) based on factors' volatilities, which makes it easier to distinguish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# high-low spread estimator (hlse) | |
def hlse(ohlc_df, frequency='daily'): | |
""" | |
Computes the high-low spread estimator, an estimate of bid-offer spreads, a measure of liquidity risk. | |
See Corwin & Schultz (2011) for details: https://papers.ssrn.com/sol3/papers.cfm?abstract_id=1106193 | |
Parameters | |
---------- | |
ohlc_df: DataFrame | |
DataFrame with DatetimeIndex and Open, High, Low and Close (OHLC) prices from which to compute the high-low spread estimates. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This function takes observable macro factors (surprises) as inputs and creates macro factor mimicking portfolios (MFMPs) as outputs. It uses | |
a novel machine-learning approach, the Principal Components Instrumental Variables FMP Estimator, | |
described by Jurczenko and Teiletche (2020) in Macro Factor-Micking Portfolios: | |
https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3363598 | |
The methodology addresses many of the common problems associated with macro factors and multifactor risk modeling and, as they show, | |
is superior to other common FMP approaches. | |
We describe the steps of the PCIV algorithm below, as well as in our Medium post: | |
For a more detailed explanation, see the link to the paper above. | |
Note that access to macroeconomic and base assets is required to estimate macro factor-mimicking portfolios. See the macro factors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A script to automatically export bookmarks from Firefox's SQLite database. | |
There does not seem to be a programmatic way to get Firefox to export its bookmarks in | |
the conventional HTML format. However, you can access the bookmark information directly | |
in Firefox's internal database, which is what this script does. | |
Always be careful when working with the internal database! If you delete data, you will | |
likely not be able to recover it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lazy data frame | |
# | |
# like idata.frame, but exploits lazy evaluation + macro code generation | |
# instead of active bindings | |
library(vadr) | |
lazy.frame <- function(df, enclos=parent.frame(), ...) UseMethod("lazy.frame") | |
lazy.frame.lazy.frame <- function(df, ...) df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
from datetime import datetime | |
from fastparquet import write | |
def compute_vwap(df): | |
q = df['foreignNotional'] | |
p = df['price'] |