Overall Statistics
Total Trades
116
Average Win
0.26%
Average Loss
0%
Compounding Annual Return
4.499%
Drawdown
11.200%
Expectancy
0
Net Profit
5.265%
Sharpe Ratio
0.323
Probabilistic Sharpe Ratio
18.806%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
-0.016
Beta
0.738
Annual Standard Deviation
0.118
Annual Variance
0.014
Information Ratio
-0.451
Tracking Error
0.077
Treynor Ratio
0.052
Total Fees
$116.19
Estimated Strategy Capacity
$83000000.00
Lowest Capacity Asset
MSFT R735QTJ8XC9X
#region imports
from AlgorithmImports import *
#endregion
from datetime import datetime
from datetime import timedelta

class BasicTemplateAlgorithm(QCAlgorithm):

    def Initialize(self):
     
        self.SetStartDate(2021,2,20)
        self.SetCash(100000)
        self.ratio = 0.25
        self.Data_Symbol = {}
        tickers = ["SPY", "AAPL", "MSFT", "AMZN"]
        #tickers = ["SPY"]

        self.SetWarmUp(30, Resolution.Daily)                
                        
        for stock in tickers:
            symbol = self.AddEquity(stock, Resolution.Minute).Symbol
            self.Data_Symbol[symbol] = SymbolData(self, symbol)
            
        ## Dictionary for whether symbol was traded; we will reset this every day
        self.symbolTraded = { symbol: False for symbol in self.Data_Symbol.keys() }    
        
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.BeforeMarketClose('SPY', 0), self.ResetSymbolTraded)
        
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.Every(timedelta(minutes=1)), self.EveryDayAfterMarketOpen)


        
    def EveryDayAfterMarketOpen(self):
        if self.IsWarmingUp: return
        
        for symbol, symbol_data in self.Data_Symbol.items():
            if not symbol_data.cci.IsReady: continue

            holdings = self.Portfolio[symbol]
            invested = holdings.Invested
            nowprice = holdings.Price
            aveprice = holdings.AveragePrice
            quantity = holdings.Quantity
            
            cci = symbol_data.cci.Current.Value
            
            ## Check whether the symbol was traded
            #sold_today = self.symbolTraded[symbol]
        
            #Buy symbol only if if CCI < 100 and that symbol was not sold today
            if (not invested) and (cci < -100) and not self.symbolTraded[symbol]:
                #self.MarketOrder(symbol, 1)
                self.SetHoldings(symbol, self.ratio)
        
                ## Note: Assuming if this triggers you want to stop the buy signal?
                ## Then add a `self.symbolTraded[symbol] = True` here
                self.symbolTraded[symbol] = True
        
                self.Debug (symbol)
                self.Debug (self.Time.date)
                
                what_to_buy = (symbol)
                when_to_buy = (self.Time.date)
                tobuy = what_to_buy and when_to_buy
                    
            if invested and (nowprice > aveprice * 1.01):
                #self.MarketOrder (symbol, (-1 * quantity))
                self.Liquidate(symbol)
                self.symbolTraded[symbol] = True
                self.Debug (symbol)
                self.Debug (self.Time.date)
                #sold_symbol = (symbol)
                when_sold = (self.Time.date)
                #sold_today = sold_symbol and when_sold
                
                

    
    def ResetSymbolTraded(self):
        ## We reset this to False at end of each day
        self.symbolTraded = { symbol: False for symbol in self.Data_Symbol.keys() }    
         
class SymbolData:
    def __init__ (self,algo,symbol):
        self.algorithm = algo
        self.symbol = symbol
       
        #CCI Functions
        self.cci = algo.CCI(symbol, 14, MovingAverageType.Simple, Resolution.Daily)