请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3121152 新帖:38

可转债跟踪---溢价跟踪(简版-爬虫)

jjdsad发表于:8 月 9 日 17:18回复(1)

可转债跟踪---溢价跟踪(简版)¶

2018年末,可转债成交量出现异动,对于日内操作,无异于是机会。

可转债套利,该套利依旧有风险,因为转债与正股有联动,出现溢价买入转债后转正股,卖出操作需隔日;融券操作,了解了券商市场,能融到指定可转债正股的难度非常大;尾盘操作,依旧需要承担风险。笔者的金融开始是以日内回转为开始,所以自身对于可转债的套利思路,单边操作可转债为最佳。2019年不少交易员按照此手法进行套利交易,至于怎么操作,笔者不多说,让各位思考。至于溢价跟踪模型,能带来什么效益,可以做到机械化跟踪和预警,但是部分重要的日内操作因素没加入跟踪模型。

友情提醒:溢价率直接获取方式可直接爬取集思路数据,该篇文章为自己爬取数据及自己计算溢价率。

# 导入需求库
from jqdata import *
from jqlib.technical_analysis import *
import numpy as np
import pandas as pd
import time
import requests
import json
import re
code_depot = 0

一、转债代码及正股信息爬取¶

目前转债基本信息获取以集思路为来源获取!

# 可转债爬虫
def code_spider():
    base_url = 'https://www.jisilu.cn/data/cbnew/cb_list/?___jsl=LST___t=1551579067513'
    code_ini_data = get_code_dict(base_url)
    return code_ini_data

# 可转债信息爬虫内部
def get_code_dict(url):
    headers = {
        'Referer': 'https://www.jisilu.cn/data/cbnew/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0'
    }
    response = requests.get(url, headers= headers)
    text = response.text
    
    # 爬取转债代码
    bond_list = re.findall('pre_bond_id\"\:\"s[h|z]\d{6}', text)
    bond_list = re.findall('s[h|z]\d{6}', str(bond_list))
    
    # 爬取转债名称
    bond_name_list = re.findall(u'bond_nm\"\:\"(.+?)\"', text)
    
    # 爬取正股代码
    stock_list = re.findall('stock_cd\"\:\"\d{6}', text)
    stock_list = re.findall('\d{6}', str(stock_list))
    
    # 爬取正股名称
    stock_name_list = re.findall(u'stock_nm\"\:\"(.+?)\"', text)
    
    # 爬取转股日期
    convert_list = re.findall('convert_price\"\:\"(.+?)\"', text)
    convert_list = re.findall('\d{1,2}.\d{3}', str(convert_list))
    convert_list = map(float, convert_list)
    convert_dt_list = re.findall('convert_dt\"\:\"(.+?)\"', text)
    
    # 爬取最后日期
    maturity_dt_list = re.findall('\"maturity_dt\"\:\"(.+?)\"', text)
    data = {"bond_code": bond_list,"bond_name": bond_name_list ,"stock_code": stock_list, "stock_name": stock_name_list, "convert_price": convert_list, "convert_dt": convert_dt_list, "maturity_dt": maturity_dt_list}
    code_ini_data = pd.DataFrame(data)
    
    # 修改为sina交易代码
    for i in range(len(code_ini_data.index)):
        if code_ini_data['bond_code'][i][0:2] == 'sz':
            code_ini_data.loc[[i], ['stock_code']] = 'sz' + code_ini_data['stock_code'][i]
        elif code_ini_data['bond_code'][i][0:2] == 'sh':
            code_ini_data.loc[[i], ['stock_code']] = 'sh' + code_ini_data['stock_code'][i]
    return code_ini_data
code_depot = code_spider()
code_depot.head(5)
bond_code bond_name convert_dt convert_price maturity_dt stock_code stock_name
0 sh113539 圣达转债 2020-01-09 28.78 2025-07-02 sh603079 圣达生物
1 sh113533 参林转债 2019-10-10 36.50 2025-04-02 sh603233 大参林
2 sz128060 中装转债 2019-10-08 6.21 2025-03-26 sz002822 中装建设
3 sz128024 宁行转债 2018-06-11 17.70 2023-12-05 sz002142 宁波银行
4 sh113015 隆基转债 2018-05-08 18.66 2023-11-01 sh601012 隆基股份

二、可转债行情获取(SINA)¶

