Analysis of Stock Market Cycles With Fbprophet Package in Python PDF
Analysis of Stock Market Cycles With Fbprophet Package in Python PDF
S tock market cycles are the long-term price patterns of stock markets
and are often associated with general business cycles. They are key to
technical analysis where the approach to investing is based on cycles or
repeating price patterns. If we have better understanding toward the
cycles of stock market, we can always buy with relative low-price and
sell at relative high price in each cycle, and we’ll always have positive
return. What a wonderful world! Of course, there is no superior
strategy in stock market which can make money forever, but
fbprophet package in Python or R can help us look deeper into the
hidden cycles in stock market. In this analysis, we can take a look at
how fbprophet can assist us to make an investment decision, and all
codes are available from here.
. . .
Introduction to fbprophet
F bprophet is an open source released by Facebook in order to provide
some useful guidance for producing forecast at scale. By default, it
would divide a time series into trend and seasonality, which might
contain yearly, weekly and daily. However, analysts can define their
own seasonality. To get better understanding about the package, the
document from Prophet is really helpful.
One of the feature for package is its simplicity and flexibility. Since
cycles in stock market we want to figure out are not limited to yearly,
weekly or daily, we should define our own cycles and find out which
can fit the data better. Besides, we should not use weekly seasonality
since there is no trading on weekend. We can also define our
‘self_define_cycle’ by add_seasonality function. All the settings can
be done with only two lines of code.
https://towardsdatascience.com/analysis-of-stock-market-cycles-with-fbprophet-package-in-python-7c36db32ecd0 1/10
01/05/2019 Analysis of Stock Market Cycles with fbprophet package in Python
1 m = Prophet(weekly_seasonality=False,yearly_seasonality=Fals
2 m.add_seasonality('self_define_cycle',period=8,fourier_order
https://towardsdatascience.com/analysis-of-stock-market-cycles-with-fbprophet-package-in-python-7c36db32ecd0 2/10
01/05/2019 Analysis of Stock Market Cycles with fbprophet package in Python
• print_ind: whether to print the projected return per cycle and out-
sample mean squared error or not (optional, default False)
1 def cycle_analysis(data,split_date,cycle,mode='additive',fo
2 training = data[:split_date].iloc[:-1,]
3 testing = data[split_date:]
4 predict_period = len(pd.date_range(split_date,max(data.
5 df = training.reset_index()
6 df.columns = ['ds','y']
7 m = Prophet(weekly_seasonality=False,yearly_seasonality
8 m.add_seasonality('self_define_cycle',period=cycle,four
9 m.fit(df)
10 future = m.make_future_dataframe(periods=predict_period
11 forecast = m.predict(future)
12 if forecast_plot:
13 m.plot(forecast)
14 plt.plot(testing.index,testing.values,'.',color='#f
15 plt.xlabel('Date',fontsize=12,fontweight='bold',col
16 plt.ylabel('Price',fontsize=12,fontweight='bold',co
17 plt.show()
18 ret = max(forecast.self_define_cycle)-min(forecast.self
19 model_tb = forecast['yhat']
In the Figure 2 and Figure 3, we applied the function with two different
lengths of cycle, 30 and 300 respectively, on Costco stock price and
took 2018/4/1 as the split date for training and testing. As we can see,
if we pick a length that is too short (e.g 30 days,) the return within one
cycle is small and we need to make transaction frequently(Figure 2;) in
contrast, if we pick a length which is too long (e.g 300 days,) it would
distort our prediction (Figure 3.)
https://towardsdatascience.com/analysis-of-stock-market-cycles-with-fbprophet-package-in-python-7c36db32ecd0 3/10
01/05/2019 Analysis of Stock Market Cycles with fbprophet package in Python
https://towardsdatascience.com/analysis-of-stock-market-cycles-with-fbprophet-package-in-python-7c36db32ecd0 4/10
01/05/2019 Analysis of Stock Market Cycles with fbprophet package in Python
Figure 4: Projected Return and Out-Sample Mean Squared Error for di erent length of cycle
To further illustrate the investment strategy, we can see the buying and
selling dates between 2015/10/1 and 2018/10/1. The Return_Dates
function could return all buy and sell dates as output, with input:
https://towardsdatascience.com/analysis-of-stock-market-cycles-with-fbprophet-package-in-python-7c36db32ecd0 5/10
01/05/2019 Analysis of Stock Market Cycles with fbprophet package in Python
During 2015/10/1 and 2018/10/1, we would buy and sell Costco four
times. In summary, we would spend $604.56 on buying and get
$744.78 in return when we sold them on those specific dates. For a
simplified return rate (without considering reinvestment, time value
and so on,) it is 23.2% for 3 years. Probably not very attractive, but at
least it is positive return.
https://towardsdatascience.com/analysis-of-stock-market-cycles-with-fbprophet-package-in-python-7c36db32ecd0 6/10
01/05/2019 Analysis of Stock Market Cycles with fbprophet package in Python
https://towardsdatascience.com/analysis-of-stock-market-cycles-with-fbprophet-package-in-python-7c36db32ecd0 7/10
01/05/2019 Analysis of Stock Market Cycles with fbprophet package in Python
For Microsoft and Nike we cannot find any cycle matched to our
requirement of more than $10 return per cycle. For Costco, Apple and
Home Depot, we can find a cycle around 250 days and make a good
prediction and decent return.
https://towardsdatascience.com/analysis-of-stock-market-cycles-with-fbprophet-package-in-python-7c36db32ecd0 8/10
01/05/2019 Analysis of Stock Market Cycles with fbprophet package in Python
. . .
Summary
With the help of Python and fbprophet package, we can have better
understanding toward the stock market. Taking Costco as an example,
we can find a cycle with 252 days so that it can generate enough return
and have a good fit of data. According to the cycle we found, we can
have about 23% return for 3 years. Perhaps this investment strategy
cannot satisfy what you want, but you can always set your own
approaches based on your knowledge and experience. Powerful
fbprophet package can make your analysis on stock market much
deeper and easier.
https://towardsdatascience.com/analysis-of-stock-market-cycles-with-fbprophet-package-in-python-7c36db32ecd0 9/10
01/05/2019 Analysis of Stock Market Cycles with fbprophet package in Python
https://towardsdatascience.com/analysis-of-stock-market-cycles-with-fbprophet-package-in-python-7c36db32ecd0 10/10