Equity trading strategies with macro and random forests #

The random forest is a machine learning model composed of many different “decision tree” models. Decision trees are sequences of “if-else” statements, where “learning” in the regression case corresponds to learning good decision rules from data. The random forest constructs each of these trees to, hopefully, be both reasonable forecasters and be as uncorrelated with one another as possible. The average prediction made by the trees is the prediction made by the random forest.

Get packages and JPMaQS data #

Packages #

import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from macrosynergy.download import JPMaQSDownload
import macrosynergy.management as msm
import macrosynergy.panel as msp
import macrosynergy.pnl as msn
import macrosynergy.signal as mss
import macrosynergy.learning as msl
import macrosynergy.visuals as msv

from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.pipeline import Pipeline
from sklearn.metrics import make_scorer

from timeit import default_timer as timer
from datetime import timedelta, date, datetime

import warnings

from IPython.display import HTML

warnings.filterwarnings("ignore")

Previously prepared quantamental categories #

# Import data from csv file created preparation notebook
# https://macrosynergy.com/academy/notebooks/sectoral-equity-indicators/

INPUT_PATH = os.path.join(os.getcwd(), r"../../../equity_sectoral_notebook_data.csv")

df_csv = pd.read_csv(INPUT_PATH, index_col=0)
df_csv["real_date"] = pd.to_datetime(df_csv["real_date"]).dt.date

df_csv = msm.utils.standardise_dataframe(df_csv)
df_csv = df_csv.sort_values(["cid", "xcat", "real_date"])
# Equity sector labels and cross sections

sector_labels = {
    "ALL": "All sectors",
    "COD": "Cons. discretionary",
    "COS": "Cons. staples",
    "CSR": "Communication services",
    "ENR": "Energy",
    "FIN": "Financials",
    "HLC": "Healthcare",
    "IND": "Industrials",
    "ITE": "Information tech",
    "MAT": "Materials",
    "REL": "Real estate",
    "UTL": "Utilities",
}
cids_secs = list(sector_labels.keys())

# Equity countries cross sections

cids_eq = [
    "AUD",
    "CAD",
    "CHF",
    "EUR",
    "GBP",
    "ILS",
    "JPY",
    "NOK",
    "NZD",
    "SEK",
    "SGD",
    "USD",
]
# Base category tickes of quantamental categories created by data preparation notebook:
# https://macrosynergy.com/academy/notebooks/sectoral-equity-indicators/

output_growth = [
    # industrial prod
    "XIP_SA_P1M1ML12_3MMA",
    "XIP_SA_P1M1ML12_3MMA_WG",
    # construction
    "XCSTR_SA_P1M1ML12_3MMA",
    "XCSTR_SA_P1M1ML12_3MMA_WG",
    # Excess GDP growth
    "XRGDPTECH_SA_P1M1ML12_3MMA",
    "XRGDPTECH_SA_P1M1ML12_3MMA_WG",
]
private_consumption = [
    # Consumer surveys
    "CCSCORE_SA",
    "CCSCORE_SA_D3M3ML3",
    "CCSCORE_SA_WG",
    "CCSCORE_SA_D3M3ML3_WG",
    
    "XNRSALES_SA_P1M1ML12_3MMA",
    "XRRSALES_SA_P1M1ML12_3MMA",
    "XNRSALES_SA_P1M1ML12_3MMA_WG",
    "XRRSALES_SA_P1M1ML12_3MMA_WG",
    "XRPCONS_SA_P1M1ML12_3MMA",
    "XRPCONS_SA_P1M1ML12_3MMA_WG",    
]
export = [
    "XEXPORTS_SA_P1M1ML12_3MMA",
]
labour_market = [
    "UNEMPLRATE_NSA_3MMA_D1M1ML12",
    "UNEMPLRATE_SA_3MMAv5YMA",
    "UNEMPLRATE_NSA_3MMA_D1M1ML12_WG",
    "UNEMPLRATE_SA_3MMAv5YMA_WG",
    "XEMPL_NSA_P1M1ML12_3MMA",
    "XEMPL_NSA_P1M1ML12_3MMA_WG",
    "XRWAGES_NSA_P1M1ML12",
]
business_surveys = [
    # Manufacturing
    "MBCSCORE_SA",
    "MBCSCORE_SA_D3M3ML3",
    "MBCSCORE_SA_WG",
    "MBCSCORE_SA_D3M3ML3_WG",
    # Services
    "SBCSCORE_SA",
    "SBCSCORE_SA_D3M3ML3",
    "SBCSCORE_SA_WG",
    "SBCSCORE_SA_D3M3ML3_WG",
    # Construction
    "CBCSCORE_SA",
    "CBCSCORE_SA_D3M3ML3",
    "CBCSCORE_SA_WG",
    "CBCSCORE_SA_D3M3ML3_WG",
]
private_credit = [
    "XPCREDITBN_SJA_P1M1ML12",
    "XPCREDITBN_SJA_P1M1ML12_WG",
    # liquidity conditions
    "INTLIQGDP_NSA_D1M1ML1",
    "INTLIQGDP_NSA_D1M1ML6",
]
broad_inflation = [
    # Inflation
    "XCPIC_SA_P1M1ML12",
    "XCPIH_SA_P1M1ML12",
    "XPPIH_NSA_P1M1ML12",    
]
specific_inflation = [
    "XCPIE_SA_P1M1ML12",
    "XCPIF_SA_P1M1ML12",
    "XCPIE_SA_P1M1ML12_WG",
    "XCPIF_SA_P1M1ML12_WG",
]
private_and_public_debt = [
    "HHINTNETGDP_SA_D1M1ML12",
    "HHINTNETGDP_SA_D1M1ML12_WG",
    "CORPINTNETGDP_SA_D1Q1QL4",
    "CORPINTNETGDP_SA_D1Q1QL4_WG",
    "XGGDGDPRATIOX10_NSA",
]
commodity_inventories = [
    "BMLXINVCSCORE_SA",
    "REFIXINVCSCORE_SA",
    "BASEXINVCSCORE_SA",
]
commodity_markets = [
    "BMLCOCRY_SAVT10_21DMA",
    "COXR_VT10vWTI_21DMA"    
]
real_appreciation_tot = [
    "CXPI_NSA_P1M12ML1",
    "CMPI_NSA_P1M12ML1",
    "CTOT_NSA_P1M12ML1",
    "REEROADJ_NSA_P1M12ML1",
]
interest_rates = [
    "RIR_NSA",
    "RYLDIRS02Y_NSA",
    "RYLDIRS05Y_NSA",
    "RSLOPEMIDDLE_NSA",
]

# All economic categories
ecos = output_growth + private_consumption + export + labour_market + business_surveys + private_credit + broad_inflation + specific_inflation + private_and_public_debt + commodity_inventories + commodity_markets + real_appreciation_tot + interest_rates

# Equity categories
eqrets = [
    "EQC" + sec + ret for sec in cids_secs for ret in ["XR_NSA", "R_NSAvALL", "R_VT10vALL"]
]

# All categories
all_xcats = [x + suff for x in ecos + ecos for suff in ["_ZN", "_ZN_NEG"]] + eqrets 

# Resultant tickers

tickers = [cid + "_" + xcat for cid in cids_eq for xcat in all_xcats]
print(f"Maximum number of tickers is {len(tickers)}")
Maximum number of tickers is 3552

Download additional data from JPMaQS #

# Additional tickers for download from JPMaQS

untradeable = [
    "EQCCODUNTRADABLE_NSA",
    "EQCCOSUNTRADABLE_NSA",
    "EQCCSRUNTRADABLE_NSA",
    "EQCENRUNTRADABLE_NSA",
    "EQCFINUNTRADABLE_NSA",
    "EQCHLCUNTRADABLE_NSA",
    "EQCINDUNTRADABLE_NSA",
    "EQCITEUNTRADABLE_NSA",
    "EQCMATUNTRADABLE_NSA",
    "EQCRELUNTRADABLE_NSA",
    "EQCUTLUNTRADABLE_NSA",   
]  # dummy variables for dates where certain sectors were untradeable

bmrs = [
    "USD_EQXR_NSA",
    "USD_EQXR_VT10"
]  # U.S. equity returns for correlation analysis

xtickers = [cid + "_" + xcat for cid in cids_eq for xcat in untradeable] + bmrs
print(f"Maximum number of tickers is {len(xtickers)}")
Maximum number of tickers is 134
# Download series from J.P. Morgan DataQuery by tickers
start_date = "2000-01-01"

# Retrieve credentials

client_id: str = os.getenv("DQ_CLIENT_ID")
client_secret: str = os.getenv("DQ_CLIENT_SECRET")

# Download from DataQuery
with JPMaQSDownload(client_id=client_id, client_secret=client_secret) as downloader:
    start = timer()
    assert downloader.check_connection()
    df_jpmaqs = downloader.download(
        tickers=xtickers,
        start_date=start_date,
        metrics=["value"],
        suppress_warning=True,
        show_progress=True,
    )
    end = timer()

print("Download time from DQ: " + str(timedelta(seconds=end - start)))
Downloading data from JPMaQS.
Timestamp UTC:  2025-08-13 13:48:59
Connection successful!
Requesting data: 100%|███████████████████████████████████████████████████████████████████| 7/7 [00:01<00:00,  4.73it/s]
Downloading data: 100%|██████████████████████████████████████████████████████████████████| 7/7 [00:12<00:00,  1.75s/it]
Some expressions are missing from the downloaded data. Check logger output for complete list.
1 out of 134 expressions are missing. To download the catalogue of all available expressions and filter the unavailable expressions, set `get_catalogue=True` in the call to `JPMaQSDownload.download()`.
Download time from DQ: 0:00:15.734149
df = msm.update_df(df_csv, df_jpmaqs)
# Dictionary of featire category labels

