python quantitative transaction Chapter 2

Posted by lilRachie on Thu, 03 Feb 2022 11:42:39 +0100

I What is a stock

1.1 origin of shares
In the 15th century, maritime trade and colonial plunder rose, which was a way to get rich. Western countries gave birth to a batch of ocean voyagers, which also enriched western countries who were good at sea trade and plunder. To organize long-distance trade, fleets must be formed, which requires huge funds. At the same time, long-distance shipping is often at risk of marine hurricanes and attacks by indigenous peoples. At that time, few people could have such huge team building funds and such risk tolerance. In order to raise capital and share risks, there is share financing, that is, before each voyage, find the source of funds and form the form of shares by shares. After the voyage, return the capital to the investor, and distribute the profits according to the proportion of the share capital. This is the initial form of the stock, which comes from the way of ocean shipping financing.
1.2 shares
The certificate of ownership issued by a joint stock company is an investment tool and investment medium.
1.3 characteristics of shares
Stock: the ownership certificate of a joint-stock company, which is responsible for its own profits and losses
Fund: Portfolio (stocks, bonds, cash)
Bonds: marketable securities that repay principal and interest (Treasury bonds, corporate bonds, financial bonds)
Risk: Stocks (10%) > funds (5%) > bonds (1%)
Income: stocks > funds > bonds

II Use JQData to query market data

2.1JQData
JQData is a local quantitative financial data service provided by jukuan data team for financial institutions, academic groups and quantitative researchers. Using JQData, you can quickly view and calculate financial data, and solve the needs of local, Web and financial terminals to call data without obstacles. After five years of precipitation, 400000 kuanke and 3500 well-known quantitative institutions have verified the investment and research transactions. In terms of use, JQData is applicable to a variety of operating systems such as Windows, Mac and Linux, and supports python2, python3 and as well as any programming language.
2.2 installation and use of JQData
(1) Install jqdatasdk package
(2) Log in to JQData:

from jqdatasdk import *
auth('ID','Password') #ID is the mobile phone number filled in at the time of application; Password is the login password of jukuan official website

2.3 obtaining stock market data

#get_price
get_price(security, start_date=None, end_date=None, frequency='daily', fields=None, skip_paused=False, fq='pre', count=None, panel=True, fill_paused=True)

