Overall Statistics
Total Trades
32
Average Win
0.25%
Average Loss
0%
Compounding Annual Return
-1.000%
Drawdown
6.600%
Expectancy
0
Net Profit
-1.906%
Sharpe Ratio
-0.17
Probabilistic Sharpe Ratio
2.563%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
-0.012
Beta
0.211
Annual Standard Deviation
0.037
Annual Variance
0.001
Information Ratio
-0.246
Tracking Error
0.13
Treynor Ratio
-0.03
Total Fees
$160.00
Estimated Strategy Capacity
$870000000.00
Lowest Capacity Asset
AAPL 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(5000000)
        self.Data_Symbol = {}
        tickers = ["SPY", "AAPL", "MSFT", "AMZN", ]

        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 we can trade this symbol; we will reset this at end of each day
        self.canTradeSymbol = { symbol: True 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
            OpenOrders = self.Transactions.GetOpenOrders(symbol)
            
            cci = symbol_data.cci.Current.Value
            
            close = symbol_data.close.Current.Value
            openprice = symbol_data.open.Current.Value
            high = symbol_data.high.Current.Value
            low = symbol_data.low.Current.Value
            
            # Check whether we can trade this symbol
            can_trade = self.canTradeSymbol[symbol]
            if not can_trade:
                continue
            if OpenOrders: return  #Store only 1 order per ticker     
            #Buy symbol if CCI < 100
            if (not invested) and (cci < -100) and nowprice > openprice:
                self.MarketOrder(symbol, 1000)
                self.Debug(f'Bought {symbol} at {self.Time}')
                
            # Sell symbol        
            if invested and (nowprice > aveprice * 1.05):
                self.MarketOrder (symbol, (-1 * quantity))
                self.Debug(f'Liquidated {symbol} at {self.Time}.')
                
                # Now after a buy/sell, we prevent future buys for the rest of the day
                self.canTradeSymbol[symbol] = False

    def ResetSymbolTraded(self):
        ## We reset this to Trade at end of each day
        self.canTradeSymbol = { symbol: True 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)
        
        self.close = algo.Identity(symbol, Resolution.Daily,Field.Close)
        self.open = algo.Identity(symbol, Resolution.Daily,Field.Open)
        self.high = algo.Identity(symbol, Resolution.Daily,Field.High)
        self.low = algo.Identity(symbol, Resolution.Daily,Field.Low)