def classify_sectors_uptrend(self, table_name=None, time_from=None, time_to=None, stocks=None, from_db=False, is_industries=False, separator=""): # print(self.trend_data) if stocks is None: stocks = self.get_trend_slice(table_name=table_name, time_from=time_from, time_to=time_to, from_db=from_db) # technical, healthcare, financials etc. # print(stocks) if stocks is not None: stocks = Utils.add_first_last_perc_diff(stocks) stocks[ "industry"] = stocks["sector"] + separator + stocks["industry"] group_by = "sector" if is_industries is False else "industry" stocks = stocks.groupby(by=group_by).mean() return stocks else: return stocks
def set_spy_sectors_trend(self): table_name = TableName.DAY if self.trend_data is None: self.classify_sectors_uptrend(table_name=table_name, time_from="-14d", time_to="0") df1 = self.trend_data.iloc[0:int(round(len(self.trend_data) / 2, 0))] df2 = self.trend_data.iloc[int(round(len(self.trend_data) / 2, 0)):len(self.trend_data)] # ---------------------------set sectors trend -------------------- self.sectors_trend.append(self.classify_sectors_uptrend(stocks=df1)) self.sectors_trend.append(self.classify_sectors_uptrend(stocks=df2)) # ----------------------------set spy trend----------------------------- dfs1 = df1[df1.sym == "SPY"] dfs2 = df2[df2.sym == "SPY"] self.spy_trend.append(Utils.add_first_last_perc_diff(dfs1)) self.spy_trend.append(Utils.add_first_last_perc_diff(dfs2)) print("set_spy_sectors_trend() - done")
def sectors_uptrend_by_month(self, yf=2017, yt=None, show_chart=True): # technical, healthcare, financials etc. sectors = self.db.get_sectors() sectors_month_stats = pd.DataFrame() sec_year_stats = {} # print(sectors) # self.sectors = [sec] stocks = sdf.retype(self.db.load_data(TableName.DAY)) stocks.index = pd.to_datetime(stocks.index, utc=True) stocks['month'] = stocks.index.to_series().dt.month stocks['year'] = stocks.index.to_series().dt.year if not yt: max_year = stocks.year.max() else: max_year = yt # data contain more sectors since 2016 for year in range(yf, max_year + 1): one_year_stocks = stocks.where( cond=stocks['year'] == year).dropna() sectors_month_stats = pd.DataFrame() # print(one_year_stocks) min_month = one_year_stocks.month.min() max_month = one_year_stocks.month.max() for month in range(int(min_month), int(max_month) + 1): one_month_stocks = one_year_stocks.where( cond=one_year_stocks['month'] == month).dropna() one_month_stocks = Utils.add_first_last_perc_diff( one_month_stocks) sectors_data = one_month_stocks.groupby( by=one_month_stocks['sector']).mean() sectors_month_stats["sector"] = sectors_data.index sectors_month_stats[month] = sectors_data.flpd.tolist() sectors_month_stats.set_index('sector', inplace=True) sec_year_stats[year] = sectors_month_stats.copy() if show_chart: self.show_sectors_stats(sec_year_stats, False, plt) return sectors_month_stats
def top_stocks(self, group=None, ascending=False, time_from=None, time_to=None, table=None, from_top=0, show_stocks_num=20, is_industries=False, separator=None): st.empty() table = TableName.DAY if time_from.find("m") > -1: table = TableName.MIN15 subject = "Loosers: " if ascending else "Gainers: " if isinstance(group, str): industry = str.split(group, separator) group = industry[1] if len(industry) > 1 else industry[0] group = [group] if is_industries: stocks = self.db.load_data( table_name=table, industries=group, time_from=time_from, time_to=time_to, ) else: stocks = self.db.load_data( table_name=table, sectors=group, time_from=time_from, time_to=time_to, ) # self.stocks = FinI.add_change(self.stocks) stocks = Utils.add_first_last_perc_diff(stocks) self.print_stocks_list(stocks, ascending, from_top=from_top, show_stocks_num=show_stocks_num)