def port(port): result = Log.get_stocks(port) if not result.success: abort(500, result.message) result.message.sort(key=operator.attrgetter('stock')) return jsonify([stock.to_json() for stock in result.message])
def positions(port, stock): result = Log.get_positions(port, stock) if not result.success: abort(500, result.message) result.message.sort(key=operator.attrgetter('open'), reverse=True) return jsonify([position.to_json() for position in result.message])
def restore(): if current_user.is_admin(): result = Log.restore() flash(result.message, result.severity) else: flash('This function requires admin priviledges', 'WARNING') return redirect(request.referrer)
def stocks(): global reverse sortby = request.args.get('sortby') if request.args.get('sortby') else "_id" prices = Log.prices() prices.sort(key=operator.attrgetter(sortby), reverse=reverse) reverse = not reverse return render_template('update.html', prices=prices)
def commit(): """ Convert a raw trade into a processed trade """ sortby = request.args.get('sortby') if request.method == 'POST': result = Log.commit_raw_trade(request.form.get('raw_id'), request.form.get('port').replace('_',' ')) flash(result.message, result.severity) result=Log.get_raw_trades() if not result.success: flash(result.message, result.severity) result.message = [] elif not result.message: flash('There are no raw trades to commit', 'WARNING') return render_template('raw_trades.html', trades=_sort(result.message, sortby), ports=[port.name.replace(' ', '_') for port in _ports()])
def open(port, stock): """ List the open positions and trades """ sortby = request.args.get('sortby') result = Log.get_open_positions(port=port, stock=stock) if not result.success: flash(result.message, result.severity) result.message = [] return render_template('open.html', open=_sort(result.message, sortby), port=port, stock=stock)
def _ports(): result = Log.get_ports() if not result.success: flash(result.message, result.severity) result.message = [] if len(result.message) == 0: flash('There are no Portfolios. Create one before proceeding further', 'WARNING') return result.message
def allclosed(port): """ List the closed positions in this portfolio """ sortby = request.args.get('sortby') closed = Log.get_closed_positions(port, None) if not closed.success: flash(closed.message, closed.severity) closed.message=[] if not closed.message: flash("This portfolio contains no closed positions", "WARNING") return render_template("all_closed.html", closed=_sort(closed.message, sortby), port=port)
def positions(port): """ List the positions in this port """ sortby = request.args.get('sortby') what = request.args.get('what') open = Log.get_open_positions(port, None) if not open.success: flash(open.message, open.severity) open.message=[] if not open.message: flash("There are no open positions", "WARNING") elif what == 'open': open.message = _sort(open.message, sortby) closed = Log.get_closed_positions(port, None) if not closed.success: flash(closed.message, closed.severity) closed.message=[] elif what == 'closed': closed.message = _sort(closed.message, sortby) if not closed.message: flash("There are no closed positions", "WARNING") return render_template("positions.html", open=open.message, closed=closed.message, port=port)
def closed(port, stock): """ List closed positions and trades """ sortby = request.args.get('sortby') result = Log.get_closed_positions(port=port, stock=stock) if not result.success: flash(result.message, result.severity) result.message = [] elif not result.message: flash(f'{stock} has no closed positions', 'WARNING') return render_template('closed.html', closed=_sort(result.message, sortby), port=port, stock=stock)
def allopen(port): """ List the positions in this port """ sortby = request.args.get('sortby') open = Log.get_open_positions(port, None) if not open.success: flash(open.message, open.severity) open.message=[] if not open.message: flash("This portfolio contains no open positions", "WARNING") return render_template("all_open.html", open=_sort(open.message, sortby), port=port)
def port(port): """ List the stocks in this port """ sortby = request.args.get('sortby') result = Log.get_stocks(port) if not result.success: flash(result.message, result.severity) result.message = [] elif not result.message: flash("This portfolio contains no stocks. Commit a trade to get started.", "WARNING") return render_template("stocks.html", port=port, stocks=_sort(result.message, sortby))
def prices(port): global reverse open = Log.get_open_positions(port, None) if not open.success: flash(open.message, open.severity) return redirect('/price') if not open.message: flash("This portfolio contains no open positions", "WARNING") sortby = request.args.get('sortby') if request.args.get('sortby') else 'stock' prices = sorted(Price.get_price(open.message), key=lambda i: i[sortby], reverse=reverse) reverse = not reverse return render_template('prices.html', prices=prices, port=port)
def new(): """ Create a new portfolio """ if request.method == 'POST': result = Log.create_portfolio({ 'name': request.form['name'], 'description': request.form['description'], }) if result.success: flash (f'Portfolio {result.message.name} successfully created', 'SUCCESS') else: flash (result.message, result.severity) return render_template('new.html')
def load(): """ select a file of raw trades to load and process """ if request.method == 'GET': return render_template('load.html', action='/load') f = request.files['file'] try: f.save('data/'+f.filename) except Exception as e: flash(str(e), 'ERROR') return render_template('load.html', action='/load') result = Log.load_trades_from(f.filename) if result.success: flash(f'{f.filename} loaded successfully! There are {result.message} new raw trades', 'SUCCESS') else: flash(f'Failed to process {f.filename}. {result.message}', result.severity) return render_template('load.html', action='/load')
def bulk(): """ select a file of raw trades to load and auto import """ if current_user.is_admin(): if request.method == 'GET': return render_template('load.html', action='/bulk') f = request.files['file'] try: f.save('data/'+f.filename) except Exception as e: flash(str(e), 'ERROR') result = Log.bulk(f.filename) if not result.success: flash(f'Failed to process {f.filename}. {result.message}', result.severity) raw = sorted(result.message, key=lambda x: x['TradeDate']) return render_template('bulk.html', raw=raw) else: flash('This function requires admin priviledges', 'WARNING') return redirect(request.referrer)
def sharepad(): """ Load trades from sharepad instead of IB """ if request.method == 'GET': return render_template('sharepad.html', ports=[port.name.replace(' ', '_') for port in _ports()]) if 'port' in request.form.keys(): port = request.form['port'].replace('_', ' ') else: port = None f = request.files['file'] try: f.save('data/'+f.filename) except Exception as e: flash(str(e), 'ERROR') return render_template('sharepad.html', ports=[port.name.replace(' ', '_') for port in _ports()]) result = Log.load_sharepad_trades('data/'+f.filename, port) if result.success: flash(f'{f.filename} loaded successfully!', 'SUCCESS') else: flash(f'Failed to load {f.filename}. {result.message}', result.severity) return render_template('sharepad.html', ports=[port.name.replace(' ', '_') for port in _ports()])
def backup(): result = Log.backup() flash(result.message, result.severity) return redirect(request.referrer)
def delete(stock): Log.delete_price(stock) return render_template('update.html', prices=Log.prices())
def update(): Log.update() return redirect(request.referrer)
def yahoo(stock, symbol): print(stock, symbol) Price.get(stock).update({'yahoo': symbol}) return render_template('update.html', prices=Log.prices())
def prices_root(): result = Log.price() if not result.success: flash(result.message, 'ERROR') return redirect(request.referrer) return render_template('port_prices.html', ports=result.message)
def home(): ports = Log.get_ports() if not ports.success: abort(500, ports.message) return jsonify([port.to_json() for port in ports.message])
def price(): result = Log.price() if not result.success: abort(500, result.message) return jsonify(result.message)
def trades(id): result = Log.get_trades(id) if not result.success: abort(500, result.message) return jsonify([trade.to_json() for trade in result.message])
def position(_id): result = Log.get_trades(_id) if not result.success: flash(result.message, result.severity) return render_template("trades.html", positions=result.message, position=result.severity)