Overall Statistics
Total Trades
27
Average Win
0.73%
Average Loss
-0.26%
Compounding Annual Return
-29.677%
Drawdown
10.000%
Expectancy
0.706
Net Profit
-2.946%
Sharpe Ratio
-0.658
Probabilistic Sharpe Ratio
27.663%
Loss Rate
55%
Win Rate
45%
Profit-Loss Ratio
2.75
Alpha
-0.186
Beta
1.429
Annual Standard Deviation
0.289
Annual Variance
0.083
Information Ratio
-0.871
Tracking Error
0.215
Treynor Ratio
-0.133
Total Fees
$27.30
Estimated Strategy Capacity
$2000000000.00
Lowest Capacity Asset
QQQ RIWIV7K5Z9LX
class FocusedAsparagusBull(QCAlgorithm):

    def Initialize(self):
        """
        (2021, 9, 13)-(2021, 9, 13)-5 stocks +3.64
        (2021, 9, 13)-(2021, 10, 13) -5 stocks +0.76
        (2021, 10, 13)-(2021, 11, 13)-5 stocks +6.08
        (2021, 11, 13)-(2021, 12, 13)-5 stocks -2.95
        (2021, 11, 13)-(2021, 12, 13)-50 stocks fail
        (2021, 11, 13)-(2021, 12, 13)-25 stocks fail
        (2021, 11, 13)-(2021, 12, 13)-15 stocks fail
        (2021, 11, 13)-(2021, 12, 13)-10 stocks fail
        (2021, 11, 13)-(2021, 12, 13)-6 stocks +1.58
        (2021, 11, 13)-(2021, 12, 13)-9 stocks -6.57
        
        
        """
        self.SetStartDate(2021, 11, 13)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        self.SetEndDate(2021, 12, 13)
        self.UniverseSettings.Resolution = Resolution.Hour
        self.AddUniverse(self.CoarseSelectionFunction)
        self._numberofsymbols = 5
        self._changes = None
        self._Grades = {}
        self._gister_winloss_alphas = {}
        self.sum = 0
        self._buy = {}
        self._sell = {}
        self._lengt_avg = 7
        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(10, 1), Action(self.SpecificTime))
        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(15, 55), Action(self.SpecificTime2))
        self.SetWarmup(timedelta(15))
       
    def SpecificTime(self):
        self._conditie_teller=0
        self.Debug("SpecificTime: Fired at : {0}".format(self.Time))
        self.sum = sum(self._gister_winloss_alphas.values())
        self._n_day_mean = 0
        self._std = 0
        self.teller_buy = 0
        self.teller_sell = 0
        self._how_many_std_last_day =0
        for key in self._gister_winloss_alphas:
            if bool(self.n_day_mean(key,self._lengt_avg)[3]):
                    if self.n_day_mean(key,self._lengt_avg)[3] > .5:
                        return
                    
            if bool(self.n_day_mean(key,self._lengt_avg)[0]):
                self._n_day_mean = self.n_day_mean(key,self._lengt_avg)[0]
                
            if bool(self.n_day_mean(key,self._lengt_avg)[1]):
                self._std = self.n_day_mean(key,self._lengt_avg)[1]
                
            
            if bool(self.n_day_mean(key,self._lengt_avg)[2]):
                self._how_many_std_last_day = self.n_day_mean(key,self._lengt_avg)[2]
                
                
            if self._n_day_mean > (self._how_many_std_last_day * self._std) and (self._how_many_std_last_day * self._std) > (self._n_day_mean - self._std):
                    self._buy[key]= (self._n_day_mean - self._std)-self._std
            
        
        for key in self._buy:
            if self.Securities[key].IsTradable:
                self.SetHoldings(key,0.4 * self._buy[key]/(sum(self._buy.values())))
        for key in self._sell:
            if self.Securities[key].IsTradable:
                self.SetHoldings(key,-0.4 * self._sell[key]/(sum(self._sell.values())))
        
    def SpecificTime2(self):
        self._lengt_avg = 14
        
        for key in self._buy:
                self.SetHoldings(key,0.4 * self._buy[key]/(sum(self._buy.values())))
      
        for key in self._sell:
                self.SetHoldings(key,0)
        
        self._sell = {}
        self._buy = {}
        
       
        
    def CoarseSelectionFunction(self, coarse):
        for cf in coarse:
            symbol = cf.Symbol
            
        self.sortedByDollarVolume = sorted(coarse, key=lambda c: c.DollarVolume, reverse=True)
        
        
        self.symbols_top = [c.Symbol for c in self.sortedByDollarVolume[:self._numberofsymbols]]# if c.Price > 10]
    
        
        return self.symbols_top
    '''
    def gisterwinloss(self,security):
      
        history = self.History([security.Symbol], 1, Resolution.Daily)
        
        self.gister_open = history.iloc[0]["open"]
        self.gister_close = history.iloc[0]["close"]
        self.winloss = self.gister_close - self.gister_open
        
        return  self.winloss
    '''
    def n_day_mean(self,security,n):
        self._winloss = 0
        self._total = 0
        self._winloss_day = 0
        self._winloss_day_list = []
        self._total_diff_to_mean_squar = 0
        self._how_many_std_last_day = 0
        self._std = 444
        if  not self.Securities[security].IsTradable:
            return 0,0,0,1
       
        if self.History([security], n, Resolution.Daily).empty:
            return 0,0,0,1
      
        history = self.History([security], n, Resolution.Daily)
        for i in range(0,n):
            self.Debug(f"aapje")
            if history.iloc[i]["open"] in history.values:
                self.Debug(f"aapjegiragge")
                self._open = history.iloc[i]["open"]
            if history.iloc[i]["close"] in history.values:
                self._close = history.iloc[i]["close"]
            self._winloss = self._winloss + (self._close - self._open)
            self._winloss_day = self._close - self._open
            self._winloss_day_list.append(self._winloss_day)
            
        for j in self._winloss_day_list:
            self._total_diff_to_mean_squar = self._total_diff_to_mean_squar + (j-(sum(self._winloss_day_list)/len(self._winloss_day_list)))**2
            
        self._std = math.sqrt(self._total_diff_to_mean_squar/(len(self._winloss_day_list)-1))
        self._how_many_std_last_day = (history.iloc[n-1]["close"] - history.iloc[n-1]["open"])/self._std
        if not bool(self._std):
            self._std = 0
        if not bool(self._winloss):
            self._winloss = 0
        if not bool(self._how_many_std_last_day):
            self._how_many_std_last_day = 0
        return  self._winloss/n, self._std , self._how_many_std_last_day,0
    
        
    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
        '''
        self.Debug("receiving data")
        if self._changes is None:
            self.Debug("no changes")
            return
      
        for security in self._changes.RemovedSecurities:
            if security in self._gister_winloss_alphas.keys():
                del self._gister_winloss_alphas[f"{security.Symbol}"]  
                
        for security in self._changes.AddedSecurities:
            self._gister_winloss_alphas[f"{security.Symbol}"] = 0#self.gisterwinloss(security)
            
        self.changes = None
        '''
        if not self.Portfolio.Invested:
               self.SetHoldings("SPY", 1)
        '''
       
    def OnSecuritiesChanged(self, changes):
        self._changes = changes