Overall Statistics
class PierceAlpha(AlphaModel):
    
    def __init__(self, algorithm, symbol, pattern):
        ## Add Quandl data for the Federal Interest Rate
        self.symbol = symbol
        self.algorithm = algorithm
        self.pattern = pattern
        
    def Update(self, algorithm, slice):
        
        insights = []
        
        ## Check for all Symbols in current data Slice
        if not slice.ContainsKey(self.symbol):
            self.algorithm.Debug("There is no data")
            return
        
        if slice is None:
            self.algorithm.Debug("No Slice")
            return []
        
        # self.algorithm.Debug("{} | Open: {} Close: {} " . format(self.symbol ,slice[self.symbol].Open, slice[self.symbol].Close) )
        # self.algorithm.Log("self pattern is {} " . format(self.pattern))
        
        if self.pattern is None: 
            self.algorithm.Debug("pattern not yet defined. skipping")
            return []
            
        if (self.pattern.Current.Value == 1):
            self.algorithm.Debug("Time: {} pattern is 1 going long".format(slice.Time) )
            insights.append(Insight(self.symbol, timedelta(minutes=60), InsightType.Price, InsightDirection.Up))
        
        elif (self.pattern.Current.Value== -1):    
            self.algorithm.Debug("pattern is -1 going short")
            insights.append(Insight(self.symbol, timedelta(minutes=60), InsightType.Price, InsightDirection.Down))
        
        else:
            self.algorithm.Debug("no pierce recognized skipping")
        
        return insights
from FederalInterestRateAlphaModel import PierceAlpha
from Execution.ImmediateExecutionModel import ImmediateExecutionModel
from datetime import timedelta

class DynamicNadionCoil(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 6, 11)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        self.SetWarmUp(20)
        
        self.symbol = self.AddEquity("TSLA", Resolution.Minute, Market.USA).Symbol
        self.Consolidate(self.symbol, timedelta(minutes = 45), self.FortyFiveMinuteBarHandler)
        self.pattern = self.CandlestickPatterns.Piercing(self.symbol)
        
        self.AddAlpha(PierceAlpha(self, symbol = self.symbol, pattern = self.pattern))

        self.SetExecution(ImmediateExecutionModel())

        self.SetPortfolioConstruction(InsightWeightingPortfolioConstructionModel())

        self.SetRiskManagement(TrailingStopRiskManagementModel(0.03))

        


    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
            Arguments:
                data: Slice object keyed by symbol containing the stock data
        '''

        if self.IsWarmingUp:
            self.Debug("Still warming up...")
            return
    
    def FortyFiveMinuteBarHandler(self, consolidated):
      self.Debug(f"{consolidated.EndTime} >> FortyFiveMinuteBarHandler >> {consolidated.Close}")