cat_labels = {
    "BASEXINVCSCORE_SA_ZN": {
        "Group": "Commodity inventories",
        "Label": "Excess crude inventory score",
        "Description": "Crude oil excess inventory z-score, seasonally adjusted",
        "Geography": "global",
    },
    "BMLCOCRY_SAVT10_21DMA_ZN": {
        "Group": "Market metrics",
        "Label": "Base metals carry",
        "Description": "Nominal carry for base metals basket, seasonally and vol-adjusted, 21 days moving average",
        "Geography": "global",
    },
    "BMLXINVCSCORE_SA_ZN": {
        "Group": "Commodity inventories",
        "Label": "Excess metal inventory score",
        "Description": "Base metal excess inventory z-score, seasonally adjusted",
        "Geography": "global",
    },
    "CBCSCORE_SA_D3M3ML3_WG_ZN": {
        "Group": "Business surveys",
        "Label": "Construction confidence, q/q",
        "Description": "Construction business confidence score, seas. adjusted, change q/q",
        "Geography": "weighted",
    },
    "CBCSCORE_SA_D3M3ML3_ZN": {
        "Group": "Business surveys",
        "Label": "Construction confidence, q/q",
        "Description": "Construction business confidence score, seas. adjusted, change q/q",
        "Geography": "local",
    },
    "CBCSCORE_SA_WG_ZN": {
        "Group": "Business surveys",
        "Label": "Construction confidence",
        "Description": "Construction business confidence score, seas. adjusted",
        "Geography": "weighted",
    },
    "CBCSCORE_SA_ZN": {
        "Group": "Business surveys",
        "Label": "Construction confidence",
        "Description": "Construction business confidence score, seas. adjusted",
        "Geography": "local",
    },
    "CCSCORE_SA_D3M3ML3_WG_ZN": {
        "Group": "Private consumption",
        "Label": "Consumer confidence, q/q",
        "Description": "Consumer confidence score, seasonally adjusted, change q/q",
        "Geography": "weighted",
    },
    "CCSCORE_SA_D3M3ML3_ZN": {
        "Group": "Private consumption",
        "Label": "Consumer confidence, q/q",
        "Description": "Consumer confidence score, seasonally adjusted, change q/q",
        "Geography": "local",
    },
    "CCSCORE_SA_WG_ZN": {
        "Group": "Private consumption",
        "Label": "Consumer confidence",
        "Description": "Consumer confidence score, seasonally adjusted",
        "Geography": "weighted",
    },
    "CCSCORE_SA_ZN": {
        "Group": "Private consumption",
        "Label": "Consumer confidence",
        "Description": "Consumer confidence score, seasonally adjusted",
        "Geography": "local",
    },
    "CMPI_NSA_P1M12ML1_ZN": {
        "Group": "Real appreciation",
        "Label": "Import prices, %oya",
        "Description": "Commodity-based import price index, %oya",
        "Geography": "local",
    },
    "CTOT_NSA_P1M12ML1_ZN": {
        "Group": "Real appreciation",
        "Label": "Terms-of-trade, %oya",
        "Description": "Commodity-based terms-of-trade, %oya",
        "Geography": "local",
    },
    "CXPI_NSA_P1M12ML1_ZN": {
        "Group": "Real appreciation",
        "Label": "Export prices, %oya",
        "Description": "Commodity-based export price index, %oya",
        "Geography": "local",
    },
    "COXR_VT10vWTI_21DMA_ZN": {
        "Group": "Market metrics",
        "Label": "Refined vs crude oil returns",
        "Description": "Refined oil products vs crude oil vol-targeted return differential, 21 days moving average",
        "Geography": "global",
    },
    "INTLIQGDP_NSA_D1M1ML1_ZN": {
        "Group": "Private credit",
        "Label": "Intervention liquidity, diff m/m",
        "Description": "Intervention liquidity to GDP ratio, change over the last month",
        "Geography": "local",
    },
    "INTLIQGDP_NSA_D1M1ML6_ZN": {
        "Group": "Private credit",
        "Label": "Intervention liquidity, diff 6m",
        "Description": "Intervention liquidity to GDP ratio, change overlast 6 months",
        "Geography": "local",
    },
    "MBCSCORE_SA_D3M3ML3_WG_ZN": {
        "Group": "Business surveys",
        "Label": "Manufacturing confidence, q/q",
        "Description": "Manufacturing business confidence score, seas. adj., change q/q",
        "Geography": "weighted",
    },
    "MBCSCORE_SA_D3M3ML3_ZN": {
        "Group": "Business surveys",
        "Label": "Manufacturing confidence, q/q",
        "Description": "Manufacturing business confidence score, seas. adj., change q/q",
        "Geography": "local",
    },
    "MBCSCORE_SA_WG_ZN": {
        "Group": "Business surveys",
        "Label": "Manufacturing confidence",
        "Description": "Manufacturing business confidence score, seasonally adjusted",
        "Geography": "weighted",
    },
    "MBCSCORE_SA_ZN": {
        "Group": "Business surveys",
        "Label": "Manufacturing confidence",
        "Description": "Manufacturing business confidence score, seasonally adjusted",
        "Geography": "local",
    },
    "REEROADJ_NSA_P1M12ML1_ZN": {
        "Group": "Real appreciation",
        "Label": "Open-adj REER, %oya",
        "Description": "Openness-adjusted real effective exchange rate, %oya",
        "Geography": "local",
    },
    "REFIXINVCSCORE_SA_ZN": {
        "Group": "Commodity inventories",
        "Label": "Excess refined oil inventory score",
        "Description": "Refined oil product excess inventory z-score, seas. adjusted",
        "Geography": "global",
    },
    "RIR_NSA_ZN": {
        "Group": "Market metrics",
        "Label": "Real 1-month rate",
        "Description": "Real 1-month interest rate",
        "Geography": "local",
    },
    "RSLOPEMIDDLE_NSA_ZN": {
        "Group": "Market metrics",
        "Label": "Real 5y-2y yield",
        "Description": "Real IRS yield differentials, 5-years versus 2-years",
        "Geography": "local",
    },
    "RYLDIRS02Y_NSA_ZN": {
        "Group": "Market metrics",
        "Label": "Real 2-year yield",
        "Description": "Real 2-year IRS yield",
        "Geography": "local",
    },
    "RYLDIRS05Y_NSA_ZN": {
        "Group": "Market metrics",
        "Label": "Real 5-year yield",
        "Description": "Real 5-year IRS yield",
        "Geography": "local",
    },
    "SBCSCORE_SA_D3M3ML3_WG_ZN": {
        "Group": "Business surveys",
        "Label": "Service confidence, q/q",
        "Description": "Services business confidence score, seas. adjusted, change q/q",
        "Geography": "weighted",
    },
    "SBCSCORE_SA_D3M3ML3_ZN": {
        "Group": "Business surveys",
        "Label": "Service confidence, q/q",
        "Description": "Services business confidence score, seas. adjusted, change q/q",
        "Geography": "local",
    },
    "SBCSCORE_SA_WG_ZN": {
        "Group": "Business surveys",
        "Label": "Service confidence",
        "Description": "Services business confidence score, seasonally adjusted",
        "Geography": "weighted",
    },
    "SBCSCORE_SA_ZN": {
        "Group": "Business surveys",
        "Label": "Service confidence",
        "Description": "Services business confidence score, seasonally adjusted",
        "Geography": "local",
    },
    "UNEMPLRATE_NSA_3MMA_D1M1ML12_WG_ZN": {
        "Group": "Labour market",
        "Label": "Unemployment rate, diff oya",
        "Description": "Unemployment rate, change oya",
        "Geography": "weighted",
    },
    "UNEMPLRATE_NSA_3MMA_D1M1ML12_ZN": {
        "Group": "Labour market",
        "Label": "Unemployment rate, diff oya",
        "Description": "Unemployment rate, change oya",
        "Geography": "local",
    },
    "UNEMPLRATE_SA_3MMAv5YMA_WG_ZN": {
        "Group": "Labour market",
        "Label": "Unemployment rate, diff vs 5yma",
        "Description": "Unemployment rate, difference vs 5-year moving average",
        "Geography": "weighted",
    },
    "UNEMPLRATE_SA_3MMAv5YMA_ZN": {
        "Group": "Labour market",
        "Label": "Unemployment rate, diff vs 5yma",
        "Description": "Unemployment rate, difference vs 5-year moving average",
        "Geography": "local",
    },
    "XCPIC_SA_P1M1ML12_ZN": {
        "Group": "Inflation - broad",
        "Label": "Excess core CPI, %oya",
        "Description": "Core CPI, %oya, in excess of effective inflation target",
        "Geography": "local",
    },
    "XCPIE_SA_P1M1ML12_WG_ZN": {
        "Group": "Inflation - specific",
        "Label": "Excess energy CPI, %oya",
        "Description": "Energy CPI, %oya, in excess of effective inflation target",
        "Geography": "weighted",
    },
    "XCPIE_SA_P1M1ML12_ZN": {
        "Group": "Inflation - specific",
        "Label": "Excess energy CPI, %oya",
        "Description": "Energy CPI, %oya, in excess of effective inflation target",
        "Geography": "local",
    },
    "XCPIF_SA_P1M1ML12_WG_ZN": {
        "Group": "Inflation - specific",
        "Label": "Excess food CPI, %oya",
        "Description": "Food CPI, %oya, in excess of effective inflation target",
        "Geography": "weighted",
    },
    "XCPIF_SA_P1M1ML12_ZN": {
        "Group": "Inflation - specific",
        "Label": "Excess food CPI, %oya",
        "Description": "Food CPI, %oya, in excess of effective inflation target",
        "Geography": "local",
    },
    "XCPIH_SA_P1M1ML12_ZN": {
        "Group": "Inflation - broad",
        "Label": "Excess headline CPI, %oya",
        "Description": "Headline CPI, %oya, in excess of effective inflation target",
        "Geography": "local",
    },
    "XCSTR_SA_P1M1ML12_3MMA_WG_ZN": {
        "Group": "Output growth",
        "Label": "Excess construction growth",
        "Description": "Construction output, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "weighted",
    },
    "XCSTR_SA_P1M1ML12_3MMA_ZN": {
        "Group": "Output growth",
        "Label": "Excess construction growth",
        "Description": "Construction output, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "local",
    },
    "XEMPL_NSA_P1M1ML12_3MMA_WG_ZN": {
        "Group": "Labour market",
        "Label": "Excess employment growth",
        "Description": "Employment growth, %oya, 3mma, in excess of population growth",
        "Geography": "weighted",
    },
    "XEMPL_NSA_P1M1ML12_3MMA_ZN": {
        "Group": "Labour market",
        "Label": "Excess employment growth",
        "Description": "Employment growth, %oya, 3mma, in excess of population growth",
        "Geography": "local",
    },
    "XEXPORTS_SA_P1M1ML12_3MMA_ZN": {
        "Group": "Exports",
        "Label": "Excess export growth",
        "Description": "Exports growth, %oya, 3mma, in excess of 5-year median GDP growth",
        "Geography": "local",
    },
    "XGGDGDPRATIOX10_NSA_ZN": {
        "Group": "Debt",
        "Label": "Excess projected gov. debt",
        "Description": "Government debt-to-GDP ratio proj. in 10 years, in excess of 100%",
        "Geography": "local",
    },
    "CORPINTNETGDP_SA_D1Q1QL4_WG_ZN": {
        "Group": "Debt",
        "Label": "Corporate debt servicing, %oya",
        "Description": "Corporate net debt servicing-to-GDP ratio, seasonally-adjusted, %oya",
        "Geography": "weighted",
    },
    "CORPINTNETGDP_SA_D1Q1QL4_ZN": {
        "Group": "Debt",
        "Label": "Corporate debt servicing, %oya",
        "Description": "Corporate net debt servicing-to-GDP ratio, seasonally-adjusted, %oya",
        "Geography": "local",
    },
    "HHINTNETGDP_SA_D1M1ML12_WG_ZN": {
        "Group": "Debt",
        "Label": "Households debt servicing, %oya",
        "Description": "Households net debt servicing-to-GDP ratio, seasonally-adjusted, %oya",
        "Geography": "weighted",
    },
    "HHINTNETGDP_SA_D1M1ML12_ZN": {
        "Group": "Debt",
        "Label": "Households debt servicing, %oya",
        "Description": "Households net debt servicing-to-GDP ratio, seasonally-adjusted, %oya",
        "Geography": "local",
    },
    "XIP_SA_P1M1ML12_3MMA_WG_ZN": {
        "Group": "Output growth",
        "Label": "Excess industry growth",
        "Description": "Industrial output, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "weighted",
    },
    "XIP_SA_P1M1ML12_3MMA_ZN": {
        "Group": "Output growth",
        "Label": "Excess industry growth",
        "Description": "Industrial output, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "local",
    },
    "XNRSALES_SA_P1M1ML12_3MMA_WG_ZN": {
        "Group": "Private consumption",
        "Label": "Excess retail sales growth",
        "Description": "Nominal retail sales, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "weighted",
    },
    "XNRSALES_SA_P1M1ML12_3MMA_ZN": {
        "Group": "Private consumption",
        "Label": "Excess retail sales growth",
        "Description": "Nominal retail sales, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "local",
    },
    "XRRSALES_SA_P1M1ML12_3MMA_WG_ZN": {
        "Group": "Private consumption",
        "Label": "Excess real retail growth",
        "Description": "Real retail sales, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "weighted",
    },
    "XRRSALES_SA_P1M1ML12_3MMA_ZN": {
        "Group": "Private consumption",
        "Label": "Excess real retail growth",
        "Description": "Real retail sales, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "local",
    },
    "XPCREDITBN_SJA_P1M1ML12_WG_ZN": {
        "Group": "Private credit",
        "Label": "Excess credit growth",
        "Description": "Private credit, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "weighted",
    },
    "XPCREDITBN_SJA_P1M1ML12_ZN": {
        "Group": "Private credit",
        "Label": "Excess credit growth",
        "Description": "Private credit, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "local",
    },
    "XPPIH_NSA_P1M1ML12_ZN": {
        "Group": "Inflation - broad",
        "Label": "Excess PPI, %oya",
        "Description": "Producer price inflation, %oya, in excess of eff. inflation target",
        "Geography": "local",
    },
    "XRGDPTECH_SA_P1M1ML12_3MMA_WG_ZN": {
        "Group": "Output growth",
        "Label": "Excess GDP growth",
        "Description": "Real GDP, %oya, 3mma, using HF data, in excess of 5-y med. GDP growth",
        "Geography": "weighted",
    },
    "XRGDPTECH_SA_P1M1ML12_3MMA_ZN": {
        "Group": "Output growth",
        "Label": "Excess GDP growth",
        "Description": "Real GDP, %oya, 3mma, using HF data, in excess of 5-y med. GDP growth",
        "Geography": "local",
    },
    "XRPCONS_SA_P1M1ML12_3MMA_WG_ZN": {
        "Group": "Private consumption",
        "Label": "Excess consumption growth",
        "Description": "Real private consumption, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "weighted",
    },
    "XRPCONS_SA_P1M1ML12_3MMA_ZN": {
        "Group": "Private consumption",
        "Label": "Excess real consum growth",
        "Description": "Real private consumption, %oya, 3mma, in excess of 5-y median GDP growth",
        "Geography": "local",
    },
    "XRWAGES_NSA_P1M1ML12_ZN": {
        "Group": "Labour market",
        "Label": "Excess real wage growth",
        "Description": "Real wage growth, %oya, in excess of medium-term productivity growth",
        "Geography": "local",
    },
}

cat_labels = pd.DataFrame(cat_labels).T
cat_alllabel_dict = cat_labels[["Label", "Geography"]].agg(", ".join, axis=1).to_dict()

cat_labels = (
    cat_labels
    .reset_index(drop=False)
    .rename(columns={"index": "Category"})
    .set_index(["Group", "Category"])
    .sort_index()
)
cat_groups_count = (
    cat_labels.index.to_frame()
    .reset_index(drop=True)
    .groupby("Group")["Category"].count()
    .sort_values(ascending=True)
)

fig = cat_groups_count.plot.barh(
    ylabel="",
    fontsize=11
)
fig.set_title(label="Number of categories by aggregate macro group", pad=20)
fig.title.set_size(16)

plt.plot()
[]
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/910df0f5a269d4624c464f1dea17ed1c89404f5e7cb09453dc15a778b2fc34fb.png

Feature filtering and imputation #

Cross-section availability requirement #

# All normalized macroeconomic categories
all_macroz = [x + "_ZN" for x in ecos] 

# Identify categories with less than 10 cross sections
df_macro = df[df["xcat"].isin(all_macroz)]
cid_counts = df_macro.groupby('xcat')['cid'].nunique()
xcatx_low_cid = cid_counts[cid_counts < 10].index.tolist()
print("Categories with less than 10 cross sections:\n")
for xcat in xcatx_low_cid:
    print(xcat)

# Remove categories with less than 10 cross sections
macroz = [x for x in all_macroz if not x in xcatx_low_cid]

# Identify categories that have short history

df_macro = df[df["xcat"].isin(macroz)]
cutoff_date = pd.Timestamp("2003-01-01")
min_dates = df_macro.groupby('xcat')['real_date'].min()
xcatx_late_start = min_dates[min_dates >= cutoff_date].index.tolist()
print("\nCategories that start after 2002:\n")
for xcat in xcatx_late_start:
    print(xcat)

# Remove categories that start late
macroz = [x for x in macroz if not x in xcatx_late_start]
Categories with less than 10 cross sections:

CBCSCORE_SA_D3M3ML3_WG_ZN
CBCSCORE_SA_D3M3ML3_ZN
CBCSCORE_SA_WG_ZN
CBCSCORE_SA_ZN
CORPINTNETGDP_SA_D1Q1QL4_WG_ZN
CORPINTNETGDP_SA_D1Q1QL4_ZN
HHINTNETGDP_SA_D1M1ML12_WG_ZN
HHINTNETGDP_SA_D1M1ML12_ZN

Categories that start after 2002:
# Reduce label dictionary

cat_label_dict = {k:v for k, v in cat_alllabel_dict.items() if k in macroz}
# Visualize remaining macroeconomic categories
msm.check_availability(df, xcats=macroz, cids=cids_eq, missing_recent=False)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/e16b2079b31c0d3765b59f8030ba967188e3e727857691710e678b74cd9f3f74.png

Conditional imputation of missing cross-sections #

# Impute cross-sectional values if majority of cross sections are available

# Set parameters
impute_missing_cids = True
min_ratio_cids = 0.4

# Exclude categories than cannot logically be imputed
non_imputables = [
    "CXPI_NSA_P1M12ML1_ZN",
    "CMPI_NSA_P1M12ML1_ZN",
    "CTOT_NSA_P1M12ML1_ZN",
    "REEROADJ_NSA_P1M12ML1_ZN",
]
imputables = list(set(macroz) - set(non_imputables))

if impute_missing_cids:
    df_impute = msp.impute_panel(
        df=df, xcats=imputables, cids=cids_eq, threshold=min_ratio_cids
    )
    dfx = msm.update_df(df, df_impute)
else:
    dfx = df.copy()
# Visualize imputed macroeconomic categories
msm.check_availability(dfx, xcats=macroz, cids=cids_eq, missing_recent=False)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/13f7d53f0080e676a5f5159e9cb000639b8c3aba9a01ab62a0c6a6aa03f0369f.png

Equity sectoral return blacklisting #

sector_blacklist = {}

for sec in list(set(cids_secs) - {"ALL"}):
    
    dfb = df[df["xcat"] == f"EQC{sec}UNTRADABLE_NSA"].loc[:, ["cid", "xcat", "real_date", "value"]]
    dfba = (
        dfb.groupby(["cid", "real_date"])
        .aggregate(value=pd.NamedAgg(column="value", aggfunc="max"))
        .reset_index()
    )
    dfba["xcat"] = f"EQC{sec}BLACK"
    
    sector_blacklist[sec] = msp.make_blacklist(dfba, f"EQC{sec}BLACK")

Visualize target availability #

