def select_stocks(self, buy_label, sell_label): if self.start_date.weekday() == 5 or self.start_date.weekday() == 6: self.start_date = datetime.datetime(self.start_date.year, self.start_date.month, self.start_date.day - 2) print((self.start_date.date(), self.end_date.date())) for ticker in self.tickers: try: data = pid.get_data_yahoo(ticker, start=self.start_date, end=self.end_date)["Close"] dates = data.index last_price = data[len(dates) - 1] end_term_ret = (last_price - data[self.start_date]) / data[self.start_date] print((ticker, end_term_ret)) self.stocks.append((ticker, end_term_ret, last_price)) except (KeyError, IOError) as e: raise e self.stocks.sort(key=lambda t: t[1]) utils.print_stocks("Stocks sorted on return", self.stocks) decile = int(len(self.stocks) * 0.10) sell_stocks = self.stocks[:decile] buy_stocks = self.stocks[decile * 9:] buy_stocks.reverse() utils.print_stocks("Sell stocks", sell_stocks) utils.print_stocks("Buy stocks", buy_stocks) utils.save_portfolio(sell_label, sell_stocks) utils.save_portfolio(buy_label, buy_stocks)
def select_stocks(self, buy_label, sell_label): if self.start_date.weekday() == 5 or self.start_date.weekday() == 6: self.start_date = datetime.datetime(self.start_date.year, self.start_date.month, self.start_date.day - 2) print((self.start_date.date(), self.end_date.date())) for ticker in self.tickers: try: data = pid.get_data_yahoo(ticker, start=self.start_date, end=self.end_date)["Close"] dates = data.index last_price = data[len(dates) - 1] end_term_ret = (last_price - data[self.start_date]) / data[self.start_date] print((ticker, end_term_ret)) self.stocks.append((ticker, end_term_ret, last_price)) except (KeyError, IOError) as e: raise e self.stocks.sort(key=lambda t: t[1]) utils.print_stocks("Stocks sorted on return", self.stocks); decile = int(len(self.stocks) * 0.10) sell_stocks = self.stocks[:decile] buy_stocks = self.stocks[decile * 9:] buy_stocks.reverse() utils.print_stocks("Sell stocks", sell_stocks) utils.print_stocks("Buy stocks", buy_stocks) utils.save_portfolio(sell_label, sell_stocks) utils.save_portfolio(buy_label, buy_stocks)
start_date = datetime.datetime(start_date.year, start_date.month, start_date.day - 2) print((start_date.date(), end_date.date())) for ticker in tickers: try: data = pid.get_data_yahoo(ticker, start=start_date, end=end_date)["Close"] dates = data.index last_price = data[len(dates) - 1] end_term_ret = (last_price - data[start_date]) / data[start_date] print((ticker, end_term_ret)) stocks.append((ticker, end_term_ret, last_price)) except (KeyError, IOError): print("f**k up") stocks.sort(key=lambda t: t[1]) utils.print_stocks("Stocks sorted on return", stocks); decile = int(len(stocks) * 0.10) sell_stocks = stocks[:decile] buy_stocks = stocks[decile * 9:] buy_stocks.reverse() utils.print_stocks("Sell stocks", sell_stocks) utils.print_stocks("Buy stocks", buy_stocks) utils.save_portfolio("Sell.csv", sell_stocks) utils.save_portfolio("Buy.csv", buy_stocks)
if start_date.weekday() == 5 or start_date.weekday() == 6: start_date = datetime.datetime(start_date.year, start_date.month, start_date.day - 2) print((start_date.date(), end_date.date())) for ticker in tickers: try: data = pid.get_data_yahoo(ticker, start=start_date, end=end_date)["Close"] dates = data.index last_price = data[len(dates) - 1] end_term_ret = (last_price - data[start_date]) / data[start_date] print((ticker, end_term_ret)) stocks.append((ticker, end_term_ret, last_price)) except (KeyError, IOError): print("f**k up") stocks.sort(key=lambda t: t[1]) utils.print_stocks("Stocks sorted on return", stocks) decile = int(len(stocks) * 0.10) sell_stocks = stocks[:decile] buy_stocks = stocks[decile * 9:] buy_stocks.reverse() utils.print_stocks("Sell stocks", sell_stocks) utils.print_stocks("Buy stocks", buy_stocks) utils.save_portfolio("Sell.csv", sell_stocks) utils.save_portfolio("Buy.csv", buy_stocks)