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
-1.544
Tracking Error
0.153
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
# Stock-Bond price correlation

from scipy.stats.stats import pearsonr

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

class StockBondCorrelation(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2020, 3, 25)
        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_price = self.History(self.stock, PERIOD + 1, Resolution.Daily)["close"][:-1]
        bond_price = self.History(self.bond , PERIOD + 1, Resolution.Daily)["close"][:-1]
        
        if len(stock_price) != len(bond_price): return

        correl = pearsonr(stock_price, bond_price)
        
        self.Plot('Correl', 'Stock-Bond price correlation', correl[0])