Query active bonds-query corresponding yield-calculate spread

Query active coupons:

from apis.factor_handler import FactorHandlerAPI
from apis.quant_handler import QuantHandlerAPI
from apis.market_handler import MarketHandlerAPI
import datetime
import pandas as pd
import numpy as np
import re
importsys
import os

#Hidden function print
def initialize_hidden_prints(activated=True):
    original_stdout = None
    if activated:
        original_stdout = sys.stdout
        sys.stdout = open(os.devnull, 'w')
    return original_stdout

def open_hidden_prints(original_stdout):
    sys.stdout.close()
    sys.stdout = original_stdout

def close_hidden_prints():
    return sys.stdout

#Active bonds information inquiry
def Active_bond(start_date,end_date,factor_id):
    FT = FactorHandlerAPI("10.253.50.110") # IP
    example = {'quote_start_time': start_date, \
               'quote_end_time': end_date, \
               'factor_id': factor_id}
    res = FT.get_actived_bond(example)
    return res["dataset"]

# Helper functions
def custom_sort(term):
        order = {'1M': 1, '3M': 2, '6M': 3, '9M': 4, '1Y': 5, '2Y': 6 , '3Y': 7, '5Y': 8, '7Y': 9, '10Y': 10, '15Y': 11, '20Y': 12, \ '30Y': 13, '50Y': 14}
        return order.get(term, 0)

# Find the specified information for the coupon
def find_info(data, dateline):
    filtered_data = []
    dateline.sort(key = custom_sort)

    # Loop through the date and data
    for date, items in sorted(data.items(), key=lambda x: custom_sort(x[0])):
        # Iterate through each data item
        for item in items:
            # Filter out data that matches a specific deadline in Dateline
            if item['residual_maturity'] in dateline:
                filtered_data.append(item)
                
    filtered_data_new = [{'quote_time': item['quote_time'],
                      'variety_code': item['variety_code'],
                      'factor_value': item['factor_value'],
                      #'residual_maturity': item['residual_maturity']
                         }
                     for item in filtered_data]
    
    df = pd.DataFrame(filtered_data_new)
    df['quote_time'] = pd.to_datetime(df['quote_time'], format='%Y-%m-%d')
    
    return df

transfer:

from Active_bond import *
from apis.market_handler import MarketHandlerAPI
import pandas as pd
import sys, os
original_stdout = initialize_hidden_prints(activated=True)

factor_id = '1696824430582472704'
start_date = '2023-08-01'
end_date = '2023-08-01'
dateline = ['5Y', '10Y']

data = Active_bond(start_date,end_date,factor_id)
open_hidden_prints(original_stdout) # Execute the code that needs to display the printout

bondinfo = find_info(data, dateline)
bondinfo

Get the rate of return:

import pandas as pd
import numpy as np
import re
from datetime import timedelta
from apis.factor_handler import FactorHandlerAPI
from apis.quant_handler import QuantHandlerAPI
from apis.market_handler import MarketHandlerAPI

# Check market information yield
"""
The function find_bond_market called by the function yield_rate_inqure
"""

def find_bond_market(start_date, end_date, bond_codes):
    from datetime import datetime
    api = MarketHandlerAPI("10.243.140.184") # IP

    extracted_data = []
    data = []
    
    # Character formatting is converted to date format
    start_date_obj = datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S')
    start_date_str =start_date_obj.strftime('%Y-%m-%d %H:%M:%S')
    end_date_obj = datetime.strptime(end_date, '%Y-%m-%d %H:%M:%S')
    end_date_str =end_date_obj.strftime('%Y-%m-%d %H:%M:%S')
    
    # Inquire about the corresponding bond information
    for bond_code in bond_codes:

        param = {
            "start_date": start_date_str,
            "end_date": end_date_str,
            "variety_code": bond_code,
            "page_size": 100,
            "quotation_type": "cmds_market_data_xswap",
            "interval": "d",
            "index_type": "3"
        }

        res = api.load_quotation(param)

        data.extend(res['dataset'])

    for item in data:

        extracted_data.append({
            "symbol": item["symbol"],
            "datetime": item["datetime"],
            "yield_rate": item["yield_rate"],
            "vt_symbol": item["vt_symbol"]
        })

    extracted_data = pd.DataFrame(extracted_data)

