Overall Statistics
Total Trades
48
Average Win
0.30%
Average Loss
-0.14%
Compounding Annual Return
0.166%
Drawdown
0.900%
Expectancy
0.279
Net Profit
0.956%
Sharpe Ratio
0.324
Loss Rate
58%
Win Rate
42%
Profit-Loss Ratio
2.07
Alpha
0.001
Beta
0.009
Annual Standard Deviation
0.005
Annual Variance
0
Information Ratio
-0.836
Tracking Error
0.131
Treynor Ratio
0.182
Total Fees
$120.00
class QuantumVerticalCircuit(QCAlgorithm):

    def Initialize(self):
        self.SetBrokerageModel(BrokerageName.AlphaStreams)
        self.syl = 'BBY'
        self.contract = 0
        self.highPrice = 0
        self.SetStartDate(2014, 1, 1)# Set Start Date
        self.SetEndDate(2019,9,30)
        self.SetCash(1000000)  # Set Strategy Cash
        self.asset = self.AddEquity(self.syl, Resolution.Daily)
        self.macd_3 = self.MACD(self.syl, 12, 26, Resolution.Daily)
        self.macd_12 = self.MACD(self.syl, 12, 26, Resolution.Daily)
        self.macd_50 = self.MACD(self.syl, 50, 100 , Resolution.Daily)
        self.ema = self.EMA(self.syl, 3, Resolution.Daily)
        self.stopMarketTicket = None
    
        
        #Setting Bar Data
        self.tradeBarWindow = RollingWindow[TradeBar](3)
        
        #EMA rolling window
        self.ema.Updated += self.EmaUpdated
        self.emaWin = RollingWindow[IndicatorDataPoint](4)
        
        #Macd_3 Bar Data
        self.macd_3.Updated += self.MacdUpdated
        self.macd3Win = RollingWindow[IndicatorDataPoint](12)
        
        #Macd_12 Bar Data
        self.macd_12.Updated += self.MacdUpdated
        self.macd12Win = RollingWindow[IndicatorDataPoint](12)
        #Macd_50 Bar Data
        self.macd_50.Updated += self.MacdUpdated
        self.macd50Win = RollingWindow[IndicatorDataPoint](12)
        

    def EmaUpdated(self, sender, updated):
        self.emaWin.Add(updated)
        
    
    def MacdUpdated(self, sender, updated):
        '''Adds updated values to rolling window'''
        self.macd3Win.Add(updated)
        self.macd12Win.Add(updated)
        self.macd50Win.Add(updated)
        
    def OnData(self, data):
        if not self.macd_3.IsReady:
            return
        if not self.macd_12.IsReady:
            return
        if not self.macd_50.IsReady:
            return
        if not self.ema.IsReady:
            return        
        
        #Creating my 3 Bars
        self.tradeBarWindow.Add(data[self.syl])
        if not (self.tradeBarWindow.IsReady and self.macd3Win.IsReady): return
        #if not self.tradeBarWindow.IsReady: return
        
        # 3 most recent trade bars 0, 1, 2
        self.bar0 = self.tradeBarWindow[0] #Previous bar inside
        self.bar1 = self.tradeBarWindow[1] # previous bar 1
        self.bar2 = self.tradeBarWindow[self.tradeBarWindow.Count-1] # Previous bar 2

        #MacD_3 set up
        self.macd3IB = self.macd3Win[4] #IB
        self.macd3_1 = self.macd3Win[7] # PB 1
        self.macd3_2 = self.macd3Win[10] # PB 2
        
        self.contraction = False
        if self.macd3IB.Value > 0:
            if self.macd3IB.Value < self.macd3_1.Value:
                self.contraction = True

        if self.contraction:
            self.contract += 1
        else:
            self.contract = 0
        
        if not self.Portfolio.Invested:
            
            if self.contract >= 5:
                self.MarketOrder(self.syl, 500)
        if self.Portfolio.Invested:
            
            if self.Portfolio.TotalUnrealizedProfit >= self.Portfolio[self.syl].HoldingsCost * .10:
                self.Sell(self.syl, 500)
            if self.Portfolio.TotalUnrealizedProfit < (self.Portfolio[self.syl].HoldingsCost * .05) * -1:
                self.Sell(self.syl, 500)
            # self.stopMarketTicket = self.StopMarketOrder(self.syl, -100,self.Securities[self.syl].Close * 0.9)
        # else:
        #     if self.Securities['SPY'].Close > self.highPrice:
        #         self.highPrice = self.Securities['SPY'].Close
        #         updateFields = UpdateOrderFields()
        #         updateFields = self.highPrice * 0.9
                # self.stopMarketTicket.Update(updateFields)
            #     self.Debug(str(self.stopMarketTicket.OrderId))
            # if self.contract => 7:
            #     self.Buy(self.syl, 10)
            
    def OnOrderEvent(self, orderEvent):
        if orderEvent.Status == OrderStatus.Filled:
            self.Log("Purchased Stock: {0}".format(orderEvent.Symbol))