参考

import tushare as ts
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import requests
from jqdatasdk import *
import librosa as rosa
import librosa.display
from IPython.display import Audio
from matplotlib.font_manager import FontProperties
import copy
from scipy import fft
import scipy
from scipy import signalfont_set = FontProperties(fname=r"/System/Library/Fonts/STHeiti Medium.ttc", size=10)
print(ts.__version__)
print(scipy.__version__)
1.2.85
1.7.3

量化平台

#tushare 要600积分
ts.set_token('xxx')
pro = ts.pro_api()
#聚宽 只能免费拿2022年数据auth('xxx','xxx') #账号是申请时所填写的手机号;密码为聚宽官网登录密码
jq_quota = get_query_count()
print(jq_quota)monthData = get_bars('000001.XSHG', count=500, unit='1M',fields=['date', 'open', 'close', 'high', 'low', 'volume'],include_now=True, end_dt=None, df=True)
print(monthData.size)
print(monthData.head(60))monthData = get_bars('000001.XSHG', count=500, unit='1M',fields=['date', 'open', 'close', 'high', 'low', 'volume'],include_now=True, end_dt='2021-12-31', df=True)
print(monthData.size)
print(monthData.head(60))

下载股票数据

#下载上证日线数据 # market: 0:沪市 1:深市
# number: 股票代码
# start_data: 起始日期:yyyymmdd
# end_data: 结束时间:yyyymmdd
def getHistoryData(market, number, start_data, end_data):url = 'http://quotes.money.163.com/service/chddata.html?code=' + market + number + '&start=' + start_data + '&end=' + end_dataf = open("temp.csv", "wb")urlt=requests.get(url).contentprint(type(urlt))f.write(urlt)f.close()df = pd.read_csv('temp.csv', encoding='gbk')dataList = []for row in range(0, df.shape[0]):dataList.append({})for col in range(0, df.shape[1]):col_name = df.columns.values[col]dataList[row][col_name] = df.loc[row, col_name]#print(type(dataList))#print(type(df))return dataListif __name__ == '__main__':hisData = getHistoryData('0', '000001', '19901219', '20220907')print(hisData)

计算同比值

#读取上证csv数据
df = pd.read_csv('sh_index.csv', encoding="gb2312")print(df.head(5))
           日期     股票代码    名称        收盘价        最高价        最低价        开盘价  \
