선물 및 주식 시장에서 가장 많이 사용되는 기술 분석 도구는 KDJ 지표, 무작위 지표 ((Stochastics), 조지 레인 박사 (George Lane) 에 의해 만들어졌다. 동력 개념, 강약 지표의 몇 가지 장점을 결합한 KDJ 지표는, 특정 기간 동안 발생한 최고 가격, 최저 가격, 폐쇄 가격 세 가지 사이의 비율 관계를 기본 데이터로 계산하여, K값, D값 및 J 값을 연결하여 곡선 도표를 형성합니다. 이는 가격 변동 동향을 반영하는 KDJ 지표를 형성합니다.
RSVt=(Ct-L9)/(H9-L9)*100 (Ct = 당일 종점 가격; L9 = 9 일 최저 가격; H9 = 9 일 최고 가격)
K값은 RSV값 3일 평평한 이동 평균, 공식은: Kt=RSVt/3+2*t-1⁄3
D값이 K값인 3일 평평한 이동 평균, 공식은: Dt=Kt/3+2*Dt-1⁄3
J값은 3배 K값 빼기 2배 D값으로, 공식은: Jt=3*Dt-2*Kt
KDJ 지표가 적용될 때 고려해야 할 몇 가지 주요 사항은:
K와 D의 평가, 범위는 0-100입니다. 80 이상 상가는 과매매 현상을 나타냅니다. 20 이하는 과매매 현상을 나타냅니다.
구매 신호: K값이 상승 추세에서 D값, K선 아래로 D선을 돌파할 때.
거래가 활발하지 않고, 발행량이 작은 주식은 KD 지표에 적용되지 않으며, 대시장과 인기 대시장에 대한 정확성은 높다.
KD에서 높은 지점이나 낮은 지점에서 주가 방향과 오차가 나타나면 행동 신호입니다.
J의 평가> 100은 과매매, 은 과매매, 모두 가격의 비정상적인 영역에 속한다.
단기 전환 경고 신호: K 및 D 값의 상승 또는 하락 속도가 약화되고 기울기가 느려집니다.
일반적으로 K, D, J 삼위 값은 20-80 사이를 경계선으로 하고, 감수성 측면에서 가장 강한 것은 J 값이며, 그 다음에는 K, 가장 느린 것은 D가 되고, 안전성 측면에서 바로 반대이다.
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
[프로그래밍 트레이더]