Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
#region imports
from AlgorithmImports import *
#endregion
class ParticleTransdimensionalAutosequencers(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 1, 21)
        self.SetEndDate(2020, 1, 22)
        self.SetCash(100000) 
        self.AddEquity("SPY", Resolution.Hour, extendedMarketHours=True)
        
        self.AddUniverseSelection(CoarseFundamentalUniverseSelectionModel(self.CoarseSelectionFunction))
        self.UniverseSettings.Resolution = Resolution.Hour
        self.UniverseSettings.extendedMarketHours = True

        self.Schedule.On(
            self.DateRules.EveryDay("SPY"),
            self.TimeRules.At(9, 30),
            self.SelectUniverse
        )
        
        self.universe = []
        self.volume_by_symbol = {}
        
        self.logged = False
        
    def OnData(self, data):
        if data.ContainsKey( 'SPY' ):
            if not self.Securities['SPY'].Exchange.Hours.IsOpen(self.Time, self.Time + timedelta(hours=1), False):
                self.Log( f'Extended Hours/ {self.Time}')
                spy_vol = self.Securities['SPY'].Volume
                self.Log(f'volume traded is {spy_vol}')
            else:
                self.Log( f'Market/ {self.Time}')    

        if len(self.volume_by_symbol) == 0:
            
            if not self.logged:
                self.logged = True
                self.Log(f"Universe size after volume filter: {len(self.universe)}")
                for symbol in self.universe:
                    self.MarketOrder(symbol,10,True)
            return 
        
        for symbol in self.volume_by_symbol.keys():
            if data.ContainsKey(symbol):
                self.volume_by_symbol[symbol] += data[symbol].Volume
    
    def CoarseSelectionFunction(self, coarse):
        self.volume_by_symbol = {c.Symbol: 0 for c in coarse if c.Price > 400}
        
        self.Log(f"Universe size before volume filter: {len(self.volume_by_symbol)}")
        
        return list(self.volume_by_symbol.keys())
        
    def SelectUniverse(self):
        self.universe = []
        first_symbol = list(self.volume_by_symbol.keys())[0]
        first_vol = list(self.volume_by_symbol.values())[0]
        self.Log(f"The cumulative volume for the {first_symbol} is {first_vol}")
        for symbol, volume in self.volume_by_symbol.items():
            if volume > 10:
                self.universe.append(symbol)
        #first_symbol = self.universe[0]
        #first_vol = self.volume_by_symbol[first_symbol]
        #self.Log(f"The cumulative volume for the {first_symbol} is {first_vol}")
        self.volume_by_symbol.clear()
        self.logged = False