targets = [
    x for x in eqrets if x.endswith(("R_NSAvALL", "R_VT10vALL"))
]
msm.check_availability(dfx, targets, missing_recent=False)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/b35f09e2cd0f98068d481d6af95dc48feab066b27c22e227a85ee1ebcd8eaf5a.png

Sectoral signals and naive PnLs #

Common pipeline for all sectors #

default_target_type = "R_VT10vALL"

Model hyperparameters #

# Model dictionary
default_models = {
    "rf": RandomForestRegressor(
        n_estimators = 100,
        random_state = 42,
    )
}

# Hyperparameter grid   
default_hparam_grid = {
    "rf": {
        "max_samples": [0.1, 0.25],
        "max_features": ["sqrt", 0.5],
        "min_samples_leaf": [1, 3, 6, 9]
    },
}

Cross-validation splitter #

default_splitter = {"Validation": msl.RecencyKFoldPanelSplit(n_periods=12, n_splits = 1)}

Validation metric #

We use the probability of significance of correlation over the panel, arising from the MAP test, accounting for cross-sectional correlations in the panel, as a suitable performance metric. This should encourage the model selection process to favour models with evidence of predictive power, as well as capturing sufficient cross-sectional variation.

default_metric = {
    "MAP": make_scorer(msl.panel_significance_probability, greater_is_better=True),
}

Dynamics of the backtest #

The initial training set is the smallest possible training set comprising two years’ of data for two cross-sections. This is specified by setting min_periods = 24 and min_cids = 2 . Model selection occurs each month, by specifying test_size = 1 . The start date of the backtest is January 2003, since the initial training set absorbs data.

# Default parameters
default_test_size = 1  # retraining interval in months
default_min_cids = 2  # minimum number of cids to start predicting
default_min_periods = 24  # minimum number of periods to start predicting
default_split_functions = None
default_start_date = "2003-01-31"  # start date for the analysis

Energy #

Factor selection and signal generation #

sector = "ENR"

enr_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": list(set(cids_eq)-set(["CHF"])), # CHF has no energy companies
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
xcatx = enr_dict["xcatx"] + [enr_dict["ret"]]
cidx = enr_dict["cidx"]

so_enr = msl.SignalOptimizer(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    blacklist=enr_dict["black"],
    freq=enr_dict["freq"],
    lag=1,
    xcat_aggs=["last", "sum"],
)
secname = enr_dict["sector_name"]
signal_name = enr_dict["signal_name"]

so_enr.calculate_predictions(
    name=signal_name,
    models=default_models,
    scorers=default_metric,
    hyperparameters=default_hparam_grid,
    inner_splitters=default_splitter,
    test_size=default_test_size,
    min_cids=default_min_cids,
    min_periods=default_min_periods,
    n_jobs_outer=-1,
    split_functions=default_split_functions,
)
so_enr.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_enr.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/133387a41575edbeaa257695221eea92eaa45c9a081192fa91cde58650fee6c2.png
enr_importances = (
    so_enr.feature_importances.describe()
    .iloc[:, 1:]
    .sort_values(by="mean", axis=1, ascending=False)
)

enr_importances
COXR_VT10vWTI_21DMA_ZN BMLXINVCSCORE_SA_ZN BMLCOCRY_SAVT10_21DMA_ZN REFIXINVCSCORE_SA_ZN BASEXINVCSCORE_SA_ZN REEROADJ_NSA_P1M12ML1_ZN SBCSCORE_SA_D3M3ML3_WG_ZN CCSCORE_SA_WG_ZN XCSTR_SA_P1M1ML12_3MMA_WG_ZN XPPIH_NSA_P1M1ML12_ZN ... MBCSCORE_SA_ZN XRGDPTECH_SA_P1M1ML12_3MMA_WG_ZN SBCSCORE_SA_ZN XGGDGDPRATIOX10_NSA_ZN CTOT_NSA_P1M12ML1_ZN XIP_SA_P1M1ML12_3MMA_ZN UNEMPLRATE_NSA_3MMA_D1M1ML12_WG_ZN UNEMPLRATE_SA_3MMAv5YMA_WG_ZN INTLIQGDP_NSA_D1M1ML6_ZN INTLIQGDP_NSA_D1M1ML1_ZN
count 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 ... 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000
mean 0.030455 0.028891 0.026671 0.022462 0.022110 0.020687 0.020431 0.020106 0.019749 0.019185 ... 0.015101 0.014944 0.014917 0.014734 0.014730 0.014712 0.014638 0.014581 0.013310 0.012464
min 0.005509 0.001709 0.006474 0.005543 0.004249 0.000000 0.001849 0.011752 0.005844 0.008915 ... 0.006511 0.006440 0.004256 0.001353 0.004696 0.006610 0.003568 0.004980 0.002894 0.002411
25% 0.024036 0.022648 0.022271 0.018113 0.018921 0.018423 0.017866 0.017204 0.016972 0.015342 ... 0.013121 0.012711 0.012879 0.012730 0.012399 0.012518 0.012539 0.012735 0.011355 0.010311
50% 0.029448 0.027714 0.026119 0.022156 0.021868 0.020471 0.019996 0.019491 0.019500 0.017840 ... 0.014916 0.014779 0.014675 0.014657 0.014061 0.014320 0.014695 0.014402 0.012799 0.012412
75% 0.036493 0.035410 0.031027 0.026457 0.025132 0.023131 0.023021 0.021967 0.021967 0.021787 ... 0.016521 0.017021 0.016202 0.016877 0.016428 0.016214 0.016409 0.016393 0.014697 0.014192
max 0.068554 0.066356 0.057257 0.050295 0.042125 0.038268 0.042256 0.040873 0.039645 0.049404 ... 0.028439 0.025921 0.034335 0.027033 0.034592 0.045678 0.035156 0.027721 0.036798 0.026930
std 0.010089 0.011124 0.007684 0.006787 0.005841 0.004444 0.004523 0.004342 0.004541 0.005796 ... 0.003319 0.003502 0.003585 0.003493 0.003725 0.003952 0.003467 0.003100 0.003817 0.003359

8 rows × 57 columns

xcatx = enr_dict["signal_name"]
secname = enr_dict["sector_name"]

so_enr.coefs_stackedbarplot(
    name=xcatx,
    ftrs=list(enr_importances.columns[:10]),
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of default random forest feature importances",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/d99ae3f552d9c263e27e1f78b9117ddc4f645aa00f9c2da26b4f9da8994dc9c7.png

Signal quality check #

xcatx = [enr_dict["signal_name"], enr_dict["ret"]]
cidx = enr_dict["cidx"]
secname = enr_dict["sector_name"]

cr_enr = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=enr_dict["freq"],
    lag=1,
    blacklist=enr_dict["black"],
    xcat_aggs=["last", "sum"],
    slip=1,
    xcat_trims=[30, 30],  # trim dodgy data point
)

cr_enr.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/b95b755c79c80c57d16749d178ceb4efdd883dd2f3584db651a2a208e3f32b86.png
xcatx = [enr_dict["signal_name"]]
cidx = enr_dict["cidx"]
secname = enr_dict["sector_name"]

pnl_enr = msn.NaivePnL(
    df=dfx,
    ret=enr_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist=enr_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_enr.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale=None,
        thresh=2,
        pnl_name=enr_dict["pnl_name"],
    )
pnl_enr.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_enr.plot_pnls(
    pnl_cats=pnl_enr.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14,
)

