def find_linear_metrics(ld: LazyDictionary) -> Iterable[str]: linear_metrics = calculate_trends(ld["data_df"]) good_linear_metrics = [] for k, t in linear_metrics.items(): if t[1] < 0.1: good_linear_metrics.append(k) return good_linear_metrics
def show_trends(request): user = request.user validate_user(user) stocks = user_watchlist(user) timeframe = Timeframe(past_n_days=300) ld = LazyDictionary() ld["cip_df"] = lambda ld: selected_cached_stocks_cip(stocks, timeframe) ld["trends"] = lambda ld: calculate_trends(ld["cip_df"]) ld["rank"] = lambda ld: rank_cumulative_change( ld["cip_df"].filter(ld["trends"].keys(), axis="index"), timeframe ) trending_companies_plot = cache_plot( f"{user.username}-watchlist-trends", lambda ld: plot_company_rank(ld), datasets=ld, ) context = { "watchlist_trends": ld["trends"], "timeframe": timeframe, "trending_companies_uri": trending_companies_plot, "trending_companies_plot_title": "Trending watchlist stocks (ranked): {}".format( timeframe.description ), } return render(request, "watchlist-rank.html", context=context)
def test_calculate_trends(): df = pd.DataFrame.from_records( [ { "asx_code": "ANZ", "2021-01-02": 1.0, "2021-01-03": 2.0, "2021-01-04": 3.0, "2021-01-05": 4.0, }, { "asx_code": "BHP", "2021-01-02": 1.0, "2021-01-03": 1.0, "2021-01-04": 1.0, "2021-01-05": 1.0, }, ], index="asx_code", ) result = calculate_trends(df) assert result is not None assert isinstance(result, OrderedDict) assert len(result) == 1 for stock, val in result.items(): assert stock == "ANZ" assert isinstance(val, tuple) assert val[0] - 1.0 < 1e-6 assert val[1] < 1e-6 assert val[ 2] == 0.0 # since 30 days data is not available, these must be zero assert val[3] == 0.0 assert val[4] == "none"
def test_calculate_trends(): df = pd.DataFrame.from_records([ { 'asx_code': 'ANZ', '2021-01-02': 1.0, '2021-01-03': 2.0, '2021-01-04': 3.0, '2021-01-05': 4.0 }, { 'asx_code': 'BHP', '2021-01-02': 1.0, '2021-01-03': 1.0, '2021-01-04': 1.0, '2021-01-05': 1.0 }, ], index='asx_code') result = calculate_trends(df, set(['ANZ', 'BHP'])) assert result is not None assert isinstance(result, OrderedDict) assert len(result) == 1 for stock, val in result.items(): assert stock == 'ANZ' assert isinstance(val, tuple) assert val[0] - 1.0 < 1e-6 assert val[1] < 1e-6 assert val[2] == '1.00' assert val[3] == 'none'
def find_exp_metrics(ld: LazyDictionary) -> Iterable[str]: exp_metrics = calculate_trends( ld["data_df"], polynomial_degree=2, nrmse_cutoff=0.05 ) good_linear_metrics = set(ld["linear_metrics"]) good_exp_metrics = [] for k, t in exp_metrics.items(): if t[1] < 0.1 and k not in good_linear_metrics: good_exp_metrics.append(k) return good_exp_metrics
def show_trends(request): validate_user(request.user) watchlist_stocks = user_watchlist(request.user) all_dates = desired_dates(start_date=300) # last 300 days cip = company_prices(watchlist_stocks, all_dates=all_dates, fields='change_in_percent', fail_missing_months=False) trends = calculate_trends(cip, watchlist_stocks, all_dates) # for now we only plot trending companies... too slow and unreadable to load the page otherwise! cip = rank_cumulative_change(cip.filter(trends.keys(), axis='index'), all_dates=all_dates) trending_companies_plot = plot_company_rank(cip) context = { 'watchlist_trends': trends, 'trending_companies_plot': trending_companies_plot, 'trending_companies_plot_title': 'Trending watchlist companies by rank (past 300 days)' } return render(request, 'trends.html', context=context)
def show_trends(request): validate_user(request.user) watchlist_stocks = user_watchlist(request.user) timeframe = Timeframe(past_n_days=300) cip = selected_cached_stocks_cip(watchlist_stocks, timeframe) trends = calculate_trends(cip, watchlist_stocks) #print(trends) # for now we only plot trending companies... too slow and unreadable to load the page otherwise! cip = rank_cumulative_change(cip.filter(trends.keys(), axis="index"), timeframe) #print(cip) trending_companies_plot = plot_company_rank(cip) context = { "watchlist_trends": trends, "trending_companies_plot": trending_companies_plot, "trending_companies_plot_title": "Trending watchlist companies by rank: {}".format( timeframe.description), } return render(request, "trends.html", context=context)