代码拉取完成,页面将自动刷新
同步操作将从 macroan/traderStock-gui 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#coding=utf-8
import talib as ta
import pandas as pd
import numpy as np
import config
import volume
import public
class Kdj:
__instance = None
def __init__(self):
pass
@classmethod
def getInstance(cls):
if(cls.__instance == None):
cls.__instance = Kdj()
return cls.__instance
#通过KDJ判断买入卖出
def get_kdj_buy(self, df):
df = df.sort_values('date', ascending=True)
low_list = pd.rolling_min(df['low'], 9)
low_list.fillna(value=pd.expanding_min(df['low']), inplace=True)
high_list = pd.rolling_max(df['high'], 9)
high_list.fillna(value=pd.expanding_max(df['high']), inplace=True)
rsv = (df['close'] - low_list) / (high_list - low_list) * 100
#18-20 K D J
df['kdj_k'] = pd.ewma(rsv, com=2)
df['kdj_d'] = pd.ewma(df['kdj_k'], com=2)
df['kdj_j'] = 3 * df['kdj_k'] - 2 * df['kdj_d']
slowkMA5 = ta.MA(df['kdj_k'].values, timeperiod=5, matype=0)
slowkMA10 = ta.MA(df['kdj_k'].values, timeperiod=10, matype=0)
slowkMA20 = ta.MA(df['kdj_k'].values, timeperiod=20, matype=0)
slowdMA5 = ta.MA(df['kdj_d'].values, timeperiod=5, matype=0)
slowdMA10 = ta.MA(df['kdj_d'].values, timeperiod=10, matype=0)
slowdMA20 = ta.MA(df['kdj_d'].values, timeperiod=20, matype=0)
df = df.sort_values('date', ascending=False)
operate = 0
'''
1.K与D值永远介于0到100之间。D大于80时,行情呈现超买现象。D小于20时,行情呈现超卖现象。
2.上涨趋势中,K值小于D值,K线向上突破D线时,为买进信号。下跌趋势中,K值大于D值,K线向下跌破D线时,为卖出信号。
3.KD指标不仅能反映出市场的超买超卖程度,还能通过交叉突破发出买卖信号。
4.KD指标不适于发行量小、交易不活跃的股票,但是KD指标对大盘和热门大盘股有极高准确性。
5.当随机指标与股价出现背离时,一般为转势的信号。
6.K值和D值上升或者下跌的速度减弱,倾斜度趋于平缓是短期转势的预警信号。
#1.K线是快速确认线——数值在90以上为超买,数值在10以下为超卖;D大于80时,
#行情呈现超买现象。D小于20时,行情呈现超卖现象。
'''
if df.iat[0,17]<=10:
operate = operate + 3
#elif df.iat[0,17]>=90:
#operate = operate - 3
if df.iat[0,18]<=20:
operate = operate + 3
#elif df.iat[0,18]>=80:
#operate = operate - 3
#2.上涨趋势中,K值大于D值,K线向上突破D线时,为买进信号。
if df.iat[0,17]> df.iat[0,18] and df.iat[1,17]<=df.iat[0,18]:
operate = operate + 10
#下跌趋势中,K小于D,K线向下跌破D线时,为卖出信号。#待修改
#elif df.iat[0, 17]< df.iat[0,18] and df.iat[1,17]>=df.iat[1,18]:
#operate = operate - 10
#3.当随机指标与股价出现背离时,一般为转势的信号。
if df.iat[0,7]<=df.iat[0,8] and df.iat[0,8]<=df.iat[0,9]:#K线下降
if (slowkMA5[0]>=slowkMA10[0] and slowkMA10[0]>=slowkMA20[0]) or \
(slowdMA5[0]>=slowdMA10[0] and slowdMA10[0]>=slowdMA20[0]): #K,D上涨
operate = operate + 1
#elif df.iat[ 0,7]>=df.iat[ 0,8] and df.iat[ 0,8]>=df.iat[ 0,9]:#K线上涨
#if (slowkMA5[0]<=slowkMA10[0] and slowkMA10[0]<=slowkMA20[0]) or \
#(slowdMA5[0]<=slowdMA10[0] and slowdMA10[0]<=slowdMA20[0]): #K,D下降
#operate = operate - 1
return operate, df
#计算k d j的值,gc:金叉 dc:死叉 R:最近的,A:历史所有
def set_kdj_data(self, df, type=1):
if df is not None and df.shape[0] > 0:
if type == 1:
low_list = df['low'].rolling(window=9,center=False).min()
low_list.fillna(value=df['low'].expanding(min_periods=1).min(), inplace=True)
high_list = df['high'].rolling(window=9,center=False).max()
high_list.fillna(value=df['high'].expanding(min_periods=1).max(), inplace=True)
rsv = (df['close'] - low_list) / (high_list - low_list) * 100
df['kdj_k'] = rsv.ewm(ignore_na=False,min_periods=0,adjust=True,com=2).mean()
df['kdj_d'] = df['kdj_k'].ewm(ignore_na=False,min_periods=0,adjust=True,com=2).mean()
df['kdj_j'] = 3 * df['kdj_k'] - 2 * df['kdj_d']
else:
k,d, j = self.KDJ_CN(df['high'], df['low'], df['close'], 9, 3, 3)
df['kdj_k'] = k
df['kdj_d'] = d
df['kdj_j'] = j
#计算kdjgc、d_gc情况
df['kdj_cross'] = ''
kdj_position = df['kdj_k'] > df['kdj_d']
df.loc[kdj_position[(kdj_position == True) & (kdj_position.shift() == False)].index, 'kdj_cross'] = 'gc'
df.loc[kdj_position[(kdj_position == False) & (kdj_position.shift() == True)].index, 'kdj_cross'] = 'dc'
#df.dropna(how='any', inplace=True)#删除所有空行值的数据
return df
# 同花顺和通达信等软件中的SMA
def SMA_CN(self, close, timeperiod) :
close = np.nan_to_num(close)
return reduce(lambda x, y: ((timeperiod - 1) * x + y) / timeperiod, close)
# 同花顺和通达信等软件中的KDJ
def KDJ_CN(self, high, low, close, fastk_period, slowk_period, fastd_period) :
kValue, dValue = ta.STOCHF(high, low, close, fastk_period, fastd_period=1, fastd_matype=0)
kValue = np.array(map(lambda x : self.SMA_CN(kValue[:x], slowk_period), range(1, len(kValue) + 1)))
dValue = np.array(map(lambda x : self.SMA_CN(kValue[:x], fastd_period), range(1, len(kValue) + 1)))
jValue = 3 * kValue - 2 * dValue
func = lambda arr : np.array([0 if x < 0 else (100 if x > 100 else x) for x in arr])
kValue = func(kValue)
dValue = func(dValue)
jValue = func(jValue)
return kValue, dValue, jValue
#计算k d j的值,gc:金叉 dc:死叉
def set_us_kdj_data(self, df):
if df is not None and df.shape[0] > 0:
low_list = df['Close'].rolling(window=9,center=False).min()
low_list.fillna(value=df['Low'].expanding(min_periods=1).min(), inplace=True)
high_list = df['High'].rolling(window=9,center=False).max()
high_list.fillna(value=df['High'].expanding(min_periods=1).max(), inplace=True)
rsv = (df['Close'] - low_list) / (high_list - low_list) * 100
df['kdj_k'] = rsv.ewm(ignore_na=False,min_periods=0,adjust=True,com=2).mean()
df['kdj_d'] = df['kdj_k'].ewm(ignore_na=False,min_periods=0,adjust=True,com=2).mean()
df['kdj_j'] = 3 * df['kdj_k'] - 2 * df['kdj_d']
'''
import talib
slowk, slowd = talib.STOCH(df['High'].values,
df['Low'].values,
df['Close'].values,
fastk_period=9,
slowk_period=3,
slowk_matype=0,
slowd_period=3,
slowd_matype=0)
df['kdj_k'] = slowk
df['kdj_d'] = slowd
df['kdj_j'] = 3 * df['kdj_k'] - 2 * df['kdj_d']
'''
#计算kdjgc、d_gc情况
df['kdj_cross'] = ''
kdj_position = df['kdj_k'] > df['kdj_d']
df.loc[kdj_position[(kdj_position == True) & (kdj_position.shift() == False)].index, 'kdj_cross'] = 'gc'
df.loc[kdj_position[(kdj_position == False) & (kdj_position.shift() == True)].index, 'kdj_cross'] = 'dc'
return df
#是否
def is_cross(self, df, index, crosstype='gc'):
if index < 0 or index > df.shape[0]-1:
return False
cross_ret = {'gc':df.iloc[index]['kdj_cross']=='gc', 'dc':df.iloc[index]['kdj_cross'] == 'dc'}
return cross_ret[crosstype]
#历史上所有KDJgc N个交易日的平均涨幅与成功率
def kdj_glodencross(df):
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
for i in xrange(0, dflen):
if df.iloc[i]['kdj_cross'] == 'gc':
meettotalCount = public.compute_succ_zf(df, i, meettotalCount, meettotal)
return meettotalCount, meettotal
#历史上所有KDJgc并成交量放大N个交易日的平均涨幅与成功率
def kdj_glodencross_volume(df):
v = volume.Volume.getInstance()
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
for i in xrange(0, dflen):
if df.iloc[i]['kdj_cross'] == 'gc' and v.is_fangliang(df, i):
meettotalCount = public.compute_succ_zf(df, i, meettotalCount, meettotal)
return meettotalCount, meettotal
#历史上所有KDJ低位gcN个交易日的平均涨幅与成功率
def kdj_low_glodencross(df):
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
for i in xrange(0, dflen):
if df.iloc[i]['kdj_cross'] == 'gc' and df.iloc[i]['kdj_d'] <= 20 and df.iloc[i]['kdj_k'] <= 20:
meettotalCount = public.compute_succ_zf(df, i, meettotalCount, meettotal)
return meettotalCount, meettotal
#历史上所有KDJ低位gc并成交量放大N个交易日的平均涨幅与成功率
def kdj_low_glodencross_volume(df):
v = volume.Volume.getInstance()
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
for i in xrange(0, dflen):
if df.iloc[i]['kdj_cross'] == 'gc' and df.iloc[i]['kdj_d'] <= 20 and\
df.iloc[i]['kdj_k'] <= 20 and v.is_fangliang(df, i):
meettotalCount = public.compute_succ_zf(df, i, meettotalCount, meettotal)
return meettotalCount, meettotal
#历史上所有KDJ低位二次gcN个交易日的平均涨幅与成功率
def kdj_lowtwo_glodencross(df):
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
target = 0
start = 0
for i in xrange(0, dflen):
if start == 0 and df.iloc[i]['kdj_cross'] == 'gc' and df.iloc[i]['kdj_d'] <= 20 and df.iloc[i]['kdj_k'] <= 20:
target = i
start = 1
continue
if i-target == 10:
target = 0
start = 0
continue
if start == 1 and df.iloc[i]['kdj_cross'] == 'gc' and df.iloc[i]['kdj_k'] <=20 and df.iloc[i]['kdj_d'] <=20:
target = 0
start = 0
meettotalCount = public.compute_succ_zf(df, target, meettotalCount, meettotal)
return meettotalCount, meettotal
#历史上所有KDJ低位二次gcN个交易日的平均涨幅与成功率
def kdj_lowtwo_glodencross_volume(df):
v = volume.Volume.getInstance()
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
target = 0
start = 0
for i in xrange(0, dflen):
if start == 0 and df.iloc[i]['kdj_cross'] == 'gc' and df.iloc[i]['kdj_d'] <= 20 and \
df.iloc[i]['kdj_k'] <= 20 and v.is_fangliang(df, i):
target = i
start = 1
continue
if i-target == 10:
target = 0
start = 0
continue
if start == 1 and df.iloc[i]['kdj_cross'] == 'gc' and df.iloc[i]['kdj_k'] <=20 and df.iloc[i]['kdj_d'] <=20:
target = 0
start = 0
meettotalCount = public.compute_succ_zf(df, target, meettotalCount, meettotal)
return meettotalCount, meettotal
#历史上所有KDJ徘徊区gcN个交易日的平均涨幅与成功率
def kdj_mid_glodencross(df):
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
for i in xrange(0, dflen):
if df.iloc[i]['kdj_cross'] == 'gc' and df.iloc[i]['kdj_d'] > 20 and df.iloc[i]['kdj_d'] < 80:
meettotalCount = public.compute_succ_zf(df, i, meettotalCount, meettotal)
return meettotalCount, meettotal
#历史上所有KDJ低位gc并成交量放大N个交易日的平均涨幅与成功率
def kdj_mid_glodencross_volume(df):
v = volume.Volume.getInstance()
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
for i in xrange(0, dflen):
if df.iloc[i]['kdj_cross'] == 'gc' and df.iloc[i]['kdj_d'] > 20 and\
df.iloc[i]['kdj_d'] < 80 and v.is_fangliang(df, i):
meettotalCount = public.compute_succ_zf(df, i, meettotalCount, meettotal)
return meettotalCount, meettotal
#历史上所有KDJ高位gcN个交易日的平均涨幅与成功率
def kdj_high_glodencross(df):
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
for i in xrange(0, dflen):
if df.iloc[i]['kdj_cross'] == 'gc' and df.iloc[i]['kdj_d'] >= 80:
meettotalCount = public.compute_succ_zf(df, i, meettotalCount, meettotal)
return meettotalCount, meettotal
#历史上所有KDJ低位gc并成交量放大N个交易日的平均涨幅与成功率
def kdj_high_glodencross_volume(df):
v = volume.Volume.getInstance()
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
for i in xrange(0, dflen):
if df.iloc[i]['kdj_cross'] == 'gc' and df.iloc[i]['kdj_d'] >= 80 and v.is_fangliang(df, i):
meettotalCount = public.compute_succ_zf(df, i, meettotalCount, meettotal)
return meettotalCount, meettotal
#历史上所有KDJ高位二次gcN个交易日的平均涨幅与成功率
def kdj_hightwo_glodencross(df):
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
target = 0
start = 0
for i in xrange(0, dflen):
if start == 0 and df.iloc[i]['kdj_cross'] == 'gc' and df.iloc[i]['kdj_d'] >= 80 and df.iloc[i]['kdj_k'] >= 80:
target = i
start = 1
continue
if i - target == 10:
target = 0
start = 0
continue
if start == 1 and df.iloc[i]['kdj_cross'] == 'gc' and df.iloc[i]['kdj_k'] >=80 and df.iloc[i]['kdj_d'] >= 80:
target = 0
start = 0
meettotalCount = public.compute_succ_zf(df, target, meettotalCount, meettotal)
return meettotalCount, meettotal
#历史上所有KDJ高位二次gc放量N个交易日的平均涨幅与成功率
def kdj_hightwo_glodencross_volume(df):
v = volume.Volume.getInstance()
meettotalCount, meettotal = public.init_succ_var()
dflen = df.shape[0]
target = 0
start = 0
for i in xrange(0, dflen):
if start == 0 and df.iloc[i]['kdj_cross'] == 'gc' and df.iloc[i]['kdj_d'] >= 80 \
and df.iloc[i]['kdj_k'] >= 80 and v.is_fangliang(df, i):
target = i
start = 1
continue
if i - target == 10:
target = 0
start = 0
continue
if start == 1 and df.iloc[i]['kdj_cross'] == 'gc' and df.iloc[i]['kdj_k'] >=80 and df.iloc[i]['kdj_d'] >= 80:
target = 0
start = 0
meettotalCount = public.compute_succ_zf(df, target, meettotalCount, meettotal)
return meettotalCount, meettotal
#======================================kdj选股====================================
#kdj 低位二次gc选股
def kdj_lowtwo_gc(df, code):
retcode = 0
dflen = df.shape[0]
if df.iloc[0]['kdj_cross'] == 'gc' and df.iloc[0]['kdj_k'] <=20 and df.iloc[0]['kdj_d'] <= 20:
for j in range(1,10):
if j < dflen:
if df.iloc[j]['kdj_cross'] == 'gc' and df.iloc[j]['kdj_k'] <=20 and df.iloc[j]['kdj_d'] <= 20:
retcode = code
break
return retcode
kdj_xuangu_func = {
'kdj_lowtwo_gc':[kdj_lowtwo_gc, u'kdj低位二次金叉'],
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。