Skip to content

Instantly share code, notes, and snippets.

View nicklatin's full-sized avatar

Nick Latinovic nicklatin

View GitHub Profile
@nicklatin
nicklatin / binance_etl.py
Last active May 3, 2022 02:04
Extract, transform, load functions for python-binance API.
import pandas as pd
import logging
# initialize binance client
client = Client(os.environ.get('BINANCE_API_KEY'), os.environ.get('BINANCE_API_SECRET'))
# get metadata from binance exchange
def get_metadata_bin(quote='usdt', as_list=False):
@nicklatin
nicklatin / macro_factor_mimicking_portfolios.py
Last active September 17, 2023 03:32
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?ab…
"""
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
@nicklatin
nicklatin / orthogonalization.py
Last active November 12, 2022 15:45
Klein and Chow (2013) propose an optimal simultaneous orthogonal transformation of factors, following the so-called symmetric procedure of Schweinler and Wigner (1970) and Löwdin (1970): https://www.sciencedirect.com/science/article/abs/pii/S1062976913000185. The data transformation allows the identification of the underlying uncorrelated compon…
# 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
@nicklatin
nicklatin / high_low_spread_estimator.py
Last active August 17, 2023 16:23
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
# 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.