#实战准备
#股票市场分析实战--数据获取
#https://finance.yahoo.com
"""
import pandas_datareader as pdr
alibaba = pdr.get_data_yahoo("BABA")
#print(alibaba.head())
# High Low Open Close Volume Adj Close
# Date
# 2014-09-19 99.699997 89.949997 92.699997 93.889999 271879400 93.889999
# 2014-09-22 92.949997 89.500000 92.699997 89.889999 66657800 89.889999
# 2014-09-23 90.480003 86.620003 88.940002 87.169998 39009800 87.169998
# 2014-09-24 90.570000 87.220001 88.470001 90.570000 32088000 90.570000
# 2014-09-25 91.500000 88.500000 91.089996 88.919998 28598000 88.919998
print(alibaba.shape)#(1163, 6)
print(alibaba.tail())
# High Low ... Volume Adj Close
# Date ...
# 2019-04-29 188.000000 185.759995 ... 8660600 186.940002
# 2019-04-30 188.250000 183.820007 ... 15076500 185.570007
# 2019-05-01 193.195007 185.880005 ... 17397500 189.309998
# 2019-05-02 192.699997 186.649994 ... 11468100 190.389999
# 2019-05-03 195.720001 191.880005 ... 14503200 195.210007
print(alibaba.describe())
# High Low ... Volume Adj Close
# count 1163.000000 1163.000000 ... 1.163000e+03 1163.000000
# mean 125.027905 121.957792 ... 1.742349e+07 123.509312
# std 44.776467 43.750452 ... 1.276305e+07 44.288017
# min 58.650002 57.200001 ... 3.775300e+06 57.389999
# 25% 84.992500 82.965000 ... 1.088550e+07 84.115002
# 50% 106.839996 104.179001 ... 1.471860e+07 105.519997
# 75% 173.044998 169.508499 ... 2.017315e+07 171.590004
# max 211.699997 207.509995 ... 2.718794e+08 210.860001
#
# [8 rows x 6 columns]
print(alibaba.info())
# <class 'pandas.core.frame.DataFrame'>
# DatetimeIndex: 1163 entries, 2014-09-19 to 2019-05-03
# Data columns (total 6 columns):
# High 1163 non-null float64
# Low 1163 non-null float64
# Open 1163 non-null float64
# Close 1163 non-null float64
# Volume 1163 non-null int64
# Adj Close 1163 non-null float64
# dtypes: float64(5), int64(1)
# memory usage: 63.6 KB
# None
"""
"""
#----------------------------------------------------
#股票市场分析实战--历史趋势分析
#基本信息
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
#股票数据的读取
import pandas_datareader as pdr
#可视化
import matplotlib.pyplot as plt
import seaborn as sns
#time
from datetime import datetime
# start = datetime(2015,9,20)
# alibaba = pdr.get_data_yahoo("BABA.csv")
# amazon = pdr.get_data_yahoo("AMZN.csv")
# alibaba.to_csv("BABA.csv")
# amazon.to_csv("AMZN.csv")
alibaba_df = pd.read_csv("BABA.csv")
amazon_df = pd.read_csv("AMZN.csv")
#print(alibaba_df.head())
# Date High Low Open Close Volume Adj Close
# 0 2015-09-21 66.400002 62.959999 65.379997 63.900002 22355100 63.900002
# 1 2015-09-22 63.270000 61.580002 62.939999 61.900002 14897900 61.900002
# 2 2015-09-23 62.299999 59.680000 61.959999 60.000000 22684600 60.000000
# 3 2015-09-24 60.340000 58.209999 59.419998 59.919998 20645700 59.919998
# 4 2015-09-25 60.840000 58.919998 60.630001 59.240002 17009100 59.240002
#收盘价走势图
# alibaba_df["Adj Close"].plot(legend=True)
# plt.show()
#交易量走势图
# alibaba_df["Volume"].plot(legend=True)
# plt.show()
#alibaba和amazon两个走势画在一起
# alibaba_df["Adj Close"].plot(legend=True)
# amazon_df["Adj Close"].plot(legend=True)
# plt.show()
alibaba_df["high-low"] = alibaba_df["High"] - alibaba_df["Low"]
#print(alibaba_df.head())
# Date High Low ... Volume Adj Close high-low
# 0 2015-09-21 66.400002 62.959999 ... 22355100 63.900002 3.440002
# 1 2015-09-22 63.270000 61.580002 ... 14897900 61.900002 1.689999
# 2 2015-09-23 62.299999 59.680000 ... 22684600 60.000000 2.619999
# 3 2015-09-24 60.340000 58.209999 ... 20645700 59.919998 2.130001
# 4 2015-09-25 60.840000 58.919998 ... 17009100 59.240002 1.920002
#
# [5 rows x 8 columns]
# alibaba_df.plot()
# plt.show()
#daily-return 每天的变化
# print(alibaba_df["Adj Close"].pct_change())
# 0 NaN
# 1 -0.031299
# 2 -0.030695
# 3 -0.001333
# 4 -0.011348
# 5 -0.031229
# 6 0.007493
# 7 0.019889
# 8 -0.001696
# 9 0.073552
# 10 0.011551
# 11 -0.000156
# 12 0.036921
# 13 0.021424
# 14 0.014919
# 15 0.023141
# 16 -0.008819
# 17 -0.016360
# 18 0.047272
# 19 0.002926
# 20 0.009168
# 21 -0.011838
# 22 -0.032177
# 23 0.021733
# 24 0.065221
# 25 0.009653
# 26 0.040472
# 27 0.036631
# 28 -0.001579
# 29 0.019582
# ...
# 881 -0.028871
# 882 0.014240
# 883 -0.003860
# 884 -0.005896
# 885 0.003954
# 886 0.026557
# 887 -0.008550
# 888 0.004699
# 889 -0.018818
# 890 0.015422
# 891 0.023637
# 892 0.006204
# 893 0.003700
# 894 -0.005342
# 895 -0.006499
# 896 0.021246
# 897 -0.030914
# 898 0.014803
# 899 0.009527
# 900 -0.003252
# 901 -0.008345
# 902 0.010303
# 903 -0.008650
# 904 0.011903
# 905 -0.004205
# 906 -0.000802
# 907 -0.007329
# 908 0.020154
# 909 0.005705
# 910 0.025316
# Name: Adj Close, Length: 911, dtype: float64
alibaba_df["daily-return"] = alibaba_df["Adj Close"].pct_change()
# alibaba_df["daily-return"].plot(figsize=(10,4),linestyle="--",marker="o")
#plt.show()
#直方图的图像:
# alibaba_df["daily-return"].plot(kind="hist")
# plt.show()
#sns展示图像:
sns.distplot(alibaba_df["daily-return"].dropna(),bins=100,color="purple")
plt.show()
"""
#------------------------------------------------------
#股票市场分析实战--风险分析
#基本信息
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
#股票数据的读取
import pandas_datareader as pdr
#可视化
import matplotlib.pyplot as plt
import seaborn as sns
#time
from datetime import datetime
start = datetime(2015,1,1)
company = ['AAPL','GOOG','MSFT','AMZN','FB']
top_tech_df = pdr.get_data_yahoo(company,start=start)["Adj Close"]
# top_tech_df.to_csv("top5.csv")
#print(top_tech_df.head())
# Date AAPL AMZN FB GOOG MSFT
# 0 2014-12-31 102.503265 310.350006 78.019997 523.521423 42.137520
# 1 2015-01-02 101.528191 308.519989 78.449997 521.937744 42.418739
# 2 2015-01-05 98.667984 302.190002 77.190002 511.057617 42.028660
# 3 2015-01-06 98.677261 295.290009 76.150002 499.212799 41.411785
# 4 2015-01-07 100.060936 298.420013 76.150002 498.357513 41.937946
top_tect_dr = top_tech_df.pct_change()
#print(top_tect_dr.head())
# top_tech_df.plot()
# plt.show()
# top_tech_df[["AAPL","FB","MSFT"]].plot()
# plt.show()
#对两只股票进行散点图的分析
# sns.jointplot("AMZN","GOOG",top_tect_dr,kind="scatter")#两个数据的对比
# plt.show()
#对多个数据进行散点图分析
# sns.pairplot(top_tect_dr.dropna())
# plt.show()
print(top_tect_dr["MSFT"].quantile(0.05))
#-0.02132405607678528 #这里表示的是有(1-0.05),即95%的概率,最大亏损在2.1个点以内
print(top_tect_dr["AAPL"].quantile(0.52))
#0.001195519921020917#同理上面
vips = pdr.get_data_yahoo("VIPS",start=start)["Adj Close"]
vips.plot()
plt.show()
print(vips.pct_change().quantile(0.2))
#-0.023114419450778745
#总结:
#网站:https://www.kaggle.com/datasets