Chỉ số KDJ

Tác giả:Giấc mơ nhỏ, Tạo: 2017-01-16 15:00:09, Cập nhật: 2019-08-01 09:22:39

Chỉ số KDJ

Chỉ số KDJ, tên đầy đủ của chỉ số ngẫu nhiên (Stochastics), được tạo ra bởi Tiến sĩ George Lane. Nó kết hợp các khái niệm về động lực, điểm mạnh và điểm yếu của chỉ số KDJ, tính toán các mối quan hệ giữa giá cao nhất, giá thấp nhất và giá đóng cửa trong một chu kỳ nhất định.

  • Phương pháp tính toán: Đầu tiên tính giá trị RSV cho chu kỳ, sau đó tính giá trị K, giá trị D, giá trị J. Ví dụ như KDJ cho chu kỳ 9 ngày:

    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 đường trung bình di chuyển trơn 3 ngày, công thức là: Kt = RSVt/3 + 2 * t-1/3

    Đường trung bình di chuyển trượt 3 ngày với giá trị D là giá trị K, 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, và công thức là: Jt = 3Dt-2Kt

    Một số khía cạnh quan trọng cần được xem xét khi áp dụng chỉ số KDJ:

    Đánh giá của K và D, trong phạm vi 0-100 và 80 thị trường có quá nhiều mua và 20 thị trường có quá nhiều bán.

    2.买进信号:K值在上涨趋势中﹤D值,K线向上突破D线时;卖出信号:K值在下跌趋势中﹥D值,K线向下跌破D线。

    3.交易不活跃、发行量小的股票并不适用KD指标,而对大盘和热门大盘的准确性却很高。

    4.在KD处在高位或低位,如果出现与股价走向的背离,则是采取行动的信号。

    Đánh giá của J > 100 là mua quá mức, < 0 là bán quá mức, đều nằm trong vùng bất thường của giá.

    6.短期转势预警信号:K值和D值上升或者下跌的速度减弱,倾斜度趋于平缓

    Thông thường, ba giá trị K, D, J nằm trong khoảng 20-80 là vùng sương, đáng quan sát, về độ nhạy cảm, giá trị J mạnh nhất, K tiếp theo, chậm nhất là D, và về an toàn, ngược lại.

  • Mã chiến lược (không phải là mã định lượng của nhà phát minh)

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

Được chuyển từ Người giao dịch theo chương trình


Thêm nữa