Overall Statistics
Total Trades
431
Average Win
3.26%
Average Loss
-0.16%
Compounding Annual Return
6.440%
Drawdown
7.400%
Expectancy
0.844
Net Profit
33.092%
Sharpe Ratio
0.925
Loss Rate
91%
Win Rate
9%
Profit-Loss Ratio
19.86
Alpha
0.054
Beta
-0.013
Annual Standard Deviation
0.057
Annual Variance
0.003
Information Ratio
-0.29
Tracking Error
0.136
Treynor Ratio
-4.136
Total Fees
$431.00
class NewsReportAlgorithm(QCAlgorithm):
    
    stopMarketTicket = None
    StopPrice = 0
    MarketOrderFillTime = datetime.min
    MarketTicket = None
    stopMarketOrderFillTime = datetime.min
    
    def Initialize(self):
        self.SetStartDate(2015, 1, 1)
        self.SetEndDate(2019, 7, 30)
        self.SetCash(100000)
        self.AMZN = self.AddEquity("AMZN", Resolution.Minute)
        self.Window = RollingWindow[TradeBar](5)
        self.Consolidate("AMZN", Resolution.Daily, self.TradeBarHandler);
        self.AMZN.SetDataNormalizationMode(DataNormalizationMode.Raw)
        
    def TradeBarHandler(self, TradeBar):
        self.Window.Add(TradeBar);
                                                   
    def OnData(self, data):
        if not (self.Window.IsReady): 
            return
        
        if (self.Time - self.MarketOrderFillTime).days < 1:
            return
        if (self.Time - self.stopMarketOrderFillTime).days < 1:
            return
        
        if not self.Portfolio.Invested:
            if self.Securities["AMZN"].Open < self.Window[0].Low - .40 and self.Securities["AMZN"].Price > self.Window[0].Low + .10:
                self.MarketOrder("AMZN", 100, True, 'Market Order');
                self.stopMarketTicket = self.StopMarketOrder("AMZN", -100, self.Securities["AMZN"].Low, 'Stop Order')
                
        else:
            if self.Window[0].Low > self.Window[1].Low:
                self.StopPrice = self.Window[0].Low -.10    
                UpdateFields = UpdateOrderFields()
                UpdateFields.StopPrice = self.StopPrice
                self.stopMarketTicket.Update(UpdateFields)        
            
    def OnOrderEvent(self, OrderEvent):
        if OrderEvent.FillQuantity == 0:
            return;
            
        Order = self.Transactions.GetOrderById(OrderEvent.OrderId)
        if self.MarketTicket is not None and self.MarketTicket.OrderId == OrderEvent.OrderId: 
            self.MarketOrderFillTime = self.Time
        if self.stopMarketTicket is not None and self.stopMarketTicket.OrderId == OrderEvent.OrderId:
            self.stopMarketOrderFillTime = self.Time    
        FillPrice = round(OrderEvent.FillPrice*1, 2)
        Profit = 1.03*FillPrice
        self.Log("ORDER NOTIFICATION >> {} >> Status: {} Symbol: {}. Quantity: "
                    "{}. Open: {}. Previous Low: {}. Low: {}. Fill Price {}".format(str(Order.Tag),
                                                   str(OrderEvent.Status),
                                                   str(OrderEvent.Symbol),
                                                   str(OrderEvent.FillQuantity),
                                                   self.Securities["AMZN"].Open,
                                                   self.Window[0].Low,
                                                   self.Securities["AMZN"].Low,
                                                   str(OrderEvent.FillPrice)));
                                                   
        if OrderEvent.Status == OrderStatus.Filled and Order.Type == OrderType.Market:
            self.LimitOrder("AMZN", -100, Profit, 'Take Profit');
            
        if OrderEvent.Status == OrderStatus.Filled and Order.Type == OrderType.StopMarket:
            self.Transactions.CancelOpenOrders();
            
        if OrderEvent.Status == OrderStatus.Filled and Order.Type == OrderType.Limit:
            self.Transactions.CancelOpenOrders();