Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
from collections import deque
from datetime import datetime, timedelta
from numpy import sum

class ConsolidatorAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2017, 1, 1)                                           # Set Start Date
        self.SetEndDate(2017, 1, 2)                                             # Set End Date
        self._startingCash = 2000                                               # Assign starting cash
        self.SetCash(self._startingCash)                                        # Set Cash
        self._ticker = "ETHUSD"                                                 # Assign ticker
        self._resolution = Resolution.Hour                                      # Assign resolution
        self.crypto = self.AddCrypto(self._ticker, self._resolution)            # Add Crpyto 
        
        self._fastPeriod = 12                                                   # Set moving average period
        
        self.SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Cash);       # Set brokerage model
        
        # Build consolidator
        consolidator = TradeBarConsolidator(TimeSpan.FromHours(1))              # Consolidate hourly resolution from hourly data
        self.SubscriptionManager.AddConsolidator(self._ticker, consolidator)    # Subscribe consolidator
        consolidator.DataConsolidated += self.OnCustomHandler
        
        # Define custom indicator using consolidated data
        self._fastSmaCustomTimeFrame = SimpleMovingAverage("custom", self._fastPeriod)
        self._fastSmaCustomTimeFrame.Updated += self.CustomUpdated
        self.customWindow = RollingWindow[IndicatorDataPoint](5)
        self.RegisterIndicator(self._ticker, self._fastSmaCustomTimeFrame, consolidator)

        # Define built-in indicator using standard resolution data
        self._fastSmaStandardResolution = self.SMA(self._ticker, self._fastPeriod, self._resolution)


    def CustomUpdated(self, sender, updated):
        self.customWindow.Add(updated)


    def OnData(self, data):

        if (not self._fastSmaCustomTimeFrame.IsReady and not self._fastSmaStandardResolution.IsReady):
            return
        
        self.Debug(f"Sma standard time resolution: {self._fastSmaStandardResolution}")

    
    def OnCustomHandler(self, sender, data):

        if (not self._fastSmaCustomTimeFrame.IsReady and not self._fastSmaStandardResolution.IsReady):
            return
        
        self.Debug(f"Sma custom time frame: {self._fastSmaCustomTimeFrame}")


# Python implementation of SimpleMovingAverage.
# Represents the traditional simple moving average indicator (SMA).
class SimpleMovingAverage(PythonIndicator):
    def __init__(self, name, period):
        self.Name = name
        self.Value = 0
        self.queue = deque(maxlen=period)

    # Update method is mandatory
    def Update(self, input):
        self.queue.appendleft(input.Value)
        count = len(self.queue)
        self.Value = sum(self.queue) / count
        return count == self.queue.maxlen