def get_stock(stock_name): """ render template for stock page of site. USe Jinja to dynamically display assignment data :return: HTML rendering """ # look up stock in mongo db stock = mongo.db.stocks.find_one({"_id": stock_name}) # check that a result was returned from db if stock is None: abort(404) # tack on plotly urls stock['price_plot'] = plot_stock_price(stock) stock['boxplot'] = plot_returns_boxplot_stock(stock) stock['returns_plot'] = plot_yearly_returns(stock) # tack on statistics stock['max'] = calculate_historical_max(stock) stock['min'] = calculate_historical_min(stock) stock['best'] = calculate_largest_daily_gain(stock) stock['worst'] = calculate_largest_daily_loss(stock) # predict future price using ML stock_data = list(mongo.db.stocks.find()) features, targets = extract_feature_data(stock_data) # create the svm and dtree svm = create_support_vector_regression(features, targets) dtree = create_decision_tree_regression(features, targets) # predict price of the stock features_stock, _ = extract_feature_data([stock]) stock['future_returns'] = (svm.predict(features_stock)[0] + dtree.predict(features_stock)[0]) / 2 # render the HTML return render_template('stock.html', stock=stock)
def test_svm_regression(self): # extract features and labels features, labels = extract_feature_data([self.test_stock, self.test_stock2]) # create decision tree classifier svm = create_support_vector_regression(features, labels) # check manually set parameters of svm correct self.assertEqual(svm.kernel, 'linear') self.assertEqual(svm.C, 100)
def test_dtree_regression(self): # extract features and labels features, labels = extract_feature_data([self.test_stock, self.test_stock2]) # create decision tree classifier dtree = create_decision_tree_regression(features, labels) # check manually set parameters of decision tree correct self.assertEqual(dtree.max_depth, 2) self.assertEqual(dtree.min_samples_leaf, 1)
def test_extra_feature_data_single(self): # extract the features and labels features, labels = extract_feature_data([self.test_stock2]) # check that features and labels correctly created self.assertEqual(len(features), 1) self.assertIn([0.0], features) self.assertEqual(len(labels), 1) self.assertIn(-40.938865746148089, list(labels))
def get_portfolio(portfolio_name): """ render template for portfolio page of site. USe Jinja to dynamically display assignment data :return: HTML rendering """ # look up portfolio in mongo db portfolio = mongo.db.portfolios.find_one({"_id": portfolio_name}) index_data = mongo.db.stocks.find_one({"_id": "^GSPC"}) # check that a result was returned from db if portfolio is None or index_data is None: abort(404) # tack on value field portfolio['value'] = get_total_value_portfolio(portfolio) # tack on the plotly urls portfolio['breakdown_plot'] = plot_portfolio_breakdown(portfolio) portfolio['value_plot'] = plot_portfolio_value_vs_time(portfolio) portfolio['returns_plot'] = plot_portfolio_returns_vs_market( portfolio, index_data) portfolio['boxplot'] = plot_returns_boxplot(portfolio, index_data) # tack on statistics portfolio['alpha'] = get_portfolio_alpha(portfolio, index_data) portfolio['beta'] = get_portfolio_beta(portfolio, index_data) portfolio['sharpe_ratio'] = get_portfolio_sharpe_ratio( portfolio, index_data) portfolio['jensen_measure'] = get_portfolio_jensen_measure( portfolio, index_data) # add stock data stock_data = list(mongo.db.stocks.find()) # perform ML features, targets = extract_feature_data(stock_data) svm = create_support_vector_regression(features, targets) dtree = create_decision_tree_regression(features, targets) # add future return fields stocks = {} for stock in stock_data: features_stock, _ = extract_feature_data([stock]) if len(features_stock) == 0: stocks[stock['symbol']] = 0.0 else: stocks[ stock['symbol']] = (svm.predict(features_stock)[0] + dtree.predict(features_stock)[0]) / 2 return render_template('portfolio.html', portfolio=portfolio, stocks=stocks)
def test_extra_feature_data_multiple(self): # extract the features and labels features, labels = extract_feature_data([self.test_stock, self.test_stock2]) # check that features and labels correctly created self.assertEqual(len(features), 2) self.assertIn([0.0], features) self.assertIn([1.2], features) self.assertEqual(len(labels), 2) self.assertIn(-40.938865746148089, list(labels)) self.assertIn(-3.6179451381311551, list(labels))
def get_stocks(): """ render template for stock page of site. USe Jinja to dynamically display assignment data :return: HTML rendering """ # look up stock in mongo db stock_data = list(mongo.db.stocks.find()) # check that a result was returned from db if stock_data is None: abort(404) # perform ML algos features, targets = extract_feature_data(stock_data) # create the svm and dtree svm = create_support_vector_regression(features, targets) dtree = create_decision_tree_regression(features, targets) # tack on plotly urls formatted_data = format_stock_data(stock_data) clusters = k_means(formatted_data, 3) cluster_assignments = get_cluster_assignments(formatted_data, clusters) plots = {} plots['kmeans'] = plot_clusters(formatted_data, clusters) plots['hierarchy'] = plot_hierarchy(formatted_data) plots['svm'] = plot_support_vector_regression(features, targets, svm) plots['dtree'] = plot_decision_tree_regression(features, targets, dtree) # add expected returns stock_rets = {} for stock in stock_data: features_stock, _ = extract_feature_data([stock]) if len(features_stock) == 0: stock_rets[stock['symbol']] = 0.0 else: stock_rets[ stock['symbol']] = (svm.predict(features_stock)[0] + dtree.predict(features_stock)[0]) / 2 # render the HTML return render_template('stocks.html', stocks=stock_data, plots=plots, clusters=cluster_assignments, stock_rets=stock_rets)
def get_svm(): """ API endpoint to get regression line for stock performance :param symbol: stock to do regression for :return: JSON representation regression line """ # get stock data from db stock_data = mongo.db.stocks.find() if stock_data is None: abort(404) # format the data features, targets = extract_feature_data(list(stock_data)) # create the svm svm = create_support_vector_regression(features, targets) # make the plot get_accuracy(features, targets, svm.predict(features)) res = plot_support_vector_regression(features, targets, svm) return jsonify({"plot_url": res}), 200