# extracted_data['datetime'] = pd.to_datetime(extracted_data['datetime'])
# extracted_data['datetime'] = extracted_data['datetime'].dt.strftime('%Y-%m-%d')

    return extracted_data


def yield_rate_inqure(start_date: str , end_date: str, df , bond_codes):
    if len(bond_codes) == 0:
        start_date = start_date + ' 00:00:00'
        end_date = end_date + ' 00:00:00'

        dt = pd.DataFrame( columns = ['symbol', 'datetime', 'yield_rate', 'vt_symbol'] )
        from datetime import datetime
        # Convert date string to a datetime object

        start_datetime = datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S')
        end_datetime = datetime.strptime(end_date, '%Y-%m-%d %H:%M:%S')

        # Define an incremental time interval of one day

        delta = timedelta(days=1)

        current_datetime = start_datetime

        # Date cycle

        while current_datetime <= end_datetime:

            filtered_rows=[]

            current_date_str = current_datetime.strftime('%Y-%m-%d')

            # Extract rows with the same current date

            filtered_rows = df[df['quote_time'] == current_date_str]

            # Skip if there is a missing or non-existent date

            if filtered_rows.empty or len(filtered_rows['variety_code']) == 1:

                current_datetime + = delta
                continue

            else:
                try:
                    # Process and search for bond codes

                    for i in range(len(filtered_rows['variety_code'])):

                        obj1 = filtered_rows['variety_code'][0]
                        obj2 = filtered_rows['variety_code'][1]

                        code_1 = obj1 + '.BOND' # 230008.BOND
                        code_2 = obj2 + '.BOND' # 230012.BOND

                        bond_codes = [code_1, code_2]

                        # Check the information of the bonds on the same day, it is 00:00:00-23:59:59

                        start = current_datetime.strftime("%Y-%m-%d") + " 00:00:00"
                        end = current_datetime.strftime("%Y-%m-%d") + " 23:59:59"

                        dft = find_bond_market(start_date, end_date, bond_codes)

                        for i in range(len(df)):

                            symbol = dft['symbol'][i]
                            dateTime = dft['datetime'][i] # Character date, the next step is to convert it to date type
                            yield_rate = dft['yield_rate'][i]
                            vt_symbol = dft['vt_symbol'][i]

                            # Add the found data to the dt data box
                            dt = dt.append({'symbol': symbol, 'datetime': dateTime, 'yield_rate': yield_rate, 'vt_symbol': vt_symbol}, ignore_index=True)

                exceptKeyError:

                    current_datetime + = delta
                    continue

            # Incremental Date
            current_datetime + = delta

        return dt
    
    else:
        from datetime import datetime
        bond_codes = [code + '.BOND' for code in bond_codes]
        api = MarketHandlerAPI("10.243.140.184") # IP
        data = []
        start_datetime = datetime.strptime(start_date, '%Y-%m-%d').strftime('%Y-%m-%d %H:%M:%S')
        start_datetime_st = str(start_datetime)
        end_datetime = datetime.strptime(end_date, '%Y-%m-%d').strftime('%Y-%m-%d %H:%M:%S')
        end_datetime_st = str(end_datetime)
        for bond_code in bond_codes:
            param = {
                "start_date": start_datetime_st,
                "end_date": end_datetime_st,
                "variety_code": bond_code,
                "page_size": 100,
                "quotation_type": "cmds_market_data_xswap",
                "interval": "d",
                "index_type": "3"
            }
            res = api.load_quotation(param)
            data.extend(res['dataset'])

        extracted_data = []

        for item in data:
            extracted_data.append({
                "symbol": item["symbol"],
                "datetime": item["datetime"], # Character date, the next step is to convert it to date type
                "yield_rate": item["yield_rate"],
                "vt_symbol": item["vt_symbol"]
            })
        extracted_data = pd.DataFrame(extracted_data)
    # extracted_data['datetime'] = pd.to_datetime(extracted_data['datetime'])
    # extracted_data['datetime'] = extracted_data['datetime'].dt.strftime('%Y-%m-%d')
        return extracted_data

transfer:

from inqure_yield import *
from Active_bond import *
from datetime import datetime
original_stdout = initialize_hidden_prints(activated=True) # Execute the code here that needs to hide the printout
# inquire active bond code
factor_id = 'factor_id' # factor_id = str '1696824430582472704'
start_date = date # date: str '2023-06-17'
end_date = date # date: str '2023-08-23'
dateline = ['term_1', 'term_2'] # date_term: list ['5Y', '10Y']

