KDJ回测

# -*- coding: utf-8 -*-

import os
import pandas as pd

# ========== 遍历数据文件夹中所有股票文件的文件名,得到股票代码列表stock_code_list
stock_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,统计这些股票在未来交易日中的收益情况
#print
print('历史上所有股票出现KDJ金叉的次数为%d,这些股票在:' %all_stock.shape[0])
#print

for 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
原文地址:https://www.cnblogs.com/wumac/p/6240934.html