Module: vanilla_swap_entry

Expand source code
# Copyright (C) 2023-present The Project Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from dataclasses import dataclass
from cl.runtime import Context
from cl.convince.entries.entry_key import EntryKey
from cl.convince.llms.gpt.gpt_llm import GptLlm
from cl.convince.retrievers.annotating_retriever import AnnotatingRetriever
from cl.tradeentry.entries.date_or_tenor_entry import DateOrTenorEntry
from cl.tradeentry.entries.fixed_rate_entry import FixedRateEntry
from cl.tradeentry.entries.pay_receive_fixed_entry import PayReceiveFixedEntry
from cl.tradeentry.entries.rates.rates_index_entry import RatesIndexEntry
from cl.tradeentry.entries.trade_entry import TradeEntry

_SIDE = "The words Buy or Sell, or the words Pay Fixed or Receive Fixed"
_MATURITY = "Either maturity date as a date, or tenor (length) as the number of years and/or months"
_FLOAT_INDEX = "Floating rate index"
_FIXED_RATE = "Fixed rate"


@dataclass(slots=True, kw_only=True)
class VanillaSwapEntry(TradeEntry):
    """Vanilla fixed-for-floating swap."""

    pay_receive_fixed: EntryKey | None = None
    """String representation of the PayFixed or ReceiveFixed flag in the format specified by the user."""

    effective: EntryKey | None = None
    """Trade or leg effective date defined as unadjusted date or time interval relative to another date."""

    maturity: EntryKey | None = None
    """Trade or leg maturity date defined as unadjusted date or time interval relative to another date."""

    tenor: EntryKey | None = None
    """Swap tenor (length) in months or years."""

    float_index: EntryKey | None = None
    """Floating interest rate index or currency (in case of currency, default index for the currency is used)."""

    fixed_rate: EntryKey | None = None
    """Fixed rate entry or breakeven rate if not specified."""

    def run_propose(self) -> None:
        """Retrieve parameters from this entry and save the resulting entries."""
        # Get retriever
        # TODO: Make configurable
        retriever = AnnotatingRetriever(
            retriever_id="test_annotating_retriever",
            llm=GptLlm(llm_id="gpt-4o"),
        )
        retriever.init_all()

        # Process fields
        context = Context.current()
        input_text = self.get_text()

        # Pay or receive fixed flag is described side
        pay_receive_fixed = PayReceiveFixedEntry(title=retriever.retrieve(self.entry_id, input_text, _SIDE))
        context.save_one(pay_receive_fixed)
        self.pay_receive_fixed = pay_receive_fixed.get_key()

        # Tenor
        maturity = DateOrTenorEntry(title=retriever.retrieve(self.entry_id, input_text, _MATURITY))
        context.save_one(maturity)
        self.maturity = maturity.get_key()

        # Floating rate index
        float_index = RatesIndexEntry(title=retriever.retrieve(self.entry_id, input_text, _FLOAT_INDEX))
        context.save_one(float_index)
        self.float_index = float_index.get_key()

        # Fixed Rate
        fixed_rate = FixedRateEntry(title=retriever.retrieve(self.entry_id, input_text, _FIXED_RATE))
        context.save_one(fixed_rate)
        self.fixed_rate = fixed_rate.get_key()

        # Save self to DB
        Context.current().save_one(self)

Classes

class VanillaSwapEntry (*, entry_id: str = None, title: str = None, body: str | None = None, data: str | None = None, approved_by: UserKey | None = None, few_shot: bool | None = None, pay_receive_fixed: EntryKey | None = None, effective: EntryKey | None = None, maturity: EntryKey | None = None, tenor: EntryKey | None = None, float_index: EntryKey | None = None, fixed_rate: EntryKey | None = None)

Vanilla fixed-for-floating swap.

