def get_tech_ind(): """ Calculate the technical indicators and cache the data """ cache.set( 'tech_ind', get_techical_indicators(cache.get('comp_eod'), cache.get('sp500_eod')))
def home(): HOME_TITLE = 'Stock Dashboard' current_cache_ticker = cache.get('comp_ticker') if not current_cache_ticker: current_cache_ticker = 'TSLA' user_ticker = request.values.get('search-bar-ticker', default=current_cache_ticker) print('CURRENT TICKER',user_ticker) get_data = True if user_ticker != cache.get('comp_ticker'): get_data = cache_data(user_ticker) if get_data: current_cache_ticker = cache.get('comp_ticker') get_tech_ind() else: flash(f"'{user_ticker}'' is an INVALID ticker symbol", 'danger') if get_data: get_ticker_news(current_cache_ticker) bar = make_plot(cache.get('comp_eod')) cache.set('eod_data_plot',bar) return render_template('overview.html', title=HOME_TITLE, comp_name=cache.get('comp_name'),comp_ticker=current_cache_ticker, search_data=search_bar_options, plot=cache.get('eod_data_plot'), gen_info=cache.get('comp_gen_info'), curr_ticker_news=cache.get('comp_news'))
def parse_date_tech_ind(): dates_to_parse = request.get_json(force=True) if dates_to_parse['start_date'] == "reset_dates": return render_template('techind.html', tech_ind_data=cache.get('tech_ind')) start_date = datetime.datetime.strptime(dates_to_parse['start_date'][:10], "%Y-%m-%d").date().strftime("%Y-%m-%d") end_date = datetime.datetime.strptime(dates_to_parse['end_date'][:10], "%Y-%m-%d").date().strftime("%Y-%m-%d") df = pd.DataFrame.from_dict(cache.get('tech_ind'), orient='index') temp_data = df.loc[start_date:end_date] output = temp_data.to_dict('index') return render_template('techind.html', tech_ind_data=output)
def parse_date(): dates_to_parse = request.get_json(force=True) if dates_to_parse['start_date'] == "reset_dates": return render_template('eoddata.html', eod_data=cache.get('comp_eod')) start_date = datetime.datetime.strptime(dates_to_parse['start_date'][:10], "%Y-%m-%d").date() end_date = datetime.datetime.strptime(dates_to_parse['end_date'][:10], "%Y-%m-%d").date() df = pd.DataFrame(cache.get('comp_eod'), columns =['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'AdjClose']) temp_data = df[(df['Date'] >= start_date) & (df['Date'] <= end_date)] output = temp_data.values.tolist() return render_template('eoddata.html', eod_data=output)
def chartdata(): CHART_DATA_TITLE = 'Chart Data' if not cache.get('comp_eod'): cache_data() return render_template('chartdata.html', title=CHART_DATA_TITLE, search_data=search_bar_options, comp_name=cache.get('comp_name'),comp_ticker=cache.get('comp_ticker'), eod_data=cache.get('comp_eod'))
def financials(): FINANCIALS_TITLE = 'Financials' if not cache.get('comp_fins'): cache_data() return render_template('financials.html', title=FINANCIALS_TITLE, search_data=search_bar_options, comp_name=cache.get('comp_name'), comp_ticker=cache.get('comp_ticker'), fin_data=cache.get('comp_fins'))
def stats(): STATISTICS_TITLE = 'Statistics' if not cache.get('comp_stats'): cache_data() return render_template('statistics.html', title=STATISTICS_TITLE, search_data=search_bar_options, comp_name=cache.get('comp_name'), comp_ticker=cache.get('comp_ticker'), stats_data=cache.get('comp_stats'))
def techindicators(): TECHNICAL_INDICATORS_TITLE = 'Technical Indicators' if not cache.get('tech_ind'): cache_data() get_tech_ind() return render_template('technicalindicators.html', title=TECHNICAL_INDICATORS_TITLE, search_data=search_bar_options, comp_name=cache.get('comp_name'), comp_ticker=cache.get('comp_ticker'), tech_ind_data=cache.get('tech_ind'))
def get_tech_ind_csv(): print("DOWNLOADING TECHNICAL INDICATORS CSV") df = pd.DataFrame.from_dict(cache.get('tech_ind'), orient='index') df.to_csv(r'outputs\technical_indicators.csv') return send_file(r'..\outputs\technical_indicators.csv', mimetype='text/csv', attachment_filename='technical_indicators.csv', as_attachment=True)
def get_daily_price_csv(): print("DOWNLOADING DAILY PRICE CSV") df = pd.DataFrame(cache.get('comp_eod'), columns =['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'AdjClose']) df.to_csv(r'outputs\daily_price.csv') return send_file(r'..\outputs\daily_price.csv', mimetype='text/csv', attachment_filename='daily_price.csv', as_attachment=True)
def get_all_data(ticker='tsla'): """ Gets the data for the specified ticker, either from the database or by webscrape Parameters: ticker: the ticker to get the data for Returns: Boolean: True, if the data exists or else false """ ticker = ticker.upper() end = _get_end_date() ticker_in_DB = ticker_exists(ticker) if ticker_in_DB: # check if the ticker exists in the DB _update_eod_DB(ticker, end, ticker_in_DB) else: added_data = _add_new_data(ticker, end) if not added_data: return added_data db.session.commit() cach_ticker = Security.query.filter(Security.ticker == ticker).first() cach_ticker.last_updated = datetime.datetime.utcnow() ticker_stats = get_stats_data(ticker) cach_ticker_eod_data = clean_eod_data(cach_ticker.eod_data.all()) cach_ticker_fiancials = clean_financials(cach_ticker.financials.all()[-1]) cach_ticker_comp_info = clean_company_information( cach_ticker.company_information.all()[-1]) cache.set('comp_eod', cach_ticker_eod_data) cache.set('comp_fins', cach_ticker_fiancials) cache.set('comp_gen_info', cach_ticker_comp_info) cache.set('comp_stats', ticker_stats) cache.set('comp_name', cach_ticker.name) cache.set('comp_ticker', cach_ticker.ticker) if not cache.get('sp500_eod'): print('GETTING SP500 DATA') _add_SP500_data("^GSPC", end) return True
def login(): if current_user.is_authenticated: return redirect(url_for('home')) LOGIN_TITLE = 'Login To Your Account' form = LoginForm() if form.validate_on_submit(): user = user_exists(form.email.data) if user and bcrypt.check_password_hash(user.password, form.password.data): login_user(user, form.remember.data) update_user_login(user) next_page = request.args.get('next')[1:] if request.args.get('next') and request.args.get('next').startswith('/') else request.args.get('next') if next_page == 'get_daily_price_csv': return render_template('chartdata.html', title='Chart Data', search_data=search_bar_options, comp_name=cache.get('comp_name'), comp_ticker=cache.get('comp_ticker'), eod_data=cache.get('comp_eod')) if next_page == 'get_tech_ind_csv': return render_template('technicalindicators.html', title='Technical Indicators', search_data=search_bar_options, comp_name=cache.get('comp_name'), comp_ticker=cache.get('comp_ticker'), tech_ind_data=cache.get('tech_ind')) return redirect(url_for(next_page)) if next_page else redirect(url_for('home')) else: flash(f'Login unsuccessful. Please try again', 'danger') return render_template('login.html', title=LOGIN_TITLE, search_data=search_bar_options, form=form)
from flask import render_template, url_for, request, flash, redirect, jsonify, make_response, send_file, session import concurrent.futures import datetime from flask_login import login_user, current_user, logout_user, login_required import pandas as pd from stockdashboard import app, cache, bcrypt, login_manager from stockdashboard.utils import search_bar_data from stockdashboard.plots.plots import make_plot from stockdashboard.signals import technical_signal_calculations from stockdashboard.controller import get_all_data, get_tech_ind, add_user, user_exists, get_latest_ticker_price, get_all_news, get_ticker_news, get_user_watchlist, add_user_watchlist, delete_user_watchlist, update_user_login from stockdashboard.forms import RegistrationForm, LoginForm search_bar_options = cache.get('search_bar_options') if not search_bar_options: search_bar_options = search_bar_data() cache.set('search_bar_options', search_bar_options) def cache_data(ticker="TSLA"): # handle various entry points print("GETTING DATA") return get_all_data(ticker) @app.template_filter('datetimeformat') def datetimeformat(value, formatted='%A, %B %d, %Y'): if isinstance(value, str): value = datetime.datetime.strptime(value, "%Y-%m-%d").date() format_time = value.strftime(formatted)