Skip to content

Instantly share code, notes, and snippets.

@robcarver17
Created May 12, 2023 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robcarver17/0433df8be55ab76a2597569dc7d6447c to your computer and use it in GitHub Desktop.
Save robcarver17/0433df8be55ab76a2597569dc7d6447c to your computer and use it in GitHub Desktop.
clustering forecasts
import matplotlib
matplotlib.use("TkAgg")
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
).percent.gross
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)
f.close()
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))
print(cluster)
def show_cluster(system, cluster):
instruments = cluster
assets = [system.data.asset_class_for_instrument(code) for code in instruments]
classes = list(set(assets))
for aclass in classes:
subset = [
code
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