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
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
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Data.Consolidators import *
from datetime import timedelta


class EndOfDayTrader(QCAlgorithm):

    def Initialize(self):
        self.SetCash(100000)
        self.SetStartDate(2021,4,15)
        self.SetEndDate(2021,4,15)
        self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.lookback = 200
        self.premarketCloseData = {}


        # define our 374 minute trade bar consolidator. we can
        # access the 374 minute bar from the DataConsolidated events
        preMarketCloseConsolidator = TradeBarConsolidator(timedelta(minutes=373))

        # attach our event handler. the event handler is a function that will
        # be called each time we produce a new consolidated piece of data.
        preMarketCloseConsolidator.DataConsolidated += self.PreMarketCloseHandler

        # this call adds our 30 minute consolidator to
        # the manager to receive updates from the engine
        self.SubscriptionManager.AddConsolidator("SPY", preMarketCloseConsolidator)

        # Schedule function 16 minutes before every market close to submit MOC orders
        
        self.Schedule.On(self.DateRules.EveryDay(self.symbol), \
                        self.TimeRules.BeforeMarketClose(self.symbol, 16), \
                        Action(self.EveryMarketClose))
                        

                        
    def EveryMarketClose(self):
        self.Log("EveryMarketClose() called.")
        # this is called at the last possible minute to submit MOC orders
        
        # get historical data - ends yesterday
        
        self.opens = self.History(self.symbol, self.lookback, Resolution.Daily)["open"].values.tolist()
        self.highs = self.History(self.symbol, self.lookback, Resolution.Daily)["high"].values.tolist()
        self.lows = self.History(self.symbol, self.lookback, Resolution.Daily)["low"].values.tolist()
        self.closes = self.History(self.symbol, self.lookback, Resolution.Daily)["close"].values.tolist()
        self.volumes = self.History(self.symbol, self.lookback, Resolution.Daily)["volume"].values.tolist()
        #self.vix_closes = self.History(CBOE,self.cboeVix, self.lookback, Resolution.Daily)["close"].values.tolist()
        

        # now append today's value of the above using data 16 minutes prior to market close
        self.opens.append(self.premarketCloseData['open'])
        self.highs.append(self.premarketCloseData['high'])
        self.lows.append(self.premarketCloseData['low'])
        self.closes.append(self.premarketCloseData['close'])
        self.volumes.append(self.premarketCloseData['volume'])
        
        self.Log("Spy closed at 415.87 on 4/15/21 at 4:00 PM, current SPY price at 3:43PM is: {}".format(self.premarketCloseData['close']))
       
        
        
        
    

    def PreMarketCloseHandler(self, sender, consolidated):
        self.Log("PreMarketCloseHandler() called, current Close price is {}".format(consolidated.Close))
        # called 17 minutes prior to market close
        
        '''This is our event handler for our 373 minute trade bar defined above in Initialize(). So each time the
        consolidator produces a new 373 minute bar, this function will be called automatically.'''
        
        self.premarketCloseData['open'] = consolidated.Open
        self.premarketCloseData['high'] = consolidated.High
        self.premarketCloseData['low'] = consolidated.Low
        self.premarketCloseData['close'] = consolidated.Close
        self.premarketCloseData['volume'] = consolidated.Volume