Overall Statistics
Total Trades
264
Average Win
3.98%
Average Loss
-1.38%
Compounding Annual Return
35.455%
Drawdown
20.600%
Expectancy
0.885
Net Profit
356.260%
Sharpe Ratio
1.952
Probabilistic Sharpe Ratio
93.000%
Loss Rate
52%
Win Rate
48%
Profit-Loss Ratio
2.89
Alpha
0.296
Beta
0.555
Annual Standard Deviation
0.195
Annual Variance
0.038
Information Ratio
1.234
Tracking Error
0.185
Treynor Ratio
0.686
Total Fees
$264.00
class RSIAlgorithm(QCAlgorithm):

    def Initialize(self):
        
        # Set our main strategy parameters
        self.SetStartDate(2015,9, 20)   # Set Start Date
        self.SetEndDate(2020,9,20)       # Set End Date
        self.SetCash(3300)             # Set Strategy Cash
        
        self.Equities = ["AMZN", "AAPL", "TSLA", "GOOGL"]
        self.TradeAmount = self.Portfolio.TotalPortfolioValue
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Cash)
        
        self.HK = dict() # Create dict to hold all our HK Indicators
        self.PairSymbol = dict()
        
        for Equity in self.Equities:
              
            # self.AddForex(Equity, Resolution.Minute, Market.Oanda)
            # self.AddEquity(Equity, Resolution.Hour)
            self.PairSymbol[Equity] = SymbolData(self,self.AddEquity(Equity, Resolution.Minute).Symbol)
            self.HK[Equity] = self.HeikinAshi(Equity, Resolution.Hour)

            
            
    def OnData(self, data):
        
        for Equity in self.Equities:
            
            # Aliases
            # -----------------------------------------------------------------
            
            # Heikin
            HK_O = self.HK[Equity].Open.Current.Value
            HK_H = self.HK[Equity].High.Current.Value
            HK_L = self.HK[Equity].Low.Current.Value
            HK_C = self.HK[Equity].Close.Current.Value
            HK_P = self.HK[Equity].Current.Price
            
            # OHLC
            # O = data[Equity].Open
            # H = data[Equity].High
            # L = data[Equity].Low
            # C = data[Equity].Close
            # P = data[Equity].Price
            
            # Work out Heikin Sentiment
            # ---------------------------------------
            if HK_O < HK_C * 1.025:
                HK_S = "Bull" 
            elif HK_O > HK_C:
                HK_S = "Bear"
            else:
                HK_S = "Neut"
    
            
            # Check if we are in the market
            # if not self.Portfolio.Invested:
            if not self.Portfolio[Equity].Quantity:
                if HK_S == "Bull" \
                and self.PairSymbol[Equity].macd.Current.Value > 0 \
                and self.PairSymbol[Equity].macd.Current.Value > self.PairSymbol[Equity].macd.Signal.Current.Value \
                and self.PairSymbol[Equity].bb.UpperBand.Current.Value > self.PairSymbol[Equity].bbUpperPrevious \
                and self.PairSymbol[Equity].rsi.Current.Value > 0.7:
                    self.SetHoldings(Equity, 0.3)
            else:
                if HK_S == "Bear":
                  self.Liquidate(Equity)
                   
    def OnOrderEvent(self, orderEvent):
        order = self.Transactions.GetOrderById(orderEvent.OrderId)
            
        if order.Status == OrderStatus.Filled:
            if order.Type == OrderType.Limit or order.Type == OrderType.Limit:
                self.Transactions.CancelOpenOrders(order.Symbol)
                
        if order.Status == OrderStatus.Canceled:
            self.Log(str(orderEvent))   
    
class SymbolData:
    def __init__(self,qcContext, symbol):
        self.qcContext = qcContext
        self.symbol = symbol
        self.macd = qcContext.MACD(self.symbol, 12, 26, 9, MovingAverageType.Exponential, Resolution.Minute)
        self.qcContext.RegisterIndicator(self.symbol, self.macd, Resolution.Minute)
        self.bb = qcContext.BB(self.symbol, 12, 2,  Resolution.Minute)
        self.qcContext.RegisterIndicator(self.symbol, self.bb, Resolution.Minute)
        self.rsi =  qcContext.RSI(self.symbol, 7,  Resolution.Minute)
        self.qcContext.RegisterIndicator(self.symbol, self.rsi, Resolution.Minute)
        self.bbUpperPrevious = self.bb.UpperBand.Current.Value 
        self.atr = qcContext.ATR(self.symbol, 7, Resolution.Daily)
        self.qcContext.RegisterIndicator(self.symbol, self.atr, Resolution.Daily)