0  2022-09-07  '000001  上证指数  3246.2943  3253.7671  3227.8225  3232.1368
1  2022-09-06  '000001  上证指数  3243.4466  3244.6353  3203.8161  3207.9330
2  2022-09-05  '000001  上证指数  3199.9118  3199.9118  3172.0394  3183.9460
3  2022-09-02  '000001  上证指数  3186.4781  3198.2812  3173.7929  3189.6439
4  2022-09-01  '000001  上证指数  3184.9797  3214.5583  3181.6256  3196.5367   前收盘       涨跌额      涨跌幅  换手率        成交量               成交金额  总市值  流通市值  \
0  3243.4466    2.8477   0.0878  NaN  301014466  3.68608741475e+11  NaN   NaN
1  3199.9118   43.5348   1.3605  NaN  318869689  3.78636070328e+11  NaN   NaN
2  3186.4781   13.4337   0.4216  NaN  280675633  3.39591536253e+11  NaN   NaN
3  3184.9797    1.4984    0.047  NaN  250456911  3.13337604115e+11  NaN   NaN
4  3202.1378  -17.1581  -0.5358  NaN  274660541  3.36978641199e+11  NaN   NaN   成交笔数
0  None
1  None
2  None
3  None
4  None
month_dict = {}def get_one_year_later_month(month):year = int(month[0:4])year += 1;next_year_month = str(year) + month[4:];return next_year_month#计算按月收盘价
for index, row in df.iterrows():month = row["日期"][0:7]month_dict[month] = row["收盘价"]
print("monthDict:")
print(month_dict)#计算同比
year_on_year_basis = {}
for each_month, each_price in month_dict.items():one_year_later_month = get_one_year_later_month(each_month)if one_year_later_month in month_dict:one_year_later_mongth_price = month_dict[one_year_later_month]print("%s one year later month %s %d now %d"%(each_month, one_year_later_month, one_year_later_mongth_price, each_price))year_on_year_basis[each_month] = np.log(one_year_later_mongth_price) - np.log(each_price)
print("year_on_year_basis")
print(year_on_year_basis)
monthDict:
{'2022-09': 3184.9797, '2022-08': 3259.9585, '2022-07': 3387.6373, '2022-06': 3182.1566, '2022-05': 3067.7587, '2022-04': 3282.7166, '2022-03': 3488.8347, '2022-02': 3429.5809, '2022-01': 3632.3289, '2021-12': 3576.8853, '2021-11': 3544.4795, '2021-10': 3592.1666, '2021-09': 3567.1008, '2021-08': 3464.2854, '2021-07': 3588.7816, '2021-06': 3624.7138, '2021-05': 3441.2826, '2021-04': 3466.3315, '2021-03': 3551.3998, '2021-02': 3505.2836, '2021-01': 3502.9584, '2020-12': 3451.9384, '2020-11': 3225.1196, '2020-10': 3272.0762, '2020-09': 3410.6068, '2020-08': 3367.9658, '2020-07': 3025.981, '2020-06': 2915.4311, '2020-05': 2878.1402, '2020-04': 2734.5215, '2020-03': 2970.9312, '2020-02': 2746.6056, '2020-01': 3085.1976, '2019-12': 2875.8072, '2019-11': 2958.1992, '2019-10': 2913.5704, '2019-09': 2924.1063, '2019-08': 2908.766, '2019-07': 3044.9028, '2019-06': 2890.0809, '2019-05': 2906.464, '2019-04': 3170.361, '2019-03': 2994.005, '2019-02': 2618.2323, '2019-01': 2465.291, '2018-12': 2654.798, '2018-11': 2606.2372, '2018-10': 2716.5104, '2018-09': 2720.7344, '2018-08': 2824.5337, '2018-07': 2775.557, '2018-06': 3075.1372, '2018-05': 3081.1773, '2018-04': 3163.179, '2018-03': 3273.7549, '2018-02': 3446.9799, '2018-01': 3348.3259, '2017-12': 3317.6174, '2017-11': 3395.9125, '2017-10': 3374.3781, '2017-09': 3367.1194, '2017-08': 3292.6383, '2017-07': 3195.9116, '2017-06': 3102.6232, '2017-05': 3143.7121, '2017-04': 3270.3054, '2017-03': 3246.9335, '2017-02': 3140.17, '2017-01': 3135.9208, '2016-12': 3273.3093, '2016-11': 3122.4356, '2016-10': 3048.1428, '2016-09': 3063.3054, '2016-08': 2953.3854, '2016-07': 2932.4758, '2016-06': 2913.5077, '2016-05': 2992.6432, '2016-04': 3009.5298, '2016-03': 2733.17, '2016-02': 2688.8536, '2016-01': 3296.258, '2015-12': 3456.309, '2015-11': 3325.085, '2015-10': 3143.357, '2015-09': 3166.624, '2015-08': 3622.905, '2015-07': 4053.7, '2015-06': 4828.738, '2015-05': 4480.464, '2015-04': 3810.294, '2015-03': 3336.285, '2015-02': 3128.3, '2015-01': 3350.519, '2014-12': 2680.155, '2014-11': 2430.032, '2014-10': 2382.794, '2014-09': 2235.511, '2014-08': 2185.303, '2014-07': 2050.381, '2014-06': 2038.305, '2014-05': 2027.353, '2014-04': 2047.46, '2014-03': 2075.235, '2014-02': 2044.497, '2014-01': 2109.387, '2013-12': 2207.371, '2013-11': 2149.562, '2013-10': 2198.199, '2013-09': 2098.448, '2013-08': 2029.067, '2013-07': 1995.242, '2013-06': 2299.252, '2013-05': 2174.123, '2013-04': 2234.395, '2013-03': 2359.506, '2013-02': 2419.02, '2013-01': 2276.992, '2012-12': 1959.767, '2012-11': 2104.428, '2012-10': 2074.419, '2012-09': 2059.147, '2012-08': 2123.36, '2012-07': 2226.11, '2012-06': 2373.436, '2012-05': 2438.436, '2012-04': 2302.241, '2012-03': 2426.115, '2012-02': 2268.08, '2012-01': 2169.39, '2011-12': 2386.86, '2011-11': 2470.019, '2011-10': 2344.787, '2011-09': 2556.042, '2011-08': 2703.783, '2011-07': 2759.362, '2011-06': 2743.572, '2011-05': 2932.188, '2011-04': 2967.41, '2011-03': 2918.92, '2011-02': 2798.96, '2011-01': 2852.648, '2010-12': 2823.449, '2010-11': 3054.021, '2010-10': 2738.744, '2010-09': 2622.882, '2010-08': 2672.516, '2010-07': 2373.792, '2010-06': 2568.283, '2010-05': 2835.277, '2010-04': 3147.416, '2010-03': 3087.842, '2010-02': 2941.36, '2010-01': 3243.76, '2009-12': 3235.363, '2009-11': 3076.649, '2009-10': 2911.715, '2009-09': 2683.724, '2009-08': 3462.59, '2009-07': 3008.15, '2009-06': 2721.28, '2009-05': 2559.911, '2009-04': 2408.017, '2009-03': 2093.447, '2009-02': 2011.683, '2009-01': 1880.716, '2008-12': 1894.615, '2008-11': 1719.774, '2008-10': 2173.738, '2008-09': 2325.135, '2008-08': 2801.817, '2008-07': 2651.605, '2008-06': 3459.044, '2008-05': 3761.009, '2008-04': 3329.162, '2008-03': 4438.265, '2008-02': 4320.767, '2008-01': 5272.814, '2007-12': 4868.611, '2007-11': 5914.285, '2007-10': 5692.755, '2007-09': 5321.055, '2007-08': 4300.563, '2007-07': 3836.294, '2007-06': 4000.742, '2007-05': 3950.011, '2007-04': 3252.595, '2007-03': 2797.19, '2007-02': 2785.432, '2007-01': 2715.719, '2006-12': 2102.049, '2006-11': 1855.713, '2006-10': 1785.385, '2006-09': 1636.691, '2006-08': 1600.614, '2006-07': 1697.282, '2006-06': 1684.195, '2006-05': 1497.104, '2006-04': 1319.471, '2006-03': 1306.586, '2006-02': 1287.626, '2006-01': 1180.963, '2005-12': 1098.747, '2005-11': 1089.95, '2005-10': 1138.95, '2005-09': 1184.933, '2005-08': 1088.948, '2005-07': 1055.594, '2005-06': 1039.187, '2005-05': 1130.835, '2005-04': 1223.566, '2005-03': 1303.412, '2005-02': 1188.931, '2005-01': 1242.774, '2004-12': 1334.944, '2004-11': 1305.291, '2004-10': 1422.929, '2004-09': 1321.462, '2004-08': 1373.217, '2004-07': 1441.068, '2004-06': 1579.658, '2004-05': 1560.9351, '2004-04': 1758.147, '2004-03': 1689.76, '2004-02': 1623.88, '2004-01': 1517.193, '2003-12': 1432.703, '2003-11': 1364.055, '2003-10': 1371.685, '2003-09': 1449.816, '2003-08': 1476.969, '2003-07': 1484.704, '2003-06': 1576.528, '2003-05': 1531.867, '2003-04': 1522.546, '2003-03': 1525.483, '2003-02': 1480.169, '2003-01': 1320.63, '2002-12': 1395.675, '2002-11': 1510.76, '2002-10': 1562.993, '2002-09': 1668.774, '2002-08': 1653.008, '2002-07': 1713.705, '2002-06': 1483.348, '2002-05': 1652.368, '2002-04': 1608.506, '2002-03': 1502.54, '2002-02': 1485.77, '2002-01': 1611.393, '2001-12': 1762.562, '2001-11': 1707.754, '2001-10': 1726.533, '2001-09': 1817.19, '2001-08': 1986.928, '2001-07': 2205.985, '2001-06': 2219.591, '2001-05': 2137.994, '2001-04': 2125.084, '2001-03': 1965.121, '2001-02': 2008.032, '2001-01': 2103.469, '2000-12': 2081.843, '2000-11': 1977.363, '2000-10': 1915.35, '2000-09': 1999.859, '2000-08': 2028.151, '2000-07': 1895.637, '2000-06': 1903.488, '2000-05': 1836.637, '2000-04': 1801.003, '2000-03': 1697.744, '2000-02': 1673.943, '2000-01': 1406.371, '1999-12': 1447.116, '1999-11': 1493.096, '1999-10': 1534.515, '1999-09': 1599.552, '1999-08': 1616.276, '1999-07': 1560.787, '1999-06': 1311.594, '1999-05': 1111.757, '1999-04': 1168.504, '1999-03': 1097.975, '1999-02': 1120.531, '1999-01': 1125.819, '1998-12': 1234.358, '1998-11': 1223.478, '1998-10': 1234.779, '1998-09': 1143.125, '1998-08': 1302.102, '1998-07': 1316.441, '1998-06': 1415.575, '1998-05': 1352.926, '1998-04': 1254.965, '1998-03': 1188.306, '1998-02': 1248.04, '1998-01': 1220.473, '1997-12': 1133.087, '1997-11': 1180.854, '1997-10': 1098.903, '1997-09': 1229.865, '1997-08': 1192.828, '1997-07': 1199.061, '1997-06': 1336.283, '1997-05': 1448.926, '1997-04': 1241.028, '1997-03': 1063.045, '1997-02': 982.404, '1997-01': 919.435, '1996-12': 1134.523, '1996-11': 930.789, '1996-10': 865.486, '1996-09': 804.778, '1996-08': 835.105, '1996-07': 761.115, '1996-06': 641.092, '1996-05': 654.43, '1996-04': 566.19, '1996-03': 601.98, '1996-02': 536.68, '1996-01': 537.87, '1995-12': 633.81, '1995-11': 723.39, '1995-10': 717.79, '1995-09': 728.84, '1995-08': 715.4, '1995-07': 613.18, '1995-06': 708.67, '1995-05': 579.04, '1995-04': 656.88, '1995-03': 556.95, '1995-02': 533.72, '1995-01': 639.88, '1994-12': 682.3, '1994-11': 673.79, '1994-10': 706.42, '1994-09': 790.19, '1994-08': 445.64, '1994-07': 458.37, '1994-06': 545.05, '1994-05': 605.77, '1994-04': 698.84, '1994-03': 757.29, '1994-02': 755.15, '1994-01': 833.9, '1993-12': 974.8, '1993-11': 816.71, '1993-10': 892.55, '1993-09': 893.97, '1993-08': 843.27, '1993-07': 997.53, '1993-06': 928.58, '1993-05': 1352.24, '1993-04': 947.23, '1993-03': 1199.73, '1993-02': 1278.5, '1993-01': 814.04, '1992-12': 716.74, '1992-11': 477.61, '1992-10': 679.62, '1992-09': 824.96, '1992-08': 1031.55, '1992-07': 1194.18, '1992-06': 1183.24, '1992-05': 453.62, '1992-04': 381.01, '1992-03': 365.39, '1992-02': 314.18, '1992-01': 293.75, '1991-12': 262.19, '1991-11': 220.72, '1991-10': 182.48, '1991-09': 180.22, '1991-08': 145.24, '1991-07': 136.85, '1991-06': 115.97, '1991-05': 113.16, '1991-04': 120.73, '1991-03': 132.53, '1991-02': 129.51, '1991-01': 128.84, '1990-12': 99.98}
2021-09 one year later month 2022-09 3184 now 3567year_on_year_basis
{'2021-09': -0.11330724964124528, '2021-08': -0.060791912370287804, '2021-07': -0.05768004095893353, '2021-06': -0.13021619043042953, '2021-05': -0.11489702090647214, '2021-04': -0.054425518804084305, '2021-03': -0.017774052016541475, '2021-02': -0.02183336307119177, '2021-01': 0.036266144196906325, '2020-12': 0.03555646545141222, '2020-11': 0.09442128785638992, '2020-10': 0.0933308232328347, '2020-09': 0.04486294233991295, '2020-08': 0.028197435672300486, '2020-07': 0.17057742127830267, '2020-06': 0.21775763437489015, '2020-05': 0.17869992906800292, '2020-04': 0.23714036384134118, '2020-03': 0.1784663966005704, '2020-02': 0.2439056081983253, '2020-01': 0.12699216143960435, '2019-12': 0.18260252827474055, '2019-11': 0.08638933001104032, '2019-10': 0.11604543629144182, '2019-09': 0.1539013271428784, '2019-08': 0.14658000567038254, '2019-07': -0.006233643039712078, '2019-06': 0.008733204278632556, '2019-05': -0.009792900387592951, '2019-04': -0.1478889952600273, '2019-03': -0.00773651697651534, '2019-02': 0.04786642658875628, '2019-01': 0.22430585390466096, '2018-12': 0.079964831122596, '2018-11': 0.12667320953844108, '2018-10': 0.07003115522859726, '2018-09': 0.07208705167142959, '2018-08': 0.02938564723656345, '2018-07': 0.09261753169269227, '2018-06': -0.06206502333718422, '2018-05': -0.058374542158775355, '2018-04': 0.0022679269660823564, '2018-03': -0.08932565759042888, '2018-02': -0.2749990609525508, '2018-01': -0.3061506363210542, '2017-12': -0.22287830575459378, '2017-11': -0.2646650072286345, '2017-10': -0.2168629252207923, '2017-09': -0.21315575752117777, '2017-08': -0.1533458692816616, '2017-07': -0.1410209201696162, '2017-06': -0.008898429070258729, '2017-05': -0.02009253491721097, '2017-04': -0.03330584047814078, '2017-03': 0.008226601125654653, '2017-02': 0.09322151777817211, '2017-01': 0.06553764251916228, '2016-12': 0.01344538317907329, '2016-11': 0.08395916420525751, '2016-10': 0.10167855355798316, '2016-09': 0.09456307233491401, '2016-08': 0.10873705271862466, '2016-07': 0.08602531877999908, '2016-06': 0.06289019675266871, '2016-05': 0.049247288856415494, '2016-04': 0.08309952110984398, '2016-03': 0.17224890474842525, '2016-02': 0.1551620068086681, '2016-01': -0.049865038615587665, '2015-12': -0.05439976515476985, '2015-11': -0.06288189938553224, '2015-10': -0.030758849990505155, '2015-09': -0.0331715071126748, '2015-08': -0.20432408461532692, '2015-07': -0.32378299513789344, '2015-06': -0.5052273994131706, '2015-05': -0.4035696022214932, '2015-04': -0.23592249758211103, '2015-03': -0.19939580478303043, '2015-02': -0.1513747942392225, '2015-01': -0.016327373527619926, '2014-12': 0.2543266279959875, '2014-11': 0.31359081170081016, '2014-10': 0.277017588387932, '2014-09': 0.3481961988841942, '2014-08': 0.505521698503391, '2014-07': 0.6816044147647187, '2014-06': 0.8624665697509535, '2014-05': 0.7929956111959742, '2014-04': 0.621106350981127, '2014-03': 0.47478351222002946, '2014-02': 0.4253379325425293, '2014-01': 0.4627178752589485, '2013-12': 0.19407241402886477, '2013-11': 0.12264032553461934, '2013-10': 0.08063535998832894, '2013-09': 0.06327181339920074, '2013-08': 0.07417841021105609, '2013-07': 0.02726028329687491, '2013-06': -0.12046527252003703, '2013-05': -0.06989436353910783, '2013-04': -0.08737049737440472, '2013-03': -0.12837787481063412, '2013-02': -0.16821070600294785, '2013-01': -0.07645788972914769, '2012-12': 0.11897662601746006, '2012-11': 0.02122040421417637, '2012-10': 0.057957274259556435, '2012-09': 0.018906204502254553, '2012-08': -0.04542365821560601, '2012-07': -0.10949032138190873, '2012-06': -0.0317548415979676, '2012-05': -0.11473148534528566, '2012-04': -0.029912498483330552, '2012-03': -0.027838937438388278, '2012-02': 0.06442884086097855, '2012-01': 0.04840925163672338, '2011-12': -0.19715310598535218, '2011-11': -0.16018214667675768, '2011-10': -0.12251345174096073, '2011-09': -0.2161681489274132, '2011-08': -0.24165216377430365, '2011-07': -0.21474382596207597, '2011-06': -0.1449220267745046, '2011-05': -0.18439205184263763, '2011-04': -0.25380652208991616, '2011-03': -0.18492247231920356, '2011-02': -0.2103142610010309, '2011-01': -0.27380166367626746, '2010-12': -0.16798049266920767, '2010-11': -0.21223324008270428, '2010-10': -0.15530485492836998, '2010-09': -0.025813744911138414, '2010-08': 0.011631552678315948, '2010-07': 0.150510817076575, '2010-06': 0.06602313877354948, '2010-05': 0.033609262385656535, '2010-04': -0.05889228025036708, '2010-03': -0.05625878020010866, '2010-02': -0.04962413981207803, '2010-01': -0.1284854650587901, '2009-12': -0.13618194457922783, '2009-11': -0.007381934684289604, '2009-10': -0.06124283352097759, '2009-09': -0.022931669010962885, '2009-08': -0.2589965131526446, '2009-07': -0.2368365953548146, '2009-06': -0.057864775603204244, '2009-05': 0.10216714737558252, '2009-04': 0.2677782115104206, '2009-03': 0.388660475349508, '2009-02': 0.37990037427264767, '2009-01': 0.5450805953786171, '2008-12': 0.5351254801922734, '2008-11': 0.5816481308415868, '2008-10': 0.2922939884085638, '2008-09': 0.14342728035749985, '2008-08': 0.21174872866594807, '2008-07': 0.1261601548282849, '2008-06': -0.2398898924622781, '2008-05': -0.3847147802037929, '2008-04': -0.3239170334487911, '2008-03': -0.7514515446086323, '2008-02': -0.7644612476403543, '2008-01': -1.0309116304883092, '2007-12': -0.9437930291406191, '2007-11': -1.2351777241596027, '2007-10': -0.9627460474792944, '2007-09': -0.8278934900939738, '2007-08': -0.42847780907197386, '2007-07': -0.3693416793828179, '2007-06': -0.14548759360333285, '2007-05': -0.0490310912533527, '2007-04': 0.023267481391833655, '2007-03': 0.46164819247192046, '2007-02': 0.43902995474635187, '2007-01': 0.6635074425166625, '2006-12': 0.839896097491728, '2006-11': 1.1591016222166939, '2006-10': 1.15956023548721, '2006-09': 1.1789950713188375, '2006-08': 0.9883586387289238, '2006-07': 0.8154786483696244, '2006-06': 0.8651921390760995, '2006-05': 0.9701857884169849, '2006-04': 0.9022222405815326, '2006-03': 0.7611977133028907, '2006-02': 0.7716027652014379, '2006-01': 0.8327265360294067, '2005-12': 0.6487421436772189, '2005-11': 0.532137165157021, '2005-10': 0.449527292903956, '2005-09': 0.32299028774065075, '2005-08': 0.38517521305135993, '2005-07': 0.4749245063907832, '2005-06': 0.4828490281675739, '2005-05': 0.2805762774052507, '2005-04': 0.07546135264764509, '2005-03': 0.0024321869707621957, '2005-02': 0.0797456287799525, '2005-01': -0.05101577047325634, '2004-12': -0.19471890376875045, '2004-11': -0.18029418079287485, '2004-10': -0.22261063792882663, '2004-09': -0.10905246654209133, '2004-08': -0.2319440697862971, '2004-07': -0.31128086369609065, '2004-06': -0.4187696912395378, '2004-05': -0.32232876685907286, '2004-04': -0.36249086746455816, '2004-03': -0.2596010654520704, '2004-02': -0.3117637633186545, '2004-01': -0.19951593924394384, '2003-12': -0.07067352645664027, '2003-11': -0.04403587676682008, '2003-10': 0.03667751216732462, '2003-09': -0.09269795238236345, '2003-08': -0.07283585247132507, '2003-07': -0.029830920431382246, '2003-06': 0.0019834072048308116, '2003-05': 0.0187978118277341, '2003-04': 0.1438764799044634, '2003-03': 0.10227542579078364, '2003-02': 0.09266207693762585, '2003-01': 0.1387530215926196, '2002-12': 0.026184700650222936, '2002-11': -0.10215095427750143, '2002-10': -0.13056266175573406, '2002-09': -0.14065257325676406, '2002-08': -0.11260464367379797, '2002-07': -0.14344226755679568, '2002-06': 0.060923265606320776, '2002-05': -0.07572215765357182, '2002-04': -0.054921864263193676, '2002-03': 0.015154071856212425, '2002-02': -0.003776885942782471, '2002-01': -0.19899012683571904, '2001-12': -0.23339026317728084, '2001-11': -0.12256622142380724, '2001-10': -0.09951277861686592, '2001-09': -0.08520212683089134, '2001-08': -0.18399306886960964, '2001-07': -0.25251642782689476, '2001-06': -0.4030212495751364, '2001-05': -0.2576585956558768, '2001-04': -0.27850553313349113, '2001-03': -0.26839681166451257, '2001-02': -0.3012219815685757, '2001-01': -0.2664888642944492, '2000-12': -0.16648512669759974, '2000-11': -0.14658508197627995, '2000-10': -0.10378502207734464, '2000-09': -0.0957853261583379, '2000-08': -0.02053481316858985, '2000-07': 0.151619191364194, '2000-06': 0.15363495203271516, '2000-05': 0.15193182432166008, '2000-04': 0.16546759907319153, '2000-03': 0.14625351011726107, '2000-02': 0.1819732166491308, '2000-01': 0.4025752588838918, '1999-12': 0.36368094879150714, '1999-11': 0.28091232230969876, '1999-10': 0.2216860033557193, '1999-09': 0.22335308803628706, '1999-08': 0.22699980295599165, '1999-07': 0.1943647485765876, '1999-06': 0.3724448012315502, '1999-05': 0.501994535147742, '1999-04': 0.43261943375064416, '1999-03': 0.43583273675846357, '1999-02': 0.4013792412117718, '1999-01': 0.22250185692050461, '1998-12': 0.15902161343602295, '1998-11': 0.19915419401941747, '1998-10': 0.21732236350241418, '1998-09': 0.335957849875423, '1998-08': 0.2161448558630994, '1998-07': 0.17025829807955795, '1998-06': -0.07629261756471539, '1998-05': -0.19632800765028513, '1998-04': -0.07138338556559898, '1998-03': -0.07906118937326934, '1998-02': -0.10777164061907651, '1998-01': -0.08072771773214438, '1997-12': 0.0856052304723578, '1997-11': 0.03545971701172945, '1997-10': 0.11657959722878442, '1997-09': -0.07313866710013084, '1997-08': 0.08765292336042396, '1997-07': 0.09339313266947435, '1997-06': 0.057643929947466255, '1997-05': -0.06855293791248673, '1997-04': 0.01116761533106203, '1997-03': 0.11139133204753904, '1997-02': 0.23932697065196606, '1997-01': 0.2832344165542393, '1996-12': -0.0012665318921305513, '1996-11': 0.23796057094070378, '1996-10': 0.23877648958747244, '1996-09': 0.4240932232575165, '1996-08': 0.35652477190524934, '1996-07': 0.4545095660671139, '1996-06': 0.7344741856489971, '1996-05': 0.7948132435547999, '1996-04': 0.7847656364815592, '1996-03': 0.5686684883001103, '1996-02': 0.6046006153235561, '1996-01': 0.5361424556775187, '1995-12': 0.582218352282271, '1995-11': 0.2520841177554347, '1995-10': 0.18711415170517665, '1995-09': 0.09911223380606682, '1995-08': 0.15471563862325155, '1995-07': 0.21612593271633695, '1995-06': -0.10021700152871738, '1995-05': 0.12239306794310512, '1995-04': -0.1485716424823762, '1995-03': 0.07774875287437766, '1995-02': 0.005530656848050874, '1995-01': -0.17366376349794965, '1994-12': -0.07372021884112545, '1994-11': 0.0710300061485718, '1994-10': 0.015967085699494454, '1994-09': -0.08081919369149304, '1994-08': 0.4733303756338012, '1994-07': 0.2909818124469794, '1994-06': 0.26251244031721566, '1994-05': -0.04512881625732845, '1994-04': -0.06192046420571007, '1994-03': -0.3072708019348447, '1994-02': -0.3470450481381304, '1994-01': -0.26483283231954413, '1993-12': -0.35676287792967276, '1993-11': -0.19236558502430423, '1993-10': -0.23387257303382292, '1993-09': -0.12339879469031345, '1993-08': -0.6377757402280535, '1993-07': -0.7776055052670063, '1993-06': -0.532779003980675, '1993-05': -0.8030173795691615, '1993-04': -0.30412011831923813, '1993-03': -0.4601055392337505, '1993-02': -0.5265263897188905, '1993-01': 0.024103986248591447, '1992-12': 0.30751916895096176, '1992-11': 0.5364895748048104, '1992-10': 0.2725487160528841, '1992-09': 0.08033731725271664, '1992-08': -0.2015306130147847, '1992-07': -0.17993281285944462, '1992-06': -0.24235517989621513, '1992-05': 1.0922579124896323, '1992-04': 0.91071631442406, '1992-03': 1.188886534159125, '1992-02': 1.4034767246594475, '1992-01': 1.0192804393859793, '1991-12': 1.0056437210231168, '1991-11': 0.7718995699736153, '1991-10': 1.314893240464544, '1991-09': 1.5211565735038768, '1991-08': 1.9604302578878192, '1991-07': 2.166329600872496, '1991-06': 2.322680180482646, '1991-05': 1.3884570967189234, '1991-04': 1.1492689741571258, '1991-03': 1.0141562414080818, '1991-02': 0.8862079718362974, '1991-01': 0.8241577409953704, '1990-12': 0.9640992657915479}
#同比图
year_on_year_price_basis_view = year_on_year_basis.values()
year_on_year_price_basis = list(year_on_year_price_basis_view)
plt.plot(year_on_year_basis.keys(), year_on_year_basis.values())
[<matplotlib.lines.Line2D at 0x7fd5600d6190>]

