示例#1
0
def show_watchlist():
    form = QuoteForm()
    page = request.args.get('page', 1, type=int)
    per_page = 10
    pagination = Watchlist.query.with_parent(current_user).order_by(
        Watchlist.symbol.asc()).paginate(page, per_page)
    watchlist = pagination.items

    symbols = []
    prices = []
    changes = []
    changePercents = []
    openprices = []
    highs = []
    lows = []
    volumes = []
    week52Highs = []
    week52Lows = []

    if watchlist:
        for stock in watchlist:
            quote = lookup(stock.symbol)

            price = quote["price"]
            symbol = quote["symbol"]
            change = quote["change"]
            changePercent = quote["changePercent"]
            volume = quote["volume"]
            week52High = quote["week52High"]
            week52Low = quote["week52Low"]
            openprice = quote["open"]
            high = quote['high']
            low = quote["low"]

            symbols.append(symbol)
            prices.append(price)
            changes.append(change)
            changePercents.append(changePercent)
            openprices.append(openprice)
            highs.append(high)
            lows.append(low)
            volumes.append(volume)
            week52Highs.append(week52High)
            week52Lows.append(week52Low)
    return render_template('admin/watchlist.html',
                           watchlist=watchlist,
                           pagination=pagination,
                           form=form,
                           prices=prices,
                           changes=changes,
                           changePercents=changePercents,
                           openprices=openprices,
                           highs=highs,
                           lows=lows,
                           week52Highs=week52Highs,
                           week52Lows=week52Lows,
                           symbols=symbols,
                           volumes=volumes)
示例#2
0
def get_quote():
    form = QuoteForm()
    if form.validate_on_submit():
        symbol = form.symbol.data

        quote = lookup(symbol)

        if quote:

            return redirect(url_for(".show_quote", symbol=symbol))
        else:
            flash("Cannot find", 'warning')
            return redirect_back()
    return render_template("admin/quote.html", form=form)
示例#3
0
def index():

    if current_user.is_authenticated:
        form = QuoteForm()

        if form.validate_on_submit():

            symbol = form.symbol.data

            return redirect(url_for('admin.show_quote', symbol=symbol))

        # stocks = Portfolio.query.filter_by(symbol = "AMZN")
        # stocks = Portfolio.query.with_parent(current_user).all()
        # y = []
        # for stock in stocks:

        #     flash(stock.symbol)
        #     quote = lookup(stock.symbol)
        #     price = quote['name']
        #     y.append(stock.symbol)
        cash = current_user.cash
        page = request.args.get('page', 1, type=int)
        per_page = 15
        pagination = Portfolio.query.with_parent(current_user).order_by(
            Portfolio.name.asc()).paginate(page, per_page)
        portfolio = pagination.items
        prices = []
        position = 0
        if portfolio:
            for stock in portfolio:

                quote = lookup(stock.symbol)
                quantity = stock.quantity
                price = quote['price']
                prices.append(price)
                position += float(quantity) * float(price)

        current_user.position = position
        account_value = float(cash) + float(position)
        db.session.commit()
        return render_template("home/index.html",
                               portfolio=portfolio,
                               pagination=pagination,
                               prices=prices,
                               cash=cash,
                               position=position,
                               account_value=account_value,
                               form=form)
    else:
        return render_template("home/index.html")
示例#4
0
def get_trade(symbol):
    # symbol = request.args.get("symbol")
    form = TradeForm()
    quote = lookup(symbol)
    if form.validate_on_submit():
        symbol = form.symbol.data
        quantity = form.quantity.data
        price = form.price.data
        action = form.action.data
        return redirect(
            url_for('.review_order',
                    symbol=symbol,
                    quantity=quantity,
                    price=price,
                    action=action))
    form.symbol.data = symbol
    form.price.data = quote['price']
    return render_template("admin/get_trade.html", form=form)
示例#5
0
def show_quote(symbol):
    form = QuoteForm()
    quote = lookup(symbol)
    company = quote["name"]
    price = quote["price"]
    symbol = quote['symbol']

    # link = chart_plot_upload(symbol)
    # if form.validate_on_submit():
    #     symbol = form.symbol.data
    #     quote = lookup(symbol)
    #     company = quote["name"]
    #     price = quote["price"]
    #     return redirect(url_for(".show_quote", symbol = symbol))
    return render_template("admin/show_quote.html",
                           form=form,
                           company=company,
                           price=price,
                           symbol=symbol)
示例#6
0
    def make_template_context():
        user = User.query.first()
        if current_user.is_authenticated:
            watchlist = Watchlist.query.with_parent(current_user).order_by(
                Watchlist.symbol.asc()).all()
            priceInWatchlist = []
            if watchlist:
                for stock in watchlist:
                    quote = lookup(stock.symbol)
                    price = quote['price']
                    priceInWatchlist.append(price)

                return dict(user=user,
                            watchlist=watchlist,
                            priceInWatchlist=priceInWatchlist)
            else:
                return dicd(user=user)
        else:
            return dict(user=user, )
示例#7
0
def place_order():
    # get trade condition
    able_to_trade = request.args.get('able_to_trade')
    action = request.args.get("action")
    if able_to_trade:
        # get original cash and position
        cash = float(current_user.cash)
        # position = float(current_user.position)

        # update cash and position back to database
        if action == "Buy":
            totalprice = float(request.args.get('totalprice'))
            current_user.cash = cash - totalprice
            # current_user.position = position + totalprice
        else:
            totalprice = float(request.args.get('totalprice'))
            current_user.cash = cash + totalprice
            # current_user.position = position - totalprice

        #update trade_history
        symbol = request.args.get('symbol')
        name = lookup(symbol)['name']
        price = request.args.get('price')
        quantity = request.args.get('quantity')

        trade_history = TradeHistory(symbol=symbol,
                                     name=name,
                                     price=price,
                                     action=action,
                                     quantity=quantity)
        db.session.add(trade_history)
        current_user.trade_history.append(trade_history)

        # update portfolio
        portfolio = Portfolio.query.with_parent(current_user).all()
        if portfolio:
            # check if the stock already existed
            for company in portfolio:
                # if exists update the averge pruchase price and quantity
                if symbol == company.symbol:
                    # get purchase price and symbol
                    id = company.id
                    stock = Portfolio.query.get_or_404(id)
                    purchase_price = float(stock.purchase_price)
                    own_quantity = float(stock.quantity)

                    # caculate new purchase price and quantity
                    if action == "Buy":
                        new_purchase_price = (
                            purchase_price * own_quantity + float(quantity) *
                            float(price)) / (own_quantity + float(quantity))
                        new_own_quantity = own_quantity + float(quantity)
                    else:
                        new_own_quantity = own_quantity - float(quantity)
                        if new_own_quantity == 0:
                            db.session.delete(stock)
                            db.session.commit()
                            return redirect(url_for('home.index'))

                        new_purchase_price = (
                            purchase_price * own_quantity - float(quantity) *
                            float(price)) / (own_quantity - float(quantity))

                    stock.purchase_price = new_purchase_price
                    stock.quantity = new_own_quantity
                    db.session.commit()
                    return redirect(url_for("home.index"))
            # if not exist in portfolio, create it
            new_portfolio = Portfolio(symbol=symbol,
                                      name=name,
                                      purchase_price=price,
                                      quantity=quantity)
            current_user.portfolio.append(new_portfolio)
        else:
            new_portfolio = Portfolio(symbol=symbol,
                                      name=name,
                                      purchase_price=price,
                                      quantity=quantity)
            current_user.portfolio.append(new_portfolio)

    db.session.commit()
    return redirect(url_for("home.index"))