def test_rate_ratings(self): self.assertEqual(-1, rate_ratings(AnalystRatings(1, 1, 0)), "rate ratings - test 1") self.assertEqual(0, rate_ratings(AnalystRatings(1, 0, 1)), "rate ratings - test 2") self.assertEqual(1, rate_ratings(AnalystRatings(0, 1, 1)), "rate ratings - test 3")
def rate_small_ratings(ratings: AnalystRatings): count = ratings.count() sum = ratings.sum_weight() if count == 0: return -1 rating = round(sum / count, 1) if rating <= 1.5: return 1 if rating >= 2.5: return -1 return 0
def fromJson(self, json_str: str) -> Stock: stock_json = json.loads(json_str) stock = Stock(stock_json["stock_id"], stock_json["name"], None) for attr in stock_json.keys(): if attr == "stock_id" or attr == "stock_name": continue if attr == "history": history = stock_json["history"] stock.history = History(history["today"], history["half_a_year"], history["one_year"]) elif attr == "monthClosings": stock.monthClosings = MonthClosings() stock.monthClosings.closings = stock_json["monthClosings"].get("closings") elif attr == "ratings": ratings = stock_json["ratings"] stock.ratings = AnalystRatings(ratings["buy"], ratings["hold"], ratings["sell"]) elif attr == "reaction_to_quarterly_numbers": reaction = stock_json["reaction_to_quarterly_numbers"] stock.reaction_to_quarterly_numbers = \ ReactionToQuarterlyNumbers(reaction["price"], reaction["price_before"], reaction["index_price"], reaction["index_price_before"], reaction["date"]) else: stock.__setattr__(attr, stock_json[attr]) return stock
def scrap_analysen(soup: BeautifulSoup): h3s = soup.findAll("h3", {"class": "box-headline"}) for h3 in h3s: headline = h3.get_text().strip() if headline.startswith("Kursziele ") and headline.endswith(" Aktie"): ratings = {} ratingLegend = h3.parent.find("div", {"class": "ratingLegend"}) rows = ratingLegend.findAll("div", {"class": "clearfix"}) for row in rows: row_content = row.get_text().strip().split(": ") if len(row_content) == 2: ratings[row_content[0]] = row_content[1] return AnalystRatings(int(ratings["Buy"]), int(ratings["Hold"]), int(ratings["Sell"])) return None
def scrap_ratings(stock, stock_storage: StockStorage): filename = stock_storage.getStoragePath("ratings", "html") ratings = {"kaufen": 0, "halten": 0, "verkaufen": 0} content = stock_storage.storage_repository.load(filename) if content: soup = BeautifulSoup(content, 'html.parser') for row in soup.findAll("tr"): columns = row.findAll("td") type = columns[0].get_text().strip() count = columns[1] count.div.decompose() ratings[type] = int(count.get_text().strip()) stock.ratings = AnalystRatings(ratings["kaufen"], ratings["halten"], ratings["verkaufen"]) return stock