原始傅里叶换

##### 原始傅里叶变换_备份
fs = 12
fft_count = len(year_on_year_price_basis)
fft = np.fft.fft(year_on_year_price_basis[0:fft_count])
#f_x = np.arange(186) * fs / fft_count
f_x = np.fft.fftfreq(fft_count, 1 / fs)
print("fft_count %d"%(fft_count))
plt.stem(f_x, np.abs(fft)[0:len(f_x)], use_line_collection=True)
max_amp = 0
max_index = 0
for i in range(len(fft)):if np.abs(fft[i]) > max_amp:max_amp = np.abs(fft[i])max_index = i
print("max_amp %.5f, freq %.5f, max_index %d "%(max_amp, f_x[max_index], max_index))
second_max_amp = 0
second_max_index = 0
for i in range(len(fft)):if np.abs(fft[i]) > second_max_amp and int(np.abs(fft[i])) != int(max_amp) :second_max_amp = np.abs(fft[i])second_max_index = i
print("second_max_amp %.5f, second_freq %.5f, second_max_index %d "%(second_max_amp, f_x[second_max_index], second_max_index))
#plt.stem(fft, use_line_collection=True)
max_freq = np.argmax(np.abs(fft))
print("max_freq %.5f"%(max_freq))
tmp_freq = (fft[max_freq+1] - fft[max_freq-1]) / (2 * fft[max_freq] - fft[max_freq-1] - fft[max_freq+1])
best_freq = (max_freq - np.real(tmp_freq)) * fs / fft_count
print("best_freq = %.5f " % (best_freq))
print('bin = %.3f Hz' % (fs / fft_count))
fft_count 370
max_amp 44.22387, freq 0.12973, max_index 4
second_max_amp 38.32627, second_freq 0.00000, second_max_index 0
max_freq 4.00000
best_freq = 0.12574
bin = 0.032 Hz
##### 原始傅里叶变换修改
fs = 12
fft_count = len(year_on_year_price_basis)
year_on_year_price_basis_sin = []
for i in range(fft_count):year_on_year_price_basis_sin.append(np.sin(2 * np.pi*year_on_year_price_basis[i]))
year_on_year_price_basis = year_on_year_price_basis_sin
fft = np.fft.fft(year_on_year_price_basis[0:fft_count])
#f_x = np.arange(186) * fs / fft_count
f_x = np.fft.fftfreq(fft_count, 1 / fs)
print("fft_count %d"%(fft_count))
plt.stem(f_x, np.abs(fft)[0:len(f_x)], use_line_collection=True)
max_amp = 0
max_index = 0
for i in range(len(fft)):if np.abs(fft[i]) > max_amp:max_amp = np.abs(fft[i])max_index = i
print("max_amp %.5f, freq %.5f, max_index %d "%(max_amp, f_x[max_index], max_index))
second_max_amp = 0
second_max_index = 0
for i in range(len(fft)):if np.abs(fft[i]) > second_max_amp and int(np.abs(fft[i])) != int(max_amp) :second_max_amp = np.abs(fft[i])second_max_index = i
print("second_max_amp %.5f, second_freq %.5f, second_max_index %d "%(second_max_amp, f_x[second_max_index], second_max_index))
#plt.stem(fft, use_line_collection=True)
max_freq = np.argmax(np.abs(fft))
print("max_freq %.5f"%(max_freq))
tmp_freq = (fft[max_freq+1] - fft[max_freq-1]) / (2 * fft[max_freq] - fft[max_freq-1] - fft[max_freq+1])
best_freq = (max_freq - np.real(tmp_freq)) * fs / fft_count
print("best_freq = %.5f " % (best_freq))
print('bin = %.3f Hz' % (fs / fft_count))
fft_count 370
max_amp 53.10545, freq 0.29189, max_index 9
second_max_amp 40.13227, second_freq 0.51892, second_max_index 16
max_freq 9.00000
best_freq = 0.29798
bin = 0.032 Hz

