搭建环境:

  • centos7.5
  • Anaconda 3.6.4
  • tushare
  • python talib及C基础库依赖

实现的代码如下:

  1#!/usr/bin/env python
  2# coding=utf8
  3# ===============================================================================
  4#   Copyright (C) 2019 www.361way.com site All rights reserved.
  5#
  6#   Filename      :kdjall.py
  7#   Author        :yangbk <itybku@139.com>
  8#   Create Time   :2019-07-17 17:53
  9#   Description   :
 10# ===============================================================================
 11import requests
 12import json
 13import talib as ta
 14import tushare as ts
 15import pandas as pd
 16def gettoken(corpid,corpsecret):
 17    gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
 18    r = requests.get(gettoken_url)
 19    request_json = r.json()
 20    this_access_token = request_json['access_token']
 21    #print(this_access_token)
 22    r.close()
 23    return this_access_token
 24def senddata(access_token,content):
 25    send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
 26    send_values = {
 27        "touser":user,    #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
 28        "toparty":"",     #企业号中的部门id。
 29        "msgtype":"text", #消息类型。
 30        "agentid":"1000002",    #企业号中的应用id。
 31        "text":{
 32            #"content":subject + '\n' + content
 33            "content":content
 34           },
 35        "safe":"0"
 36        }
 37    #send_data = json.dumps(send_values, ensure_ascii=False)
 38    send_data = json.dumps(send_values)
 39    #print(send_data)
 40    r = requests.post(send_url, send_data)
 41    print('post success %s ' % r.text)
 42def Get_Stock_List():  # 获取所有stock列表,不过其代码在第一列,需要使用df.index获取
 43    df = ts.get_stock_basics()
 44    return df
 45def K_Data(code):
 46  dw = ts.get_k_data(code)
 47  # 排除新股
 48  if len(dw) > 60:
 49    dw = dw[-58:]
 50    return dw
 51def Get_KDJ(dw):
 52    dw['slowk'], dw['slowd'] = ta.STOCH(dw['high'].values,
 53                            dw['low'].values,
 54                            dw['close'].values,
 55                            fastk_period=9,
 56                            slowk_period=3,
 57                            slowk_matype=0,
 58                            slowd_period=3,
 59                            slowd_matype=0)
 60    df = pd.DataFrame(data=dw)
 61    #row = df.iloc[-10:].values
 62    v1 = df.iloc[-1].values
 63    v2 = df.iloc[-2].values
 64    k1,d1 = v1[-2:]
 65    k2,d2 = v2[-2:]
 66    if (k2 < 22) and (d2 < 22) and (k1 > d1) and (k1 > k2) and (d1 > d2) and (k1 < 38):
 67      return(code)
 68def Get_MACD(dw):
 69    close = dw.close.values
 70    dw['macd'], dw['macdsignal'], dw['macdhist'] = ta.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9)
 71    df = pd.DataFrame(data=dw)
 72    row3,row2,row1 = df.iloc[-3:].values
 73    md1,mds1,mdh1 = row1[-3:]
 74    md2,mds2,mdh2 = row2[-3:]
 75    md3,mds3,mdh3 = row3[-3:]
 76    if (md3< md2 <md1) and ( -0.4 < md1) and (mdh2 < mdh1):
 77        return code
 78codes = []
 79df = Get_Stock_List()
 80for code in df.index:
 81  #print('check ' + code + '  kdj and macd now:')
 82  dw = ts.get_k_data(code)
 83  # 排除新股
 84  if len(dw) > 60:
 85     dw = dw[-56:]
 86     kcode = Get_KDJ(dw)
 87     mcode = Get_MACD(dw)
 88     if (kcode != None) and (mcode != None):
 89        codes.append(code)
 90print(codes)
 91print(len(codes))
 92df = ts.get_realtime_quotes(codes)
 93#print(df)
 94#print(df[['code','name','price','bid','ask','volume','amount','time']])
 95print(df[['code','name','price','low','high','time']])
 96content = df[['code','name','price','low','high','time']]
 97content = content.to_string()
 98user = 'YangBaoKu'
 99corpid =  'ww814f1552c46b3243'
100corpsecret = '1pXJaRU9jN6ALOdRywb2PmEyElVbUpVuqkbFMaYBEBc'
101try:
102    accesstoken = gettoken(corpid,corpsecret)
103    senddata(accesstoken,content)
104except Exception as e:
105    print('Exception: ', e)

这里还可以加入第三个条件,成交量放大的,成交量放大的,可以个修改macd中的部分相关值,其条件部分修改如下:

1obv1,scode1,md1,mds1,mdh1 = row1[-5:]
2obv2,scode2,md2,mds2,mdh2 = row2[-5:]
3obv3,scode3,md3,mds3,mdh3 = row3[-5:]
4if (md3< md2 <md1) and ( -0.4 < md1) and (mdh2 < mdh1) and (obv3 < obv1) and (obv3 < obv2):
5    return code

if (md3< md2 三者条件如下:

1#   Description   :kdj低位金叉,macd即将金叉或金叉,近两天成交量大于前天的成交量
2KDJ : (k2 < 22) and (d2 < 22) and (k1 > d1) and (k1 > k2) and (d1 > d2) and (k1 < 35)
3MACD : (md3< md2 <md1) and ( -0.4 < md1) and (mdh2 < mdh1)
4OBV : (obv3 < obv1) and (obv3 < obv2)

如果这三个条件无数据生成,可以适当修改下kdj的k值大小。