Overall Statistics
Total Trades
2
Average Win
0%
Average Loss
0%
Compounding Annual Return
1.578%
Drawdown
0.200%
Expectancy
0
Net Profit
0.133%
Sharpe Ratio
2.546
Probabilistic Sharpe Ratio
68.351%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.004
Beta
0.04
Annual Standard Deviation
0.005
Annual Variance
0
Information Ratio
-3.425
Tracking Error
0.128
Treynor Ratio
0.35
Total Fees
$2.00
Estimated Strategy Capacity
$11000000.00
class Test_LivePremarket_Execution(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2021, 3, 2)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash

        #---------- Universe Selection -------------
        self.UniverseSettings.Resolution = Resolution.Minute
        self.UniverseSettings.ExtendedMarketHours = True  # test live if work
        self.maxSymbolsFromUniverseFilter = 1

        #-- Filtered Universe
        self.AddUniverse(self.CoarseSelectionFilter, self.FineSelectionFilter)

        #-- Add symbol manually
        self.AddEquity("SPY", Resolution.Minute, Market.USA, True, 0, True)
        
        
        self.enterLiveOrderFlag = False        


    def CoarseSelectionFilter(self, coarse):
        myuniverse = [x for x in coarse if x.HasFundamentalData]
        return [x.Symbol for x in myuniverse[:self.maxSymbolsFromUniverseFilter]]
        
    def FineSelectionFilter(self, fine):
        myuniverse = [x for x in fine]    
        return [x.Symbol for x in myuniverse]
        
        
    def OnData(self, data):
        
        # hour = 7, premarket price is available
        if (self.enterLiveOrderFlag == False) and (self.Time.hour == 7) :  # (self.LiveMode) and 
            self.Debug(f"{self.Time}: entering Orders")
            
            for kvp in self.Portfolio:
                testSymbol = kvp.Key
                holding = kvp.Value 
                
                self.Debug(f"{testSymbol.Value} -holding: {holding.Quantity}  price: {self.Securities[testSymbol].Price}")

                if self.IsMarketOpen(testSymbol):
                    self.Debug(f"onData {self.Time} {testSymbol.Value} market is open")
                    tag = str(self.Securities[testSymbol].Price)
                    ticket = self.MarketOrder(testSymbol, 10,False,tag)
                        
                else:
                    self.Debug(f"onData {self.Time} {testSymbol.Value} market is NOT open")
                    limitPrice = self.Securities[testSymbol].Price * 1.03 # use a higher price to get entry
                    tag = str(self.Securities[testSymbol].Price)
                    ticket = self.LimitOrder(testSymbol,10,limitPrice,tag)
                    self.Debug(f"test doing limit order for {testSymbol.Value} price: {self.Securities[testSymbol].Price} limitPrice: {limitPrice}")
                        
                        
            self.enterLiveOrderFlag = True