Parameter nameParameter descriptionnotes
securitySubject matterAvailable types: stocks, futures, funds, indexes and options
start_dateThe start time cannot be used together with count. When 'count' and 'start'_ When 'date' is None, the default value is' 2015-01-01 00:00:00 'When the specified frequency is minute, if only the date is passed in, the time of the day is 00:00:00 of the current day
end_dateThe end time, if not specified, defaults to '2015-12-31 00:00:00'. Need and start_date or count are used at the same timeWhen the specified frequency is minute, if only the date is passed in, the time of the day is 00:00:00 of the current day, so the returned data does not include end_date.
countIndicates get end_ Data of several frequencies before date, and start_date cannot be used at the same time.The number of rows of the returned result set, that is, get end_ count frequency data before date
frequencyThe length of the day level (i.e. 'Day' or 'minute' can also be specified as the length of the day level (i.e. 'minute')daily '(same as' 1d'), 'minute' (same as' 1m '),
fieldsThe field name of the obtained data, that is, the header. The default is none (return standard fields ['open', 'close', 'high', 'low', 'volume', 'money'])You can choose to fill in the following fields. For field descriptions, please refer to the following fields table ['open', 'close', 'low', 'high', 'volume', 'money', 'factor', 'high_limit', 'low_limit', 'avg', 'pre_close', 'paid', 'open_interest], open_ Interest is the futures position
skip_pausedWhether to skip the non trading date (including the date after suspension / unlisted / delisting)If you don't skip, the data before the suspension will be filled in during the suspension, and the data before listing or after delisting will be nan.
fill_pausedFor the price processing of suspended stocks, the default value is TrueThe default is True and pre is used_ Close (price filling); False indicates that the suspended stock price is filled with NAN.
fqOption of re weighting. The default is pre re weighting (fq = 'pre')'pre': pre resumption / 'none': no resumption, return actual price / 'post': Post resumption
panelSpecifies that the returned data format is panelThe default is True; Return dataframe format when panel=False is specified;

Field properties in fields

Field nameChinese namenotes
openPrice at the beginning of the time period
closePrice at end of time period
lowLowest price in time period
highHighest price in time period
volumeNumber of shares traded in the time periodinstitution-owned stock
moneyAmount of transactions in the time period
factorPre ': pre resumption (default) / None: no resumption, return actual price /' post ': Post resumptionPre (post) reversion data = price × Pre (post) weight recovery factor; Trading volume after pre (post) re weighting = trading volume / pre (post) re weighting factor; Transaction volume is not handled
high_limitPrice limit in time period
low_limitLimit price in time period
avgAverage price over time(1) Day level: stock is the trading volume divided by the trading volume; Futures are obtained directly from the CTP market. The calculation method is the transaction volume divided by the transaction volume and then divided by the contract multiplier; (2) Minute level: multiply the current price of all the tickets in the minute by the trading volume of the ticket, and then divide by the trading volume of the minute.
pre_closeThe price at the end of the previous unit time is the closing price of the previous day by dayFutures: pre_close – settlement of the previous day; Get is recommended_ Extras obtains the settlement price; Pre at minute frequency_ close=open
pausedbool value, whether the stock is suspended;Open / close / low / high / pre during suspension_ close; Are equal to the closing price before the suspension, volume=money=0

Get a stock example

#Obtain the data of Ping An Bank in the first four time units based on "2015-01-30 14:00:00" in a 1-minute cycle
df = get_price('000001.XSHE', end_date='2015-01-30 14:00:00',count=4, frequency='minute', fields=['open','close','high','low','volume','money'])
print(df)
                     open  close  high   low    volume      money
2015-01-30 13:57:00  8.98   8.97  8.98  8.97  295949.0  2656384.0
2015-01-30 13:58:00  8.97   8.97  8.98  8.97  339030.0  3041408.0
2015-01-30 13:59:00  8.98   8.98  8.98  8.96  459533.0  4121592.0
2015-01-30 14:00:00  8.98   8.97  8.98  8.96  469211.0  4208384.0

get_ all_ All subject information of securities (stocks, funds, indexes, futures, etc.)

get_all_securities(types=[], date=None)

parameter

types: list: used to filter the types of securities. The list elements are optional: 'stock', 'fund', 'index', 'futures',' etf ',' lof ',' fja ',' fjb '. When types is blank, all stocks are returned, excluding funds, indexes and futures
Date: date, a string or [datetime.datetime]/[datetime.date] object, which is used to obtain the information of stocks still listed on a certain date The default value is None, which means that the stock information of all dates is obtained
Return [pandas.DataFrame], for example:
get_all_securities()[:2]

display_namenamestart_dateend_datetype
000001.XSHEPing An BankPAYH1991-04-039999-01-01stock
000002.XSHEVanke AWKA1991-01-299999-01-01stock
  • display_name: Chinese name
  • name: abbreviation
  • start_date: listing date
  • end_date: delisting date. If there is no delisting, it is 2200-01-01
  • Type: type, stock, index, ETF, FJ A, FJ B, fjm, mmf open_fund (open-end fund),
    bond_fund, stock_fund (Equity Fund), QDII_fund (QDII fund),
    money_market_fund (OTC Monetary Fund), mixture_fund, options
#Get the market data of all A shares
stocks = list(get_all_securities(['stock']).index)
print(stocks)
#How to obtain stock market data
for stock_code in stocks:
    print("Getting stock market data,Stock Code:",stock_code)
    df=get_price(stock_code,count=10,end_date='2022-01-22',frequency='daily',panel=False)
    print(df)
    time.sleep(3)

III Convert time series using resample function

#Conversion cycle: day K is converted to week K
df = get_price('000001.XSHE', start_date='2021-01-01',end_date='2021-12-31', frequency='daily', panel=False)
df['weekday']=df.index.weekday#Get day K
print(df)
#Get week K (of the current week): opening price (the first day of the current week), closing price (the last day of the current week), highest price (the current week), lowest price (the current week)
df_week=pd.DataFrame()
df_week['open']=df['open'].resample('w').first()
df_week['close']=df['close'].resample('w').last()
df_week['high']=df['high'].resample('w').max()
df_week['low']=df['low'].resample('w').min()
print(df_week)
#Summary statistics: make statistics on the trading volume and turnover (sum) of the next month
df_week['volume(sum)']=df['volume'].resample('W').sum()
df_week['volume(sum)']=df['volume'].resample('W').sum()
print(df_week)

IV Query financial indicators using JQData

4.1 what are financial indicators

Financial statements include: balance sheet, income statement and cash flow statement

  • Balance sheet: reflects the company's financial background and liabilities
  • Profit statement: the company's profitability, how much it earns and how it earns implies the expectation of future profit growth, reflecting market space and growth ability
  • Cash flow statement: accrual basis VS cash basis, reflecting hematopoietic ability, competitive advantage and bargaining advantage
'''Obtain stock financial indicators'''
df=get_fundamentals(query(indicator),statDate='2020')#Obtain financial index data

df.to_csv('D:/python project/pythonProject/data/finance/finanace2020.csv')#Store data
print(df)
#Stock selection based on profit index: eps, operating_profit,roe,inc_net_profit_year_on_year
df=df[(df['eps']>0)&(df['operating_profit']>2212173617)&(df['roe']>11)&(df['inc_net_profit_year_on_year']>10)]
print(df)

V Using JQData to query valuation indicators

5.1 valuation method

  • Absolute valuation method: Pricing Model - > calculate the intrinsic value of the enterprise
  • Relative valuation method: PE P / E ratio, PB P / B ratio, PS P / B ratio

Market value data
Updated daily
Table name: value

ListingMeaning of columnexplainformula
codeStock codeWith suffix XSHE/.XSHG
daydateDate of fetching data
capitalizationTotal share capital (10000 shares)Total number of ordinary shares issued by the company (including the total share capital of A shares, B shares and H shares)
circulating_capCirculating share capital (10000 shares)The total number of shares issued by the company that have been listed and circulated in China and converted into RMB (the circulating share capital in the A-share market)
market_capTotal market value (100 million yuan)Closing price of a shares * total share capital of issued shares (A shares + B shares + H shares)
circulating_market_capCirculation market value (100 million yuan)Circulating market value refers to the total value of circulating shares obtained by multiplying the number of tradable shares at that time by the current stock price in a specific time.Closing price of A-share market * number of circulating shares in A-share market
turnover_ratioTurnover rate (%)It refers to the frequency of stock turnover in the market within a certain period of time. It is one of the indicators reflecting the strength of stock liquidity.Turnover rate = [trading volume (hands) on the specified trading day 100 / circulating share capital (shares) of shares as of that day] 100%
pe_ratioPrice earnings ratio (PE, TTM)The market price per share is a multiple of earnings per share, reflecting the price investors are willing to pay for each yuan of net profit, which is used to estimate the investment return and risk of stocksPrice earnings ratio (PE, TTM) = (closing price of shares on the specified trading date * total share capital of the company on the closing date) / net profit attributable to shareholders of the parent company TTM.
pe_ratio_lyrPrice earnings ratio (PE)The static P / E ratio calculated by earnings per share of the previous year Share price / latest annual report EPSPrice earnings ratio (PE) = (closing price of shares on the specified trading date * total share capital of the company as of that day) / net profit attributable to shareholders of the parent company.
pb_ratioPrice to book ratio (PB)Ratio of share price to net assets per sharePrice to book ratio = (closing price of shares on the specified trading date * total share capital of the company as of that day) / equity attributable to shareholders of the parent company.
ps_ratioMarket sales rate (PS, TTM)The market sales ratio is the ratio of stock price to sales income per share. The smaller the market sales ratio is, the higher the investment value is generally considered.Price to sales ratio TTM = (closing price of the stock on the specified trading date * total share capital of the company as of that day) / total operating revenue TTM
pcf_ratioPrice to cash ratio (PCF, net cash flow TTM)The market price per share is a multiple of the net cash flow per shareCash to market ratio = (closing price of shares on the specified trading date * total share capital of the company as of that day) / net increase in cash and cash equivalents TTM
'''Obtain stock valuation indicators'''
df=get_fundamentals(query(valuation),statDate=datetime.datetime.today())
print(df)
'''Obtain stock financial indicators'''
df=get_fundamentals(query(indicator),statDate='2020')#Obtain financial indicators
df.to_csv('D:/python project/pythonProject/data/finance/finanace2020.csv')#Store data
#Stock selection based on profit index: eps, operating_profit,roe,inc_net_profit_year_on_year
df=df[(df['eps']>0)&(df['operating_profit']>2212173617)&(df['roe']>11)&(df['inc_net_profit_year_on_year']>10)]
df.index=df['code']
#print(df.head())
'''Obtain stock valuation indicators'''
df_valuation=get_fundamentals(query(valuation),statDate=datetime.datetime.today())
df_valuation.index=df_valuation['code']
#print(df_valuation.head())
df['pe_ratio']=df_valuation['pe_ratio']
df=df[df['pe_ratio']<50]
print(df)

Vi Update stock data in real time

6.1 quantitative trading system
Quantitative trading platform function module

Market records and historical dataTransaction strategy and back test moduleTransaction data management and query
Subject informationTiming strategyEntrusted management
Market recordSecurity Selection Position management
Historical recordsStop profit and stop loss strategyfund management
Data exportData back testRevenue management
Chart visualizationChart visualizationChart visualization

6.2 code practice
Function:

  • def get the list of all A shares
  • def gets the data of the current stock market
  • def export stock market data
  • def conversion stock market cycle
  • def to obtain the valuation index of a single stock
import time

import pandas as pd
#Global variables:
data_root='D:/python project/pythonProject/data/'
#000002.XSHE  	 Vanke a000001 Xshe Ping An Bank
from jqdatasdk import *
auth('15608617235','Xwt688698')  #The account number is the mobile phone number filled in at the time of application; The password is the login password of jukuan official website
pd.set_option('display.max_rows',1000000)
pd.set_option('display.max_columns',10000)
import datetime

def get_stock_list():
    """
     Get all A List of shares
    :return: stock_list
    """
    stocks_list= list(get_all_securities(['stock']).index)
    return stocks_list
def get_single_stock_Price(code,time_freq,start_date,end_date):
    """
    Get the current stock market data
    :param code:
    :param time_freq:
    :param start_date:
    :param end_date:
    :return:
    """
    data = get_price(code,start_date=start_date, end_date=end_date, frequency=time_freq, panel=False)
    return data

def exoprt_data(data,filename,type):
    """
    Export stock related data
    :param data:
    :param data:Stock data types can be: price,finance
    :param filename:
    :return:
    """
    file_root=data_root+type+'/'+filename+'.csv'
    data.index.names=['date']
    data.to_csv(file_root)
    print('Successfully stored to:',file_root)
def get_csv_data(code,type):
    file_root=data_root+type+'/'+code+'.csv'
    return pd.read_csv(file_root)
def transfer_price_freq(data,time_freq):
    """
    Convert data to a specified period:Cycle opening price(First day of cycle),Closing price (the last day of the cycle), highest price (within the cycle), lowest price (within the cycle)

    :param data:
    :param time_freq:
    :return:
    """
    #Get week K (of the current week): opening price (the first day of the current week), closing price (the last day of the current week), highest price (the current week), lowest price (the current week)
    df_trans=pd.DataFrame()
    df_trans['open']=data['open'].resample(time_freq).first()
    df_trans['close']=data['close'].resample(time_freq).last()
    df_trans['high']=data['high'].resample(time_freq).max()
    df_trans['low']=data['low'].resample(time_freq).min()
    return df_trans
def get_signle_finance(code,date,statDate):
    """
    Obtain financial indicators of individual stocks
    :param code:
    :param date:
    :param statDate:
    :return:
    """
    data = get_fundamentals(query(indicator).filter(indicator.code==code), date=date,statDate=statDate)  # Obtain financial index data
    return data
def get_single_valuation(code,date,statDate):
    """
    Obtain single stock valuation index
    :param code:
    :param date:
    :param statDate:
    :return:
    """
    data = get_fundamentals(query(valuation).filter(valuation.code==code), date=date,statDate=statDate)  # Obtain financial index data
    return data


VII Actual combat: create stock database

  • Call stock: create example module - > stock script - > get data
  • Real time acquisition: obtain daily k data and update it circularly
#Call the quotation data of a stock
code='000001.XSHG'
data=st.get_single_stock_Price(code=code,time_freq='daily',start_date='2021-02-01',end_date='2021-03-01')
#Save to csv
st.exoprt_data(data=data,filename=code,type='price')
print(data)
#Get data from csv
data=st.get_csv_data(code=code,type='price')
print(data)

Topics: Python Blockchain