Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
2.283%
Drawdown
8.400%
Expectancy
0
Net Profit
3.419%
Sharpe Ratio
0.462
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.065
Beta
-2.083
Annual Standard Deviation
0.052
Annual Variance
0.003
Information Ratio
0.076
Tracking Error
0.052
Treynor Ratio
-0.011
Total Fees
$1.00
class CandlestickAlgorithm(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2018, 1, 1)
        self.SetEndDate(2019, 6, 30)
        self.SetCash(100000)
        self.Equities = ["AAPL"]
        self.MarkOrder = None
        self.SL_Order = None
        self.TP_Order = None
        self.SL = 0.95
        self.TP = 1.10
        for Equity in self.Equities:
            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);
        StopPrice = round(FillPrice * self.SL, 2);
        ProfitPrice = round(FillPrice*self.TP, 2);
        self.Log('>> SL       >> FillPrice:{} StopPrice:{}'.format(FillPrice, StopPrice));
        self.Log("ORDER NOTIFICATION >> {} >> Status: {} Symbol: {}. Quantity: "
                    "{}. Direction: {}. Fill Price {}".format(str(Order.Tag),
                                                   str(OrderEvent.Status),
                                                   str(OrderEvent.Symbol),
                                                   str(OrderEvent.FillQuantity),
                                                   str(OrderEvent.Direction),
                                                   str(OrderEvent.FillPrice)));
        if self.Portfolio.Invested:
            if self.Securities["AAPL"].Price == StopPrice:
                self.Liquidate("AAPL");
            if self.Securities["AAPL"].Price == ProfitPrice:    
                self.StopMarketOrder("AAPL", -50, ProfitPrice);
        
    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.MarkOrder = self.MarketOrder("AAPL", 100);
                #self.StopMarketOrder("AAPL", -100, StopPrice);
            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.Window[0].Open < self.Window[1].Low and self.Securities["AAPL"].Price > self.Window[0].High:
                self.MarkOrder = self.MarketOrder("AAPL", 101);
                #self.StopMarketOrder("AAPL", -101, StopPrice);
            #if self.Securities["AAPL"].Price == StopPrice:
                #self.Liquidate("AAPL");
            #if self.Securities["AAPL"].Price == ProfitLevel:
                #self.SL_Order = self.StopMarketOrder("AAPL", -50, self.Securities["AAPL"].Price);