Welcome to subscribe to the Python Data Analysis Practical: Building a Quantitative Trading System for Stocks booklet. After you finish this booklet, be sure to use your knowledge to help us analyze stocks!

preface

Believe everybody heard quantitative trade this thing!

Quantitative trading is an emerging systematic financial investment method. It integrates the knowledge of multiple disciplines, uses advanced mathematical models to replace human subjective thinking to formulate trading strategies, and uses the powerful computing power of computers to backtest the profit and loss “probability” of trading strategies from huge historical data such as stocks, bonds and futures. Helps investors make accurate decisions by managing the “probability” of profit and loss.

So, what is the suitable way for ordinary shareholders to open quantitative trading?

This article will use a ground gas data management scenario to share with you, how ordinary shareholders to use quantitative trading!

As we know that data is the source of quantitative trading, how to efficiently manage a large amount of data is a very critical link in quantitative trading analysis. A database is the best solution.

Once we get the ticked data through the API, we can store it in the database, so the next time we call it, we only need to get it from the local database.

Data table partition and stock pool

You need to consider how to divide the tables and how to set up the corresponding stock pools before you store them in the database.

Of course, according to the trader’s different operating style can have different division method. For example, all the a-share market indexes and listed stocks are grouped into A table; Can also be familiar with their own several plate stocks into a table……

Next, we will divide the data table by plate, divide the cement and semiconductor plate into the same table, and then introduce how to update the stock market data to the database in practical application.

We use the Tushare Pro version of the stock_Basic () interface to get the basic information data of all the listed stocks, including the stock symbol, name, listing date, delisting date, sector, etc.

Use the Pandas condition expression to select “cement” and “semiconductor” stocks from all listed stocks, such as:

Df_basic [(df_basic["industry"] == u")]Copy the code

The selected results are shown in the figure below:

Convert the stock name and stock code ts_code as key-value pairs to dictionary format data, and then use dump to store the Python stock pool as a JSON file. Such as:

with open("stock_pool.json", "w", encoding='utf-8') as f:
    json.dump(stock_index, f, ensure_ascii=False, indent=4)
Copy the code

The content of the created JSON file is as follows:

Create a database to update data

At present, the popular databases are Oracle, MySQL, MongoDB, Redis, SQLite…… The choice of database is usually based on performance, data integrity, and application requirements. Each database has its own characteristics and the most suitable applications.

A lightweight relational database SQLite is recommended here. We are only used for local data management, without multi-user access, data capacity is less than 2TB, without massive data processing, the key is easy to transplant, simple to use, rapid processing. SQLite is recommended for local data management.

Of course, if you are good at MySQL, MongoDB, or have more advanced gameplay, you can choose MySQL, MongoDB, such a database.

The first time the database is created, it is necessary to complete the historical trend of individual stocks, and then it is updated every trading day.

Use open() to open the JSON file, and load() to convert the JSON encoded strings in the file to Python data types. For example:

with open("stock_pool.json", 'r') as load_f:
    stock_index = json.load(load_f)
Copy the code

Then you can use the for loop to iterate to obtain the stock data corresponding to the stock pool, or you can speed up the acquisition of stock data by multitasking.

For example, if you create the data table for the first time, for example 000401, we specify the start time and end time of the Daily () interface of the Tushare Pro version as ‘20190101’ and ‘20200101’ respectively, and the data content is as follows:

If you want to update the data for the next trading day, just change the start time.

Create database code:

conn = sqlite3.connect('stock-boards.db')
Copy the code

Update data to a database table by calling the dataframe.to_sql () interface directly:

df_stock.to_sql('cement_semicon', conn, index=False, if_exists='append')
Copy the code

To read the entire table, call pd.read_sql_query:

df_table = pd.read_sql_query("select * from 'cement_semicon';" , conn)Copy the code

For example, we have access to all the stock market data of cement plate into the database table, the data format read out is as follows:

With the database, we can quickly query and statistics some data.

For example, query ‘20190125’ for stocks that rose more than 2% on the trading day:

df_target = pd.read_sql_query("select * from 'cement_semicon' where pct_chg > 2 and trade_date == '20191225'", conn)
Copy the code

According to statistics, only 2 stocks meet the requirements, as shown in the figure below:

Matters needing attention

In the real world of quantitative trading, the accuracy of the data is very important, that is, the stock quotes stored in the database must be accurate enough.

How do you make sure the data is accurate?

One suggestion is to take two data sources and verify each other. If you use the free API interface, you can use the two interfaces to obtain data, save the data in the local CSV file, and then import the market data in the file respectively for comparison. When there is a deviation, it is necessary to distinguish manually, and then transfer the data to the database after passing the test.

For higher quality data, you can use charging access to data, such as financial data terminals provided by Wind, Flush and other companies, JQData data service provided by Jukuan team, and financial data download service provided by forecaster website.

Of course, you can also use the crawler method to obtain from financial websites, such as Oriental Fortune:

After studying the book, if you want to upgrade your study again, you can add the knowledge planet “Play with quantitative trading stocks” (click to see the catalog).

conclusion

Through this simple and practical stock quantitative scenario, I hope to give the majority of friends for quantitative trading have an intuitive feeling.

Then, we should upgrade their own way to fry, the stock before their own that set of methods, abstract into a strategic model, with quantitative methods to the whole market back test evaluation, and then let the program to help us monitor the trend of the market.

This is the quantitative trade that ordinary shareholder place suits opens way!

— — — — — — — — — — — — — — — — — — — — — — — —If you want to have a more comprehensive and systematic introduction to the knowledge points involved from 0-1 way, here I recommend my book to youQuant Trading in Python Stocks from Beginning to Practice! Tmall, JINGdong, dangdang fully open for sale!

At the same time, you are welcome to follow my wechat official account [Yuanxiao Master Takes you to quantitative Trading in Python] to learn more about quantitative trading in Python