KDJ回測

來源:互聯網
上載者:User

標籤:frame   log   span   false   blog   dex   排序   drop   比例   

# -*- coding: utf-8 -*-import osimport pandas as pd# ========== 遍曆資料檔案夾中所有股票檔案的檔案名稱,得到股票代號列表stock_code_liststock_code_list = []for root, dirs, files in os.walk(‘all_stock_data‘):# 注意:這裡請填寫資料檔案在您電腦中的路徑    if files:        for f in files:            if ‘.csv‘ in f:                stock_code_list.append(f.split(‘.csv‘)[0])# ========== 根據上一步得到的代碼清單,遍曆所有股票,將這些股票合并到一張表格all_stock中all_stock = pd.DataFrame()# 遍曆每個創業板的股票for code in stock_code_list:    print(code)    # 從csv檔案中讀取該股票資料    stock_data = pd.read_csv(‘all_stock_data/‘ + code + ‘.csv‘,                             parse_dates=[1],encoding=‘gbk‘)    stock_data.sort_values(‘日期‘, inplace=True)    # 計算KDJ指標    low_list=stock_data[‘最低價‘].rolling(window=9).min()    low_list.fillna(value=stock_data[‘最低價‘].expanding().min(), inplace=True)    high_list = stock_data[‘最高價‘].rolling(window=9).max()    high_list.fillna(value=stock_data[‘最高價‘].expanding().max(), inplace=True)    rsv = (stock_data[‘收盤價‘] - low_list) / (high_list - low_list) * 100    stock_data[‘KDJ_K‘] = rsv.ewm(com=2).mean()    stock_data[‘KDJ_D‘] = stock_data[‘KDJ_K‘].ewm(com=2).mean()    stock_data[‘KDJ_J‘] = 3 * stock_data[‘KDJ_K‘] - 2 * stock_data[‘KDJ_D‘]    # 計算KDJ指標金叉、死叉情況    stock_data[‘KDJ_金叉死叉‘] = ‘‘    kdj_position = stock_data[‘KDJ_J‘] > 0    p2 = stock_data[‘成交量‘] < stock_data[‘成交量‘].shift()#shift()表示昨天如果按早到晚排序,參數為正,表示前些天,參數為負,表現後些天    stock_data.loc[kdj_position[(kdj_position == True) & (kdj_position.shift() == False)&p2==True].index, ‘KDJ_金叉死叉‘] = ‘金叉‘    stock_data.loc[kdj_position[(kdj_position == False) & (kdj_position.shift() == True)].index, ‘KDJ_金叉死叉‘] = ‘死叉‘    # 計算接下來幾個交易日的收益率    for n in [1, 2, 3, 5, 10, 20]:        stock_data[‘接下來‘+str(n)+‘個交易日漲跌幅‘] = stock_data[‘收盤價‘].shift(-1*n) / stock_data[‘收盤價‘] - 1.0    # 刪除所有有空值的資料行    stock_data.dropna(how=‘any‘, inplace=True)    # 篩選出KDJ金叉的資料,並將這些資料合併到all_stock中    stock_data = stock_data[(stock_data[‘KDJ_金叉死叉‘] == ‘金叉‘)]    if stock_data.empty:        continue    all_stock = all_stock.append(stock_data, ignore_index=True)# ========== 根據上一步得到的所有股票KDJ金叉資料all_stock,統計這些股票在未來交易日中的收益情況#printprint(‘曆史上所有股票出現KDJ金叉的次數為%d,這些股票在:‘ %all_stock.shape[0])#printfor n in [1, 2, 3, 5, 10, 20]:    print("金叉之後的%d個交易日內," % n)    print("平均漲幅為%.2f%%," % (all_stock[‘接下來‘+str(n)+‘個交易日漲跌幅‘].mean() * 100))    print("其中上漲股票的比例是%.2f%%。" %           (all_stock[all_stock[‘接下來‘+str(n)+‘個交易日漲跌幅‘] > 0].shape[0]/float(all_stock.shape[0]) * 100))    #print

 

KDJ回測

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.