Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-0.463
Tracking Error
0.169
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
#region imports
from AlgorithmImports import *
#endregion
# Stock-Bond Correlation

from scipy.stats.stats import pearsonr

# ---------------------------------------
STOCK = 'SPY'; BOND = 'TLT'; PERIOD = 21;
# ---------------------------------------

class StockBondCorrelation(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2008, 2, 1)
     #   self.SetEndDate(2022, 3, 25)
        self.SetCash(100000)
        self.stock = self.AddEquity(STOCK, Resolution.Daily).Symbol
        self.bond = self.AddEquity(BOND, Resolution.Daily).Symbol
        self.SetWarmUp(PERIOD + 1, Resolution.Daily)
        
        
    def OnData(self, data):
        if self.IsWarmingUp: return
        
        stock_roc = self.History(self.stock, PERIOD + 1, Resolution.Daily)["close"].pct_change()[1:-1]
        bond_roc = self.History(self.bond , PERIOD + 1, Resolution.Daily)["close"].pct_change()[1:-1]
        
        if len(stock_roc) != len(bond_roc): return

        correl = pearsonr(stock_roc, bond_roc)[0]
        
        self.Plot('Correl', 'Stock-Bond Correlation', correl)