avatar of 发明者量化-小小梦 发明者量化-小小梦
フォロー ダイレクトメッセージ
4
フォロー
1271
フォロワー

定量的取引戦略 - KDJ インジケーター

作成日:: 2017-01-16 15:00:09, 更新日:: 2019-08-01 09:22:39
comments   0
hits   3915

定量的取引戦略 - KDJ インジケーター

期貨と株式市場で最もよく使用される技術分析ツールであるKDJ指数,全名ランダム指数 ((Stochastics),ジョージ・レーン博士によって作成された.動力の概念と強弱指数のいくつかの優位性を融合したKDJ指数は,特定の周期で発生した最高価格,最低価格,閉店価格の3つの間の比率関係を基本データとして計算し,K値,D値とJ値を曲線図に結び,価格変動の傾向を反映したKDJ指数を形成する.

  • #### 計算方法:最初に周期のRSV値を計算し,次にK値,D値,J値を計算する.例えば,9日周期のKDJ:

RSVt=(Ct-L9)/(H9-L9)*100 (Ct=当日の閉店価格;L9=9日間の最低価格;H9=9日間の最高価格)

K値はRSV値3日平滑移動平均で,公式は:Kt=RSVt/3+2*t-13

DはK値の3日平滑移動平均で,公式はDt=Kt/3+2*Dt-13

J値は3倍K値減2倍D値で,公式は:Jt=3*Dt-2*Kt

KDJ指数は,KDJ指数で表される数値の1つで,

1.KとDの値化,範囲は0-100,80以上は買い過ぎ,20以下は売り過ぎ.

  1. 買入シグナル:K値が上昇傾向にあるときD値,K線がD線を下落して突破する時

  2. 取引が活発でない,発行量が少ない株はKD指数には適用されないが,大市場と熱門大市場には高い精度がある.

  3. KDの高値や低値で,株価の方向から逸脱した場合は,行動するシグナルである.

5.Jの値付け>100は超買い,は超売れ,どちらも価格の異常領域に属している.

  1. 短期転換警告信号: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

プログラム化されたトレーダー