data = Active_bond(start_date,end_date,factor_id)
bondinfo = find_info(data, dateline)
bondinfo

# inquire bond yield
open_hidden_prints(original_stdout) # Execute the code that needs to display the printout

start_date_1 = date # date: str '2023-06-17', start_date_1 > start_date
end_date_1 = date # date: str '2023-08-23', start_date_2 < start_date
"""
When querying yield for the entire period, the rule is that bondinfo is not empty.
When querying yield for a specific bond code, bondinfo is empty and the code
is filled in to the corresponding bond_codes. default bond_codes = [] ,bondinfo != [].
"""
#bondinfo = []

#bond_codes = [] # This parameter is not necessary
"""
# bond_codes = ['symbol_1' , 'symbol_2']
# 'symbol_1' , 'symbol_2' must be in bondinfo
# eg: 'symbol_1' , 'symbol_2' = '230008' , '230012'
"""
#start_date_1 , end_date_1 , bondinfo : These parameters are necessary
inqure = yield_rate_inqure(start_date_1 , end_date_1 ,bondinfo , bond_codes)
inquire

Spread calculation:

Python
File
Edit
View
Language
1
import re
2
import numpy as np
3
import pandas as pd
4
def yield_diff(df_filtered):
5
?
6
    rate_diff = pd.DataFrame(columns=['datetime', 'vt_symbol_1', 'yield_rate_1', 'vt_symbol_2', 'yield_rate_2','yield_diff'])
7
?
8
    # List of dates to be filled
9
    date_list = df_filtered['datetime'].unique()
10
?
11
    # Loop iterate over dates
12
    for date in date_list:
13
?
14
        # Get data for the current date
15
        data = df_filtered[df_filtered['datetime'] == date]
16
?
17
        data_code = data['symbol'].unique()
18
?
19
        # Get the value of data
20
        datetime_val = data['datetime'].values[0]
twenty one
        symbol_1_val = data[data['symbol'] == data_code[0]]['vt_symbol'].values[0]
twenty two
        yield_rate_1_val = data[data['symbol'] == data_code[0]]['yield_rate'].values[0]
twenty three
        symbol_2_val = data[data['symbol'] == data_code[1]]['vt_symbol'].values[0]
twenty four
        yield_rate_2_val = data[data['symbol'] == data_code[1]]['yield_rate'].values[0]
25
        yield_diff_val = min(yield_rate_1_val, yield_rate_2_val) - max(yield_rate_1_val, yield_rate_2_val)
26
?
27
        # Fill the data frame with values
28
        rate_diff.loc[len(rate_diff)] = [datetime_val, symbol_1_val, yield_rate_1_val, symbol_2_val, yield_rate_2_val, yield_diff_val]
29
?
30
    print(rate_diff)

transfer:

from inqure_yield import *
from Active_bond import *
from rate_diff import *
from datetime import datetime
import pandas as pd
original_stdout = initialize_hidden_prints(activated=True) # Execute the code here that needs to hide the printout
# inquire active bond code
factor_id = 'factor_id' # factor_id = str '1696824430582472704'
start_date = date # date: str '2023-06-17'
end_date = date # date: str '2023-08-23'
dateline = ['term_1', 'term_2'] # date_term: list ['5Y', '10Y']

data = Active_bond(start_date,end_date,factor_id)
bondinfo = find_info(data, dateline)
bondinfo

# inquire bond yield
open_hidden_prints(original_stdout) # Execute the code that needs to display the printout

start_date_1 = date # date: str '2023-06-17', start_date_1 > start_date
end_date_1 = date # date: str '2023-08-23', start_date_2 < start_date
"""
When querying yield for the entire period, the rule is that bondinfo is not empty.
When querying yield for a specific bond code, bondinfo is empty and the code
is filled in to the corresponding bond_codes. default bond_codes = [] ,bondinfo != [].
"""
#bondinfo = []

#bond_codes = [] # This parameter is not necessary
"""
# bond_codes = ['symbol_1' , 'symbol_2']
# 'symbol_1' , 'symbol_2' must be in bondinfo
# eg: 'symbol_1' , 'symbol_2' = '230008' , '230012'
"""
#start_date_1 , end_date_1 , bondinfo : These parameters are necessary
inqure = yield_rate_inqure(start_date_1 , end_date_1 ,bondinfo , bond_codes)
inquire