Esempio n. 1
0
def stock_crypto():
    if not g.user:
        flash("Access unauthorized.", "error")
        return redirect("/")
    form = TickerForm()
    user = g.user
    if form.validate_on_submit():
        symbol = form.ticker.data
        region = form.region.data
        data = search_stock(symbol, region)
        if data["type"] == "stock":
            if Stock.query.filter_by(ticker_symbol=symbol).first():
                stock = Stock.query.filter_by(ticker_symbol=symbol).first()
                return render_template('stock-profile.html',
                                       data=data,
                                       user=user,
                                       stock_data=data,
                                       stock=stock)
            else:
                stock = Stock(stock_name=data["name"],
                              ticker_symbol=data["symbol"],
                              region=region,
                              which_type=data["type"],
                              stock_price=data["price"])
                db.session.add(stock)
                db.session.commit()
                return render_template('stock-profile.html',
                                       data=data,
                                       user=user,
                                       stock_data=data,
                                       stock=stock)
        elif data["type"] == "crypto":
            if Cryptocurrency.query.filter_by(ticker_symbol=symbol).first():
                crypto = Cryptocurrency.query.filter_by(
                    ticker_symbol=symbol).first()
                return render_template('stock-profile.html',
                                       data=data,
                                       user=user,
                                       crypto_data=data,
                                       crypto=crypto)
            else:
                crypto = Cryptocurrency(crypto_name=data["name"],
                                        ticker_symbol=data["symbol"],
                                        region=region,
                                        which_type=data["type"],
                                        crypto_price=data["price"])
                db.session.add(crypto)
                db.session.commit()
                return render_template('stock-profile.html',
                                       data=data,
                                       user=user,
                                       crypto_data=data,
                                       crypto=crypto)
        elif data["type"] == "not found":
            flash(
                "Please insert correct ticker symbol for stocks and cryptocurrencies only!",
                "error")
            return redirect(f'/users/{user.username}')
    else:
        return redirect(f'/users/{user.username}')
Esempio n. 2
0
def index():
  script = None
  div = None
  show_form = True
  form = TickerForm()
  if form.validate_on_submit():
    ticker = form.ticker_symbol.data
    #flash('You entered: %s' % (company))

    dates = get_dates()

    sdata = get_stock_data(ticker, dates[0], dates[1])
    #if sdata.empty:
    #  bad_result = 'Data for {} not found'.format(company)
    #else:
    values = [form.closing_price.data, form.adj_close.data, form.opening_price.data, form.adj_opening.data]
    print(values)
    script, div = plot_stocks(sdata, ticker, values)
    show_form = False
    
  #script, div = plot_stocks(get_stock_data('AAPL','2017-09-06','2017-10-06'), 'AAPL')
  return render_template('index.html', form = form,script=script, div=div, show_form = show_form)