Visual risk analysis of new debt issuance based on Tushare
Recent work content
Where is the theory?
-
Theory of conversion premium arbitrage -
Amendment clause of convertible bonds -
Financial Psychology
How to achieve
Difficulties
-
Get the date of the previous 30 trading days
from chinese_calendar import is_holiday,is_workday from datetime import datetime, date, timedelta today = datetime.today() yesterday = today + timedelta(days = -1) # print(yesterday.strftime('%Y%m%d')) #Back 30 trading days n = 0 while n <29: if is_workday(yesterday): n = n + 1 yesterday = yesterday + timedelta(days = -1) last30day = yesterday.strftime('%Y%m%d') #The day of the previous 30 trading days ## print(last30day)
-
Obtain information on convertible bonds and equity
-
Matplotlib.pyplot drawing
Global Code
import tushare as ts from chinese_calendar import is_holiday,is_workday from datetime import datetime, date, timedelta import matplotlib.pyplot as plt today = datetime.today() yesterday = today + timedelta(days = -1) # print(yesterday.strftime('%Y%m%d')) #Back to the trading day n = 0 while n <29: if is_workday(yesterday): n = n + 1 yesterday = yesterday + timedelta(days = -1) last30day = yesterday.strftime('%Y%m%d') #The day of the previous 30 trading days ## print(last30day) #Tushare login Ts. set_token ('Fill in the key yourself ') pro = ts.pro_api() #Stock Code Stock_code=input ('Inter stock code ') #'002714. SZ' #Conversion price Bond_exchange=float (input ('Input the conversion price ') # 47.91 #Today's date today_simple = today.strftime('%Y%m%d') #210816 today = today.strftime('%Y-%m-%d') #21-08-16 # print(is_holiday(datetime.today())) data = pro.query('stock_basic',ts_code=stock_code) stock_name = data['name'][0] df = pro.query('daily', ts_code=stock_code, start_date=last30day, end_date=today_simple) #Current price of positive shares price = df['close'][0] #Conversion value valueOfExchange = 100 / bond_exchange * price #Premium rate premiumRate = (100 - valueOfExchange)/valueOfExchange bad = 0 loss = 0 close_price_array = [] for i in range(0,29): close_price_array.append(df['close'][i]) if df['close'][i]< bond_exchange*0.85 : #Amendment clause of share conversion bad = bad + 1 if df['close'][i]< bond_exchange: # loss = loss + 1 #Start drawing ##Input data x = df['trade_date'] y = df['close'] ##Title and Legend plt.xlabel('30 days') plt.ylabel('close price') plt.title("30 trade days price") ##Reference line plt.axhline(y=bond_exchange*0.85, c="r", ls="--", lw=2) plt.axhline(y=bond_exchange, c="g", ls="--", lw=2) plt.gca().invert_xaxis() ## plt.plot(x,y,marker='o') Plt. xticks (rotation=90) # The abscissa rotates 90 degrees for each value plt.show() Print ('convertible bond analysis report: ') Print (stock_name, stock_code, "current premium rate of convertible bonds", round (premierRate * 100,2), '%') Print (stock_name, stock_code, "the number of days below the conversion price in the last 30 trading days:", loss) Print (stock_name, stock_code, "the number of days below 85% of the conversion price in the last 30 trading days:", bad) print('---------------------------------------------------------') Print ("Note: the green line refers to the conversion of bonds to stock prices, the red line refers to 85% of the conversion price, and the negative conversion premium rate leads to arbitrage opportunities") Print ('The more the blue dot of the positive stock is lower than the green line of the conversion price, the higher the investment risk is, because there will be losses once the bonds are converted into shares') Print ('The blue point of the stock is lower than 85% of the stock price, and the number of red line dates is closer to 15, because the stock price can be revised down, the bond value will be higher ')
Later goals