Beispiel #1
0
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])
Beispiel #2
0
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])
Beispiel #3
0
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)
Beispiel #4
0
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)
Beispiel #5
0
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()])
Beispiel #6
0
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)
Beispiel #7
0
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
Beispiel #8
0
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)
Beispiel #9
0
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)
Beispiel #10
0
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)
Beispiel #11
0
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)
Beispiel #12
0
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))
Beispiel #13
0
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)
Beispiel #14
0
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')
Beispiel #15
0
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')
Beispiel #16
0
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)
Beispiel #17
0
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()])
Beispiel #18
0
def backup():
    result = Log.backup()
    flash(result.message, result.severity)
    return redirect(request.referrer)
Beispiel #19
0
def delete(stock):
    Log.delete_price(stock)
    return render_template('update.html', prices=Log.prices())
Beispiel #20
0
def update():
    Log.update()
    return redirect(request.referrer)
Beispiel #21
0
def yahoo(stock, symbol):
    print(stock, symbol)
    Price.get(stock).update({'yahoo': symbol})
    return render_template('update.html', prices=Log.prices())
Beispiel #22
0
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)
Beispiel #23
0
def home():
    ports = Log.get_ports()
    if not ports.success:
        abort(500, ports.message)
    return jsonify([port.to_json() for port in ports.message])
Beispiel #24
0
def price():
    result = Log.price()
    if not result.success: abort(500, result.message)
    return jsonify(result.message)
Beispiel #25
0
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])
Beispiel #26
0
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)