量化分析预测股市?试试这个 Python 库
!pip install quandl
!pip install fbprophet
!pip install plotly
from stocker importStocker
现在在 Python 代码中有 Stocker 类,我们可以使用它来创建该类的实例。在 Python 中,类的实例称为对象,创建对象的行为有时称为实例化或构造。为了创建 Stocker 对象,我们需要传入有效股票代码的名称。
# MSFT Stocker Initialized. Data covers 1986-03-13 to 2018-01-16.
microsoft = Stocker('MSFT')
MSFT StockerInitialized. Data covers 1986-03-1300:00:00 to 2018-03-2700:00:00.
microsoft
对象。Stocker 建立在 quandl WIKI
数据库上,该数据库使我们可以访问 3000 多只美国股票以及多年的每日价格数据(完整列表)。对于此示例,我们将坚持使用 Microsoft 数据。因为微软正在拥抱开源社区(包括 Python)。# Stock is an attribute of the microsoft object
stock_history = microsoft.stock
stock_history.head()
# A method (function) requires parentheses
microsoft.plot_stock()
MaximumAdj. Close= 96.77 on 2018-03-1200:00:00.
MinimumAdj. Close= 0.06 on 1986-03-2400:00:00.
CurrentAdj. Close= 89.47 on 2018-03-2700:00:00.
plot_stock
函数有许多可选参数。默认情况下,此方法绘制整个日期范围的调整后收盘价,但我们可以选择范围、要绘制的统计数据以及绘图类型。例如,如果我们想将价格的每日变化与调整后的交易量(股票数量)进行比较,我们可以在函数调用中指定这些。microsoft.plot_stock(start_date = '2000-01-03', end_date = '2018-01-16', stats = ['Daily Change', 'Adj. Volume'], plot_type='pct')
MaximumDailyChange= 2.08 on 2008-10-1300:00:00.
MinimumDailyChange= -3.34 on 2017-12-0400:00:00.
CurrentDailyChange= -5.47 on 2018-03-2700:00:00.
MaximumAdj. Volume= 591052200.00 on 2006-04-2800:00:00.
MinimumAdj. Volume= 7425503.00 on 2017-11-2400:00:00.
CurrentAdj. Volume= 53704562.00 on 2018-03-2700:00:00.
plot_stock
,我们可以调查任何日期范围内数据中的任何数量,并寻找与现实世界事件的相关性。现在,我们将继续讨论 Stocker 中更有趣的部分之一:赚假钱!microsoft.buy_and_hold(start_date='1986-03-13', end_date='2018-01-16', nshares=100)
MSFT Total buy and hold profit from1986-03-13 to 2018-01-16for100 shares = $8829.11
model, model_data = microsoft.create_prophet_model()
model.plot_components(model_data)
plt.show()
weekly_seasonality
属性将其添加到先知模型中:print(microsoft.weekly_seasonality)
microsoft.weekly_seasonality = True
print(microsoft.weekly_seasonality)
False
True
weekly_seasonality
的默认值为 False
,但我们更改了该值以在我们的模型中包含每周模式。然后我们再次调用 create_prophet_model
并绘制结果组件。microsoft.changepoint_date_analysis()
Changepoints sorted by slope rate of change (2nd derivative):
DateAdj. Close delta
4102016-09-0855.811396-1.378093
3382016-05-2650.1134531.116720
2172015-12-0252.572008-0.882359
4582016-11-1557.5898190.603127
482015-04-0237.6125900.442776
microsoft.changepoint_date_analysis(search = 'Microsoft profit')
TopRelatedQueries:
query value
0 microsoft non profit 100
1 microsoft office 60
2 apple profit 40
3 microsoft 36540
4 apple 35
RisingRelatedQueries:
query value
0 apple stock 170
1 microsoft 365130
2 apple profit 50
“Microsoft profit”
与微软股价之间似乎没有相关性。microsoft.changepoint_date_analysis(search = 'Microsoft Office')
TopRelatedQueries:
query value
0 microsoft office download 100
1 microsoft office 201090
2 office 201085
3 microsoft office 201375
4 office 201370
RisingRelatedQueries:
query value
0 microsoft office 2016 key 80300
1 office 201673200
2 download microsoft office 201672150
3 microsoft office 2016 mac 69350
4 microsoft office 201667650
Microsoft Office
的搜索量下降会导致股价上涨。也许有人应该让微软知道。model, future = microsoft.create_prophet_model(days=180)
PredictedPrice on 2018-07-21= $102.40
E N D
评论