The original link
My GitHub blog address
In my last article, I used 4000 words to introduce some basic knowledge related to finance and quantitative trading, as well as the advantages and disadvantages of artificial intelligence in finance. In this article we will use a specific code to carry out a wave of stock price forecasting.
As mentioned before, quantitative trading itself is just a trading mode. As long as the strategy is entered into the quantitative trading software, the computer will conduct automatic trading according to the strategy. The machine learning generated by us is only the strategy stage in the quantitative trading step. For the moment, we don’t need to consider the access of trading, which involves a lot of knowledge related to trading API. Trading platform development framework, I introduce a Github based on Python open source framework, you can understand: vnpy.
OK, let me just focus on price forecasting. We all know that whatever we do with machine learning, we first need some data source, and we need some way to test back. There are many ways to obtain data such as stocks or other financial positions. Today, I would like to introduce a platform I often use: RiceQuant Quantitative trading platform (non-advertising).
The platform has its own Set of Python apis and compiler iPython NoteBook for developing quantitative trading strategies. The code just needs to implement the logic in a specific method to generate test results with one click, and the results will automatically parse the back test revenue curve and some metrics for your reference. Like this
I will not introduce the specific API, but if you are interested, you can go to the official website to check the API documentation, which is very detailed. www.ricequant.com/api/python/…
Here, I’m going to do some simple machine learning with the data, without using apis that can be backtested. The get_price function is used to retrieve the historical data for the CSI300 index for the past year. It returns the familiar DataFrame in pandas.
df = get_price('CSI300.INDX'.'2005-01-05'.'2015-01-01').reset_index()[['open'.'close']]
Copy the code
This function can only be used in the Basket line compiler. Once we have this data, we can perform fancy operations using Python data analysis of the three musketeers. We use a few simple features here to make our predictions. That is the rise and fall of the previous two days, take the rise and fall of the previous two days as the characteristic input, and then take the actual rise and fall of that day as the output, such a set of data as a training sample.
Of course, the selection of such features is very simple, and the prediction result is certainly not ideal. In practical engineering, the qualitative and quantitative characteristics are the most important and complex. For example, there are many indicators in the market, such as MACD and VR, which are very important indicators.
Now that we’re using the index as a feature, we need to get the index out first. We just got the opening and closing prices of each day from get_price, so it’s easy to calculate the index
>>> up_and_down=df['close']-df['open'] > 0 # get the ups and downs
>>> rate_of_return = (df['close']-df['open'])/df['open'] # get up or down
>>> up_and_down_statistic = up_and_down.value_counts() # get up and down days
Copy the code
We then use matplotlib.pyplot to plot the following bar chart of up and down days
Oddly enough, when using Pyplot in ipython Notebook, it sometimes draws curved lines
From the chart, we can clearly see that in the past 10 years, the final closing price was a little higher. We can also look at some other data, so for example, let’s just print out the daily ups and downs.
rate_of_return.plot(kind='line', style='k--', figsize=(15.10), title='Daily Yield Changes Over Time Series')
Copy the code
From this chart, we can clearly see some market conditions in the more than 2000 trading days. For example, there is a wide range of volatility in the middle, which indicates that this will be a bull market, corresponding to the bull market in 2007. I still remember when I was just in middle school, every day at noon home, my father would tell me how much money he made in the stock market today, can be happy.
Of course, you can do a lot more data analysis with graphics, so we’re going to write a simple machine learning code to see how much of the price prediction we can get right using this simple feature.
What we are predicting here is the situation of closing up and down, which is a classification problem, so we choose SVM for classification here. Each new data is fit fit again to improve the accuracy of the next prediction. The code is as follows:
import pandas as pd
import numpy as np
from __future__ import division
from sklearn import svm
from collections import deque
import matplotlib.pyplot as plt
window = 2
DataFrame to retrieve historical data for a variety from 2005-01-04
df = get_price('CSI300.INDX'.'2005-01-05'.'2015-07-25').reset_index()[['open'.'close']]
up_and_down = df['close'] - df['open'] > 0
print(len(up_and_down))
X = deque()
y = deque()
clf = svm.LinearSVC() # SVM
prediction = 0
test_num = 0 Total number of tests
win_num = 0 # Correct prediction number
current_index = 400 # start position
for current_index in range(current_index, len(up_and_down)- 1.1):
fact = up_and_down[current_index+1]
X.append(list(up_and_down[(current_index-window): current_index]))
y.append(up_and_down[current_index])
if len(y) > 100 and len(y) % 50= =0:
test_num += 1
clf.fit(X, y)
prediction = clf.predict(list(up_and_down[(current_index-window+1): current_index+1]))
if prediction[0] == fact:
win_num += 1
print(win_num)
print("Prediction accuracy is",win_num/test_num)
Copy the code
The results of the final output of 58.53%, if a price forecasting software through the historical data alone can reach sixty percent accuracy, this is actually a very good result, but we are here not convincing enough, the result of the characteristic of the first of all, we too simple, then there is in order to save time in the training, I actually shortened the sample size so much that I only predicted at multiples of 50, so I ended up with about 40 samples.
Part of the code above refers to a student named Lu Dongxu in the Rice basket community. The community is still quite active, and the posts I read are from three years ago. It can be said that there was such a community of quantitative trading and AI in China a few years ago. It can be seen that people were already on the road, and by the time we all think we need to contact these things, they have already gone further.
But it doesn’t matter, the latecomer is also a very interesting thing.
The above code is actually the most crude and primitive code, and it would be too much trouble to write your own loop, so the platform has since updated the API, which, as I said at the beginning of this article, allows for quick backtesting by simply filling in the logic in the provided empty methods. For example, here is the official API documentation.
Write any initialization logic in this method. The context object will pass between any method of your algorithmic strategy.
def init(context):
context.s1 = "000001.XSHE"
Has the order been sent
context.fired = False
# This logic will be triggered by data updates for the securities you select, such as daily or minute historical data slices or real-time data slices
def handle_bar(context, bar_dict):
Start writing your main algorithmic logic
# bar_dict[order_book_id] retrieves bar information for a security
# context.portfolio gets the current portfolio status information
# Order_shares (ID_or_ins, amount
# TODO:Start writing your algorithm!
if not context.fired:
# order_percent and pass in 1 to buy the stock and make it 100% of the portfolio
order_percent(context.s1, 1)
context.fired = True
Copy the code
Well, today is a quick look at how to do some interesting things with Python’s data analysis library using some third-party data retrieval apis. See you in the next article!
Recommended reading
Machine learning stock Price Prediction from crawler to prediction (Data crawling part)
Share some tips for learning AI
What’s up with quantitative trading and artificial intelligence
Pay attention to the public number to get more articles -AI geek training station