Overall Statistics
Total Trades
26
Average Win
0.54%
Average Loss
0%
Compounding Annual Return
62.603%
Drawdown
1.000%
Expectancy
0
Net Profit
8.271%
Sharpe Ratio
9.166
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.198
Beta
0.675
Annual Standard Deviation
0.053
Annual Variance
0.003
Information Ratio
1.496
Tracking Error
0.039
Treynor Ratio
0.726
Total Fees
$41.82
import numpy as np

class BasicTemplateAlgorithm(QCAlgorithm):

    def Initialize(self):
        
        self.SetCash(100000)
        self.SetStartDate(2017,1,1)
        self.SetEndDate(2017,3,1)
        
        
        self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol
        self.qqq = self.AddEquity("QQQ", Resolution.Minute).Symbol
        self.stock_list = [self.spy, self.qqq]
        self.hold_day={} # Make an empty dictionary to store holding days
        
        self.Schedule.On(self.DateRules.EveryDay(),
        self.TimeRules.AfterMarketOpen(self.spy, 15), 
        Action(self.rebalance))
        
        
    def rebalance(self):
        
        self.check_days(self.stock_list) # deploy 'check_days()' function
        
        
        for stock in self.stock_list:
            if not self.Portfolio[stock].Invested:
                self.SetHoldings(stock, 0.5)
                
                self.hold_day[stock] = 0 # Add stock and 0 days to the dictionary
    
            if self.Portfolio[stock].Invested:
                if self.hold_day[stock] ==5:
                    self.Liquidate(stock)
                    self.hold_day[stock] = -1 # make days -1 if sold

    def check_days(self, stock_list):
        
        for stock in stock_list:
            if self.Portfolio[stock].Invested:
                self.hold_day[stock] += 1 # Increment on each holding stock by 1 day