Skip to content

Instantly share code, notes, and snippets.

Created May 12, 2023 13:05
What would you like to do?
clustering forecasts
import matplotlib
from syscore.constants import arg_not_supplied
from systems.basesystem import System
from systems.provided.rob_system.run_system import futures_system
from sysquant.estimators.clustering_correlations import *
import pandas as pd
system = futures_system()
instrument_list = system.get_instrument_list()
all_pandl = dict()
instrument_weights = system.config.instrument_weights
for rule_name in list(system.rules.trading_rules().keys()):
pandl_this_rule = dict()
for instrument_code in list(instrument_weights.keys()):
pandl_this_code_rule = system.accounts.pandl_for_instrument_forecast(
instrument_code, rule_name
pandl_this_code_rule = (
pandl_this_code_rule * instrument_weights[instrument_code]
pandl_this_rule[instrument_code] = pandl_this_code_rule
pandl_this_rule = pd.concat(pandl_this_rule, axis=1)
pandl_this_rule = pandl_this_rule.sum(axis=1)
all_pandl[rule_name] = pandl_this_rule
all_pandl = pd.concat(all_pandl, axis=1)
import pickle
f = open("/home/rob/temp.pck", "wb")
pickle.dump(all_pandl, f)
from sysquant.estimators.correlation_estimator import correlationEstimate
corr_matrix = correlationEstimate.from_pd(all_pandl.corr())
def display_clusters(system, clusters):
for idx, cluster in enumerate(clusters):
print("Cluster %d" % (idx + 1))
def show_cluster(system, cluster):
instruments = cluster
assets = [ for code in instruments]
classes = list(set(assets))
for aclass in classes:
subset = [
for (code, asset_class) in zip(instruments, assets)
if asset_class == aclass
print("%s: %s" % (aclass, str(subset)))
N = 12
clusters = cluster_correlation_matrix(corr_matrix, N)
display_clusters(system, clusters)
## get turnovers
## doesn't matter which instrument as long as long history
turnovers = dict()
for rule_name in corr_matrix.columns:
turnovers[rule_name] = system.accounts.forecast_turnover("SP500_micro", rule_name)
print({k: v for k, v in sorted(turnovers.items(), key=lambda item: item[1])})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment