Overall Statistics
Total Orders
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Start Equity
100000
End Equity
100000
Net Profit
0%
Sharpe Ratio
0
Sortino Ratio
0
Probabilistic 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
Estimated Strategy Capacity
$0
Lowest Capacity Asset
Portfolio Turnover
0%
# community_consolidator_5min_10May2024
# investigating. 5min consolidator live  does not match
# ref: https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/DataConsolidationAlgorithm.py
# region imports
from AlgorithmImports import *
# endregion

class WellDressedTanAlbatross(QCAlgorithm):

    def Initialize(self):
        #community_consolidator_5min_10May2024
        if self.LiveMode:
            self.Debug(f"######################################################################################################")
            self.Debug(f"community_consolidator_5min_10May2024. 15.May.24")
            self.log(f"Paper Trading. Data=QuantConnect")
            #self.log(f"IB. Data = IB")
        else:
            #self.Debug(f"Backtesting Live run on 10.May.2025. algorithm-log_L-670cdeb6f6bea36bc53ec2e7125f18f3.txt")
            self.Debug(f"Backtesting Live run on 15.May.2025. algorithm-log_L-3efb948db47052bfaffaafbe244ca0ab_15.May.txt")

        # self.start_dt = datetime(2024, 5, 9, 9, 30, 0)
        # self.end_dt = self.start_dt + timedelta(hours=1)
        #10.May
        # self.start_dt = datetime(2024, 5, 10, 12, 57, 0)  #NY time
        # self.end_dt = datetime(2024, 5, 10, 16, 0, 0)
        #15 & 16th .May
        self.start_dt = datetime(2024, 5, 15, 11, 10, 0)  #NY time
        self.end_dt = datetime(2024, 5, 16, 16, 0, 0)
        
        self.restrict_logging = True  # if true restrict logging outside start_dt/end_dt. only applies in backtesting

        self.set_start_date(self.start_dt)
        self.set_end_date(self.end_dt)
        self.SetCash(100000)

        self.ticker_name = 'SPY'
        self.my_symbol = self.add_equity(self.ticker_name, Resolution.MINUTE).symbol

        self.time_frame_1 = 5  # mins
        self.time_frame_2 = 60  # mins

        thirty_minute_consolidator = TradeBarConsolidator(timedelta(minutes=self.time_frame_1))

        thirty_minute_consolidator.data_consolidated += self.on_five_minute_bar_handler

        self.subscription_manager.add_consolidator(self.ticker_name, thirty_minute_consolidator)

        self.schedule.on(self.date_rules.every_day(self.ticker_name),
                        self.time_rules.after_market_open(self.ticker_name, -1),
                        self.market_about_to_open)
        self.schedule.on(self.date_rules.every_day(self.ticker_name),
                        self.time_rules.before_market_close(self.ticker_name, 1),
                        self.market_about_to_close)


        self.log(f"start_dt = {self.start_dt}")
        self.log(f"end_dt = {self.end_dt}")

    def do_not_log(self):
        #to restrict too much logging in BT        
        if not self.LiveMode and self.restrict_logging:
            if self.Time < self.start_dt or self.Time > self.end_dt:
                return True
        return False


    def OnData(self, data: Slice):
        #uncomment to restrict logging
        if self.do_not_log():
            return

        bar = data[self.my_symbol]
        self.log(f"OnData,\tbar end={bar.EndTime},\tself.Time={self.Time},\tnow={self.Time.now()},\tbar.Close={bar.Close}")

    def print_bar(self, tf, bar):
        #uncomment to restrict logging
        if self.do_not_log():
            return

        self.log(f"{tf} min,\tbar end={bar.EndTime},\tself.Time={self.Time},\tnow={self.Time.now()},\tbar.Close={bar.Close}")

    def on_five_minute_bar_handler(self, sender, consolidated):
        self.print_bar(self.time_frame_1, consolidated)

    def on_end_of_day(self, symbol):
        self.log(f"on_end_of_day. self.Time={self.Time}, symbol={symbol}")

    def on_end_of_algorithm(self):
        self.log(f"on_end_of_algorithm. self.Time={self.Time}")

    def market_about_to_open(self):
        self.log("------------------------------------------------------------------------")
        self.log(f"market_about_to_open. self.Time={self.Time}")

    def market_about_to_close(self):
        self.log(f"market_about_to_close. self.Time={self.Time} *************************")