Overall Statistics
Total Trades
8235
Average Win
0.28%
Average Loss
-0.19%
Compounding Annual Return
28.019%
Drawdown
10.300%
Expectancy
0.059
Net Profit
85.071%
Sharpe Ratio
2.008
Probabilistic Sharpe Ratio
96.672%
Loss Rate
56%
Win Rate
44%
Profit-Loss Ratio
1.43
Alpha
0.192
Beta
-0.002
Annual Standard Deviation
0.095
Annual Variance
0.009
Information Ratio
0.468
Tracking Error
0.232
Treynor Ratio
-86.789
Total Fees
$96183.80
Estimated Strategy Capacity
$540000.00
Lowest Capacity Asset
CDN R735QTJ8XC9X
# Bollinger Bands of spread pair trading

from AlgorithmImports import *
# ----------------------------
BB_PERIOD = 5; STD_MULT = 0.1;
# ----------------------------
class BollingerBandsOfSpreadPairTrading(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 1, 1)
        self.SetEndDate(2022, 6, 28)
        self.SetCash(1000000)
        res = Resolution.Hour
        self.stock1 = self.AddEquity("SNPS", res).Symbol   
        self.stock2 = self.AddEquity("CDNS", res).Symbol 
        i1 = self.Identity(self.stock1, res)
        i2 = self.Identity(self.stock2, res)
        self.spread = IndicatorExtensions.Minus(i1, i2)
        bb = BollingerBands(BB_PERIOD, STD_MULT, MovingAverageType.Simple)
        self.bb_spread = IndicatorExtensions.Of(bb, self.spread)
        self.SetWarmUp(BB_PERIOD, res)
        

    def OnData(self, data): 
        if self.IsWarmingUp: return
        if not (self.spread.IsReady and self.bb_spread.IsReady): return

        if self.spread.Current.Value >= self.bb_spread.UpperBand.Current.Value:
            self.SetHoldings(self.stock1, -0.49)
            self.SetHoldings(self.stock2, 0.49)

        elif self.spread.Current.Value < self.bb_spread.LowerBand.Current.Value:
            self.SetHoldings(self.stock1, 0.49)
            self.SetHoldings(self.stock2, -0.49) 

        self.Plot("spread", "spread", self.spread.Current.Value)
        self.Plot("spread", "bb_ub", self.bb_spread.UpperBand.Current.Value)
        self.Plot("spread", "bb_lb", self.bb_spread.LowerBand.Current.Value)