未加星标

Analysis of Stock Market Cycles with fbprophet package in Python

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏
Analysis of Stock Market Cycles with fbprophet package inpython IMPORTANT: THIS IS NOT INVESTMENT ADVICE.

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.

Stock Market Cycles Analysis ― taking Costco as anexample

We can use close prices for Costco from 2015/10/1 to 2018/10/1 as an example to have better understanding about what we are doing. With pandas_datareader we can access stock price easily. The documentation is here . In the Figure 1 we can see there is a strong trend of growing price from 2015. However, there are still a lot of up-and-down, or cycles, during the journey, and these are what we want to make money.


Analysis of Stock Market Cycles with fbprophet package in Python
Figure 1: Costco stock price from 2015/10/01 to 2018/10/01

For prediction models, one way to evaluate them is out-sample mean squared error. We can use 2015/10/1 to 2018/3/31 for training and keep the last 6 months for testing and calculating out-sample mean squared error. Within each cycle, we can optimized our return by buying at the lowest price and selling at the highest. To make the process easier, we use self-define function cycle_analysis. The output is a list containing projected return per cycle and out-sample mean squared error. Inputs for the function require:

data: Pandas dataframe with time index split_date: the date to split training and testing data cycle: periods (days) per cycle mode: additive or multiplicative for seasonality (optional, default additive) forecast_plot: whether to print the forecast plot or not (optional, default False) print_ind: whether to print the projected return per cycle and out-sample mean squared error or not (optional, default False)

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.)


Analysis of Stock Market Cycles with fbprophet package in Python
Figure 2: Prediction with cycle equal to 30days
Analysis of Stock Market Cycles with fbprophet package in Python
Figure 3: Prediction with cycle equal to 300days

We can apply a loop over our cycle_analysis function to calculate projected return and out-sample mean squared error for different length of cycle, and we displayed the outcome in Figure 4. As we can see, the longer the length, both projected return per cycle and out-sample mean square error would increase. In consideration of the cost of transaction, the projected return within per cycle should be greater than $10. Under this constrain, we can choose the cycle that leads to minimum out-sample mean squared error, and it is 252 days. The projected return per cycle is $17.12 and out-sample mean squared error is 15.936. Both are pretty good!


Analysis of Stock Market Cycles with fbprophet package in Python
Figure 4: Projected Return and Out-Sample Mean Squared Error for different length ofcycle
Analysis of Stock Market Cycles with fbprophet package in Python
Figure 5: Prediction with cycle equal to 252days

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:

forecast: fbprophet forecast object stock_data: Pandas dataframe with time index cycle: length of cycle cycle_name: name for the cycle column in forecast object time_name: name for the time column in forecast object

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.


Analysis of Stock Market Cycles with fbprophet package in Python
Applying to morestocks

D efinitely, this method could be applied to as many stocks as possible. We listed average buy price, average sell price, length of cycle, out-sample mean squared error, number of buying, number of selling and projected return within each cycle for Costco, Apple, Microsoft, Home Depot and Nike.

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 go

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

tags: cycle,return,out,Figure
分页:12
转载请注明
本文标题:Analysis of Stock Market Cycles with fbprophet package in Python
本站链接:https://www.codesec.net/view/604922.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(13)