优化傅里叶变换

##### 优化傅里叶变换 v1
sample_rate = 12
price_count = len(year_on_year_price_basis)
# price_count 370
print("price_count: %d"%(price_count))
fft_src_data = np.asarray(year_on_year_price_basis[0:price_count])
print("fft_src_data type")
print(type(fft_src_data))
fft_list =  np.fft.fft(fft_src_data)
amp_list = abs(fft_list)  # 幅度谱
normalization_fft = amp_list / price_count  # 归一化#f_x = np.arange(fft_list.size) * sample_rate / price_count
f_x = np.fft.fftfreq(price_count, 1 / sample_rate)#滤波
filter_fft_list = copy.deepcopy(fft_list)
for i in range(len(filter_fft_list)):print("i %d f_x[i] %d filter_fft_list[i] %d"%(i, f_x[i], filter_fft_list[i]))if f_x[i] > 0.3:filter_fft_list[i] = 0if f_x[i] < 0.1:filter_fft_list[i] = 0print("fft_list")
print(fft_list)
print("size of fft")
# size of fft 186
print(fft_list.size)
print("f_x")
print(f_x)
print("size of f_x")
print(f_x.size)#plt.tight_layout()
plt.figure(figsize=(10, 10))
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, \wspace=0.45, hspace=0.45)
plt.subplot(711)plt.title('原始信号',fontproperties=font_set)
plt.plot(year_on_year_price_basis[0:price_count])  plt.subplot(712)
plt.title('傅里叶变换信号(幅度取正负)',fontproperties=font_set)
plt.plot(f_x,fft_list[0:fft_list.size])  plt.subplot(713)
plt.title('傅里叶变换信号(幅度)',fontproperties=font_set)
plt.plot(f_x, np.abs(fft_list)[0:len(f_x)])  plt.subplot(714)
plt.title('傅里叶棉棒信号',fontproperties=font_set)
plt.stem(f_x, np.abs(fft_list)[0:len(f_x)], use_line_collection=True)plt.subplot(715)
plt.title("未归一化的幅度谱",fontproperties=font_set)
plt.plot(f_x, amp_list)plt.subplot(716)
plt.title("归一化的幅度谱",fontproperties=font_set)
plt.plot(f_x, normalization_fft)plt.subplot(717)
plt.title("滤波后的幅度谱",fontproperties=font_set)
plt.plot(f_x,filter_fft_list)
price_count: 370
fft_src_data type
<class 'numpy.ndarray'>[<matplotlib.lines.Line2D at 0x7fceb3fe44f0>]
##### 优化傅里叶变换 v2
fs = 12
year_on_year_price_basis = list(year_on_year_price_basis_view)
fft_count = len(year_on_year_price_basis)
year_on_year_price_basis_sin = []
for i in range(fft_count):year_on_year_price_basis_sin.append(np.sin(2 * np.pi*year_on_year_price_basis[i]))
fft_count = len(year_on_year_price_basis_sin)
fft = np.fft.fft(year_on_year_price_basis_sin[0:fft_count])
normalization_fft = fft / fft_count  # 归一化
#f_x = np.arange(186) * fs / fft_count
f_x = np.fft.fftfreq(fft_count, 1 / fs)
print("fft_count %d"%(fft_count))#计算最佳频率
max_amp = 0
max_index = 0
for i in range(len(normalization_fft)):if np.abs(normalization_fft[i]) > max_amp:max_amp = np.abs(normalization_fft[i])max_index = i
print("max_amp %.5f, freq %.5f, max_index %d "%(max_amp, f_x[max_index], max_index))
second_max_amp = 0
second_max_index = 0
for i in range(len(normalization_fft)):if np.abs(normalization_fft[i]) > second_max_amp and int(np.abs(normalization_fft[i])) != int(max_amp) :second_max_amp = np.abs(normalization_fft[i])second_max_index = i
print("second_max_amp %.5f, second_freq %.5f, second_max_index %d "%(second_max_amp, f_x[second_max_index], second_max_index))
#plt.stem(fft, use_line_collection=True)
max_freq = np.argmax(np.abs(fft))
print("max_freq %.5f"%(max_freq))
tmp_freq = (fft[max_freq+1] - fft[max_freq-1]) / (2 * fft[max_freq] - fft[max_freq-1] - fft[max_freq+1])
best_freq = (max_freq - np.real(tmp_freq)) * fs / fft_count
print("best_freq = %.5f " % (best_freq))
print('bin = %.3f Hz' % (fs / fft_count))#滤波
'''
这里假设采样频率为1000hz,信号本身最大的频率为500hz,要滤除100hz以下,400hz以上频率成分,即截至频率为100,400hz,则wn1=2*100/1000=0.2,Wn1=0.2;
wn2=2*400/1000=0.8,Wn2=0.8。Wn=[0.02,0.8]
b, a = signal.butter(8, [0.2,0.8], 'bandpass')   #配置滤波器 8 表示滤波器的阶数采样 12, 滤除0.1以下,0.3以上,
wn1 = 2 * 0.1 / 12 =0.0167
wn2 = 2 * 0.3 / 12 = 0.05
'''
#b, a = signal.butter(8, [0.0167,0.05], 'bandpass')   #配置滤波器 8 表示滤波器的阶数
b, a = signal.butter(8, 0.05, 'lowpass')   #配置滤波器 8 表示滤波器的阶数
filter_price_sin = signal.filtfilt(b, a, year_on_year_price_basis_sin)
filter_price = signal.filtfilt(b, a, year_on_year_price_basis)  #画图
plt.figure(figsize=(10, 10))
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, \wspace=0.45, hspace=0.45)plt.subplot(711)
plt.title('原始信号',fontproperties=font_set)
plt.plot(year_on_year_price_basis[0:fft_count]) plt.subplot(712)
plt.title('原始sin信号',fontproperties=font_set)
plt.plot(year_on_year_price_basis_sin[0:fft_count]) plt.subplot(713)
plt.title("未归一化的棉棒图",fontproperties=font_set)
plt.stem(f_x, np.abs(fft)[0:len(f_x)], use_line_collection=True)plt.subplot(714)
plt.title("归一化的幅度谱",fontproperties=font_set)
plt.plot(f_x, np.abs(normalization_fft)[0:len(normalization_fft)])plt.subplot(715)
plt.title("还原信号",fontproperties=font_set)
restore_fft = np.fft.ifft(fft)
plt.plot(restore_fft)plt.subplot(716)
plt.title("滤波后的sin",fontproperties=font_set)
plt.plot(filter_price_sin)plt.subplot(717)
plt.title("滤波后原始",fontproperties=font_set)
year_on_year_month_basis = list(year_on_year_basis.keys())
plt.plot(filter_price)
plt.show()plt.plot(year_on_year_month_basis, filter_price)
fft_count 370
max_amp 0.14353, freq 0.29189, max_index 9
second_max_amp 0.00000, second_freq 0.00000, second_max_index 0
max_freq 9.00000
best_freq = 0.29798
bin = 0.032 Hz/Users/feivirus/opt/anaconda3/lib/python3.9/site-packages/matplotlib/cbook/__init__.py:1298: ComplexWarning: Casting complex values to real discards the imaginary partreturn np.asarray(x, float)

