Overall Statistics
Total Trades
54
Average Win
1.50%
Average Loss
-1.07%
Compounding Annual Return
22.655%
Drawdown
62.700%
Expectancy
1.002
Net Profit
66.255%
Sharpe Ratio
0.673
Loss Rate
17%
Win Rate
83%
Profit-Loss Ratio
1.40
Alpha
0.361
Beta
-2.369
Annual Standard Deviation
0.466
Annual Variance
0.217
Information Ratio
0.63
Tracking Error
0.466
Treynor Ratio
-0.132
Total Fees
$54.00
class CandlestickAlgorithm(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2017, 1, 1)
        self.SetEndDate(2019, 6, 30)
        self.SetCash(100000)
        self.AddEquity("AAPL", Resolution.Minute)
        self.Window = RollingWindow[TradeBar](5)
        self.Consolidate("AAPL", Resolution.Daily, self.TradeBarHandler);
        
    def TradeBarHandler(self, TradeBar):
        self.Window.Add(TradeBar);
    
    def OnOrderEvent(self, OrderEvent):
        if OrderEvent.FillQuantity == 0:
            return;
        
        Order = self.Transactions.GetOrderById(OrderEvent.OrderId)
        FillPrice = round(OrderEvent.FillPrice*1, 2)
        ProfitPrice = round(FillPrice*1.15, 2)
        StopPrice = round(FillPrice*0.95, 2) #5% of Fill Price
        self.Log("ORDER NOTIFICATION >> {} >> Status: {} Symbol: {}. Quantity: "
                    "{}. Fill Price {}".format(str(Order.Tag),
                                                   str(OrderEvent.Status),
                                                   str(OrderEvent.Symbol),
                                                   str(OrderEvent.FillQuantity),
                                                   str(OrderEvent.FillPrice)));
        if OrderEvent.Status == OrderStatus.Filled and Order.Type == OrderType.Market:
            self.StopMarketOrder("AAPL", -100, StopPrice, 'Stop Loss');
            self.LimitOrder("AAPL", -100, ProfitPrice, '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();
                                                   
    def OnData(self, data):
        if not (self.Window.IsReady): 
            return
        #if not self.Portfolio.Invested:
        if self.Window[1].Low < self.Window[2].Low and self.Window[0].Low < self.Window[1].Low and self.Securities["AAPL"].Open < self.Window[0].Low and self.Securities["AAPL"].Price > self.Window[0].Close:
            self.MarketOrder("AAPL", 100);
        elif self.Window[2].Low < self.Window[3].Low and self.Window[1].Low < self.Window[2].Low and self.Window[0].Open < self.Window[1].Low and self.Securities["AAPL"].Price > self.Window[0].High:
            self.MarketOrder("AAPL", 100);