Overall Statistics
Total Trades
4
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
$154.82
Estimated Strategy Capacity
$1200000.00
Lowest Capacity Asset
XCUR X6M4FK1BUGYT
class QuantumHorizontalRegulators(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2021, 8, 2)  # Set Start Date
        self.SetEndDate(2021, 8, 2)
        self.SetCash(333333)  # Set Strategy Cash
        self.AddEquity("XCUR", Resolution.Second)
        self.scaning = False
        self.lastToggle = None
        self.needs_reset = False
        
        self.__numberOfSymbols = 1
        self.UniverseSettings.Resolution = Resolution.Second

        self.AddAlpha(ShortSqueezeModel(self))
        
        self.SetExecution(ImmediateExecutionModel())

        self.SetPortfolioConstruction(AccumulativeInsightPortfolioConstructionModel(lambda time: None))
        
        self.AddRiskManagement(TrailingStopRiskManagementModel(0.07))
        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("XCUR", 0), self.toggleScan)
        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("XCUR", 375), self.toggleScan)
        
    def toggleScan(self):
        self.scaning = not self.scaning
        self.lastToggle = self.Time
        
        if not self.scaning:
            self.needs_reset = True
    def OnOrderEvent(self, orderEvent):
        order = self.Transactions.GetOrderById(orderEvent.OrderId)
        if orderEvent.Status == OrderStatus.Filled: 
           self.Log("{0}: {1}: {2}".format(self.Time, order.Type, orderEvent))
            
        
class ShortSqueezeModel(AlphaModel):
    symbolData = {}

    def __init__(self, algo):
        self.algo = algo
        
    
    def Update(self, algorithm, slice):
        if algorithm.IsWarmingUp:
            return []
    
        # If it's the end of the day, update the yesterday close of each indicator
        if not algorithm.Securities['XCUR'].Exchange.ExchangeOpen:
            for symbol in self.symbolData:
                if symbol in slice.Bars:
                    self.symbolData[symbol].yest_close = slice.Bars[symbol].Close
    
        if not self.algo.scaning:
            # Reset max indicator
            if self.algo.needs_reset:
                for symbol in self.symbolData:
                    self.symbolData[symbol].max.Reset()
                self.algo.needs_reset = False
            return []
        
        insights = []
        
        insight_seconds = 99999999999
        
        # Create insights for symbols up at least 10% on the day
        for symbol in self.symbolData:
            # If already invested, continue to next symbol
            if algorithm.Securities[symbol].Invested or symbol not in slice.Bars or self.symbolData[symbol].max.Samples == 0:
                continue
            
            
            
            # Calculate return sign yesterday's close
            yest_close = self.symbolData[symbol].yest_close
            close = slice[symbol].Close 
            ret = (close - yest_close) / yest_close
            high_of_day_break = close > self.symbolData[symbol].max.Current.Value
            algorithm.Log(f'{algorithm.Time} : ret={ret}, high_of_day_break={high_of_day_break}')
            if ret >= 0.1 and high_of_day_break:    # Up 10% on the day & breaks high of day
                
                hours = algorithm.Securities[symbol].Exchange.Hours
        # 5-minute before the close
                closeTime = hours.GetNextMarketClose(algorithm.Time, False) - timedelta(minutes=5)
                insights.append(Insight.Price(symbol, closeTime, InsightDirection.Up))
                
                
        # Update max indicator for all symbols
        for symbol in self.symbolData:
            if symbol in slice.Bars:
                self.symbolData[symbol].max.Update(slice.Time, slice.Bars[symbol].High)
        return Insight.Group(insights)
    
    
    
    def OnSecuritiesChanged(self, algorithm, changes):
        if len(changes.AddedSecurities) > 0:
            # Get history of symbols over lookback window
            added_symbols = [x.Symbol for x in changes.AddedSecurities]
            history = algorithm.History(added_symbols, 1, Resolution.Daily)
            if history.empty:
                    return
            history  = history['close']
            
            for added in changes.AddedSecurities:
                # Save yesterday's close
                closes = history.loc[[str(added.Symbol.ID)]].values
                if len(closes) < 1:
                    continue
                self.symbolData[added.Symbol] = SymbolData(closes[0])
            
        for removed in changes.RemovedSecurities:
            # Delete yesterday's close tracker
            self.symbolData.pop(removed.Symbol, None)

class SymbolData:
    def __init__(self, yest_close):
        self.yest_close = yest_close
        self.max = Maximum(480*60) # 45 minutes