Overall Statistics
import datetime
import random
class TestAlgo(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2014, 10, 16)  # Set Start Date
        self.SetEndDate(2017, 10, 16)
        self.SetCash(100000)  # Set Strategy Cash
        
        
        # Set Parameter
        self.sellSize = 1
        
        #self.future = self.AddFuture("OJ", Resolution.Hour)
        self.future = self.AddSecurity(SecurityType.Equity, "SPY", Resolution.Hour)
        self.sym = self.future.Symbol
        self.Debug('New asset added - Symbol: %s, Resolution: %s, Type: %s' % (self.future.Symbol, self.future.Resolution, self.future.Type))
        self.Debug('is portfolio invested? {}'.format(self.Portfolio.Invested))
        self.Debug(datetime.datetime(2014, 10, 16))
        
    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
            Arguments:
                data: Slice object keyed by symbol containing the stock data
        '''
        
        #self.Debug(f'Symbol present? {data.ContainsKey(self.future.Symbol)}')
        #self.Debug(f'w/o bars: {data[self.sym].Low} vs. w/ bars: {data.Bars[self.sym].Low}')
         
        if data.Time > datetime.datetime(2016, 10, 16) and data.Bars.ContainsKey(self.sym):
            if self.Portfolio[self.future.Symbol].Quantity < 100:
                self.buyOrder = self.MarketOrder(self.future.Symbol, 1, self.Time, "Buy")
                # order is still an orderTicket with no Price, not filled yet
                #self.stopLoss = self.StopMarketOrder(self.future.Symbol, -1, (self.buyOrder.Price * .9))#, self.Time, "fdsfs")
                # StopMarketOrder with tag unrecognized... :(
                #self.stopLoss = self.StopMarketOrder(self.future.Symbol, -1.0, (data.Bars[self.sym].Close * .9), self.Time, "Stop Loss")
                self.stopLoss = self.StopMarketOrder(self.future.Symbol, -1.0, (data.Bars[self.sym].Close * .9))
                
            if self.Portfolio[self.future.Symbol].Quantity >= 1:
                if random.random() >= .5:
                    self.MarketOrder(self.future.Symbol, -self.sellSize, self.Time, "Sell")
            
            
    def OnOrderEvent(self, OrderEvent):
        """Event when the order is filled. Debug log the order fill. :OrderEvent:"""
        
        if OrderEvent.FillQuantity == 0:
            return

        
        else:
            
            #self.Debug(f'tag: {self.buyOrder.Tag}')
            order = self.Transactions.GetOrderById(OrderEvent.OrderId)
            
            self.Debug("Order Filled! Symbol: {}. Quantity: {}. Direction: {}. Price: {}. Tag: {}. Total Holding {}."
                       .format(str(OrderEvent.Symbol),
                               str(OrderEvent.FillQuantity),
                               str(OrderEvent.Direction),
                               str(OrderEvent.FillPrice),
                               order.Tag,
                               self.Portfolio[self.future.Symbol].Quantity))