The Python In Action – Building a Stock Based Quantitative Trading System booklet focuses on Python in action, but the content design provides a pre-introductory chapter to help readers quickly learn how to use the basic tools. In the meantime, we will continue to update some Python and quantity-related extension articles to help you strengthen your foundation and add value to your learning. The following articles have been published:
In the pamphlet “Stock Data Analysis: Common Stock Trading Data Interface for Differentiated Analysis” section, the method of obtaining daily stock quotation data is introduced. In fact, the most original data is transaction data, and the data of various periods of quotation software are formed through the conversion of transaction data across periods.
In this column, we will introduce the acquisition and processing of transaction data.
First, use tushare get_tick_data() interface to obtain the historical pen data of zhejiang University new 2019-08-08, as shown below:
df = ts.get_tick_data('600797',date='2019-08-08',src='tt')
print(df.head(10))
"""Time price change volume amount type 0 09:25:05 7.89 0.09 385 303765 1 09:30:03 7.90 0.01 591 466402 2 09:30:06 7.90 0.00 371 292885 3 09:30:08 7.91 0.01 96 75837 buy 4 09:30:12 7.92 0.01 43 34021 buy 5 09:30:15 7.90-0.02 1 790 sell 6 09:30:187.90 0.00 61 48143 7 09:30:21 7.91 0.01 15 11860 8 09:30:24 7.91 0.00 5 3955 9 09:30:27 7.90-0.01 10 7900 offer"""
Copy the code
Return values include time, price transaction price, change price change, volume transaction amount, Amount transaction amount (yuan), type Transaction type [buy, sell, neutral].
Convert the row index of the pen data to a time series, as shown below:
df_tick.index = pd.to_datetime(df_tick.time)
df_tick.drop(axis=1, columns='time', inplace=True)
print(df_tick.head(10))
""Price change volume amount type time 2019-08-10 09:25:05 7.89 0.09 385 303765 2019-08-10 09:30:03 7.90 0.01 591 466402 buy 2019-08-10 09:30:06 7.90 0.00 371 292885 sell 2019-08-10 09:30:08 7.91 0.01 96 75837 buy 2019-08-10 09:30:12 7.92 0.01 43 34021 buy 2019-08-10 09:30:15 7.90-0.02 1 790 sell 2019-08-10 09:30:18 7.90 0.00 61 48143 buy 2019-08-10 09:30:21 2019-08-10 09:30:27 7.90-0.01 10 7900 0.00 5 3955 2019-08-10 09:30:27 7.90-0.01 10 7900 0.00"""
Copy the code
Pandas provides the resample() method for resampling the time series. The obtained TICK data is combined into one-minute data. As follows:
print(df_tick.price.resample('1min', closed='left', label='left').ohlc().head(20))
"""The open high low, the close time 2019-08-10 09:25:00 7.89 7.89 7.89 7.89 2019-08-10 09:26:00 NaN NaN NaN NaN in the 2019-08-10 s 09:27:00 NaN NaN NaN NaN 2019-08-10 09:28:00 NaN NaN NaN NaN 2019-08-10 09:29:00 NaN NaN NaN NaN 2019-08-10 09:30:00 7.90 7.92 7.90 7.92 7.91 7.92 7.90 7.91 09:31:00 2019-08-10 2019-08-10 09:32:00 7.90 7.91 7.89 7.90 2019-08-10 09:33:00 7.89 7.91 7.89 7.90 7.91 7.92 7.90 7.92 09:34:00 2019-08-10 2019-08-10 09:35:00 7.92 7.94 7.91 7.93 2019-08-10 09:36:00 7.93 7.93 7.92 7.92 7.93 7.93 7.91 7.91 09:37:00 2019-08-10 2019-08-10 09:38:00 7.90 7.91 7.90 7.90 2019-08-10 09:39:00 7.90 7.90 7.89 7.90 7.89 7.90 7.89 7.90 09:40:00 2019-08-10 2019-08-10 09:41:00 7.90 7.90 7.89 7.89 2019-08-10 09:42:00 7.89 7.90 7.88 7.89 7.89 7.89 7.88 7.89 09:43:00 2019-08-10 2019-08-10 09:44:00 7.88 7.89 7.86 7.86"""
Copy the code
Process NaN data for non-trading periods and delete rows with NaN columns as follows:
df_min_ohlc = df_min_ohlc.dropna(axis=0,how='all') Delete this line only if all values are missing
print(df_min_ohlc.head())
"""The open high low, the close time 2019-08-10 09:25:00 7.89 7.89 7.89 7.89 2019-08-10 09:30:00 7.90 7.92 7.90 7.92 2019-08-10 09:31:00 7.91 7.92 7.90 7.91 7.90 7.91 7.89 7.90 09:32:00 2019-08-10 2019-08-10 09:33:00 7.89 7.91 7.89 7.90"""
Copy the code
Next, we can call the code of the booklet “Stock Data Visualization: Custom Matplotlib Version Stock Ticker Interface” to draw the K-chart, and draw the k-chart of the 1min and 15min levels respectively, as shown below:
The get_SINA_dd () interface is used to retrieve large trades. The default value is greater than or equal to 400 trades. The vol parameter can also be used to return transaction data of specific volume, as shown in the following figure:
# Obtain the data of large trades, the default value is greater than or equal to 400 lots, data from Sina Finance.
data=ts.get_sina_dd('600797',date = '2019-08-08')
print(data.head(10))
"""Code name time price volume preprice type 0 600797 更 新 15:00:00 8.01 253542 8.01 更 新 15:56:188.01 45700 8.01 sell 2 600797 Zhejiang university net new 14:54:39 8.01 116400 8.01 buy 3 600797 Zhejiang university net new 14:18:18 8.00 50000 8.00 buy 4 600797 Zhejiang university net new 13:35:57 8.02 53100 8.01 sell 5 600797 Zhejiang University net new 13:33:57 8.03 42200 8.03 buy 6 600797 Zhejiang university net new 13:25:18 8.01 64100 8.01 buy 7 600797 Zhejiang university net new 13:25:15 8.01 41800 8.01 buy 8 600797 Zhejiang University net new 13:22:57 8.04 135500 8.03 buy 9 600797 Zhejiang University net new 13:22:00 8.01 44600 8.01 buy"""
Copy the code
The get_toDAY_ticks () interface gets the breakdown data that has been generated for the current trading day, but is used during the transaction.
Get_realtime_quotes () gets real-time pen data. It can get current stock quotes and trades in realtime. One scenario is to write a Python timed program that calls this interface (it can be executed once every two or three seconds, basically the same performance as the quotes software). Then through the DataFrame matrix calculation to achieve trade monitoring, real-time monitoring of trading volume and price changes.
Get_today_all () retrieves the current trading data of all stocks at once (the previous trading day if it is a holiday, the speed of the result depends on the network speed). As follows:
data=ts.get_today_all()
print(data)
""" [Getting data:]############################################################ code name ... Mktcap NMC 0 688388 mkTCAP NMC 0 688388 1.470680e+06 + 3.365835e+05 1 + 688333 +... 7.000000 1.571451 e+05 e+05 2 688188 BaiChu electronic... 2.425100e+06 + 5.555751e+05 + 3 688122 western superconducting... 2.370954e+06 + 1.133204e +05 + + 5.598821e+05 + 5 688088... 2.791250e+06 2.612927e+05 6 688066 space grand picture... + 1.011336e+06 + 2.293842e+05 + 7 + 688033 2.216313e+06 2.145303e+05 8 688029 nanmicro medical... 1.628215 3.735600 e+05 e+06... . . . . . 3743 600234 *ST Landscape... 1.447488 1.447488 e+05 e+05 3744 600228 ST chang nine... 1.607191e+05 + 3745 600217 + $+05 + 1.607191e+05 + 3745 7.179371 6.818769 e+05 e+05 3746 600209 ST lawton... 1.290693 1.290693 e+05 e+05 3747 600193 ST chong hing... 1.552611e+05 1.552611e+05 3748 600186 *ST 1.741720 1.741720 e+05 e+05 3749 600179 on ST... + 1.836408E +05 + 1.737990 +05 + 3750 600163 3.568091e+05 3.568091e+05 3751 600149 ST square exhibition [rows x + 1] "1.927411e+ 1"""The returned value includes code code, name name, ChangePercent increase or decrease, trade current price, open open price, high highest price, LOW lowest price, settlement closing price yesterday, volume turnover, turnoverratio, amount transaction Amount, PER P/E ratio, PB p/B ratio, MKTCAP total market value, NMC circulating market value.Copy the code
The complete code can be obtained by joining the pamphlet exchange group. You are welcome to subscribe for more quantitative trading content