KDJ là công cụ phân tích kỹ thuật được sử dụng phổ biến nhất trên thị trường tương lai và thị trường chứng khoán, được gọi là chỉ số ngẫu nhiên (Stochastics), được tạo ra bởi Tiến sĩ George Lane. KDJ kết hợp khái niệm động lực, chỉ số mạnh và yếu, một số lợi thế của chỉ số KDJ, được tính toán làm dữ liệu cơ bản thông qua mối quan hệ tỷ lệ giữa ba giá cao nhất, thấp nhất, giá và giá đóng cửa trong một khoảng thời gian nhất định, kết hợp giá trị K, giá trị D và giá trị J thành một đường cong, tạo thành chỉ số KDJ phản ánh xu hướng biến động giá.
RSVt=(Ct-L9)/(H9-L9)*100 (Ct = giá đóng cửa trong ngày; L9 = giá thấp nhất trong 9 ngày; H9 = giá cao nhất trong 9 ngày)
Giá trị K là giá trị RSV của trung bình di chuyển phẳng 3 ngày, công thức là: Kt = RSVt / 3 + 2*t-1⁄3
D là giá trị K của trung bình di chuyển 3 ngày, công thức là: Dt = Kt / 3 + 2*Dt-1⁄3
Giá trị J là 3 lần giá trị K trừ 2 lần giá trị D, công thức là: Jt = 3*Dt-2*Kt
Các yếu tố cần xem xét khi áp dụng KDJ:
Tín hiệu mua: K giá trị trong xu hướng tăng D giá trị, K đường đi xuống phá vỡ D đường.
Các cổ phiếu không hoạt động, ít được phát hành không áp dụng cho chỉ số KD, trong khi độ chính xác cao đối với thị trường lớn và thị trường lớn.
Trong khi KD ở mức cao hoặc thấp, nếu có sự lệch với giá cổ phiếu, đó là tín hiệu hành động.
Đánh giá của J> 100 là quá mua, < 0 là quá bán, đều thuộc vùng giá không bình thường.
Tín hiệu cảnh báo chuyển hướng ngắn hạn: K và D tăng hoặc giảm chậm hơn, nghiêng chậm lại
Thông thường, ba giá trị K, D và J nằm trong khoảng từ 20 đến 80, tốt để xem, về độ nhạy mạnh nhất là giá trị J, tiếp theo là K, chậm nhất là D, và về an toàn, ngược lại.
import numpy as np
import pandas as pd
from pandas import DataFrame
import talib as ta
start = '2006-01-01' # 回测起始时间
end = '2015-08-17' # 回测结束时间
benchmark = 'HS300' # 策略参考标准
universe = set_universe('HS300')
capital_base = 100000 # 起始资金
refresh_rate = 1 # 调仓频率,即每 refresh_rate 个交易日执行一次 handle_data() 函数
longest_history=20
MA=[5,10,20,30,60,120] #移动均线参数
def initialize(account):
account.kdj=[]
def handle_data(account):
# 每个交易日的买入卖出指令
sell_pool=[]
hist = account.get_history(longest_history)
#data=DataFrame(hist['600006.XSHG'])
stock_pool,all_data=Get_all_indicators(hist)
pool_num=len(stock_pool)
if account.secpos==None:
print 'null'
for i in stock_pool:
buy_num=int(float(account.cash/pool_num)/account.referencePrice[i]/100.0)*100
order(i, buy_num)
else:
for x in account.valid_secpos:
if all_data[x].iloc[-1]['closePrice']<all_data[x].iloc[-1]['ma1'] and (all_data[x].iloc[-1]['ma1']-all_data[x].iloc[-1]['closePrice'])/all_data[x].iloc[-1]['ma1']>0.05 :
sell_pool.append(x)
order_to(x, 0)
if account.cash>500 and pool_num>0:
try:
sim_buy_money=float(account.cash)/pool_num
for l in stock_pool:
#print sim_buy_money,account.referencePrice[l]
buy_num=int(sim_buy_money/account.referencePrice[l]/100.0)*100
#buy_num=10000
order(l, buy_num)
except Exception as e:
#print e
pass
def Get_kd_ma(data):
indicators={}
#计算kd指标
indicators['k'],indicators['d']=ta.STOCH(np.array(data['highPrice']),np.array(data['lowPrice']),np.array(data['closePrice']),\
fastk_period=9,slowk_period=3,slowk_matype=0,slowd_period=3,slowd_matype=0)
indicators['ma1']=pd.rolling_mean(data['closePrice'], MA[0])
indicators['ma2']=pd.rolling_mean(data['closePrice'], MA[1])
indicators['ma3']=pd.rolling_mean(data['closePrice'], MA[2])
indicators['ma4']=pd.rolling_mean(data['closePrice'], MA[3])
indicators['ma5']=pd.rolling_mean(data['closePrice'], MA[4])
indicators['closePrice']=data['closePrice']
indicators=pd.DataFrame(indicators)
return indicators
def Get_all_indicators(hist):
stock_pool=[]
all_data={}
for i in hist:
try:
indicators=Get_kd_ma(hist[i])
all_data[i]=indicators
except Exception as e:
#print 'error:%s'%e
pass
if indicators.iloc[-2]['k']<indicators.iloc[-2]['d'] and indicators.iloc[-1]['k']>indicators.iloc[-2]['d']:
stock_pool.append(i)
elif indicators.iloc[-1]['k']>=10 and indicators.iloc[-1]['d']<=20 and indicators.iloc[-1]['k']>indicators.iloc[-2]['k'] and indicators.iloc[-2]['k']<indicators.iloc[-3]['k']:
stock_pool.append(i)
return stock_pool,all_data
Tải về từ Programmable Trader