Expand source code
@dataclass(slots=True, kw_only=True)
class VanillaSwapEntry(TradeEntry):
    """Vanilla fixed-for-floating swap."""

    pay_receive_fixed: EntryKey | None = None
    """String representation of the PayFixed or ReceiveFixed flag in the format specified by the user."""

    effective: EntryKey | None = None
    """Trade or leg effective date defined as unadjusted date or time interval relative to another date."""

    maturity: EntryKey | None = None
    """Trade or leg maturity date defined as unadjusted date or time interval relative to another date."""

    tenor: EntryKey | None = None
    """Swap tenor (length) in months or years."""

    float_index: EntryKey | None = None
    """Floating interest rate index or currency (in case of currency, default index for the currency is used)."""

    fixed_rate: EntryKey | None = None
    """Fixed rate entry or breakeven rate if not specified."""

    def run_propose(self) -> None:
        """Retrieve parameters from this entry and save the resulting entries."""
        # Get retriever
        # TODO: Make configurable
        retriever = AnnotatingRetriever(
            retriever_id="test_annotating_retriever",
            llm=GptLlm(llm_id="gpt-4o"),
        )
        retriever.init_all()

        # Process fields
        context = Context.current()
        input_text = self.get_text()

        # Pay or receive fixed flag is described side
        pay_receive_fixed = PayReceiveFixedEntry(title=retriever.retrieve(self.entry_id, input_text, _SIDE))
        context.save_one(pay_receive_fixed)
        self.pay_receive_fixed = pay_receive_fixed.get_key()

        # Tenor
        maturity = DateOrTenorEntry(title=retriever.retrieve(self.entry_id, input_text, _MATURITY))
        context.save_one(maturity)
        self.maturity = maturity.get_key()

        # Floating rate index
        float_index = RatesIndexEntry(title=retriever.retrieve(self.entry_id, input_text, _FLOAT_INDEX))
        context.save_one(float_index)
        self.float_index = float_index.get_key()

        # Fixed Rate
        fixed_rate = FixedRateEntry(title=retriever.retrieve(self.entry_id, input_text, _FIXED_RATE))
        context.save_one(fixed_rate)
        self.fixed_rate = fixed_rate.get_key()

        # Save self to DB
        Context.current().save_one(self)

Ancestors

Static methods

def check_entry_id(entry_id: str) -> None

Inherited from: TradeEntry.check_entry_id

Check that the unique identifier is compliant with the expected format.

def get_entry_id(record_type: str, title: str, body: str | None = None, data: str | None = None) -> str

Inherited from: TradeEntry.get_entry_id

Create the unique identifier from parameters.

def get_key_type() -> Type

Inherited from: TradeEntry.get_key_type

Return key type even when called from a record.

def parse_optional_bool(field_value: str | None, *, field_name: str | None = None) -> bool | None

Inherited from: TradeEntry.parse_optional_bool

Parse an optional boolean value.

def parse_required_bool(field_value: str | None, *, field_name: str | None = None) -> bool

Inherited from: TradeEntry.parse_required_bool

Parse an optional boolean value.

Fields

var approved_by -> UserKey | None

Inherited from: TradeEntry.approved_by

User who recorded the approval.

var body -> str | None

Inherited from: TradeEntry.body

Optional body of the entry if not completely described by the title (included in MD5 hash).

var data -> str | None

Inherited from: TradeEntry.data

Optional supporting data in YAML format (included in MD5 hash).

var effective -> EntryKey | None

Trade or leg effective date defined as unadjusted date or time interval relative to another date.

var entry_id -> str

Inherited from: TradeEntry.entry_id

Based on record type, title and MD5 hash of body and data if present, EntryUtil.create_id is used to generate.

var few_shot -> bool | None

Inherited from: TradeEntry.few_shot

If True, use this entry as a few-shot example.

var fixed_rate -> EntryKey | None

Fixed rate entry or breakeven rate if not specified.

var float_index -> EntryKey | None

Floating interest rate index or currency (in case of currency, default index for the currency is used).

var maturity -> EntryKey | None

Trade or leg maturity date defined as unadjusted date or time interval relative to another date.

var pay_receive_fixed -> EntryKey | None

String representation of the PayFixed or ReceiveFixed flag in the format specified by the user.

var tenor -> EntryKey | None

Swap tenor (length) in months or years.

var title -> str

Inherited from: TradeEntry.title

Title of a long entry or complete description of a short one (included in MD5 hash).

Methods

def get_key(self) -> EntryKey

Inherited from: TradeEntry.get_key

Return a new key object whose fields populated from self, do not return self.

def get_text(self) -> str

Inherited from: TradeEntry.get_text

Get the complete text of the entry.

def init(self) -> None

Inherited from: TradeEntry.init

Generate entry_id in ‘type: title’ format followed by an MD5 hash of body and data if present.

def init_all(self) -> None

Inherited from: TradeEntry.init_all

Invoke ‘init’ for each class in the order from base to derived, then validate against schema.

def run_propose(self) -> None

Retrieve parameters from this entry and save the resulting entries.