Running in the

Recently the stock market rose, many people are busy running into the market. As a conservative “investor”, the main investment index fund: CSI 300. In this wave of market, just 2 months, there are also 13% book gains. Although I know that index class is suitable for long-term holding, but also curious about whether this point is high. To solve this problem. We’re going to run a set of algorithmic models today, see if we can find anything.

The value of temporal forecasting

Prediction of temporal problems is very common in life. For example: online game forecast, consumption forecast, O2O store forecast, traffic flow forecast, accurate prediction of these scenarios, for the allocation of resources play a significant reference role. From the individual point of view, the service and experience are also greatly improved.

To that end, Facebook has open-source a tool called Prophet, which is dedicated to temporal prediction. Here, we’re going to use it to take a look at the stock market.

The principle of time series prediction

For time series problems, the common technique is the decomposition of time series: there is something similar to the Fourier transform. Decompose a function into the sum product of regular functions. Common components of time series include seasonal term, trend term and noise. In Prophet, they added the holiday project to the actual situation. In a previous Kaggle contest, we also found that holiday data fluctuations are similar to the weekend effect. That is, the data before and after holidays, similar to the data before and after Saturday. After the data are revised, the indicators are much better.


Without further ado, let’s get to work.

Prophet in Shanghai and Shenzhen 300

Tool Package Installation

pip install fbprophet

Data preparation and cleaning

import pandas as pd
import numpy as np
from fbprophet import Prophet
Copy the code

Data preparation

  • Data source is netease Finance, csi 300 index.
data = pd.read_csv('.. /data/000300.csv',encoding='GB2312') 
data.head()
Copy the code

Data cleaning

  • Select the required data and perform log/Box-Cox transformation on the data to make the data more consistent with linear and normal distribution and reduce variance differences. Economic systems, like ecosystems, have exponential growth and saturation. We’re going to use the log transformation here.
df = data[[U 'date'.U 'closing price']]
df.columns = ['ds'.'y']
df['y'] = df['y'].apply(lambda x: np.log(int(x)))
Copy the code

Model fitting and prediction

Define it briefly and then fit it.

m = Prophet()
m.fit(df)
Copy the code

Forecast the market in the coming year

future = m.make_future_dataframe(periods=365)
forecast = m.predict(future)
Copy the code

Look at the last 15 days in detail

forecast[['ds'.'yhat'.'yhat_lower'.'yhat_upper']].tail(15)
future_predict['yhat'] = future_predict['yhat'].apply(lambda x:np.exp(x))
future_predict['yhat_lower'] = future_predict['yhat_lower'].apply(lambda x:np.exp(x))
future_predict['yhat_upper'] = future_predict['yhat_upper'].apply(lambda x:np.exp(x))
Copy the code

Yhat’s forecast is conservative, with 2020,307 up 100 points from the current 2019,0308. Expected maximum profit :(6536-3658)/3658≈ 78%, expected maximum loss :(2158-3658)/3658≈-41%

What does the model see

Prophet has a function of component splitting. Let’s take a look at the trend of the domestic stock market and what the seasonal factors are.

fig1 = m.plot(forecast)
Copy the code

Model fitting

Trend term

conclusion

The model data show that the Shanghai and Shenzhen stock markets may fluctuate in the short term, but the possibility of rising in the long term is greater. In the short term, there is little reference significance for our investment method. In the long term, it is stable and good. I will continue to invest in Shanghai and Shenzhen 300. Of course, investing is risky and decisions need to be made carefully. Do not watch the Wolf eat meat, but do not see him beaten. At my worst point last year, I lost 15%…

Again, models are not everything. There’s a single piece of information, but in the real world, there’s multiple pieces of information working together in the marketplace. The model has not been fully adjusted, and there is no partition data for verification, so the actual use value is limited.

What do you think about that? Please leave your thoughts in the comments section.

Kuhungio.me /2019/prophe…