def gen_receipt_spot(quoterequest, quote): bacen_code = databus.get(f'Currencies/{quoterequest.Currency}/CodeBACEN') market_type = str( databus.get(f'LegalEntities/{quoterequest.CustomerID}/FXMarketType')) default_transaction_type = databus.get( f'LegalEntities/{quoterequest.CustomerID}/DefaultFXTransaction') side_dict = { messages.EnumSide.SELL: 'A', # se cliente está vendendo, banco está comprando messages.EnumSide.BUY: 'P', # se cliente está comprando, banco está vendendo } buy_sell_pt = { messages.EnumSide.SELL: 'VENDA', messages.EnumSide.BUY: 'COMPRA' } side = side_dict.get(quoterequest.Side, 'ERROR') side_pt = buy_sell_pt.get(quoterequest.Side, 'ERROR') transaction_type = default_transaction_type settlement_type_brl = "" if market_type == "1": # TODO Improve this case if default_transaction_type == "IMPORT_EXPORT": transaction_type = "IMPORTACAO_EXPORTACAO" settlement_type_brl = "" elif default_transaction_type == "FINANCIAL": transaction_type = "FINANCEIRO" settlement_type_brl = "" elif market_type == "2": if default_transaction_type == 'INTERBANK_CLEARING': transaction_type = f'INTERBANCARIO_{side_pt}_PRONTO_COM_CLEARING' settlement_type_brl = 'CAMARA' elif default_transaction_type == 'INTERBANK_NO_CLEARING': transaction_type = f'INTERBANCARIO_{side_pt}_PRONTO_SEM_CLEARING' settlement_type_brl = 'RESERVA' result = { "Transaction_ID": quoterequest.QuoteReqID, "Trading_Day": quoterequest.TradeDate, "Client_CNPJ": quoterequest.CustomerID, "Client_Name": quoterequest.CustomerStr, "MarketType": market_type, "Side": side, "CurrencyCode_ISO": quoterequest.Currency, "CurrencyCode_BACEN": bacen_code, "Amount_CCY": quoterequest.OrderQty, "Amount_BRL": quote.Details['amount_brl'], "Amount_USD": quote.Details['amount_usd'], "SPOT_CCY_CostRate": quote.Details['s_cost'], "FX_ClientRate": quote.Details['quote'], "USD_Parity": quote.Details['usdbrl_quote'], "SettlementDate_CCY": quote.Details['settlement_date_ccy'], "SettlementDate_BRL": quote.Details['settlement_date_brl'], "Transaction_Type": transaction_type, "SettlementType_BRL": settlement_type_brl, } return result
def get_spread(self, cnpj, ccy, side, du_ccy): side = side.upper() # side foi colocado em upper no barramento. spreads = databus.get(f'ClientSpreads/CounterpartySpreads/{cnpj}/FXSPOT/{ccy}/{side}') if spreads is None or spreads[du_ccy] is None: group_name = databus.get(f'LegalEntitiesRelationships/Groups_Spreads_FXSPOT_Memberships/{cnpj}') if group_name is None or group_name == "": raise RejectException('Spread não cadastrado para CNPJ/Grupo') spreads = databus.get(f'ClientSpreads/GroupSpreads/{group_name}/FXSPOT/{ccy}/{side}') raise_if_none(spreads, 'Spread não cadastrado para CNPJ/Grupo') spread = spreads[du_ccy] raise_if_none(spread, 'Spread não cadastrado para CNPJ/Grupo') else: spread = spreads[du_ccy] return spread
def get_lists_risk_curve(curve_name): config = databus.get_dict("Curves/{}".format(curve_name)) print_red(f"DEBUG: get_lists_risk_curve(curve_name): config: {config}") curve_dates = get_list_date_risk_curve(curve_name) print_red( f"DEBUG: get_lists_risk_curve(curve_name): curves: {curve_dates}") if not curve_dates: raise Exception('Empty curves dates!') if config["DayCount"] == "BIZ": holidays = list( map( np.datetime64, databus.get("Calendars/{}".format(config.get("Currency"))), )) curve_days = list( convert_dates_to_days(get_reference_date(), curve_dates, True, holidays)) else: curve_days = list( convert_dates_to_days(get_reference_date(), curve_dates, False)) curve_bid = get_list_rates_bid_risk_curve(curve_name) curve_offer = get_list_rates_offer_risk_curve(curve_name) risk_vertices = get_risk_vertices_for_curve(curve_name) interp_base = float(config.get("Base")) result_bid, result_offer = [], [] for vertice in risk_vertices: idx_right = bisect_left(curve_days, vertice) if idx_right == 0: bid = curve_bid[0] offer = curve_offer[0] elif idx_right == len(curve_days): bid = curve_bid[-1] offer = curve_offer[-1] else: idx_left = idx_right - 1 days_l = curve_days[idx_left] days_r = curve_days[idx_right] bid_l = curve_bid[idx_left] bid_r = curve_bid[idx_right] offer_l = curve_offer[idx_left] offer_r = curve_offer[idx_right] bid = calculate_interp_rate_expression(bid_l, days_l, bid_r, days_r, vertice, interp_base) offer = calculate_interp_rate_expression(offer_l, days_l, offer_r, days_r, vertice, interp_base) result_bid.append(bid) result_offer.append(offer) return result_bid, result_offer
def get_spread(self, cnpj, ccy, dc): def get_bucket(buckets): bucket = None for index in range(0, len(buckets)): if int(buckets[index]) >= dc: bucket = index break return bucket buckets = databus.get( f'ClientSpreads/CounterpartySpreads/{cnpj}/FXNDF/Buckets') spreads = databus.get( f'ClientSpreads/CounterpartySpreads/{cnpj}/FXNDF/Spreads/{ccy}/BUYSELL' ) bucket = get_bucket(buckets) if buckets is not None else None counterparty_valid_spread = True if None in (bucket, spreads) or (spreads and spreads[bucket] is None): counterparty_valid_spread = False if not counterparty_valid_spread: group_name = databus.get( f'LegalEntitiesRelationships/Groups_Spreads_FXNDF_Memberships/{cnpj}' ) if group_name is None or group_name == "": raise RejectException(f'Spread não cadastrado para CNPJ/Grupo') buckets = databus.get( f'ClientSpreads/GroupSpreads/{group_name}/FXNDF/Buckets') spreads = databus.get( f'ClientSpreads/GroupSpreads/{group_name}/FXNDF/Spreads/{ccy}/BUYSELL' ) bucket = get_bucket(buckets) if buckets is not None else None if None in (bucket, spreads) or (spreads and spreads[bucket] is None): raise RejectException('Spread não cadastrado para CNPJ/Grupo') return spreads[bucket]
def update_riskdata(): global previous_error_risk curve_names_tuple = tuple(databus.get('Curves')) print(f"DEBUG: update_riskdata: curve_names_tuple: {curve_names_tuple}") for curve_name in curve_names_tuple: try: print(f"DEBUG: update_riskdata: curve_name: {curve_name}") bid_list, offer_list = get_lists_risk_curve(curve_name) curve_dict = { "RatesBid": bid_list, "RatesOffer": offer_list, "LastUpdate": get_Lastdt(curve_name) } set_riskdata(curve_name, curve_dict) if previous_error_risk is not None: print("Recovered", flush=True) previous_error_risk = None except Exception as e: previous_error_risk = treat_error(e, previous_error_risk, curve_names_tuple)
def gen_receipt_ndf(quoterequest, quote): side_dict = { messages.EnumSide.SELL: 'A', # se cliente está vendendo, banco está comprando messages.EnumSide.BUY: 'P', # se cliente está comprando, banco está vendendo } side = side_dict.get(quoterequest.Side, 'ERROR') codigo_cliente_databus_key = f"Transactions/Messages/QuoteRequest/{quoterequest.QuoteReqID}/GLOB_Client_ID" codigo_cliente = databus.get(codigo_cliente_databus_key) result = { "Transaction_ID": quoterequest.QuoteReqID, "Client_CNPJ": quoterequest.CustomerID, "Client_ID": codigo_cliente, # ??? # RESPOSTA GLOB (BIO FICOU DE VERIFICAR) "Client_ID2": "", # ??? # RESPOSTA GLOB (BIO FICOU DE VERIFICAR) "StartDate": quoterequest.TradeDate, "Maturity": quoterequest.FutSettDate, "FixingConvention": quote.Details['dn'], # EXPLICADO NO DOC "Side": side, "CurrencyCode_ISO": quoterequest.Currency, "Amount_CCY": quoterequest.OrderQty, "SPOT_CCY_CostRate": quote.Details['s_cost'], "TransactionValue_BRL": quote.Details['value_brl'], # NOTIONAL * S_COST "FWD_CCY_ClientRate": quote.Details['quote'], # QUOTE "FWD_CCY_CostRate": quote.Details['f_cost'], # F_COST "FWD_BRL_Amount": quote.Details['value_brl'], # NOTIONAL * QUOTE "Yield_CCY_ClientRate": quote. Details['y_ccy_client'], # CUPOM CAMBIAL +- SPREAD IMPLI. PAG. 47 "Yield_CCY_CostRate": quote.Details['y_ccy'], # CUPOM CAMBIAL "Yield_CCY_Convention": "TAXA_LINEAR_AA_360", "Yield_BRL_ClientRate": quote.Details['y_brl'], # TAXA PRE "Yield_BRL_CostRate": quote.Details['y_brl'], # TAVA PRE "Yield_BRL_Convention": "TAXA_OVER_252", } return result
def get_or_raise(key, message): value = databus.get(key) if value is None: raise RejectException(message) return value
def get_holidays(currency): holidays = databus.get('Calendars/{ccy}'.format(ccy=currency)) if holidays is not None: return list(map(np.datetime64, holidays)) return []
def get_risk_vertices_for_curve(curve_name): bus_str = "RiskData/CURVE/{curve_name}/RiskVertices".format( curve_name=curve_name) return databus.get(bus_str)
def get_list_rates_offer_risk_curve(curve_name): bus_str = "MarketData/Curves/{curve_name}/rates_offer".format( curve_name=curve_name) return databus.get(bus_str)
def get_list_date_risk_curve(curve_name): bus_str = "MarketData/Curves/{curve_name}/dates".format( curve_name=curve_name) return databus.get(bus_str)
def get_Lastdt(curve_name): return databus.get(f'MarketData/Curves/{curve_name}/LastUpdate')
def get_customer_name(cnpj): return databus.get("LegalEntities/{cnpj}/CounterpartyName".format(cnpj=cnpj))