傅里叶变换分析A股大盘周期相关推荐

  1. 申宝概述周五A股大盘指数虽站稳3500点

    周五A股大盘指数虽站稳3500点,但指数继续保持缩量震荡,赚钱效应不够明显,个股涨多跌少,航空.酒店.船舶等行业出现涨幅榜前列,煤炭.钢铁等板块江河日下,全天量能比昨日继续萎缩.市场进入胶着状态,此消 ...

  2. java 傅里叶变换 频谱_傅里叶变换分析频谱(FFT)

    此前因为英语不好,看不下去help,进而懒得看demo,这才发现,原来Demo里面有如此多而有用的信息.学会了. 傅里叶变换的物理意义: 图像的频率是表征图像中灰度变化剧烈程度的指标,是灰度在平面空间 ...

  3. 快速傅里叶变换python_利用Python通过快速傅里叶变换分析太阳黑子活动的周期

    从网址 http://www.sidc.be/silso/datafiles 下载太阳黑子数据 (建议选择每月观测一次的数 据), 做傅里叶变换求太阳黑子的周期. 数据:http://www.sidc ...

  4. 周期信号的傅里叶级数展开

    傅里叶级数展开的定义 将一个周期信号分解为一个直流分量和一系列复指数信号分量之和的过程被称为傅里叶级数展开. 周期信号f(t)f(t)f(t)的傅里叶级数展开式为:f(t)=∑k=−∞∞ckejkw0 ...

  5. 从大盘数据看A股是否为政策市

    我国股市一向有"政策市"."消息市"的美誉.A股小道消息满天飞,各种概念层出不穷,只要站在消息.概念的台风口,落汤鸡也能变成金凤凰,即使是ST股也能连续拉升30 ...

  6. 大盘为何回血以及盐湖股份和科达制造

    1 科达制造和盐湖股份都还有想象空间. 科达制造主业很强,盐湖股份一季报更是爆的. 有读者说,科达制造和盐湖股份的卤水谈崩了,但是我是不太相信的. 科达的定增,我觉得问题并不大.科达主业陶瓷和建材机械 ...

  7. 炒股软件周期分析怎么调,股票周期设置怎么设置

    通达信软件如何设置多周期联动? 通达信炒股软件是一款定位于提供多功能服务的证券信息平台,由深圳财富趋势科技股份有限公司设计的一款移动证券软件.通达信允许用户自由划分屏幕,并规定每一块对应哪个内容. 应 ...

  8. 股市行情大盘走势最新解读 抢先布局选股有妙招

    股票市场的发展和存在趋势.中国股市(大陆地区)主要由上海证券交易所和深圳证券交易所的上市公司股票构成,如包含香港则亦包含香港联交所上市的公司.并有所谓"新三板"的柜台交易股票. 财 ...

  9. 【控制】傅里叶系列(一)傅里叶级数 (Fourier series) 的推导

    傅里叶级数 傅立叶级数 (Fourier series) 1.把一个周期函数表示成三角级数: 2.麦克劳林公式中的待定系数法: 3.三角函数的正交性: 4.函数展开成傅里叶级数: 傅立叶级数 (Fou ...

最新文章

  1. AndroidManifest.xml文件详解(activity)(三)四种工作模式
  2. Hive的基本操作-自定义函数
  3. C++ 面向对象(一)—— 类(Classes)
  4. mixin机制 vue_vue mixins组件复用的几种方式(小结)
  5. 分布式系统研发初体验
  6. 科大讯飞语音识别 支持多语音欢迎指点
  7. 关系型数据库管理系统(RDBMS)与非关系型数据库(NoSQL)之间的区别
  8. 如何获取每周的星期一和星期天的日期
  9. 哔哩哔哩视频下载助手
  10. opencc中文维基wiki百科简体繁体之间的转换解决
  11. 解决IE上登陆oracle OEM时报:“证书错误,导航已阻止”的错误
  12. ubuntu 18.04快捷显示桌面
  13. 代码自动生成-宏递归思想
  14. 我与CSDN的2021 --从路人到一名万粉博主的自述
  15. libuv 的Processes
  16. 目标检测网络的介绍及应用(一) -- 目标检测任务
  17. ISO七层模型和TCP/IP四层模型
  18. Code-server阿里云ECS服务器部署
  19. 诊所数字化:诊所医护人员绩效指标评估方式
  20. PHP代码从数据库中获取数据

热门文章

  1. 微信开发者工具进入页面卡死
  2. playmaker_Playmaker上的Spotlight:视觉脚本,可让您绕过代码并释放创造力
  3. c语言null和nullptr,NULL和nullptr
  4. scrapy+招聘网站爬虫笔记
  5. bagging与boosting的区别
  6. 选矿自动化及计算机应用,选矿自动化的历史进程表
  7. 浏览器IE6、IE7、IE8、css bug兼容性处理
  8. 微信一键登录解密手机号出现javax.crypto.BadPaddingException: pad block corrupted错误
  9. 解决Python安装alipay-sdk-python3.3.398遇到的错误
  10. 微信小程序吸底区域适配iPhone X