Overall Statistics
class fading_the_gap(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2018, 7, 19)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        # self.AddEquity("TSLA", Resolution.Minute)


        # Create an event to run every day 0-minute after the close
from sklearn import linear_model
import numpy as np
import pandas as pd 
from scipy import stats
from math import floor 
from datetime import timedelta 

class PairsTradingAlgo(QCAlgorithm):
    
    def Initialize(self):
        
        self.SetStartDate(2018,1,1)
        self.SetEndDate(2020,1,20)
        self.SetCash(10000)
        
        self.numdays = 250   # set length of the training period 
        tickers = ["AMD","ADBE"]
        self.symbols = []
        
        self.threshold = 1.
        for i in tickers :
            self.symbols.append(self.AddSecurity(SecurityType.Equity , i , Resolution.Daily).Symbol)
            for i in self.symbols:
                i.hist_window = RollingWindow[Tradebar](self.numdays)
                
                
                
        
        
        
    def OnData(self, data):
        
        
        if not (data.ContainsKey("AMD") and data.ContainsKey("ADBE")): return
    
        for symbol in self.symbol():
            symbol.hist_window.Add(data[symbol])
            
            
        price_x = pd.Series([float(i.Close) for i in self.symbols[0].hist_window],
                             index = [i.Time for i in self.symbols[0].hist_window])
        
        
        price_y = pd.Series([float(i.Close) for i in self.symbols[1].hist_window],
                             index = [i.Time for i in self.symbols[1].hist_window])
                             
                             
        if len (price_x) < 250: return
        spread = self.regr(np.log(price_x), np.log(price_y))    
        #try later the rolling version and see if you can get a better results  
        mean = np.mean(spread)
        std = np.std(spread)
        # >>> s = pd.Series([5, 5, 6, 7, 5, 5, 5])
        # >>> s.rolling(3).std()
        
        ratio = floor(self.Portfolio[self.symbols[1]].Price / self.Portfolio[self.symbols[0]].Price)
        # quantity = float(self.CalculateOrderQuantity(self.symbols[0] , 0.4)
        
        # upper threshold i.e. exit and entry signals 
        if spread [-1] > mean + self.threshold * std:
            if not self.Portfolio[self.symbols[0]].Quantity > 0 and not self.Portfolio[self.symbols[0]].Quantity < 0:
                self.Sell(self.symbols[1], 100)
                self.Buy(self.symbols[0], ratio * 100)
                
        elif spread [-1] < mean - self.threshold * std:
            if not self.Portfolio[self.symbols[0]].Quantity < 0 and not self.Portfolio[self.symbols[0]].Quantity > 0:
                self.Sell(self.symbols[0], 100)
                self.Buy(self.symbols[1], ratio * std)
        
        else:
            self.Liquidate()
            
        
        
        
    
            
                             
                            
    
    

# Your New Python File