# 可转债盘口数据
def get_bond_data(url):
    real_price = 0
    a1_v = 0
    a1_p = 0
    a2_v = 0
    a2_p = 0
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0'
    }
    response = requests.get(url, headers= headers)
    text = response.text
    if len(text) >= 100:
        ret = re.split(',',text)
        real_price = float(ret[3])
        a1_v = int(ret[20]) / 10
        a1_p = float(ret[21])
        a2_v = int(ret[22]) / 10
        a2_p = float(ret[23])
    return real_price, a1_v, a1_p, a2_v, a2_p

# 可转债盘口数据打印
real_price, a1_v, a1_p, a2_v, a2_p = get_bond_data('http://hq.sinajs.cn/list=sh113539')
print('可转债:{},最新价格:{},卖一量:{},卖一价:{},卖二量:{},卖二价:{}'.format('113539',real_price, a1_v, a1_p, a2_v, a2_p))
可转债:113539,最新价格:108.84,卖一量:3,卖一价:108.89,卖二量:32,卖二价:108.9

三、正股行情获取(SINA)¶

# 正股盘口数据
def get_stock_data(url):
    real_price = 0
    b1_v = 0
    b1_p = 0
    b2_v = 0
    b2_p = 0
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0'
    }
    response = requests.get(url, headers= headers)
    text = response.text
    if len(text) >= 100:
        ret = re.split(',',text)
        real_price = float(ret[3])
        b1_v = int(ret[10]) / 100
        b1_p = float(ret[11])
        b2_v = int(ret[12]) / 100
        b2_p = float(ret[13])
    return real_price, b1_v, b1_p, b2_v, b2_p

# 正股盘口数据打印
real_price, b1_v, b1_p, b2_v, b2_p = get_stock_data('http://hq.sinajs.cn/list=sh603079')
print('正股:{},最新价格:{},买一量:{},买一价:{},买二量:{},买二价:{}'.format('603079',real_price, b1_v, b1_p, b2_v, b2_p))
正股:603079,最新价格:33.26,买一量:7,买一价:33.25,买二量:30,买二价:33.23

四、可转债/正股数据整合¶

# 整合数据集
def bond_spider():
    code_data = []
    bond_list = []
    stock_list = []
    bond_price_list = []
    bond_a1_v_list = []
    bond_a1_p_list = []
    bond_a2_v_list = []
    bond_a2_p_list = []
    stock_price_list = []
    stock_b1_v_list = []
    stock_b1_p_list = []
    stock_b2_v_list = []
    stock_b2_p_list = []
    base_url = 'http://hq.sinajs.cn/list={}'
    code_dict = code_depot
    bond_list = code_dict['bond_code']
    stock_list = code_dict['stock_code']
    
    # 遍历可转债及正股行情数据
    for i in range(len(code_dict.index)):
        get_bond = base_url.format(code_dict['bond_code'][i])
        bond_price, bond_a1_v, bond_a1_p, bond_a2_v, bond_a2_p = get_bond_data(get_bond)
        bond_price_list.append(bond_price)
        bond_a1_v_list.append(bond_a1_v)
        bond_a1_p_list.append(bond_a1_p)
        bond_a2_v_list.append(bond_a2_v)
        bond_a2_p_list.append(bond_a2_p)
        
        get_stock = base_url.format(code_dict['stock_code'][i])
        stock_price, stock_b1_v, stock_b1_p, stock_b2_v, stock_b2_p = get_stock_data(get_stock)
        stock_price_list.append(stock_price)
        stock_b1_v_list.append(stock_b1_v)
        stock_b1_p_list.append(stock_b1_p)
        stock_b2_v_list.append(stock_b2_v)
        stock_b2_p_list.append(stock_b2_p)
    # 整合数据
    data = {"bond_code": bond_list, "bond_price": bond_price_list, "bond_a1_p": bond_a1_p_list, "bond_a1_v": bond_a1_v_list, \
            "bond_a2_p": bond_a2_p_list, "bond_a2_v": bond_a2_v_list, "stock_code": stock_list,"stock_price": stock_price_list, \
            "stock_b1_p": stock_b1_p_list, "stock_b1_v": stock_b1_v_list, "stock_b2_p": stock_b2_p_list, "stock_b2_v": stock_b2_v_list}
    code_data_init = pd.DataFrame(data)
    code_data = pd.merge(code_data_init,code_dict,on=None,how='outer',sort=False) 
    code_data = code_data[code_data['bond_price'] != 0]
    return code_data

