Overall Statistics
Total Trades
18
Average Win
0%
Average Loss
0.00%
Compounding Annual Return
-0.972%
Drawdown
0.000%
Expectancy
-1
Net Profit
-0.019%
Sharpe Ratio
-19.67
Probabilistic Sharpe Ratio
0%
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.006
Beta
-0.002
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-10.602
Tracking Error
0.076
Treynor Ratio
4.338
Total Fees
$18.00
Estimated Strategy Capacity
$1200000000.00
class SPY_SMA(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2021, 4, 10)  # Set Start Date
        self.SetEndDate(2021, 4, 17) # Set End Date
        self.SetCash(100000)  # Set Strategy Cash
        
        symbol = "SPY"
        # spy = self.AddEquity(symbol , Resolution.Minute , extendedMarketHours = True)
        spy = self.AddEquity(symbol , Resolution.Minute )
        self.spy = spy.Symbol
        
        fiveCountConsolidator = TradeBarConsolidator(TimeSpan.FromMinutes(5))  # 构建函数合成30min数据            
        fiveCountConsolidator.DataConsolidated += self.FiveBarHandler 
        self.SubscriptionManager.AddConsolidator(self.spy, fiveCountConsolidator)
        self.sma_5min_window = RollingWindow[float](2)
        self.sma_30min_window = RollingWindow[float](2)
        self.close_window = RollingWindow[float](2)
        
        self.sma_5min_5 = SimpleMovingAverage(5)    # 计算基于5min 的MA5  
        self.sma_5min_30 = SimpleMovingAverage(30)  # 计算基于30min 的MA30 
        self.RegisterIndicator(self.spy, self.sma_5min_5, fiveCountConsolidator)  # 注册 sma5
        self.RegisterIndicator(self.spy, self.sma_5min_30, fiveCountConsolidator)  
        
        self.initFinished = False
        self.quantity = 0
        
        self.SetWarmUp(30)
        

    def OnData(self, data):
        pass
        
    def FiveBarHandler(self, sender, bar):
        self.close_window.Add(bar.Close)
        self.sma_5min_window.Add(self.sma_5min_5.Current.Value)
        self.sma_30min_window.Add(self.sma_5min_30.Current.Value)
        if self.initFinished and self.sma_30min_window.IsReady:
            if (self.sma_5min_window[0] > self.sma_30min_window[0]) and (self.close_window[0] > self.sma_5min_window[0]):
                self.OrderSecurity("Positive")
            elif (self.sma_5min_window[0] < self.sma_30min_window[0]) and (self.close_window[0] < self.sma_5min_window[0]):
                self.OrderSecurity("Negative")
                
    def OrderSecurity(self, Tag):
        if self.quantity >= 0 and Tag is "Negative":
            self.MarketOrder(self.spy, -self.quantity - 1) 
            self.quantity = -1
        if self.quantity < 0 and Tag is "Positive":
            self.MarketOrder(self.spy, -self.quantity + 1 )
            self.quantity = 1
    
    def OnWarmupFinished(self):
        self.initFinished = True