Overall Statistics
Total Trades
10
Average Win
0%
Average Loss
-4.14%
Compounding Annual Return
-38.774%
Drawdown
64.000%
Expectancy
-1
Net Profit
-56.721%
Sharpe Ratio
-0.865
Probabilistic Sharpe Ratio
0.108%
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.143
Beta
-1.957
Annual Standard Deviation
0.296
Annual Variance
0.087
Information Ratio
-1.119
Tracking Error
0.411
Treynor Ratio
0.131
Total Fees
$11.19
Estimated Strategy Capacity
$16000000.00
Lowest Capacity Asset
QQQ RIWIV7K5Z9LX
from QuantConnect.Data.Consolidators import CalendarInfo
from AlgorithmImports import *

class UncoupledMultidimensionalAutosequencers(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 5, 28)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        self.leverage = 1
        
        tickers = ["QQQ"]
        
        # Dictionary to hold Symbol Data
        self.symbolData = {}
        
        for ticker in tickers:
            # Add equity data
            symbol = self.AddEquity(ticker, Resolution.Minute).Symbol
            # Create symbol data for respective symbol
            self.symbolData[symbol] = SymbolData(self, symbol)
            
        self.vix = self.AddIndex("VIX", Resolution.Daily).Symbol            
        self.staticAssets = [self.vix]    
        
class SymbolData:
    
    def __init__(self, algorithm, symbol):
        self.algorithm = algorithm
        self.symbol = symbol
        # Define our consolidator for 15 min bars
        fifteenMinuteConsolidator = TradeBarConsolidator(self.Custom)
        fifteenMinuteConsolidator.DataConsolidated += self.OnDataConsolidated
        algorithm.SubscriptionManager.AddConsolidator(symbol, fifteenMinuteConsolidator)
        
        # Define our indicator
        self.adx = AverageDirectionalIndex(10)
        self.padxone = IndicatorExtensions.Of(Delay(1), self.adx)
        self.padxtwo = IndicatorExtensions.Of(Delay(2), self.adx)
        self.padxthree = IndicatorExtensions.Of(Delay(3), self.adx)
        self.rsi = RelativeStrengthIndex(28)
        self.psar = ParabolicStopAndReverse(0.02, 0.02, 0.2)
        # Register indicator to our consolidator
        algorithm.RegisterIndicator(symbol, self.adx, fifteenMinuteConsolidator)
        algorithm.RegisterIndicator(symbol, self.rsi, fifteenMinuteConsolidator)
        algorithm.RegisterIndicator(symbol, self.psar, fifteenMinuteConsolidator) 
        
        # Rolling window to hold 15 min bars
        self.barWindow = RollingWindow[TradeBar](2)
        
    # Store every 15 minute bar in rolling window
    def OnDataConsolidated(self, sender, bar):
        self.barWindow.Add(bar)

        if self.IsReady:
            
            lastBar = self.barWindow[1]
            currentBar = self.barWindow[0]
            price = currentBar.Close
            
            currentRsi = self.rsi.Current.Value
            psar = self.psar.Current.Value
            vix = self.algorithm.Securities["VIX"].Price
            
            previous3adx = self.padxthree.Current.Value          
            previous2adx = self.padxtwo.Current.Value 
            previous1adx = self.padxone.Current.Value           
            currentadx = self.adx.Current.Value

            self.algorithm.Debug(str(self.algorithm.Time) + " Symbol Name: " + str(self.algorithm.Portfolio[bar.Symbol])+ " Symbol Quantity: " + str(self.algorithm.Portfolio[bar.Symbol].Quantity))
            
            if self.algorithm.Portfolio[bar.Symbol].Quantity == 0: 
                
                if currentRsi < 66 and currentadx > 27 and price > psar and currentadx > previous1adx > previous2adx:
                    quantity = self.algorithm.CalculateOrderQuantity(currentBar.Symbol, self.algorithm.leverage)
                    marketTicket = self.algorithm.MarketOrder(currentBar.Symbol, quantity, False, "Long")
                    self.algorithm.Debug(str(self.algorithm.Time) + " ADX: " + str(currentadx)+ " RSI: " + str(currentRsi)+ " PSAR: " + str(psar)+ " Price: " + str(currentBar.Close))
                    
                if currentRsi > 34 and currentadx > 27 and price < psar and currentadx > previous1adx > previous2adx and vix > 20 and vix < 40:
                    quantity = self.algorithm.CalculateOrderQuantity(currentBar.Symbol, self.algorithm.leverage)
                    marketTicket = self.algorithm.MarketOrder(currentBar.Symbol, -quantity, False, "Short") 
                    self.algorithm.Debug(str(self.algorithm.Time) + " ADX: " + str(currentadx)+ " RSI: " + str(currentRsi)+ " PSAR: " + str(psar)+ " Price: " + str(currentBar.Close))

            if self.algorithm.Portfolio[currentBar.Symbol].Quantity > 0 and self.algorithm.Portfolio.TotalUnrealizedProfit > 0 and price < psar:
                self.algorithm.Liquidate(currentBar.Symbol)
                    
            if self.algorithm.Portfolio[currentBar.Symbol].Quantity < 0 and self.algorithm.Portfolio.TotalUnrealizedProfit > 0 and price > psar:
                self.algorithm.Liquidate(currentBar.Symbol)        





            # if self.algorithm.Portfolio[currentBar.Symbol].Quantity > 0:
            #     if currentadx < 20 or currentRsi > 70 or price < psar:
            #       self.algorithm.Liquidate(bar.Symbol)
                    
            # if self.algorithm.Portfolio[currentBar.Symbol].Quantity < 0:
            #     if currentadx < 20 or currentRsi < 30 or price > psar:
            #         self.algorithm.Liquidate(currentBar.Symbol)                    
                    

    @property
    def IsReady(self):
        return self.adx.IsReady and self.barWindow.IsReady and self.padxthree.IsReady and self.psar.IsReady 
        
    def Custom(self, dt):
        start = dt.replace(minute=int(dt.minute / 15) * 15, second=0, microsecond=0)
        return CalendarInfo(start, timedelta(minutes=15))