Overall Statistics
Total Trades
1741
Average Win
0.21%
Average Loss
-0.19%
Compounding Annual Return
29.137%
Drawdown
10.600%
Expectancy
0.095
Net Profit
14.554%
Sharpe Ratio
1.201
Probabilistic Sharpe Ratio
53.269%
Loss Rate
49%
Win Rate
51%
Profit-Loss Ratio
1.15
Alpha
0.219
Beta
-0.115
Annual Standard Deviation
0.176
Annual Variance
0.031
Information Ratio
0.328
Tracking Error
0.444
Treynor Ratio
-1.845
Total Fees
$374.20
from datetime import timedelta, datetime

class SMAPairsTrading(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 1, 1)   
        self.SetEndDate(2020, 7, 31)
        self.SetCash(1000)
        
        self.SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Margin)
        
        symbols = [Symbol.Create("BTCUSD", SecurityType.Crypto, Market.Bitfinex ), 
                   Symbol.Create("ETHUSD", SecurityType.Crypto, Market.Bitfinex)]
        self.AddUniverseSelection(ManualUniverseSelectionModel(symbols))
        self.UniverseSettings.Resolution = Resolution.Hour
        self.UniverseSettings.DataNormalizationMode = DataNormalizationMode.Raw
        
        self.AddAlpha(PairsTradingAlphaModel())
        
        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
        self.SetExecution(ImmediateExecutionModel())

class PairsTradingAlphaModel(AlphaModel):

    def __init__(self):
        self.pair = [ ]
        self.spreadMean = SimpleMovingAverage(500)
        self.spreadStd = StandardDeviation(500)
        self.period = timedelta(minutes=30)
        
    def Update(self, algorithm, data):
        spread = self.pair[1].Price - self.pair[0].Price
        self.spreadMean.Update(algorithm.Time, spread)
        self.spreadStd.Update(algorithm.Time, spread) 
        
        upperthreshold = self.spreadMean.Current.Value + self.spreadStd.Current.Value
        lowerthreshold = self.spreadMean.Current.Value - self.spreadStd.Current.Value

        if spread > upperthreshold:
            return Insight.Group(
                [
                    Insight.Price(self.pair[0].Symbol, self.period, InsightDirection.Up),
                    Insight.Price(self.pair[1].Symbol, self.period, InsightDirection.Down)
                ])
        
        if spread < lowerthreshold:
            return Insight.Group(
                [
                    Insight.Price(self.pair[0].Symbol, self.period, InsightDirection.Down),
                    Insight.Price(self.pair[1].Symbol, self.period, InsightDirection.Up)
                ])

        return []
    
    def OnSecuritiesChanged(self, algorithm, changes):
        self.pair = [x for x in changes.AddedSecurities]
        
        #1. Call for 500 bars of history data for each symbol in the pair and save to the variable history
        history = algorithm.History([x.Symbol for x in self.pair], 500)
        #2. Unstack the Pandas data frame to reduce it to the history close price
        history = history.close.unstack(level=0)
        #3. Iterate through the history tuple and update the mean and standard deviation with historical data 
        for tuple in history.itertuples():
            self.spreadMean.Update(tuple[0], tuple[2]-tuple[1])
            self.spreadStd.Update(tuple[0], tuple[2]-tuple[1])