enr_dict["pnls"] = pnl_enr
pnl_enr.evaluate_pnls(pnl_cats=pnl_enr.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/d1c10dd3c0d32e267a38b30b5a01d99b5e8613925e9f2566e7ec762f01e2edbf.png
xcat Energy learning-based signal Energy always long versus all-sector basket
Return % 34.750927 -17.270525
St. Dev. % 51.819453 48.872513
Sharpe Ratio 0.670615 -0.353379
Sortino Ratio 0.962592 -0.484741
Max 21-Day Draw % -76.587999 -72.170247
Max 6-Month Draw % -95.541511 -172.585695
Peak to Trough Draw % -154.83739 -773.428918
Top 5% Monthly PnL Share 0.655089 -1.354751
USD_EQXR_NSA correl -0.07969 -0.040251
Traded Months 272 272
secname = enr_dict["sector_name"]

pnl_enr.signal_heatmap(
    pnl_name=enr_dict["pnl_name"],
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/18b8627478ee9391d86bc77a9b00c47e204aefbf9beee9c64b449848c204c785.png

Materials #

Factor selection and signal generation #

sector = "MAT"

mat_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
xcatx = mat_dict["xcatx"] + [mat_dict["ret"]]
cidx = mat_dict["cidx"]

so_mat = msl.SignalOptimizer(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    blacklist=mat_dict["black"],
    freq=mat_dict["freq"],
    lag=1,
    xcat_aggs=["last", "sum"],
)
secname = mat_dict["sector_name"]
signal_name = mat_dict["signal_name"]

so_mat.calculate_predictions(
    name=signal_name,
    models=default_models,
    scorers=default_metric,
    hyperparameters=default_hparam_grid,
    inner_splitters=default_splitter,
    test_size=default_test_size,
    min_cids=default_min_cids,
    min_periods=default_min_periods,
    n_jobs_outer=-1,
    split_functions=default_split_functions,
)
so_mat.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_mat.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/b0f330456ea4a3d6fe41250f805a7d2f7999a2e28535b55c6f905f142e78d941.png
mat_importances = (
    so_mat.feature_importances.describe()
    .iloc[:, 1:]
    .sort_values(by="mean", axis=1, ascending=False)
)

mat_importances
REFIXINVCSCORE_SA_ZN COXR_VT10vWTI_21DMA_ZN SBCSCORE_SA_D3M3ML3_WG_ZN BMLCOCRY_SAVT10_21DMA_ZN REEROADJ_NSA_P1M12ML1_ZN SBCSCORE_SA_D3M3ML3_ZN CCSCORE_SA_WG_ZN BASEXINVCSCORE_SA_ZN RIR_NSA_ZN BMLXINVCSCORE_SA_ZN ... XGGDGDPRATIOX10_NSA_ZN UNEMPLRATE_NSA_3MMA_D1M1ML12_ZN XRGDPTECH_SA_P1M1ML12_3MMA_WG_ZN MBCSCORE_SA_ZN XCPIE_SA_P1M1ML12_ZN UNEMPLRATE_NSA_3MMA_D1M1ML12_WG_ZN XIP_SA_P1M1ML12_3MMA_ZN XRGDPTECH_SA_P1M1ML12_3MMA_ZN INTLIQGDP_NSA_D1M1ML1_ZN INTLIQGDP_NSA_D1M1ML6_ZN
count 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 ... 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000
mean 0.024450 0.024089 0.023131 0.022834 0.021872 0.020496 0.020055 0.019982 0.019964 0.019684 ... 0.015376 0.015332 0.015311 0.015133 0.014845 0.014191 0.013942 0.013383 0.011892 0.011543
min 0.009042 0.008542 0.007450 0.000731 0.005534 0.010004 0.005654 0.002825 0.002460 0.006276 ... 0.005241 0.007043 0.000000 0.003465 0.007733 0.005331 0.005215 0.000847 0.000769 0.000000
25% 0.020501 0.021062 0.020152 0.019233 0.018965 0.018103 0.018087 0.017341 0.017495 0.015610 ... 0.013130 0.013169 0.013416 0.013293 0.013253 0.012358 0.011693 0.011758 0.010226 0.010093
50% 0.023928 0.023583 0.022733 0.022907 0.021547 0.019998 0.019710 0.019810 0.019810 0.020059 ... 0.015208 0.015169 0.015104 0.015032 0.014400 0.014044 0.013646 0.013257 0.012012 0.011241
75% 0.027983 0.026993 0.025182 0.026978 0.024163 0.022420 0.021872 0.022646 0.022573 0.023615 ... 0.017193 0.017306 0.017235 0.016715 0.016015 0.015735 0.015437 0.015220 0.013831 0.012805
max 0.053290 0.043895 0.045020 0.038999 0.036748 0.038795 0.039068 0.036511 0.033870 0.034816 ... 0.035066 0.026076 0.029530 0.025718 0.032171 0.025882 0.035764 0.022468 0.021910 0.026116
std 0.006526 0.005139 0.004948 0.006000 0.004460 0.004136 0.003833 0.004647 0.004377 0.005632 ... 0.003624 0.003063 0.003473 0.003055 0.002793 0.002770 0.003567 0.002841 0.003104 0.002646

8 rows × 57 columns

xcatx = mat_dict["signal_name"]
secname = mat_dict["sector_name"]

so_mat.coefs_stackedbarplot(
    name=xcatx,
    ftrs=list(mat_importances.columns[:10]),
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of default random forest feature importances",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/2ec6f0b42511a3761928a2ab50e3105056fe916a434a78c935696ffc9ddcaf06.png

Signal quality check #

xcatx = [mat_dict["signal_name"], mat_dict["ret"]]
cidx = mat_dict["cidx"]
secname = mat_dict["sector_name"]

cr_mat = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=mat_dict["freq"],
    blacklist=mat_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
    xcat_trims=[2, 20],
)

cr_mat.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/4e26a4dda646daa98e417b6adbb2b4abb08d37d2ea120ad1bdccaa3ae85f81d8.png
xcatx = [mat_dict["signal_name"]]
cidx = mat_dict["cidx"]
secname = mat_dict["sector_name"]
pnl_name = mat_dict["pnl_name"]

pnl_mat = msn.NaivePnL(
    df=dfx,
    ret=mat_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist=mat_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_mat.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale=None,
        thresh=2,
        pnl_name=pnl_name,
    )
pnl_mat.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_mat.plot_pnls(
    pnl_cats=pnl_mat.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14,
)

mat_dict["pnls"] = pnl_mat
pnl_mat.evaluate_pnls(pnl_cats=pnl_mat.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/414fc6054d9c174f72a1fe3a63f8f6cccc311762733b2f1d9276f06dad249840.png
xcat Materials learning-based signal Materials always long versus all-sector basket
Return % 16.551132 -22.142443
St. Dev. % 42.285506 39.241085
Sharpe Ratio 0.391414 -0.564267
Sortino Ratio 0.547859 -0.77997
Max 21-Day Draw % -71.358517 -58.653828
Max 6-Month Draw % -87.862456 -145.683081
Peak to Trough Draw % -115.995191 -700.140403
Top 5% Monthly PnL Share 1.208077 -0.693009
USD_EQXR_NSA correl -0.004249 0.037619
Traded Months 272 272
secname = mat_dict["sector_name"]
xcatx = mat_dict["signal_name"]

pnl_mat.signal_heatmap(
    pnl_name=f"{secname} learning-based signal",
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/877d430e998b604a1506df7d2a9e8656541300ba3380a6338d935ab1524b6fa1.png

Industrials #

Factor selection and signal generation #

sector = "IND"

ind_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
xcatx = ind_dict["xcatx"] + [ind_dict["ret"]]
cidx = ind_dict["cidx"]

so_ind = msl.SignalOptimizer(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    blacklist=ind_dict["black"],
    freq=ind_dict["freq"],
    lag=1,
    xcat_aggs=["last", "sum"],
)
secname = ind_dict["sector_name"]
signal_name = ind_dict["signal_name"]

so_ind.calculate_predictions(
    name=signal_name,
    models=default_models,
    scorers=default_metric,
    hyperparameters=default_hparam_grid,
    inner_splitters=default_splitter,
    test_size=default_test_size,
    min_cids=default_min_cids,
    min_periods=default_min_periods,
    n_jobs_outer=-1,
    split_functions=default_split_functions,
)
so_ind.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_ind.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/3ce378f9b56a228f103c6428966d03088e802172b88c66e707a6f1705fd625b5.png
ind_importances = (
    so_ind.feature_importances.describe()
    .iloc[:, 1:]
    .sort_values(by="mean", axis=1, ascending=False)
)

ind_importances
BMLCOCRY_SAVT10_21DMA_ZN BMLXINVCSCORE_SA_ZN CCSCORE_SA_WG_ZN BASEXINVCSCORE_SA_ZN XCPIC_SA_P1M1ML12_ZN COXR_VT10vWTI_21DMA_ZN MBCSCORE_SA_D3M3ML3_ZN XCPIE_SA_P1M1ML12_WG_ZN XRWAGES_NSA_P1M1ML12_ZN XGGDGDPRATIOX10_NSA_ZN ... UNEMPLRATE_NSA_3MMA_D1M1ML12_ZN XEMPL_NSA_P1M1ML12_3MMA_ZN UNEMPLRATE_NSA_3MMA_D1M1ML12_WG_ZN XIP_SA_P1M1ML12_3MMA_ZN RIR_NSA_ZN XRGDPTECH_SA_P1M1ML12_3MMA_ZN XRPCONS_SA_P1M1ML12_3MMA_WG_ZN XRPCONS_SA_P1M1ML12_3MMA_ZN XCPIH_SA_P1M1ML12_ZN INTLIQGDP_NSA_D1M1ML1_ZN
count 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 ... 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000
mean 0.031630 0.025337 0.021137 0.020714 0.020651 0.020301 0.019594 0.019543 0.019425 0.019292 ... 0.015229 0.015060 0.014980 0.014886 0.014776 0.014502 0.014483 0.014414 0.014172 0.013460
min 0.005875 0.000338 0.010903 0.001023 0.003111 0.007974 0.010478 0.000000 0.007017 0.006907 ... 0.003477 0.002908 0.004710 0.002713 0.005407 0.005077 0.002831 0.005281 0.004026 0.002729
25% 0.024961 0.020915 0.018205 0.017620 0.018539 0.017656 0.017046 0.016885 0.017354 0.016768 ... 0.013453 0.013165 0.012943 0.012731 0.012515 0.012696 0.012548 0.012808 0.012533 0.011329
50% 0.029707 0.024984 0.020548 0.020771 0.020559 0.020174 0.018731 0.018782 0.019252 0.018901 ... 0.014881 0.014841 0.015022 0.014878 0.014855 0.014250 0.014088 0.014362 0.014204 0.012921
75% 0.037710 0.029484 0.023483 0.024213 0.022473 0.022249 0.021975 0.021481 0.021427 0.022001 ... 0.016453 0.016543 0.016654 0.016601 0.017020 0.016173 0.016316 0.015782 0.015631 0.015248
max 0.074429 0.063414 0.038962 0.034778 0.032717 0.049834 0.039051 0.051315 0.031714 0.057063 ... 0.040714 0.031488 0.030014 0.040794 0.025897 0.028709 0.042252 0.032272 0.037768 0.027120
std 0.010216 0.009114 0.004331 0.005482 0.003771 0.004688 0.004415 0.005173 0.003454 0.004781 ... 0.003481 0.003062 0.003320 0.003571 0.003254 0.002801 0.003341 0.003182 0.003375 0.003719

8 rows × 57 columns

xcatx = ind_dict["signal_name"]
secname = ind_dict["sector_name"]

so_ind.coefs_stackedbarplot(
    name=xcatx,
    ftrs=list(ind_importances.columns[:10]),
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of default random forest feature importances",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/2d7c7da7a756cdcb3f6be90d6ddb2618397262308af869cd1d1c529c7b3353e8.png

Signal quality check #

xcatx = [ind_dict["signal_name"], ind_dict["ret"]]
cidx = ind_dict["cidx"]
secname = ind_dict["sector_name"]

cr_ind = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=ind_dict["freq"],
    blacklist=ind_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_ind.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/10fd3d52cf0e385607299904637b8f4cd71222a4bab89e89ce0ff1fbfe552deb.png
xcatx = [ind_dict["signal_name"]]
cidx = ind_dict["cidx"]
secname = ind_dict["sector_name"]
pnl_name = ind_dict["pnl_name"]

pnl_ind = msn.NaivePnL(
    df=dfx,
    ret=ind_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    bms=["USD_EQXR_NSA"],
    blacklist=ind_dict["black"],
)

for xcat in xcatx:
    pnl_ind.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale=None,
        thresh=2,
        pnl_name=pnl_name,
    )

    pnl_ind.make_long_pnl(
        vol_scale=None, label=f"{secname} always long versus all-sector basket"
    )

pnl_ind.plot_pnls(
    pnl_cats=pnl_ind.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14,
)

ind_dict["pnls"] = pnl_ind
pnl_ind.evaluate_pnls(pnl_cats=pnl_ind.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/9c7361a0f8a550e7c069548f075cc44e4baded9e3e5960848d489f70dcebc934.png
xcat Industrials learning-based signal Industrials always long versus all-sector basket
Return % 9.988343 19.020392
St. Dev. % 29.123118 32.3917
Sharpe Ratio 0.34297 0.5872
Sortino Ratio 0.496285 0.839932
Max 21-Day Draw % -40.478676 -53.005336
Max 6-Month Draw % -56.047224 -62.385972
Peak to Trough Draw % -126.122062 -98.8884
Top 5% Monthly PnL Share 1.381842 0.767952
USD_EQXR_NSA correl 0.00005 0.268923
Traded Months 272 272
xcatx = ind_dict["signal_name"]
pnl_ind.signal_heatmap(
    pnl_name=f"{secname} learning-based signal",
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/6265ee71aeba18c8b626c1f8fad61cb224dc12ed1f70728abc7c9e3c05f53929.png

Consumer discretionary #

Factor selection and signal generation #

sector = "COD"

cod_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
xcatx = cod_dict["xcatx"] + [cod_dict["ret"]]
cidx = cod_dict["cidx"]

so_cod = msl.SignalOptimizer(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    blacklist=cod_dict["black"],
    freq=cod_dict["freq"],
    lag=1,
    xcat_aggs=["last", "sum"],
)
secname = cod_dict["sector_name"]
signal_name = cod_dict["signal_name"]

so_cod.calculate_predictions(
    name=signal_name,
    models=default_models,
    scorers=default_metric,
    hyperparameters=default_hparam_grid,
    inner_splitters=default_splitter,
    test_size=default_test_size,
    min_cids=default_min_cids,
    min_periods=default_min_periods,
    n_jobs_outer=-1,
    split_functions=default_split_functions,
)
so_cod.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_cod.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/5f5115bd0459313260843f30119aebda84d18464baae0a227452dc98a124ada0.png
cod_importances = (
    so_cod.feature_importances.describe()
    .iloc[:, 1:]
    .sort_values(by="mean", axis=1, ascending=False)
)

cod_importances
COXR_VT10vWTI_21DMA_ZN XGGDGDPRATIOX10_NSA_ZN CXPI_NSA_P1M12ML1_ZN BMLCOCRY_SAVT10_21DMA_ZN RIR_NSA_ZN REEROADJ_NSA_P1M12ML1_ZN SBCSCORE_SA_WG_ZN CCSCORE_SA_D3M3ML3_ZN CCSCORE_SA_WG_ZN RYLDIRS05Y_NSA_ZN ... XPCREDITBN_SJA_P1M1ML12_ZN XRRSALES_SA_P1M1ML12_3MMA_WG_ZN MBCSCORE_SA_WG_ZN MBCSCORE_SA_D3M3ML3_WG_ZN XRGDPTECH_SA_P1M1ML12_3MMA_WG_ZN XRPCONS_SA_P1M1ML12_3MMA_ZN XPCREDITBN_SJA_P1M1ML12_WG_ZN XRGDPTECH_SA_P1M1ML12_3MMA_ZN INTLIQGDP_NSA_D1M1ML6_ZN INTLIQGDP_NSA_D1M1ML1_ZN
count 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 ... 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000
mean 0.024340 0.021854 0.020933 0.020676 0.020388 0.020366 0.019906 0.019842 0.019817 0.019657 ... 0.015709 0.015541 0.015502 0.015313 0.015061 0.014935 0.014744 0.014681 0.014310 0.012838
min 0.009720 0.007440 0.003781 0.002630 0.006197 0.004253 0.008072 0.007013 0.007702 0.006474 ... 0.002378 0.005408 0.006803 0.003496 0.003719 0.000000 0.006738 0.004514 0.005872 0.002931
25% 0.020722 0.018944 0.018390 0.017912 0.017744 0.018344 0.017956 0.017819 0.017419 0.016922 ... 0.013565 0.013685 0.012891 0.013907 0.013107 0.013003 0.012711 0.013185 0.011736 0.010559
50% 0.023385 0.021540 0.020334 0.020241 0.019704 0.019983 0.020123 0.019501 0.019481 0.019025 ... 0.015701 0.015430 0.015205 0.015408 0.015268 0.015143 0.014670 0.014667 0.013213 0.012765
75% 0.027342 0.024746 0.023162 0.023010 0.022879 0.022045 0.022188 0.021791 0.021911 0.021734 ... 0.017710 0.017575 0.017517 0.016956 0.017045 0.016690 0.016502 0.016198 0.015377 0.014904
max 0.048688 0.033246 0.039388 0.082954 0.038722 0.042718 0.029961 0.031235 0.035353 0.039639 ... 0.029448 0.024643 0.031998 0.022177 0.028028 0.021841 0.026969 0.027422 0.041324 0.023900
std 0.005683 0.004338 0.004342 0.006114 0.004311 0.003977 0.003509 0.003502 0.003939 0.004260 ... 0.003357 0.002972 0.003963 0.002854 0.003392 0.002939 0.003092 0.002887 0.004607 0.003077

8 rows × 57 columns

xcatx = cod_dict["signal_name"]
secname = cod_dict["sector_name"]

so_cod.coefs_stackedbarplot(
    name=xcatx,
    ftrs=list(cod_importances.columns[:10]),
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of default random forest feature importances",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/96c2361416d547f0c13a4d61f99717fac6b51d5fab5030c3a0c81bc6d6b38d0c.png

Signal quality check #

xcatx = [cod_dict["signal_name"], cod_dict["ret"]]
cidx = cod_dict["cidx"]
signal_name = cod_dict["signal_name"]

cr_cod = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=cod_dict["freq"],
    blacklist=cod_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_cod.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/0529720a11aac529b1c8981f9fa7c75f612d77d646f841a2678bdc31a7d891b6.png
xcatx = [cod_dict["signal_name"]]
cidx = cod_dict["cidx"]
secname = cod_dict["sector_name"]
signal_name = cod_dict["signal_name"]
pnl_name = cod_dict["pnl_name"]

pnl_cod = msn.NaivePnL(
    df=dfx,
    ret=cod_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist=cod_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_cod.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale=None,
        thresh=2,
        pnl_name=pnl_name,
    )
pnl_cod.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_cod.plot_pnls(
    pnl_cats=pnl_cod.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14,
)

cod_dict["pnls"] = pnl_cod
pnl_cod.evaluate_pnls(pnl_cats=pnl_cod.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/2837704bfda8bdd3937f2f4f5fda3baf4ccaa3ef12a650d3df987152c46ed85f.png
xcat Cons. discretionary learning-based signal Cons. discretionary always long versus all-sector basket
Return % 4.021003 -15.130599
St. Dev. % 32.725707 32.040102
Sharpe Ratio 0.12287 -0.472239
Sortino Ratio 0.171408 -0.655123
Max 21-Day Draw % -40.567912 -65.455325
Max 6-Month Draw % -72.501531 -93.787514
Peak to Trough Draw % -155.489425 -348.445877
Top 5% Monthly PnL Share 3.29675 -0.791231
USD_EQXR_NSA correl -0.018456 0.109204
Traded Months 272 272
pnl_name = cod_dict["pnl_name"]
secname = cod_dict["sector_name"]

pnl_cod.signal_heatmap(
    pnl_name=f"{secname} learning-based signal",
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/22160f319b0432aa36802e02feab4cb8aba3ac735dedd7fbbf31ee9a171314e7.png

Consumer staples #

Factor selection and signal generation #

sector = "COS"

cos_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
xcatx = cos_dict["xcatx"] + [cos_dict["ret"]]
cidx = cos_dict["cidx"]

so_cos = msl.SignalOptimizer(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    blacklist=cos_dict["black"],
    freq=cos_dict["freq"],
    lag=1,
    xcat_aggs=["last", "sum"],
)
secname = cos_dict["sector_name"]
signal_name = cos_dict["signal_name"]

so_cos.calculate_predictions(
    name=signal_name,
    models=default_models,
    scorers=default_metric,
    hyperparameters=default_hparam_grid,
    inner_splitters=default_splitter,
    test_size=default_test_size,
    min_cids=default_min_cids,
    min_periods=default_min_periods,
    n_jobs_outer=-1,
    split_functions=default_split_functions,
)
so_cos.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_cos.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/ff516cb099377e552049b1e4f8e82c7ca1663b56067676f6349b70c16655d0c0.png
cos_importances = (
    so_cos.feature_importances.describe()
    .iloc[:, 1:]
    .sort_values(by="mean", axis=1, ascending=False)
)

cos_importances
BMLCOCRY_SAVT10_21DMA_ZN CCSCORE_SA_WG_ZN REEROADJ_NSA_P1M12ML1_ZN COXR_VT10vWTI_21DMA_ZN SBCSCORE_SA_D3M3ML3_WG_ZN XCPIF_SA_P1M1ML12_WG_ZN XRGDPTECH_SA_P1M1ML12_3MMA_WG_ZN XGGDGDPRATIOX10_NSA_ZN XIP_SA_P1M1ML12_3MMA_WG_ZN RYLDIRS05Y_NSA_ZN ... XNRSALES_SA_P1M1ML12_3MMA_WG_ZN MBCSCORE_SA_D3M3ML3_WG_ZN XPPIH_NSA_P1M1ML12_ZN UNEMPLRATE_NSA_3MMA_D1M1ML12_ZN XRPCONS_SA_P1M1ML12_3MMA_ZN XRPCONS_SA_P1M1ML12_3MMA_WG_ZN UNEMPLRATE_NSA_3MMA_D1M1ML12_WG_ZN CMPI_NSA_P1M12ML1_ZN INTLIQGDP_NSA_D1M1ML6_ZN INTLIQGDP_NSA_D1M1ML1_ZN
count 277.000000 277.000000 277.000000 277.000000 277.000000 277.000000 277.000000 277.000000 277.000000 277.000000 ... 277.000000 277.000000 277.000000 277.000000 277.000000 277.000000 277.000000 277.000000 277.000000 277.000000
mean 0.027913 0.023514 0.023145 0.020556 0.020110 0.019925 0.019830 0.019796 0.019346 0.019308 ... 0.015831 0.015708 0.015643 0.015578 0.015549 0.015522 0.015415 0.014644 0.014636 0.014226
min 0.010073 0.014296 0.009281 0.009934 0.006148 0.009133 0.007232 0.007736 0.000822 0.009116 ... 0.005421 0.005231 0.006545 0.002136 0.005351 0.004188 0.007013 0.006169 0.006308 0.004330
25% 0.023564 0.019806 0.019866 0.018060 0.018163 0.016857 0.015854 0.016311 0.016663 0.017201 ... 0.013914 0.014068 0.013817 0.013731 0.013964 0.013836 0.013273 0.012784 0.012415 0.012428
50% 0.027188 0.022714 0.022077 0.020129 0.019666 0.018764 0.018183 0.020317 0.018518 0.019133 ... 0.015660 0.015885 0.015589 0.015374 0.015678 0.015385 0.015013 0.014433 0.014131 0.014045
75% 0.031985 0.025420 0.025284 0.023002 0.021445 0.021636 0.022264 0.023359 0.021333 0.021422 ... 0.017405 0.017168 0.017420 0.017210 0.017091 0.017028 0.017167 0.016374 0.016661 0.015928
max 0.058962 0.046972 0.063331 0.039355 0.043595 0.060244 0.054348 0.033516 0.039543 0.034376 ... 0.025763 0.030118 0.026586 0.029126 0.027130 0.026871 0.028413 0.025640 0.026311 0.022135
std 0.007147 0.005127 0.005557 0.004475 0.004125 0.005354 0.006490 0.005164 0.004649 0.003589 ... 0.003044 0.002901 0.003056 0.003400 0.002956 0.002913 0.003214 0.002955 0.003348 0.002990

8 rows × 57 columns

xcatx = cos_dict["signal_name"]
secname = cos_dict["sector_name"]

so_cos.coefs_stackedbarplot(
    name=xcatx,
    ftrs=list(cos_importances.columns[:10]),
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of default random forest feature importances",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/a2336d6d4f4badd10bb2de0872b827c7514b1f37641e31826993471798552b20.png

Signal quality check #

xcatx = [cos_dict["signal_name"], cos_dict["ret"]]
cidx = cos_dict["cidx"]
secname = cos_dict["sector_name"]

cr_cos = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=cos_dict["freq"],
    blacklist=cos_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_cos.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/5c39919c80a994587f5fd4191c2a86b520fdeb3294410f3d5427f8ff9fad33dc.png
xcatx = [cos_dict["signal_name"]]
cidx = cos_dict["cidx"]
secname = cos_dict["sector_name"]
signal_name = cos_dict["signal_name"]
pnl_name = cos_dict["pnl_name"]

pnl_cos = msn.NaivePnL(
    df=dfx,
    ret=cos_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist=cos_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_cos.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale=None,
        thresh=2,
        pnl_name=pnl_name,
    )
pnl_cos.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_cos.plot_pnls(
    pnl_cats=pnl_cos.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14,
)

cos_dict["pnls"] = pnl_cos
pnl_cos.evaluate_pnls(pnl_cats=pnl_cos.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/5c62e90bc5b62f7284315dd2644f1ac1fdae233504b28b81daf3229904e67c9e.png
xcat Cons. staples learning-based signal Cons. staples always long versus all-sector basket
Return % 11.775461 7.715795
St. Dev. % 36.952958 38.515323
Sharpe Ratio 0.318661 0.200331
Sortino Ratio 0.451798 0.288431
Max 21-Day Draw % -49.057581 -36.485328
Max 6-Month Draw % -80.288526 -95.448259
Peak to Trough Draw % -173.311671 -209.342772
Top 5% Monthly PnL Share 1.597291 2.261383
USD_EQXR_NSA correl -0.029234 -0.134912
Traded Months 272 272
pnl_name = cos_dict["pnl_name"]
secname = cos_dict["sector_name"]

pnl_cos.signal_heatmap(
    pnl_name=pnl_name,
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/d7cc2e1623f5ec87b4fadd8c5fe5f9fb9f5b23233deae2f830c148450447dabf.png

Healthcare #

Factor selection and signal generation #

sector = "HLC"

hlc_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
xcatx = hlc_dict["xcatx"] + [hlc_dict["ret"]]
cidx = hlc_dict["cidx"]

so_hlc = msl.SignalOptimizer(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    blacklist=hlc_dict["black"],
    freq=hlc_dict["freq"],
    lag=1,
    xcat_aggs=["last", "sum"],
)
secname = hlc_dict["sector_name"]
signal_name = hlc_dict["signal_name"]

so_hlc.calculate_predictions(
    name=signal_name,
    models=default_models,
    scorers=default_metric,
    hyperparameters=default_hparam_grid,
    inner_splitters=default_splitter,
    test_size=default_test_size,
    min_cids=default_min_cids,
    min_periods=default_min_periods,
    n_jobs_outer=-1,
    split_functions=default_split_functions,
)
so_hlc.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_hlc.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/8d72c61ba311729e5ae739b084ecfdc6c98d8ed48c8fae77c3afaf758b2c6f45.png
hlc_importances = (
    so_hlc.feature_importances.describe()
    .iloc[:, 1:]
    .sort_values(by="mean", axis=1, ascending=False)
)

hlc_importances
BMLCOCRY_SAVT10_21DMA_ZN COXR_VT10vWTI_21DMA_ZN XCPIC_SA_P1M1ML12_ZN XRWAGES_NSA_P1M1ML12_ZN BMLXINVCSCORE_SA_ZN XCSTR_SA_P1M1ML12_3MMA_ZN XGGDGDPRATIOX10_NSA_ZN REEROADJ_NSA_P1M12ML1_ZN REFIXINVCSCORE_SA_ZN CXPI_NSA_P1M12ML1_ZN ... RYLDIRS05Y_NSA_ZN XPCREDITBN_SJA_P1M1ML12_WG_ZN XRPCONS_SA_P1M1ML12_3MMA_ZN XRGDPTECH_SA_P1M1ML12_3MMA_WG_ZN XIP_SA_P1M1ML12_3MMA_ZN UNEMPLRATE_SA_3MMAv5YMA_WG_ZN RYLDIRS02Y_NSA_ZN XRGDPTECH_SA_P1M1ML12_3MMA_ZN UNEMPLRATE_NSA_3MMA_D1M1ML12_WG_ZN INTLIQGDP_NSA_D1M1ML1_ZN
count 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 ... 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000
mean 0.024830 0.024086 0.020743 0.020127 0.019887 0.019706 0.019671 0.019273 0.019249 0.018967 ... 0.015826 0.015634 0.015575 0.015419 0.015410 0.015366 0.014926 0.014789 0.014586 0.012469
min 0.012493 0.007477 0.008646 0.005502 0.006942 0.008941 0.006576 0.006388 0.000000 0.010626 ... 0.001602 0.006126 0.005259 0.001396 0.007435 0.006342 0.003866 0.002925 0.005955 0.001402
25% 0.020697 0.021432 0.018225 0.017653 0.015810 0.017358 0.017048 0.017243 0.015573 0.016610 ... 0.014112 0.013870 0.014082 0.013392 0.013401 0.013766 0.012994 0.012757 0.012391 0.010732
50% 0.024002 0.023738 0.020341 0.020317 0.020372 0.019321 0.019767 0.019208 0.018984 0.018257 ... 0.015691 0.015498 0.015621 0.015335 0.015327 0.015455 0.014989 0.014912 0.014175 0.012283
75% 0.027163 0.026786 0.022871 0.022177 0.023764 0.022053 0.022252 0.021297 0.022783 0.020667 ... 0.017643 0.017457 0.017228 0.017337 0.017160 0.016997 0.016997 0.016386 0.016142 0.013910
max 0.057866 0.040510 0.034367 0.034621 0.032086 0.038387 0.035862 0.030717 0.035772 0.043952 ... 0.035210 0.026219 0.025586 0.025888 0.040163 0.026179 0.030559 0.025592 0.027840 0.025865
std 0.006318 0.004904 0.004061 0.003855 0.005228 0.003721 0.004287 0.003500 0.005746 0.003865 ... 0.003319 0.003134 0.002726 0.003210 0.003768 0.002846 0.003370 0.002970 0.003192 0.002993

8 rows × 57 columns

xcatx = hlc_dict["signal_name"]
secname = hlc_dict["sector_name"]

so_hlc.coefs_stackedbarplot(
    name=xcatx,
    ftrs=list(hlc_importances.columns[:10]),
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of default random forest feature importances",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/a400ee33888d83f2b8e6eec7c236f0de49f7d0867df9b3b0dfa396937c2ac9a6.png

Signal quality check #

xcatx = [hlc_dict["signal_name"], hlc_dict["ret"]]
cidx = hlc_dict["cidx"]
secname = hlc_dict["sector_name"]

cr_hlc = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=hlc_dict["freq"],
    blacklist=hlc_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_hlc.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/4ffdbc1c1362c9f71e31bca67a8c4aae4371ce9361051b82d81009ac7deb5de5.png
xcatx = [hlc_dict["signal_name"]]
cidx = hlc_dict["cidx"]
secname = hlc_dict["sector_name"]
signal_name = hlc_dict["signal_name"]
pnl_name = hlc_dict["pnl_name"]

pnl_hlc = msn.NaivePnL(
    df=dfx,
    ret=hlc_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist=hlc_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_hlc.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale=None,
        thresh=2,
        pnl_name=pnl_name,
    )
pnl_hlc.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_hlc.plot_pnls(
    pnl_cats=pnl_hlc.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14,
)

hlc_dict["pnls"] = pnl_hlc
pnl_hlc.evaluate_pnls(pnl_cats=pnl_hlc.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/44b0d0544c2205fce2817b566531622916f10f3f975e1049159081c27a258e2f.png
xcat Healthcare learning-based signal Healthcare always long versus all-sector basket
Return % 19.339383 -4.407761
St. Dev. % 37.057168 39.122083
Sharpe Ratio 0.52188 -0.112667
Sortino Ratio 0.746459 -0.161241
Max 21-Day Draw % -38.108908 -48.311662
Max 6-Month Draw % -73.24248 -91.147334
Peak to Trough Draw % -169.64987 -277.332348
Top 5% Monthly PnL Share 0.826467 -3.802043
USD_EQXR_NSA correl 0.034007 -0.148982
Traded Months 272 272
pnl_name = hlc_dict["pnl_name"]
secname = hlc_dict["sector_name"]

pnl_hlc.signal_heatmap(
    pnl_name=pnl_name,
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/d4ff0832d1ce75d410858b6eb4d69774a07bf6d41c37719673d75896e3e7f668.png

Financials #

Factor selection and signal generation #

sector = "FIN"

fin_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
xcatx = fin_dict["xcatx"] + [fin_dict["ret"]]
cidx = fin_dict["cidx"]

so_fin = msl.SignalOptimizer(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    blacklist=fin_dict["black"],
    freq=fin_dict["freq"],
    lag=1,
    xcat_aggs=["last", "sum"],
)
secname = fin_dict["sector_name"]
signal_name = fin_dict["signal_name"]

so_fin.calculate_predictions(
    name=signal_name,
    models=default_models,
    scorers=default_metric,
    hyperparameters=default_hparam_grid,
    inner_splitters=default_splitter,
    test_size=default_test_size,
    min_cids=default_min_cids,
    min_periods=default_min_periods,
    n_jobs_outer=-1,
    split_functions=default_split_functions,
)
so_fin.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_fin.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/02316d63b22f93c87d52d5e9b80abfd55402417ed4e054d1c66ee84290a8699e.png
fin_importances = (
    so_fin.feature_importances.describe()
    .iloc[:, 1:]
    .sort_values(by="mean", axis=1, ascending=False)
)

fin_importances
BMLCOCRY_SAVT10_21DMA_ZN CCSCORE_SA_ZN COXR_VT10vWTI_21DMA_ZN XGGDGDPRATIOX10_NSA_ZN XNRSALES_SA_P1M1ML12_3MMA_ZN REEROADJ_NSA_P1M12ML1_ZN REFIXINVCSCORE_SA_ZN CXPI_NSA_P1M12ML1_ZN CCSCORE_SA_WG_ZN XCSTR_SA_P1M1ML12_3MMA_ZN ... XPCREDITBN_SJA_P1M1ML12_WG_ZN INTLIQGDP_NSA_D1M1ML6_ZN SBCSCORE_SA_ZN XEMPL_NSA_P1M1ML12_3MMA_WG_ZN XRGDPTECH_SA_P1M1ML12_3MMA_ZN RIR_NSA_ZN INTLIQGDP_NSA_D1M1ML1_ZN XRPCONS_SA_P1M1ML12_3MMA_ZN XRGDPTECH_SA_P1M1ML12_3MMA_WG_ZN UNEMPLRATE_NSA_3MMA_D1M1ML12_WG_ZN
count 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 ... 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000
mean 0.029546 0.023404 0.023034 0.022009 0.021869 0.021560 0.021163 0.021125 0.019810 0.019502 ... 0.015316 0.015283 0.015268 0.015193 0.015169 0.014731 0.014730 0.014061 0.013891 0.013651
min 0.002187 0.007882 0.009273 0.013580 0.003747 0.010654 0.000000 0.006440 0.003292 0.000000 ... 0.003879 0.003752 0.000000 0.001195 0.006763 0.002836 0.002149 0.002521 0.006144 0.000000
25% 0.024383 0.019517 0.020006 0.018944 0.018529 0.017890 0.015928 0.018549 0.017028 0.017549 ... 0.013190 0.012092 0.013257 0.013629 0.013244 0.012696 0.012424 0.012588 0.011778 0.011941
50% 0.028278 0.022681 0.022733 0.021492 0.020774 0.020119 0.021165 0.020589 0.019369 0.019279 ... 0.015166 0.014232 0.015175 0.015194 0.014898 0.014746 0.014479 0.014060 0.013477 0.013930
75% 0.034474 0.026261 0.026350 0.024103 0.024446 0.023308 0.026613 0.022970 0.022264 0.021203 ... 0.017234 0.017028 0.016929 0.017198 0.016811 0.016783 0.016925 0.015770 0.015646 0.015406
max 0.056049 0.048031 0.036185 0.040476 0.038473 0.053352 0.042114 0.048488 0.034667 0.034394 ... 0.042352 0.050951 0.043975 0.041043 0.031871 0.026975 0.025603 0.022486 0.026698 0.025853
std 0.008493 0.005838 0.005015 0.004327 0.005197 0.006015 0.007193 0.004598 0.004371 0.003487 ... 0.003693 0.005634 0.003552 0.003661 0.003091 0.003384 0.003772 0.002641 0.003014 0.003140

8 rows × 57 columns

xcatx = fin_dict["signal_name"]
secname = fin_dict["sector_name"]

so_fin.coefs_stackedbarplot(
    name=xcatx,
    ftrs=list(fin_importances.columns[:10]),
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of default random forest feature importances",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/62dad9109539477d44de339c500218e48a22ae6aa5644692d2a9982b07d2902c.png

Signal quality check #

xcatx = [fin_dict["signal_name"], fin_dict["ret"]]
cidx = fin_dict["cidx"]
secname = fin_dict["sector_name"]

cr_fin = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=fin_dict["freq"],
    blacklist=fin_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_fin.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/ade056d48a5d9e58d478280c190141cc0977916710ad140f809fbf27e8f4a8bf.png
xcatx = [fin_dict["signal_name"]]
cidx = fin_dict["cidx"]
secname = fin_dict["sector_name"]
signal_name = fin_dict["signal_name"]
pnl_name = fin_dict["pnl_name"]

pnl_fin = msn.NaivePnL(
    df=dfx,
    ret=fin_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist=fin_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_fin.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale=None,
        thresh=2,
        pnl_name=pnl_name,
    )
pnl_fin.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_fin.plot_pnls(
    pnl_cats=pnl_fin.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14,
)

fin_dict["pnls"] = pnl_fin
pnl_fin.evaluate_pnls(pnl_cats=pnl_fin.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/885c90ebc04418421cd2252525d46749eb40ea164f8aa7e24f0afc64a6d611c2.png
xcat Financials learning-based signal Financials always long versus all-sector basket
Return % 11.51545 7.392016
St. Dev. % 37.608522 38.545653
Sharpe Ratio 0.306193 0.191773
Sortino Ratio 0.427123 0.280003
Max 21-Day Draw % -84.834901 -74.060105
Max 6-Month Draw % -86.011855 -88.216683
Peak to Trough Draw % -114.702183 -286.459232
Top 5% Monthly PnL Share 1.561329 2.514546
USD_EQXR_NSA correl -0.023998 0.231232
Traded Months 272 272
xcatx = fin_dict["signal_name"]
pnl_name = fin_dict["pnl_name"]
secname = fin_dict["sector_name"]

pnl_fin.signal_heatmap(
    pnl_name=pnl_name,
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/92a287db9833e396cc1b1f103295005e1003a8029c153611d2fc0fc5e6efad17.png

Technology #

Factor selection and signal generation #

sector = "ITE"

ite_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
xcatx = ite_dict["xcatx"] + [ite_dict["ret"]]
cidx = ite_dict["cidx"]

so_ite = msl.SignalOptimizer(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    blacklist=ite_dict["black"],
    freq=ite_dict["freq"],
    lag=1,
    xcat_aggs=["last", "sum"],
)
secname = ite_dict["sector_name"]
signal_name = ite_dict["signal_name"]

so_ite.calculate_predictions(
    name=signal_name,
    models=default_models,
    scorers=default_metric,
    hyperparameters=default_hparam_grid,
    inner_splitters=default_splitter,
    test_size=default_test_size,
    min_cids=default_min_cids,
    min_periods=default_min_periods,
    n_jobs_outer=-1,
    split_functions=default_split_functions,
)
so_ite.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_ite.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/3e3662703afc3f28a4ebce3efef4778e938f984cae380730b09c32fcd51ac49b.png
ite_importances = (
    so_ite.feature_importances.describe()
    .iloc[:, 1:]
    .sort_values(by="mean", axis=1, ascending=False)
)

ite_importances
CCSCORE_SA_WG_ZN COXR_VT10vWTI_21DMA_ZN BASEXINVCSCORE_SA_ZN CCSCORE_SA_ZN RYLDIRS05Y_NSA_ZN XCSTR_SA_P1M1ML12_3MMA_ZN XCPIC_SA_P1M1ML12_ZN XRWAGES_NSA_P1M1ML12_ZN CCSCORE_SA_D3M3ML3_ZN BMLCOCRY_SAVT10_21DMA_ZN ... XRGDPTECH_SA_P1M1ML12_3MMA_ZN XRPCONS_SA_P1M1ML12_3MMA_WG_ZN MBCSCORE_SA_WG_ZN XPCREDITBN_SJA_P1M1ML12_ZN XCPIE_SA_P1M1ML12_ZN XRPCONS_SA_P1M1ML12_3MMA_ZN UNEMPLRATE_NSA_3MMA_D1M1ML12_ZN XRGDPTECH_SA_P1M1ML12_3MMA_WG_ZN INTLIQGDP_NSA_D1M1ML6_ZN INTLIQGDP_NSA_D1M1ML1_ZN
count 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 ... 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000
mean 0.030921 0.024902 0.024040 0.023414 0.021785 0.020550 0.020358 0.019929 0.019926 0.019477 ... 0.015227 0.015215 0.015095 0.014871 0.014602 0.014487 0.014356 0.013653 0.013432 0.013226
min 0.016009 0.009102 0.000000 0.008516 0.006890 0.002840 0.005768 0.004753 0.008550 0.007371 ... 0.005971 0.004511 0.002975 0.000650 0.004877 0.000000 0.002598 0.001892 0.004719 0.003846
25% 0.023542 0.020639 0.021032 0.019215 0.016764 0.018041 0.018121 0.016866 0.017162 0.016752 ... 0.013436 0.013302 0.013118 0.013280 0.013165 0.013084 0.012756 0.011626 0.011413 0.011108
50% 0.028069 0.023315 0.023852 0.022548 0.020024 0.020540 0.020070 0.020029 0.018980 0.019022 ... 0.015013 0.015195 0.015093 0.015250 0.014861 0.014644 0.014483 0.013868 0.013088 0.012867
75% 0.035575 0.027544 0.027278 0.027101 0.024355 0.022468 0.022520 0.022909 0.021465 0.021250 ... 0.016943 0.016813 0.017147 0.016848 0.016403 0.016152 0.016183 0.015799 0.015165 0.015145
max 0.073474 0.082297 0.049635 0.044347 0.095593 0.035076 0.041827 0.033052 0.049851 0.072466 ... 0.027928 0.028264 0.025551 0.023855 0.023482 0.024011 0.028797 0.022465 0.042284 0.024515
std 0.010002 0.007640 0.005661 0.005542 0.008920 0.004055 0.004038 0.004610 0.004793 0.005943 ... 0.003060 0.002912 0.003270 0.003009 0.002925 0.003228 0.003057 0.003169 0.003762 0.003429

8 rows × 57 columns

xcatx = ite_dict["signal_name"]
secname = ite_dict["sector_name"]

so_ite.coefs_stackedbarplot(
    name=xcatx,
    ftrs=list(ite_importances.columns[:10]),
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of default random forest feature importances",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/41280a663c65a315297fbc17cdf9cd0609b9d60b91fe3fd7cba81b4b9a3fc1da.png

Signal quality check #

xcatx = [ite_dict["signal_name"], ite_dict["ret"]]
cidx = ite_dict["cidx"]
secname = ite_dict["sector_name"]

cr_ite = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=ite_dict["freq"],
    blacklist=ite_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_ite.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/f06e2b49fda2da02d6d7f197b9fd973fb175b6b77b22b6bf9c9acefe8b8c4199.png
xcatx = [ite_dict["signal_name"]]
cidx = ite_dict["cidx"]
secname = ite_dict["sector_name"]
signal_name = ite_dict["signal_name"]
pnl_name = ite_dict["pnl_name"]

pnl_ite = msn.NaivePnL(
    df=dfx,
    ret=ite_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist=ite_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_ite.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale=None,
        thresh=2,
        pnl_name=pnl_name,
    )
pnl_ite.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_ite.plot_pnls(
    pnl_cats=pnl_ite.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14,
)

ite_dict["pnls"] = pnl_ite
pnl_ite.evaluate_pnls(pnl_cats=pnl_ite.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/5f2f7b3ca0b9daa4f557eb4205deca3bae53e221eb8af08083d19f68bcd36729.png
xcat Information tech learning-based signal Information tech always long versus all-sector basket
Return % 11.798462 -7.509828
St. Dev. % 30.263784 38.747602
Sharpe Ratio 0.389854 -0.193814
Sortino Ratio 0.551278 -0.267657
Max 21-Day Draw % -45.002509 -45.904939
Max 6-Month Draw % -65.447658 -126.290197
Peak to Trough Draw % -107.84372 -402.943982
Top 5% Monthly PnL Share 1.296036 -2.157236
USD_EQXR_NSA correl -0.02682 0.044153
Traded Months 272 272
pnl_name = ite_dict["pnl_name"]
secname = ite_dict["sector_name"]

pnl_ite.signal_heatmap(
    pnl_name=pnl_name,
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/43119b8a45a4767d00b341094b8f520bf16c4a83fc3a150611f111e6bbfa2db7.png

Communication #

Factor selection and signal generation #

sector = "CSR"

csr_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
xcatx = csr_dict["xcatx"] + [csr_dict["ret"]]
cidx = csr_dict["cidx"]

so_csr = msl.SignalOptimizer(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    blacklist=csr_dict["black"],
    freq=csr_dict["freq"],
    lag=1,
    xcat_aggs=["last", "sum"],
)
secname = csr_dict["sector_name"]
signal_name = csr_dict["signal_name"]

so_csr.calculate_predictions(
    name=signal_name,
    models=default_models,
    scorers=default_metric,
    hyperparameters=default_hparam_grid,
    inner_splitters=default_splitter,
    test_size=default_test_size,
    min_cids=default_min_cids,
    min_periods=default_min_periods,
    n_jobs_outer=-1,
    split_functions=default_split_functions,
)
so_csr.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_csr.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/50d172cd754d2617f1a8fa0c00a24a6dbaf0fe129313dfd87fdc77015893cf53.png
csr_importances = (
    so_csr.feature_importances.describe()
    .iloc[:, 1:]
    .sort_values(by="mean", axis=1, ascending=False)
)

csr_importances
BMLCOCRY_SAVT10_21DMA_ZN BASEXINVCSCORE_SA_ZN COXR_VT10vWTI_21DMA_ZN BMLXINVCSCORE_SA_ZN REEROADJ_NSA_P1M12ML1_ZN CCSCORE_SA_D3M3ML3_ZN XCPIC_SA_P1M1ML12_ZN XCPIF_SA_P1M1ML12_WG_ZN CCSCORE_SA_WG_ZN REFIXINVCSCORE_SA_ZN ... XPCREDITBN_SJA_P1M1ML12_ZN XRRSALES_SA_P1M1ML12_3MMA_ZN XRRSALES_SA_P1M1ML12_3MMA_WG_ZN MBCSCORE_SA_D3M3ML3_WG_ZN RYLDIRS02Y_NSA_ZN MBCSCORE_SA_WG_ZN XRPCONS_SA_P1M1ML12_3MMA_ZN UNEMPLRATE_NSA_3MMA_D1M1ML12_WG_ZN INTLIQGDP_NSA_D1M1ML6_ZN INTLIQGDP_NSA_D1M1ML1_ZN
count 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 ... 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000
mean 0.032015 0.025424 0.024751 0.021888 0.020991 0.019489 0.019418 0.019236 0.018976 0.018796 ... 0.015570 0.015542 0.015467 0.015448 0.015253 0.015206 0.015059 0.014577 0.014364 0.013194
min 0.012726 0.002597 0.010056 0.000584 0.006100 0.008162 0.006923 0.005905 0.006410 0.003799 ... 0.003710 0.006378 0.007807 0.005079 0.002716 0.002861 0.004406 0.003306 0.001851 0.004221
25% 0.024561 0.020470 0.020050 0.019112 0.018824 0.017097 0.017651 0.017200 0.017193 0.016708 ... 0.013874 0.013556 0.013729 0.013731 0.013246 0.013380 0.013787 0.012899 0.012679 0.011051
50% 0.028585 0.024646 0.022727 0.021829 0.020700 0.018998 0.019362 0.019113 0.018902 0.018681 ... 0.015675 0.015808 0.015460 0.015645 0.015808 0.015403 0.015202 0.014683 0.014582 0.013001
75% 0.034667 0.030082 0.026603 0.024482 0.022988 0.020942 0.021154 0.020929 0.020512 0.020944 ... 0.017117 0.017236 0.016786 0.017311 0.017687 0.017014 0.016493 0.016066 0.016014 0.015093
max 0.090128 0.058819 0.058267 0.039654 0.047464 0.039351 0.031616 0.041377 0.039412 0.029689 ... 0.029411 0.028400 0.043804 0.024689 0.025586 0.037180 0.027165 0.026713 0.024789 0.029340
std 0.012423 0.008344 0.007553 0.005224 0.004171 0.003934 0.003252 0.003665 0.003496 0.004309 ... 0.003280 0.003230 0.003385 0.003182 0.003539 0.003462 0.002573 0.002909 0.003311 0.003281

8 rows × 57 columns

xcatx = csr_dict["signal_name"]
secname = csr_dict["sector_name"]

so_csr.coefs_stackedbarplot(
    name=xcatx,
    ftrs=list(csr_importances.columns[:10]),
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of default random forest feature importances",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/94be10a5fdacabd0a6df0b6dcaa3a2ae9f0317176d9b34f283d567d31c67cfa9.png

Signal quality check #

xcatx = [csr_dict["signal_name"], csr_dict["ret"]]
cidx = csr_dict["cidx"]
secname = csr_dict["sector_name"]

cr_csr = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=csr_dict["freq"],
    blacklist=csr_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_csr.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/c9caf4c7a3192b7f7e1c08be978ec3a88c462dda234fdf11b125bd62c91812bb.png
xcatx = [csr_dict["signal_name"]]
cidx = csr_dict["cidx"]
secname = csr_dict["sector_name"]
signal_name = csr_dict["signal_name"]
pnl_name = csr_dict["pnl_name"]

pnl_csr = msn.NaivePnL(
    df=dfx,
    ret=csr_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist=csr_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_csr.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale=None,
        thresh=2,
        pnl_name=pnl_name,
    )
pnl_csr.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_csr.plot_pnls(
    pnl_cats=pnl_csr.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14,
)

csr_dict["pnls"] = pnl_csr
pnl_csr.evaluate_pnls(pnl_cats=pnl_csr.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/605579afc47c55a54b5afe5a95edab552c134dd3d329d06eb19e72791815b11b.png
xcat Communication services learning-based signal Communication services always long versus all-sector basket
Return % 6.040625 -7.270927
St. Dev. % 30.71054 36.523768
Sharpe Ratio 0.196696 -0.199074
Sortino Ratio 0.27866 -0.284637
Max 21-Day Draw % -39.943496 -40.812664
Max 6-Month Draw % -57.259071 -108.893846
Peak to Trough Draw % -211.48857 -326.842257
Top 5% Monthly PnL Share 2.608879 -2.078456
USD_EQXR_NSA correl -0.014868 -0.040145
Traded Months 272 272
pnl_name = csr_dict["pnl_name"]
secname = csr_dict["sector_name"]

pnl_csr.signal_heatmap(
    pnl_name=pnl_name,
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/888cc8f34c1f85e7218d1b5cd45bde02f1089cc8d83cbafdbec624be058b81be.png

Utilities #

Factor selection and signal generation #

sector = "UTL"

utl_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
xcatx = utl_dict["xcatx"] + [utl_dict["ret"]]
cidx = utl_dict["cidx"]

so_utl = msl.SignalOptimizer(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    blacklist=utl_dict["black"],
    freq=utl_dict["freq"],
    lag=1,
    xcat_aggs=["last", "sum"],
)
secname = utl_dict["sector_name"]
signal_name = utl_dict["signal_name"]

so_utl.calculate_predictions(
    name=signal_name,
    models=default_models,
    scorers=default_metric,
    hyperparameters=default_hparam_grid,
    inner_splitters=default_splitter,
    test_size=default_test_size,
    min_cids=default_min_cids,
    min_periods=default_min_periods,
    n_jobs_outer=-1,
    split_functions=default_split_functions,
)
so_utl.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_utl.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/8bd54968644346eb984b49199d68ef9f828f0b844cc87dfc8eb7073a382a18dd.png
utl_importances = (
    so_utl.feature_importances.describe()
    .iloc[:, 1:]
    .sort_values(by="mean", axis=1, ascending=False)
)

utl_importances
COXR_VT10vWTI_21DMA_ZN XCPIC_SA_P1M1ML12_ZN BASEXINVCSCORE_SA_ZN REEROADJ_NSA_P1M12ML1_ZN XCSTR_SA_P1M1ML12_3MMA_ZN BMLXINVCSCORE_SA_ZN XGGDGDPRATIOX10_NSA_ZN CCSCORE_SA_D3M3ML3_ZN CCSCORE_SA_WG_ZN XIP_SA_P1M1ML12_3MMA_ZN ... XNRSALES_SA_P1M1ML12_3MMA_WG_ZN RYLDIRS02Y_NSA_ZN XPPIH_NSA_P1M1ML12_ZN XCPIE_SA_P1M1ML12_ZN XEMPL_NSA_P1M1ML12_3MMA_WG_ZN UNEMPLRATE_NSA_3MMA_D1M1ML12_WG_ZN XRGDPTECH_SA_P1M1ML12_3MMA_WG_ZN UNEMPLRATE_SA_3MMAv5YMA_WG_ZN INTLIQGDP_NSA_D1M1ML6_ZN INTLIQGDP_NSA_D1M1ML1_ZN
count 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 ... 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000 278.000000
mean 0.026260 0.024605 0.020739 0.020604 0.020334 0.020306 0.020293 0.020026 0.019974 0.018942 ... 0.015639 0.015622 0.015532 0.015372 0.015207 0.015124 0.015075 0.014710 0.013772 0.012325
min 0.008823 0.007566 0.003374 0.006614 0.008781 0.006018 0.008407 0.008143 0.008049 0.006894 ... 0.003987 0.002840 0.005702 0.007740 0.002892 0.005193 0.003064 0.000000 0.004792 0.005051
25% 0.021682 0.020662 0.017539 0.018400 0.017624 0.015310 0.017518 0.017205 0.017383 0.015928 ... 0.013340 0.013492 0.013525 0.013351 0.013050 0.012638 0.012489 0.012330 0.011716 0.010271
50% 0.025603 0.024027 0.020557 0.020331 0.019543 0.020434 0.020115 0.019315 0.019348 0.017912 ... 0.015317 0.015889 0.015358 0.015143 0.015349 0.015092 0.014363 0.014308 0.013610 0.012109
75% 0.029962 0.028373 0.023386 0.022790 0.022262 0.025289 0.022911 0.022283 0.022227 0.021148 ... 0.017236 0.018254 0.017147 0.016981 0.016946 0.017224 0.016778 0.017037 0.015522 0.013930
max 0.051112 0.045223 0.037886 0.038241 0.040151 0.035936 0.035751 0.042744 0.034117 0.035561 ... 0.031732 0.028811 0.029483 0.028654 0.026228 0.034438 0.031333 0.032387 0.026583 0.025523
std 0.006538 0.006110 0.005502 0.004234 0.004144 0.006406 0.004636 0.004356 0.004245 0.004746 ... 0.003780 0.004023 0.003360 0.003238 0.003339 0.003928 0.003959 0.003690 0.003140 0.003172

8 rows × 57 columns

xcatx = utl_dict["signal_name"]
secname = utl_dict["sector_name"]

so_utl.coefs_stackedbarplot(
    name=xcatx,
    ftrs=list(utl_importances.columns[:10]),
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of default random forest feature importances",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/13a30b1c6fda0a58cc4493dda9f13f9ce31c556820a3ac2984191b60d628e7fe.png

Signal quality check #

xcatx = [utl_dict["signal_name"], utl_dict["ret"]]
cidx = utl_dict["cidx"]
secname = utl_dict["sector_name"]

cr_utl = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=utl_dict["freq"],
    blacklist=utl_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_utl.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/20f8fa4f78e668b9fd2d8596eb16cdbcc9728f2f50100645416cbd6190c9ee82.png
xcatx = [utl_dict["signal_name"]]
cidx = utl_dict["cidx"]
secname = utl_dict["sector_name"]
signal_name = utl_dict["signal_name"]
pnl_name = utl_dict["pnl_name"]

pnl_utl = msn.NaivePnL(
    df=dfx,
    ret=utl_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist=utl_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_utl.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale=None,
        thresh=2,
        pnl_name=pnl_name,
    )
pnl_utl.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_utl.plot_pnls(
    pnl_cats=pnl_utl.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14,
)

utl_dict["pnls"] = pnl_utl
pnl_utl.evaluate_pnls(pnl_cats=pnl_utl.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/1ba30656045ce36b2aa523b7e751cc92a5e410d2d907492e9f3bbf45224a020f.png
xcat Utilities learning-based signal Utilities always long versus all-sector basket
Return % 16.704207 12.794011
St. Dev. % 32.815732 39.854214
Sharpe Ratio 0.50903 0.32102
Sortino Ratio 0.742895 0.470673
Max 21-Day Draw % -45.230702 -46.281163
Max 6-Month Draw % -61.05667 -95.617861
Peak to Trough Draw % -94.26615 -258.423263
Top 5% Monthly PnL Share 0.985236 1.473884
USD_EQXR_NSA correl -0.068346 -0.19322
Traded Months 272 272
pnl_name = utl_dict["pnl_name"]
secname = utl_dict["sector_name"]

pnl_utl.signal_heatmap(
    pnl_name=pnl_name,
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/fe5d49437702bd853631297787cc47711e88353548bdbf59850d9d5f37b3c3f8.png

Real estate #

Factor selection and signal generation #

sector = "REL"

rel_dict = {
    "sector_name": sector_labels[sector],
    "signal_name": f"{sector}SOL",
    "pnl_name": f"{sector_labels[sector]} learning-based signal",
    "xcatx": macroz,
    "cidx": cids_eq,
    "ret": f"EQC{sector}{default_target_type}",
    "freq": "M",
    "black": sector_blacklist[sector],
    "srr": None,
    "pnls": None,
}
xcatx = rel_dict["xcatx"] + [rel_dict["ret"]]
cidx = rel_dict["cidx"]

so_rel = msl.SignalOptimizer(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    blacklist=rel_dict["black"],
    freq=rel_dict["freq"],
    lag=1,
    xcat_aggs=["last", "sum"],
)
secname = rel_dict["sector_name"]
signal_name = rel_dict["signal_name"]

so_rel.calculate_predictions(
    name=signal_name,
    models=default_models,
    scorers=default_metric,
    hyperparameters=default_hparam_grid,
    inner_splitters=default_splitter,
    test_size=default_test_size,
    min_cids=default_min_cids,
    min_periods=default_min_periods,
    n_jobs_outer=-1,
    split_functions=default_split_functions,
)
so_rel.models_heatmap(
    signal_name,
    cap=10,
    title=f"{secname} sector: model selection heatmap",
)

# Store signals
dfa = so_rel.get_optimized_signals()
dfx = msm.update_df(dfx, dfa)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/f36238d12c3287ab58c2121630069048673ea56a25c3bd0facf020777dd034b4.png
rel_importances = (
    so_rel.feature_importances.describe()
    .iloc[:, 1:]
    .sort_values(by="mean", axis=1, ascending=False)
)

rel_importances
BMLCOCRY_SAVT10_21DMA_ZN RSLOPEMIDDLE_NSA_ZN COXR_VT10vWTI_21DMA_ZN SBCSCORE_SA_D3M3ML3_ZN BASEXINVCSCORE_SA_ZN REEROADJ_NSA_P1M12ML1_ZN CCSCORE_SA_D3M3ML3_ZN SBCSCORE_SA_D3M3ML3_WG_ZN CCSCORE_SA_D3M3ML3_WG_ZN BMLXINVCSCORE_SA_ZN ... XRRSALES_SA_P1M1ML12_3MMA_WG_ZN XEXPORTS_SA_P1M1ML12_3MMA_ZN XRPCONS_SA_P1M1ML12_3MMA_ZN XRGDPTECH_SA_P1M1ML12_3MMA_WG_ZN CTOT_NSA_P1M12ML1_ZN XPPIH_NSA_P1M1ML12_ZN UNEMPLRATE_NSA_3MMA_D1M1ML12_WG_ZN XIP_SA_P1M1ML12_3MMA_ZN XIP_SA_P1M1ML12_3MMA_WG_ZN INTLIQGDP_NSA_D1M1ML1_ZN
count 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 ... 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000 276.000000
mean 0.028239 0.025126 0.024010 0.021233 0.020700 0.020502 0.020481 0.019759 0.019335 0.019229 ... 0.015720 0.015288 0.015221 0.015153 0.015088 0.015078 0.014787 0.014742 0.014514 0.011093
min 0.004714 0.010863 0.011225 0.012110 0.006274 0.004373 0.008533 0.010933 0.010754 0.000758 ... 0.002803 0.006912 0.006999 0.002214 0.007112 0.006015 0.005467 0.007456 0.004190 0.004152
25% 0.023357 0.019808 0.020355 0.018393 0.017169 0.018271 0.018274 0.017631 0.017420 0.016084 ... 0.014005 0.013680 0.013356 0.013383 0.013426 0.013674 0.012822 0.012664 0.012629 0.009693
50% 0.027623 0.022941 0.023741 0.020228 0.020712 0.020282 0.020169 0.019294 0.018856 0.019747 ... 0.015659 0.015202 0.014843 0.015109 0.015005 0.015159 0.014705 0.014437 0.014484 0.011121
75% 0.033057 0.026174 0.027033 0.023399 0.023935 0.022510 0.022379 0.021558 0.020834 0.023085 ... 0.017192 0.017088 0.017077 0.016618 0.016302 0.016492 0.016540 0.016323 0.016367 0.012415
max 0.045024 0.066800 0.050676 0.039183 0.036715 0.032614 0.035695 0.033503 0.033785 0.031072 ... 0.026899 0.026260 0.029793 0.030923 0.027331 0.024520 0.032823 0.030211 0.027140 0.029127
std 0.007521 0.008860 0.005316 0.004399 0.005518 0.003609 0.003798 0.003365 0.003457 0.005324 ... 0.003090 0.002701 0.003053 0.003340 0.003012 0.002584 0.003053 0.003445 0.003174 0.002541

8 rows × 57 columns

xcatx = rel_dict["signal_name"]
secname = rel_dict["sector_name"]

so_rel.coefs_stackedbarplot(
    name=xcatx,
    ftrs=list(rel_importances.columns[:10]),
    ftrs_renamed=cat_label_dict,
    title=f"{secname} sector: annual averages of default random forest feature importances",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/6da5e6ccda4b3b14432816dfe121eff1cfde0c524241aa83138cd4ad02b5b78e.png

Signal quality check #

xcatx = [rel_dict["signal_name"], rel_dict["ret"]]
cidx = rel_dict["cidx"]
secname = rel_dict["sector_name"]

cr_rel = msp.CategoryRelations(
    df=dfx,
    xcats=xcatx,
    cids=cidx,
    freq=rel_dict["freq"],
    blacklist=rel_dict["black"],
    lag=1,
    xcat_aggs=["last", "sum"],
    slip=1,
)

cr_rel.reg_scatter(
    title=f"{secname} sector: learning-based signal and subsequent returns",
    labels=False,
    prob_est="map",
    xlab=f"{secname} signal, end-of-month, based on concurrent best model",
    ylab=f"Relative return of {secname.lower()} sector (vol-targeted), next month, %",
    coef_box="upper left",
    size=(12, 8),
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/fd4bcbeb035609b1f6327499eac3de45023f540b97cc88e2ccf8d7b0a141f0fe.png
xcatx = [rel_dict["signal_name"]]
cidx = rel_dict["cidx"]
secname = rel_dict["sector_name"]
signal_name = rel_dict["signal_name"]
pnl_name = rel_dict["pnl_name"]

pnl_rel = msn.NaivePnL(
    df=dfx,
    ret=rel_dict["ret"],
    sigs=xcatx,
    cids=cidx,
    start=default_start_date,
    blacklist=rel_dict["black"],
    bms=["USD_EQXR_NSA"],
)

for xcat in xcatx:
    pnl_rel.make_pnl(
        sig=xcat,
        sig_op="zn_score_pan",
        rebal_freq="monthly",
        neutral="zero",
        rebal_slip=1,
        vol_scale=None,
        thresh=3,
        pnl_name=pnl_name,
    )
pnl_rel.make_long_pnl(
    vol_scale=None, label=f"{secname} always long versus all-sector basket"
)

pnl_rel.plot_pnls(
    pnl_cats=pnl_rel.pnl_names,
    title=f"{secname} sector: naive PnLs of positions versus all-sector basket",
    title_fontsize=14,
)

rel_dict["pnls"] = pnl_rel
pnl_rel.evaluate_pnls(pnl_cats=pnl_rel.pnl_names)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/592af2f39f4d4cb308ef22f4ef3be72ba187be43c5a1e0a4459da56187e008e6.png
xcat Real estate learning-based signal Real estate always long versus all-sector basket
Return % 28.423107 26.809869
St. Dev. % 39.196377 43.259396
Sharpe Ratio 0.725146 0.619747
Sortino Ratio 1.033804 0.887918
Max 21-Day Draw % -124.704785 -105.055332
Max 6-Month Draw % -87.137843 -119.375748
Peak to Trough Draw % -190.999307 -239.236813
Top 5% Monthly PnL Share 0.857699 0.877659
USD_EQXR_NSA correl -0.032961 -0.049724
Traded Months 272 272
pnl_name = rel_dict["pnl_name"]
secname = rel_dict["sector_name"]

pnl_rel.signal_heatmap(
    pnl_name=pnl_name,
    figsize=(12, 3),
    title=f"{secname} sector: signal heatmap",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/23eed2b17b9fa269370e345116f03d4da5c727182efc626fa4eabdfebde0ddee.png

Summary #

Sector-specific signals and returns #

sec_catregs = {
    "enr": cr_enr,
    "mat": cr_mat,
    "ind": cr_ind,
    "cod": cr_cod, 
    "cos": cr_cos,
    "hlc": cr_hlc,
    "fin": cr_fin,
    "ite": cr_ite, 
    "csr": cr_csr,
    "utl": cr_utl, 
    "rel": cr_rel,
}


msv.multiple_reg_scatter(
    cat_rels=list(sec_catregs.values()),
    ncol=3,
    nrow=4,
    figsize=(15, 15),
    title="Random forest macro signals and subsequent sectoral equity returns, 11 currency areas, since 2003",
    title_xadj=0.5,
    title_yadj=0.99,
    title_fontsize=20,
    xlab="Sector-specific random forest regression signal based on macro-quantamental categories",
    ylab="Sector return versus equal weighted local index (all vol-targeted), next month %",
    coef_box="lower right",
    prob_est="map",
    single_chart=True,
    subplot_titles=[sector_labels[sector.upper()] for sector in sec_catregs.keys()],
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/711fe7062cc677c42f4d6eb534442e0675cc7bfd6815c5ac5ba6627c978a1c68.png

Combined cross-sector trading PnL #

sec_pnls = {
    "enr": pnl_enr,
    "mat": pnl_mat,
    "ind": pnl_ind,
    "cod": pnl_cod,
    "cos": pnl_cos,
    "hlc": pnl_hlc,
    "fin": pnl_fin,
    "ite": pnl_ite,
    "csr": pnl_csr,
    "utl": pnl_utl,
    "rel": pnl_rel,
}

ma_pnl = msn.MultiPnL()
for sec, pnl in sec_pnls.items():
    ma_pnl.add_pnl(
        pnl, pnl_xcats=[f"{sector_labels[sec.upper()]} learning-based signal"]
    )
ma_pnl.plot_pnls(
    pnl_xcats=[
        f"{sector_labels[sec.upper()]} learning-based signal" for sec in sec_pnls.keys()
    ],
    title="Naive PnLs for random-forest-based relative sector strategies",
    xcat_labels=[sector_labels[sec.upper()] for sec in sec_pnls.keys()],
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/d7a9435b0631b6ee7850ea3cbd0ada37c1e6f18e799fcbe14caeb36b613c73f3.png
cpname = "Simple average PnL of relative sector strategies based on machine learning and macro signals"

macro_sector_pnl = ma_pnl.combine_pnls(
    pnl_xcats=[
        f"{sector_labels[sec.upper()]} learning-based signal" for sec in sec_pnls.keys()
    ],
    composite_pnl_xcat=cpname,
    weights=None,
)
ma_pnl.plot_pnls(
    [cpname],
    title="Cumulative naive PnL value of random forest-based cross-sectoral equity allocation",
)
https://macrosynergy.com/notebooks.build/data-science/using-random-forests-to-create-equity-trading-signals/_images/649f922dd978c263b99ce9ed806f108ea31720166dcfc5f49ee98082383bd74b.png
tbr = ma_pnl.evaluate_pnls()
tbr = tbr.rename(
    columns={
        **{
            f"{sector_labels[sec.upper()]} learning-based signal/EQC{sec.upper()}R_VT10vALL": f"{sector_labels[sec.upper()]}"
            for sec in sec_pnls.keys()
        },
        **{
            "Simple average PnL of relative sector strategies based on machine learning and macro signals": "Simple average"
        },
    }
)

# selected_rows = ["Return %", "St. Dev. %", "Sharpe Ratio", "Sortino Ratio", "USD_EQXR_NSA correl"]
selected_columns = ["Simple average"] + [
    sector_labels[sec.upper()] for sec in sec_pnls.keys()
]
selected_pnl_stats = tbr.loc[:, selected_columns].T
selected_pnl_stats.columns

cols = [
    "Sharpe Ratio",
    "Sortino Ratio",
    "Top 5% Monthly PnL Share",
    "USD_EQXR_NSA correl",
]
display(selected_pnl_stats[cols].style.format("{:.2f}"))
Sharpe Ratio Sortino Ratio Top 5% Monthly PnL Share USD_EQXR_NSA correl
Simple average 1.18 1.70 0.42 nan
Energy 0.67 0.96 0.66 -0.08
Materials 0.39 0.55 1.21 -0.00
Industrials 0.34 0.50 1.38 0.00
Cons. discretionary 0.12 0.17 3.30 -0.02
Cons. staples 0.32 0.45 1.60 -0.03
Healthcare 0.52 0.75 0.83 0.03
Financials 0.31 0.43 1.56 -0.02
Information tech 0.39 0.55 1.30 -0.03
Communication services 0.20 0.28 2.61 -0.01
Utilities 0.51 0.74 0.99 -0.07
Real estate 0.73 1.03 0.86 -0.03

Appendix #

Appendix 1 - Macro quantamental indicators description #

# Convert the dictionary to an HTML table with custom inline CSS
html_table = cat_labels.to_html(index=True, table_id="custom_table")

# Inject CSS to align text to the left and reduce font size
css = """
<style>
#custom_table th, #custom_table td {
    text-align: left;
    font-size: 12px; /* Adjust the font size as needed */
}
</style>
"""
# Display the styled HTML table
HTML(css + html_table)
Label Description Geography
Group Category
Business surveys CBCSCORE_SA_D3M3ML3_WG_ZN Construction confidence, q/q Construction business confidence score, seas. adjusted, change q/q weighted
CBCSCORE_SA_D3M3ML3_ZN Construction confidence, q/q Construction business confidence score, seas. adjusted, change q/q local
CBCSCORE_SA_WG_ZN Construction confidence Construction business confidence score, seas. adjusted weighted
CBCSCORE_SA_ZN Construction confidence Construction business confidence score, seas. adjusted local
MBCSCORE_SA_D3M3ML3_WG_ZN Manufacturing confidence, q/q Manufacturing business confidence score, seas. adj., change q/q weighted
MBCSCORE_SA_D3M3ML3_ZN Manufacturing confidence, q/q Manufacturing business confidence score, seas. adj., change q/q local
MBCSCORE_SA_WG_ZN Manufacturing confidence Manufacturing business confidence score, seasonally adjusted weighted
MBCSCORE_SA_ZN Manufacturing confidence Manufacturing business confidence score, seasonally adjusted local
SBCSCORE_SA_D3M3ML3_WG_ZN Service confidence, q/q Services business confidence score, seas. adjusted, change q/q weighted
SBCSCORE_SA_D3M3ML3_ZN Service confidence, q/q Services business confidence score, seas. adjusted, change q/q local
SBCSCORE_SA_WG_ZN Service confidence Services business confidence score, seasonally adjusted weighted
SBCSCORE_SA_ZN Service confidence Services business confidence score, seasonally adjusted local
Commodity inventories BASEXINVCSCORE_SA_ZN Excess crude inventory score Crude oil excess inventory z-score, seasonally adjusted global
BMLXINVCSCORE_SA_ZN Excess metal inventory score Base metal excess inventory z-score, seasonally adjusted global
REFIXINVCSCORE_SA_ZN Excess refined oil inventory score Refined oil product excess inventory z-score, seas. adjusted global
Debt CORPINTNETGDP_SA_D1Q1QL4_WG_ZN Corporate debt servicing, %oya Corporate net debt servicing-to-GDP ratio, seasonally-adjusted, %oya weighted
CORPINTNETGDP_SA_D1Q1QL4_ZN Corporate debt servicing, %oya Corporate net debt servicing-to-GDP ratio, seasonally-adjusted, %oya local
HHINTNETGDP_SA_D1M1ML12_WG_ZN Households debt servicing, %oya Households net debt servicing-to-GDP ratio, seasonally-adjusted, %oya weighted
HHINTNETGDP_SA_D1M1ML12_ZN Households debt servicing, %oya Households net debt servicing-to-GDP ratio, seasonally-adjusted, %oya local
XGGDGDPRATIOX10_NSA_ZN Excess projected gov. debt Government debt-to-GDP ratio proj. in 10 years, in excess of 100% local
Exports XEXPORTS_SA_P1M1ML12_3MMA_ZN Excess export growth Exports growth, %oya, 3mma, in excess of 5-year median GDP growth local
Inflation - broad XCPIC_SA_P1M1ML12_ZN Excess core CPI, %oya Core CPI, %oya, in excess of effective inflation target local
XCPIH_SA_P1M1ML12_ZN Excess headline CPI, %oya Headline CPI, %oya, in excess of effective inflation target local
XPPIH_NSA_P1M1ML12_ZN Excess PPI, %oya Producer price inflation, %oya, in excess of eff. inflation target local
Inflation - specific XCPIE_SA_P1M1ML12_WG_ZN Excess energy CPI, %oya Energy CPI, %oya, in excess of effective inflation target weighted
XCPIE_SA_P1M1ML12_ZN Excess energy CPI, %oya Energy CPI, %oya, in excess of effective inflation target local
XCPIF_SA_P1M1ML12_WG_ZN Excess food CPI, %oya Food CPI, %oya, in excess of effective inflation target weighted
XCPIF_SA_P1M1ML12_ZN Excess food CPI, %oya Food CPI, %oya, in excess of effective inflation target local
Labour market UNEMPLRATE_NSA_3MMA_D1M1ML12_WG_ZN Unemployment rate, diff oya Unemployment rate, change oya weighted
UNEMPLRATE_NSA_3MMA_D1M1ML12_ZN Unemployment rate, diff oya Unemployment rate, change oya local
UNEMPLRATE_SA_3MMAv5YMA_WG_ZN Unemployment rate, diff vs 5yma Unemployment rate, difference vs 5-year moving average weighted
UNEMPLRATE_SA_3MMAv5YMA_ZN Unemployment rate, diff vs 5yma Unemployment rate, difference vs 5-year moving average local
XEMPL_NSA_P1M1ML12_3MMA_WG_ZN Excess employment growth Employment growth, %oya, 3mma, in excess of population growth weighted
XEMPL_NSA_P1M1ML12_3MMA_ZN Excess employment growth Employment growth, %oya, 3mma, in excess of population growth local
XRWAGES_NSA_P1M1ML12_ZN Excess real wage growth Real wage growth, %oya, in excess of medium-term productivity growth local
Market metrics BMLCOCRY_SAVT10_21DMA_ZN Base metals carry Nominal carry for base metals basket, seasonally and vol-adjusted, 21 days moving average global
COXR_VT10vWTI_21DMA_ZN Refined vs crude oil returns Refined oil products vs crude oil vol-targeted return differential, 21 days moving average global
RIR_NSA_ZN Real 1-month rate Real 1-month interest rate local
RSLOPEMIDDLE_NSA_ZN Real 5y-2y yield Real IRS yield differentials, 5-years versus 2-years local
RYLDIRS02Y_NSA_ZN Real 2-year yield Real 2-year IRS yield local
RYLDIRS05Y_NSA_ZN Real 5-year yield Real 5-year IRS yield local
Output growth XCSTR_SA_P1M1ML12_3MMA_WG_ZN Excess construction growth Construction output, %oya, 3mma, in excess of 5-y median GDP growth weighted
XCSTR_SA_P1M1ML12_3MMA_ZN Excess construction growth Construction output, %oya, 3mma, in excess of 5-y median GDP growth local
XIP_SA_P1M1ML12_3MMA_WG_ZN Excess industry growth Industrial output, %oya, 3mma, in excess of 5-y median GDP growth weighted
XIP_SA_P1M1ML12_3MMA_ZN Excess industry growth Industrial output, %oya, 3mma, in excess of 5-y median GDP growth local
XRGDPTECH_SA_P1M1ML12_3MMA_WG_ZN Excess GDP growth Real GDP, %oya, 3mma, using HF data, in excess of 5-y med. GDP growth weighted
XRGDPTECH_SA_P1M1ML12_3MMA_ZN Excess GDP growth Real GDP, %oya, 3mma, using HF data, in excess of 5-y med. GDP growth local
Private consumption CCSCORE_SA_D3M3ML3_WG_ZN Consumer confidence, q/q Consumer confidence score, seasonally adjusted, change q/q weighted
CCSCORE_SA_D3M3ML3_ZN Consumer confidence, q/q Consumer confidence score, seasonally adjusted, change q/q local
CCSCORE_SA_WG_ZN Consumer confidence Consumer confidence score, seasonally adjusted weighted
CCSCORE_SA_ZN Consumer confidence Consumer confidence score, seasonally adjusted local
XNRSALES_SA_P1M1ML12_3MMA_WG_ZN Excess retail sales growth Nominal retail sales, %oya, 3mma, in excess of 5-y median GDP growth weighted
XNRSALES_SA_P1M1ML12_3MMA_ZN Excess retail sales growth Nominal retail sales, %oya, 3mma, in excess of 5-y median GDP growth local
XRPCONS_SA_P1M1ML12_3MMA_WG_ZN Excess consumption growth Real private consumption, %oya, 3mma, in excess of 5-y median GDP growth weighted
XRPCONS_SA_P1M1ML12_3MMA_ZN Excess real consum growth Real private consumption, %oya, 3mma, in excess of 5-y median GDP growth local
XRRSALES_SA_P1M1ML12_3MMA_WG_ZN Excess real retail growth Real retail sales, %oya, 3mma, in excess of 5-y median GDP growth weighted
XRRSALES_SA_P1M1ML12_3MMA_ZN Excess real retail growth Real retail sales, %oya, 3mma, in excess of 5-y median GDP growth local
Private credit INTLIQGDP_NSA_D1M1ML1_ZN Intervention liquidity, diff m/m Intervention liquidity to GDP ratio, change over the last month local
INTLIQGDP_NSA_D1M1ML6_ZN Intervention liquidity, diff 6m Intervention liquidity to GDP ratio, change overlast 6 months local
XPCREDITBN_SJA_P1M1ML12_WG_ZN Excess credit growth Private credit, %oya, 3mma, in excess of 5-y median GDP growth weighted
XPCREDITBN_SJA_P1M1ML12_ZN Excess credit growth Private credit, %oya, 3mma, in excess of 5-y median GDP growth local
Real appreciation CMPI_NSA_P1M12ML1_ZN Import prices, %oya Commodity-based import price index, %oya local
CTOT_NSA_P1M12ML1_ZN Terms-of-trade, %oya Commodity-based terms-of-trade, %oya local
CXPI_NSA_P1M12ML1_ZN Export prices, %oya Commodity-based export price index, %oya local
REEROADJ_NSA_P1M12ML1_ZN Open-adj REER, %oya Openness-adjusted real effective exchange rate, %oya local