Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
23.693%
Drawdown
5.100%
Expectancy
0
Net Profit
0.117%
Sharpe Ratio
0.859
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.071
Beta
1.041
Annual Standard Deviation
0.762
Annual Variance
0.581
Information Ratio
3.102
Tracking Error
0.03
Treynor Ratio
0.629
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
'''Notes:'''
    
    
import numpy as np
from datetime import datetime, timedelta
from Alphas.MacdAlphaModel import MacdAlphaModel
from Risk.MaximumDrawdownPercentPerSecurity import MaximumDrawdownPercentPerSecurity

class DataConsolidationAlgorithm(QCAlgorithm):

    def Initialize(self):
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

        self.SetStartDate(DateTime(2020, 1, 1, 0, 0, 0))         #Set Start Date
        self.SetEndDate(self.StartDate + timedelta(1))          #Set End Date
        self.SetRiskManagement(MaximumDrawdownPercentPerSecurity(0.03))

        # Find more symbols here: http://quantconnect.com/data
        self.resolution = Resolution.Minute
        self.CryptoSymbols = ["BTCUSD"]
        # initialize our crypto data 
        for symbol in self.CryptoSymbols:
            self.AddCrypto(symbol, self.resolution)
            self.symbol = symbol
            self.cryptoCall = str(symbol[:-3])
            self.fiatCall = str(symbol[3:])
            self.Log(str(symbol) + str(" is added to Securities"))
            self.SetBenchmark(symbol)
    
        self.SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash)
        self.startingBTC = 0.0870
        self.SetCash(self.cryptoCall, self.startingBTC) 
        self.SetCash(self.fiatCall, 0)
        
        self.holdings = self.startingBTC
        self.cash = 0
        
        self.ticketSellOrder = None
        self.tickerBuyOrder = None
    
        self.limitSellStopPrice = None
        self.limitBuyStopPrice = None
        
        
        '''Data Consolidator Initialization and Indicator Subscription Registration'''
        # We do not need to register 1 minute indicators because they come in at our global resolution. 
        # All indicators need to be defined here (with timescaled names), and then subscribed to their various timescales. 
        
        # 1 Minute Indicators
        self.price = self.SMA(self.symbol, 1, Resolution.Minute)
        self.oneBB = BollingerBands(20, MovingAverageType.Simple)
        
        # 5 Minute Indicators 
        self.fiveBB = BollingerBands(20, MovingAverageType.Simple)
        self.fiveBBW = self.fiveBB.BandWidth
        self.fiveRSI = self.RSI(self.symbol, 14, MovingAverageType.Simple)
        self.fiveResistance = None
        self.fiveSupport = None
        
        # 30 Minute Indicators 
        self.thirtyBB = BollingerBands(20, MovingAverageType.Simple)
        self.thirtyBBW = self.thirtyBB.BandWidth
        self.thirtyRSI = self.RSI(self.symbol, 14, MovingAverageType.Simple)
        self.thirtyResistance = None
        self.thirtySupport = None
        
        # 1 Hour Indicators 
        self.sixtyBB = BollingerBands(20, MovingAverageType.Simple)
        self.sixtyBBW = self.sixtyBB.BandWidth
        self.sixtyRSI = self.RSI(self.symbol, 14, MovingAverageType.Simple)
        self.sixtyResistance = None
        self.sixtySupport = None
        
        
        # 4 Hour Indicators 
        self.fourBB = BollingerBands(20, MovingAverageType.Simple)
        self.fourBBW = self.fourBB.BandWidth
        self.fourRSI = self.RSI(self.symbol, 14, MovingAverageType.Simple)
        self.fourResistance = None
        self.fourSupport = None
        
        # 1 Day Indicators 
        self.dayBB = BollingerBands(20, MovingAverageType.Simple)
        self.dayBBW = self.dayBB.BandWidth
        self.dayRSI = self.RSI(self.symbol, 14, MovingAverageType.Simple)
        self.dayResistance = None
        self.daySupport = None
        
        # 1 Week Indicators 
        self.weekBB = BollingerBands(20, MovingAverageType.Simple)
        self.weekResistance = None
        self.weekSupport = None
        # Week rolling window
        self.RollingWeek = RollingWindow[TradeBar](30)
        
        # define our 5 minute trade bar consolidator. we can
        # access the 5 minute bar from the DataConsolidated events
        fiveMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=5))
        # attach our event handler. the event handler is a function that will
        # be called each time we produce a new consolidated piece of data.
        fiveMinuteConsolidator.DataConsolidated += self.fiveMinuteBarHandler
        # this call adds our 5 minute consolidator to
        # the manager to receive updates from the engine
        self.SubscriptionManager.AddConsolidator(self.symbol, fiveMinuteConsolidator)
        
        self.fiveMinuteIndicators = [self.fiveBB, self.fiveRSI]
        for indicator in self.fiveMinuteIndicators:
            self.RegisterIndicator(self.symbol, indicator, fiveMinuteConsolidator)
            self.Log(str("5 minute indicator registered"))
    
        # define our 30 minute trade bar consolidator. we can
        # access the 30 minute bar from the DataConsolidated events
        thirtyMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=30))
        # attach our event handler. the event handler is a function that will
        # be called each time we produce a new consolidated piece of data.
        thirtyMinuteConsolidator.DataConsolidated += self.thirtyMinuteBarHandler
        # this call adds our 30 minute consolidator to
        # the manager to receive updates from the engine
        self.SubscriptionManager.AddConsolidator(self.symbol, thirtyMinuteConsolidator)
        
        self.thirtyMinuteIndicators = [self.thirtyBB, self.thirtyRSI]
        for indicator in self.thirtyMinuteIndicators:
            self.RegisterIndicator(self.symbol, indicator, thirtyMinuteConsolidator)
            self.Log(str("30 minute indicator registered"))
        # Rolling Window for 30 Min BBW comparison
        self.thirtyBBW = RollingWindow[IndicatorDataPoint](4)
        self.thirtyBB.BandWidth.Updated += self.thirtyBBWUpdated
    
        # define our 60 minute trade bar consolidator. we can
        # access the 60 minute bar from the DataConsolidated events
        sixtyMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=60))
        # attach our event handler. the event handler is a function that will
        # be called each time we produce a new consolidated piece of data.
        sixtyMinuteConsolidator.DataConsolidated += self.sixtyMinuteBarHandler
        # this call adds our 60 minute consolidator to
        # the manager to receive updates from the engine
        self.SubscriptionManager.AddConsolidator(self.symbol, sixtyMinuteConsolidator)
        
        self.sixtyMinuteIndicators = [self.sixtyBB, self.sixtyRSI]
        for indicator in self.sixtyMinuteIndicators:
            self.RegisterIndicator(self.symbol, indicator, sixtyMinuteConsolidator)
            self.Log(str("60 minute indicator registered"))
        
        # define our 4 hour trade bar consolidator. we can
        # access the 4 hour bar from the DataConsolidated events
        fourHourConsolidator = TradeBarConsolidator(timedelta(minutes=240))
        # attach our event handler. the event handler is a function that will
        # be called each time we produce a new consolidated piece of data.
        fourHourConsolidator.DataConsolidated += self.fourHourBarHandler
        # this call adds our 4 hour consolidator to
        # the manager to receive updates from the engine
        self.SubscriptionManager.AddConsolidator(self.symbol, fourHourConsolidator)
        
        self.fourHourIndicators = [self.fourBB, self.fourRSI]
        for indicator in self.fourHourIndicators:
            self.RegisterIndicator(self.symbol, indicator, fourHourConsolidator)
            self.Log(str("4 hour indicator registered"))
        
        # first define a one day trade bar -- this produces a consolidated piece of data after a day has passed
        oneDayConsolidator = TradeBarConsolidator(timedelta(1))
        # attach our event handler. the event handler is a function that will
        # be called each time we produce a new consolidated piece of data.
        oneDayConsolidator.DataConsolidated += self.oneDayBarHandler
        # this call adds our 1 Day consolidator to
        # the manager to receive updates from the engine
        self.SubscriptionManager.AddConsolidator(self.symbol, oneDayConsolidator)
        
        self.oneDayIndicators = [self.dayBB, self.dayRSI]
        for indicator in self.oneDayIndicators:
            self.RegisterIndicator(self.symbol, indicator, oneDayConsolidator)
            self.Log(str("Day indicator registered"))
        
        # first define a one week trade bar -- this produces a consolidated piece of data after a week has passed
        oneWeekConsolidator = TradeBarConsolidator(timedelta(7))
        # attach our event handler. the event handler is a function that will
        # be called each time we produce a new consolidated piece of data.
        oneWeekConsolidator.DataConsolidated += self.oneWeekBarHandler
        # this call adds our 1 Week consolidator to
        # the manager to receive updates from the engine
        self.SubscriptionManager.AddConsolidator(self.symbol, oneWeekConsolidator)
        
        self.oneWeekIndicators = [self.weekBB]
        for indicator in self.oneWeekIndicators:
            self.RegisterIndicator(self.symbol, indicator, oneWeekConsolidator)
            self.Log(str("Week indicator registered"))
        
        '''Argument Initialization'''
        # Time
        self.consolidatedFiveMinute = False
        self.consolidatedFifteenMinute = False
        self.consolidatedThirtyMinute = False
        self.consolidatedSixtyMinute = False
        self.consolidatedFourHour = False
        self.consolidatedDay = False
        self.consolidatedWeek = False
        self.__last = None
       
        
        # Warm Up of our longest timeframe (1 week) defined in minutes.
        self.SetWarmUp(10080, Resolution.Minute)
        
        

    def OnData(self, data):
        # 1 minute bar
    
        if self.IsWarmingUp:
            return
        
        pass
    
    def minuteRollingPriceUpdated(self, sender, updated):
        self.minuteRollingPrice.Add(updated)

    def fiveMinuteBarHandler(self, sender, consolidated):
        ''''''
        if self.IsWarmingUp:
            return
        
        if self.price.Current.Value > self.fiveBB.MiddleBand.Current.Value:
            self.fiveResistance = self.fiveBB.UpperBand.Current.Value
            self.fiveSupport = self.fiveBB.MiddleBand.Current.Value
            
        if self.price.Current.Value < self.fiveBB.MiddleBand.Current.Value:
            self.fiveResistance = self.fiveBB.MiddleBand.Current.Value
            self.fiveSupport = self.fiveBB.LowerBand.Current.Value
        
        self.Log("fiveMinuteBarHandler")
        self.Debug("self.price.Current.Value:" + str(self.price.Current.Value))
        self.Debug("self.fiveBB.UpperBand.Current.Value:" + str(self.fiveBB.UpperBand.Current.Value))
        self.Debug("self.fiveBB.MiddleBand.Current.Value:" + str(self.fiveBB.MiddleBand.Current.Value))
        self.Debug("self.fiveBB.LowerBand.Current.Value:" + str(self.fiveBB.LowerBand.Current.Value))
        self.Log("self.fiveResistance" + str(self.fiveResistance))
        self.Log("self.fiveSupport" + str(self.fiveSupport))

        self.consolidatedFiveMinute = True
        
    def thirtyBBWUpdated(self, sender, updated):
        self.thirtyBBW.Add(updated)
        
    def thirtyMinuteBarHandler(self, sender, consolidated):
        ''''''
        if self.IsWarmingUp:
            return
        
        if self.price.Current.Value > self.thirtyBB.MiddleBand.Current.Value:
            self.thirtyResistance = self.thirtyBB.UpperBand.Current.Value
            self.thirtySupport = self.thirtyBB.MiddleBand.Current.Value
            
        if self.price.Current.Value < self.thirtyBB.MiddleBand.Current.Value:
            self.thirtyResistance = self.thirtyBB.MiddleBand.Current.Value
            self.thirtySupport = self.thirtyBB.LowerBand.Current.Value
            
        self.Log("thirtyMinuteBarHandler")
        self.Log("self.price.Current.Value:" + str(self.price.Current.Value))
        self.Log("self.thirtyBB.MiddleBand.Current.Value:" + str(self.thirtyBB.MiddleBand.Current.Value))
        self.Log("self.thirtyResistance" + str(self.thirtyResistance))
        self.Log("self.thirtySupport" + str(self.thirtySupport))

        self.consolidatedThirtyMinute = True

    def sixtyMinuteBarHandler(self, sender, consolidated):
        ''''''
        if self.IsWarmingUp:
            return
        
        if self.price.Current.Value > self.sixtyBB.MiddleBand.Current.Value:
            self.sixtyResistance = self.sixtyBB.UpperBand.Current.Value
            self.sixtySupport = self.sixtyBB.MiddleBand.Current.Value
            
        if self.price.Current.Value < self.fourBB.MiddleBand.Current.Value:
            self.sixtyResistance = self.sixtyBB.MiddleBand.Current.Value
            self.sixtySupport = self.sixtyBB.LowerBand.Current.Value
        
        self.Log("sixtyMinuteBarHandler")
        self.Log("self.price.Current.Value:" + str(self.price.Current.Value))
        self.Log("self.sixtyBB.MiddleBand.Current.Value:" + str(self.sixtyBB.MiddleBand.Current.Value))
        self.Log("self.sixtyResistance" + str(self.sixtyResistance))
        self.Log("self.sixtySupport" + str(self.sixtySupport))
        

        self.consolidatedHour = True

    def fourHourBarHandler(self, sender, consolidated):
        ''''''
        if self.IsWarmingUp:
            return
        
        if self.price.Current.Value > self.fourBB.MiddleBand.Current.Value:
            self.fourResistance = self.fourBB.UpperBand.Current.Value
            self.fourSupport = self.fourBB.MiddleBand.Current.Value
            
        if self.price.Current.Value < self.fourBB.MiddleBand.Current.Value:
            self.fourResistance = self.fourBB.MiddleBand.Current.Value
            self.fourSupport = self.fourBB.LowerBand.Current.Value
            
        self.Log("fourHourBarHandler")
        self.Log("self.price.Current.Value:" + str(self.price.Current.Value))
        self.Log("self.fourBB.MiddleBand.Current.Value:" + str(self.fourBB.MiddleBand.Current.Value))
        self.Log("self.fourResistance" + str(self.fourResistance))
        self.Log("self.fourSupport" + str(self.fourSupport))

        self.consolidatedFourHour = True
        
    def oneDayBarHandler(self, sender, consolidated):
        
        ''''''
        if self.IsWarmingUp:
            return
        
        if self.price.Current.Value > self.dayBB.MiddleBand.Current.Value:
            self.dayResistance = self.dayBB.UpperBand.Current.Value
            self.daySupport = self.dayBB.MiddleBand.Current.Value
            
        if self.price.Current.Value < self.dayBB.MiddleBand.Current.Value:
            self.dayResistance = self.dayBB.MiddleBand.Current.Value
            self.daySupport = self.dayBB.LowerBand.Current.Value
            
        self.Log("oneDayBarHandler")
        self.Log("self.price.Current.Value:" + str(self.price.Current.Value))
        self.Log("self.dayBB.MiddleBand.Current.Value:" + str(self.dayBB.MiddleBand.Current.Value))
        self.Log("self.dayResistance" + str(self.dayResistance))
        self.Log("self.daySupport" + str(self.daySupport))

        self.consolidatedDay = True
        
    def oneWeekBarHandler(self, sender, consolidated):
        
        ''''''
        if self.IsWarmingUp:
            return
        
        if self.price.Current.Value > self.weekBB.MiddleBand.Current.Value:
            self.weekResistance = self.weekBB.UpperBand.Current.Value
            self.weekSupport = self.weekBB.MiddleBand.Current.Value
            
        if self.price.Current.Value < self.weekBB.MiddleBand.Current.Value:
            self.weekResistance = self.weekBB.MiddleBand.Current.Value
            self.weekSupport = self.weekBB.LowerBand.Current.Value
        
        self.Log("oneWeekBarHandler")
        self.Log("self.price.Current.Value:" + str(self.price.Current.Value))
        self.Log("self.weekBB.MiddleBand.Current.Value:" + str(self.weekBB.MiddleBand.Current.Value))
        self.Log("self.weekResistance" + str(self.weekResistance))
        self.Log("self.weekSupport" + str(self.weekSupport))
        
        self.consolidatedWeek = True