This article was written 1138 days ago and last revised 1138 days ago. Some of the information may be outdated.

Visual risk analysis of new debt issuance based on Tushare

Recent work content

Returned to the most essential and stupid working method, wrote the task on the notebook (paper), and then sent WeChat one by one against the customer list... Prompt for typing

Why do you want to do such a clumsy job? What RPA or Seleium or JS visualization I learned before is still useless

So we have today's project, which I wrote in the afternoon. Based on Python's tushare financial database, we can achieve a certain degree of convertible bond risk analysis

Where is the theory?

  1. Theory of conversion premium arbitrage
  2. Amendment clause of convertible bonds
  3. Financial Psychology

How to achieve

First, the figure above is made in jupyter. It's my online python terminal. Haha, it can be used anywhere on the server. The figure is a blind example

Difficulties

  1. Get the date of the previous 30 trading days

Since the stock market trading day is generally from Monday to Friday, and it is not open on holidays, the date of the first 30 trading days is required to obtain the date of downward correction of the stock price. It is simple to obtain today's date, but if you obtain the first 30 trading days, you will go backwards chinese_calendar This library

 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)
  1. Obtain information on convertible bonds and equity

Because convertible bonds are the senior authority of tushare and cost money, they can only be queried and entered manually. After all, there are less bonds issued every day and the workload is acceptable

The information of the positive shares can be directly obtained through tushare. The closing price of the 30 days can be obtained, and the name of the positive shares can be

  1. Matplotlib.pyplot drawing

The plt does not support Chinese. You can fix it by google

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

The web side implements this service, and uses js to perform screenshot splicing operations

Reference article

  1. The latest financial management related code project of Moechu