# 获取整合数据并打印
code_data = bond_spider()
code_data.head(5)
bond_a1_p bond_a1_v bond_a2_p bond_a2_v bond_code bond_price stock_b1_p stock_b1_v stock_b2_p stock_b2_v stock_code stock_price bond_name convert_dt convert_price maturity_dt stock_name
0 108.89 3 108.900 32 sh113539 108.84 33.25 7 33.23 30 sh603079 33.26 圣达转债 2020-01-09 28.78 2025-07-02 圣达生物
1 137.40 100 137.430 15 sh113533 137.27 52.32 9 52.31 4 sh603233 52.33 参林转债 2019-10-10 36.50 2025-04-02 大参林
2 111.70 31 111.735 2 sz128060 111.70 7.21 2 7.20 1 sz002822 7.23 中装转债 2019-10-08 6.21 2025-03-26 中装建设
3 127.85 504 127.900 1 sz128024 127.85 22.77 2 22.76 81 sz002142 22.77 宁行转债 2018-06-11 17.70 2023-12-05 宁波银行
4 133.34 5 133.430 7 sh113015 133.21 24.92 213 24.91 24 sh601012 24.92 隆基转债 2018-05-08 18.66 2023-11-01 隆基股份

五、筛选溢价数据¶

# 溢价筛选
def get_bond_premium():
    code_data = bond_spider()
    
    # 转股价值计算
    code_data['convertible_value'] = code_data['stock_price'] * 100 / code_data['convert_price']
    
    # 溢价率计算
    code_data['premium_rate'] = code_data['bond_price'] / code_data['convertible_value'] - 1 
    
    code_data = code_data.sort_index(axis = 0,ascending = True,by = 'premium_rate').reset_index(drop=True)
    code_data = code_data.head(1)
    code_data['convertible_value'] = code_data['convertible_value'].apply(lambda x: format(x, '.2f'))
    code_data['premium_rate'] = code_data['premium_rate'].apply(lambda x: format(x, '.2%'))
    if len(code_data.index) > 0:
        print('------------{}可转债溢价报价------------'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
        for i in range(0,len(code_data.index)):
            
            print('\n套利时间:{0},\n套利溢价:{1},\n转债代码:{2},\n转债名称:{3},\n债券卖一:{4},\n卖一数量:{5},\n转债卖二:{6},\n卖二数量:{7},\n转股价值:{8},\n正股代码:{9},\n正股名称:{10}\n正股买一:{11},\n买一数量:{12},\n正股买二:{13},\n买二数量:{14},\n转股日期:{15},\n到期时间:{16}'.format( \
                     time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), code_data['premium_rate'][i], code_data['bond_code'][i], code_data['bond_name'][i], \
                     code_data['bond_a1_p'][i], code_data['bond_a1_v'][i], code_data['bond_a2_p'][i], code_data['bond_a2_v'][i], code_data['convertible_value'][i], \
                     code_data['stock_code'][i], code_data['stock_name'][i], code_data['stock_b1_p'][i], code_data['stock_b1_v'][i], code_data['stock_b2_p'][i], \
                     code_data['stock_b2_v'][i], code_data['maturity_dt'][i], code_data['convert_dt'][i]))

# 打印数据
get_bond_premium()
------------2019-08-08 22:29:37可转债溢价报价------------

套利时间:2019-08-08 22:29:37,
套利溢价:-5.82%,
转债代码:sh113539,
转债名称:圣达转债,
债券卖一:108.89,
卖一数量:3,
转债卖二:108.9,
卖二数量:32,
转股价值:115.57,
正股代码:sh603079,
正股名称:圣达生物
正股买一:33.25,
买一数量:7,
正股买二:33.23,
买二数量:30,
转股日期:2025-07-02,
到期时间:2020-01-09

六、总结¶

上文内容,无剔除尚不可转股的可转债,并且并无剔除低量可转债。
可转债套利模式,多样,更多数据整合,还需要再深入分析,但是万变不离量价两者关系,特别是市场强势时候最为明显。

可共同交流可转债内容,如有时间,将继续更新可转债其他内容。

 

全部回复

0/140

量化课程

    移动端课程