def get(self, underlying_id):
     db_underlying = db_session.query(Underlying).get(underlying_id)
     underlying_price = get_underlying_price(db_underlying)
     result = {
         "underlying_id": underlying_id,
         "market_price": underlying_price,
         "timestamp": datetime.now()
     }
     return jsonify(result)
def set_curve_settings_by_price(vol_curve_id):
    db_vol_curve = db_session.query(VolCurve).get(vol_curve_id)
    input_dict = db_vol_curve.get_input_dict()
    request_json = request.get_json()
    for input_name, price_increment in request_json.iteritems():
        price_increment = float(price_increment)
        if price_increment != 0:
            vol_curve_input = input_dict[input_name]
            vol_curve_input.update_by_price(price_increment)
    db_session.commit()
    return get_curve_settings(vol_curve_id)
def get_positions(portfolio_id):
    db_portfolio = db_session.query(Portfolio).get(portfolio_id)
    results = defaultdict(lambda: defaultdict(dict))
    for db_account in db_portfolio.accounts:
        account_name = db_account.name
        day_trade_positions = db_account.get_day_trade_positions()
        committed_positions = db_account.get_committed_positions()
        for inst_id, day_trade_position in day_trade_positions.iteritems():
            results[account_name][inst_id]["day_trade_position"] = day_trade_position
        for inst_id, committed_position in committed_positions.iteritems():
            results[account_name][inst_id]["committed_position"] = committed_position
    return jsonify(results)
def get_curve_settings(vol_curve_id):
    db_vol_curve = db_session.query(VolCurve).get(vol_curve_id)
    underlying_price = get_underlying_price_from_request(request)
    if underlying_price is None:
        underlying_price = get_underlying_price(db_vol_curve.expiry.underlying)
    vol_curve_settings = db_vol_curve.get_input_settings(underlying_price)
    input_sensitivities = calc_normalization_sensitivities_to_inputs(db_vol_curve.expiry, underlying_price)
    for input_name in vol_curve_settings:
        vol_curve_settings[input_name]["sensitivity"] = input_sensitivities.get(input_name, 0.0)
    vol_curve_settings["portfolio_symbol_id"] = db_vol_curve.expiry.portfolio_symbol_id
    vol_curve_settings["expiry_id"] = db_vol_curve.expiry_id
    vol_curve_settings["vol_curve_id"] = db_vol_curve.id
    return jsonify(vol_curve_settings)
def get_underlying_risk(underlying_id):
    db_underlying = db_session.query(Underlying).get(underlying_id)
    underlying_price = get_underlying_price_from_request(request)
    if underlying_price is None:
        underlying_price = get_underlying_price(db_underlying)
    underlying_risk = calc_underlying_risk(db_underlying, underlying_price)
    underlying_risk["account_id"] = underlying_risk.index.get_level_values("account_id")
    underlying_risk["position_type"] = underlying_risk.index.get_level_values("position_type")
    results = {
        "underlying_id": underlying_id,
        "underlying_price": underlying_price,
        "results": underlying_risk.to_json(orient="records")
    }
    return jsonify(results)
def get_risk(expiry_id):
    db_expiry = db_session.query(Expiry).get(expiry_id)
    underlying_price = get_underlying_price_from_request(request)
    if underlying_price is None:
        underlying_price = get_underlying_price(db_expiry.underlying)
    expiry_risk = calc_expiry_risk(db_expiry, underlying_price)
    expiry_risk["account_id"] = expiry_risk.index.get_level_values("account_id")
    expiry_risk["position_type"] = expiry_risk.index.get_level_values("position_type")
    results = {
        "expiry_id": expiry_id,
        "underlying_price": underlying_price,
        "results": expiry_risk.to_json(orient="records")
    }
    return jsonify(results)
 def get(self, portfolio_id):
     portfolio_id = int(portfolio_id)
     db_portfolio = db_session.query(Portfolio).get(portfolio_id)
     results = _dictify_portfolio(db_portfolio)
     return jsonify(results)
 def index(self):
     q = db_session.query(Portfolio.id, Portfolio.name).all()
     results = {q_row.id: q_row.name for q_row in q}
     return jsonify(results)