├── .gitignore ├── BOLL.py ├── CCI.py ├── InOutRatio.py ├── MTM.py ├── OBV.py ├── README.md ├── TRIX.py ├── minute_k.py ├── plan_AddRate.py ├── rate.py ├── stock_detail.py └── stock_rate.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask stuff: 57 | instance/ 58 | .webassets-cache 59 | 60 | # Scrapy stuff: 61 | .scrapy 62 | 63 | # Sphinx documentation 64 | docs/_build/ 65 | 66 | # PyBuilder 67 | target/ 68 | 69 | # IPython Notebook 70 | .ipynb_checkpoints 71 | 72 | # pyenv 73 | .python-version 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # dotenv 79 | .env 80 | 81 | # virtualenv 82 | venv/ 83 | ENV/ 84 | 85 | # Spyder project settings 86 | .spyderproject 87 | 88 | # Rope project settings 89 | .ropeproject 90 | -------------------------------------------------------------------------------- /BOLL.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | BOLL(布林带) 5 | 计算方法: 6 | 1、计算中轨 7 | MB=(当前k线收盘价+前k线收盘价+...+前N-1根k线收盘价)/N N默认为20 8 | 2、计算标准差 9 | 先计算N根K线差额的平方和 10 | sum=(当前K线收盘价-当前k线MB)^2+(前K线收盘价-前k线MB)^2+...+(第N根K线收盘价-第N根k线MB)^2 11 | 再计算标准差(Sqrt表示开方,计算平方根) 12 | MD=K*Sqrt(sum/N) K为标准差值,默认值为2;N默认为20 13 | 3、计算上轨、下轨 14 | UP=MB+MD (上轨值计算) 15 | DN=MB-MD (下轨值计算) 16 | """ 17 | 18 | import requests,math,os 19 | 20 | def Get_close_allday(stock_code): 21 | allday=[] #日期 22 | close=[] #收盘价数组 23 | url = 'http://mk.api.guxiansheng.cn/quote/?mod=quote&' \ 24 | 'method=kline_data&begindate=&' \ 25 | 'finalCode=' + stock_code + '&fqtype=&num=200&' \ 26 | 'type=day&appid=gxs' #参数中num为获取日K的点数,200为200个日期点 27 | r = requests.get(url) 28 | dates = r.json()['data']['timeZ'] #获取日K的所有日期 29 | datasdetail = r.json()['data']['datas'] #获取日K中有点的数据明细 30 | 31 | for datadetail in datasdetail: 32 | close.append(round((datadetail["NPRI"]),2)) #循环遍历,把明细中的收盘价存在Close数组中 33 | 34 | for data in dates: 35 | allday.append(int(data.replace('-',''))) #循环遍历,把交易日期存在allday数组中 36 | # print close[-20:] 37 | return close,allday 38 | 39 | def Get_Boll(close,allday): 40 | L = len(close) #计算收盘价数组长度,用于处理MID20的值 41 | N=20 #默认N值为0 42 | start = 0 #数组分割,起始分割索引为:0 43 | end = 20 #数组分割,结束索引为:20 与N值相同 44 | 45 | while True: #循环遍历:遍历指定日K长度,用于计算每个日期的MID值 46 | for price in range(0,len(close)): 47 | sum_close = 0 48 | # print start,end,price 49 | MID=round(sum(close[start:end])/N,2) #MB的计算公式:计算方法为收盘价数组中,最后一个点的收盘价加前19个点的 50 | #收盘价只和,再除以20;每循环一次:最后一个点位移一个点 51 | for i in range(0,20): 52 | # print close[price+i] 53 | sum_close= sum_close + round(abs((close[price+i]-MID)**2),2) 54 | UPPER = MID + 2 * round(math.sqrt(sum_close/20),2) 55 | LOWER = MID - (UPPER-MID) 56 | 57 | f.write('日期:' + str(allday[price]) + '的MID值为:' + str(MID) + 58 | ';UPPER值为:' + str(UPPER) + 59 | ';LOWER值为:' + str(LOWER) + "\n") #计算结果写入:本地文档 60 | print (u'日期:' + str(allday[price]) + 61 | u'的MID值为:' + str(MID) + 62 | u';UPPER值为:' + str(UPPER) + 63 | u';LOWER值为:' + str(LOWER)) 64 | start = start + 1 65 | end = end + 1 66 | L = L-1 67 | if L - N == -1: #改判断用于判断不足20个点,无法计算时,退出for循环 68 | f.close() 69 | break 70 | break #for循环结束后,退出while循环 71 | 72 | if __name__ == "__main__": 73 | 74 | f = open("D:/Desktop/detail.txt",'w') #文件写入路径 75 | close,allday = Get_close_allday('sz002023') 76 | Get_Boll(close,allday) 77 | os.system("pause") 78 | # Get_Upper(MID,close) 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /CCI.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | TYP:=(HIGH+LOW+CLOSE)/3; 4 | CCI:(TYP-MA(TYP,N))/(0.015*AVEDEV(TYP,N)); 5 | TYP比较容易理解,(最高价+最低价+收盘价)÷3 6 | MA(TYP,N) 也比较简单,就是N天的TYP的平均值 7 | AVEDEV(TYP,N) 比较难理解,是对TYP进行绝对平均偏差的计算。 8 | 也就是说N天的TYP减去MA(TYP,N)的绝对值的和的平均值。 9 | 表达式: 10 | MA = MA(TYP,N) 11 | AVEDEV(TYP,N) =( | 第N天的TYP - MA | + | 第N-1天的TYP - MA | + ...... + | 第1天的TYP - MA | ) ÷ N 12 | CCI = (TYP-MA)÷ AVEDEV(TYP,N) ÷0.015 13 | """ 14 | 15 | import requests,os 16 | 17 | def Get_stock_detail(stock_code): 18 | times=[] #日期 19 | close=[] #收盘价 20 | hpri = [] #最高价 21 | lpri = [] #最低价 22 | url = "http://mk.api.guxiansheng.cn/quote/?mod=quote&method=kline_data&begindate=&" \ 23 | "finalCode=" + stock_code + "&fqtype=&num=200&type=day&appid=gxs" #参数中num为获取日K的点数,200为200个日期点 24 | r = requests.get(url) 25 | all_time = r.json()['data']['timeZ'] #获取日K的所有日期 26 | all_data = r.json()['data']['datas'] #获取日K中所有日期的数据明细 27 | 28 | for time in all_time: #交易时间 29 | times.append(time) 30 | 31 | for npri in all_data: #当日收盘价 32 | close.append(npri['NPRI']) 33 | 34 | for today_hpri in all_data: #当日最高价 35 | hpri.append(today_hpri['HPRI']) 36 | 37 | for today_lpri in all_data: #当日最低价 38 | lpri.append(today_lpri['LPRI']) 39 | TYP = [] 40 | for i in range(0,len(close)): #根据最高价、最低、收盘价 算出中价 41 | typ_value = (close[i] + hpri[i] + lpri[i])/3 42 | TYP.append(typ_value) 43 | 44 | MA = [] 45 | N = 14 46 | for j in range(0,len(TYP)): #计算中间中价14天的MA值 47 | ma_value = sum(TYP[j:N])/14 48 | MA.append(ma_value) 49 | N = N + 1 50 | if N == len(TYP)+1: 51 | break 52 | 53 | AVEDEV = [] 54 | for x in range(0,len(MA)): #计算收盘价、最高价、最低价对应的中价的绝对平均偏差 55 | avedev_sum = 0 56 | for y in range(0,14): 57 | abs_sum = abs(TYP[x+y]-MA[x]) 58 | avedev_sum = avedev_sum + abs_sum 59 | avedev_sum = avedev_sum/14 60 | AVEDEV.append(avedev_sum) 61 | 62 | CCI = [] 63 | for index in range(0,len(MA)): #计算CCI 64 | value = round((TYP[index]- MA[index])/(AVEDEV[index]*0.015),2) 65 | CCI.append(value) 66 | 67 | for i in range(0,len(CCI)): #输出对应日期的CCI 68 | print str(times[i]) + u"的CCI值为:" + str(CCI[i]) 69 | 70 | if __name__ == "__main__": 71 | Get_stock_detail("sh603868") #传入股票代码,加前缀 72 | os.system("pause") 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /InOutRatio.py: -------------------------------------------------------------------------------- 1 | # encoding:utf-8 2 | from __future__ import division 3 | import redis,time 4 | 5 | class hqtest(): 6 | 7 | def __init__(self,file_path,redis_key,stock_name): 8 | self.file_path = file_path 9 | self.redis_key = redis_key 10 | self.price = [] #成交价 11 | self.mvol =[] #成交量 12 | self.price_index = [] #数组中价格对应的角标 13 | self.mark = 0 #数组中每个元素的角标,根据角标,方便取出指定分K的开盘价,昨日收盘价,以及计算成交量 14 | self.time_price=[] #当前分时点的价格 15 | self.stock_name = stock_name 16 | 17 | def run(self): 18 | r = redis.Redis(host="xxxxxxxxx",port =xxxxxxx,db=0) 19 | sum_row = len(r.zrange(self.redis_key,0,5000)) #redis库连接地址 20 | # f = open(self.file_path,'a') 21 | # f.write("***************************" + self.stock_name + str(self.end_time +1)+ "分K为*****************************" + "\n") 22 | now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 23 | print u"开始执行:" + str(now_time) 24 | try: 25 | for index in range(0,sum_row): #获取redis中指定股票的所有交易明细valu值条数 26 | hq = r.zrange(self.redis_key,index,index) #遍历redis中指定股票的所有交易明细valu值 27 | detail = str(hq[0]).split(',') #把每一行value值处理为一个数组,方便取出value值中指定的数据 28 | # print detail 29 | if (self.end_time >= int(detail[0]) > self.start_time): #因为不确定当前时间段,在redis中value值是第几行,目前采取遍历所有行;处理所有行中改时间段的分K 30 | # print u"开始写入" 31 | self.price.append(float(detail[5])) #把每笔成交价格写入price数组中 32 | self.mvol.append(int(detail[6])) #把每笔成交量写入mvol数组中 33 | # f.write("交易时间:" +str(detail[0]) + "; 成交价:" + str(detail[5]) + 34 | # "; 成交量:" + str(detail[6]) + '\n') #把改分K时间段的数据写入本地 35 | self.mark =self.mark +1 36 | if (int(detail[0]) >= self.k_tiem): #分K开始时间 37 | self.price_index.append(self.mark) #把分K开始时间的角标,存入price_index角标数组中 38 | self.time_price.append(self.price[self.mark-1]) #把分K第一点开始的价格存入time_price数组中 39 | 40 | print ("交易时间:" +str(detail[0]) + ";卖5-" + str(detail[12]) + ";卖4-" + str(detail[14]) + 41 | ";卖3-" + str(detail[16]) + ";卖2-" + str(detail[18])+ 42 | ";卖1-" + str(detail[20]) + "\n") 43 | 44 | toal_vol=round(float((self.mvol[-1]-self.mvol[self.price_index[0]-2])/100),2) #计算改分K的总成交量 45 | # zf = round(((self.price[-1]-self.price[self.price_index[0]-2])/self.price[self.price_index[0]-2])*100,2) #计算改分K的涨幅 46 | # f.write("***************************" + self.stock_name + str(self.end_time +1)+ "分K为*****************************" + "\n") 47 | # f.write("开盘价为:" + str(self.price[self.price_index[0]-1]) + "\n") 48 | # f.write("最新价为:" + str(self.price[-1]) + "\n") 49 | # f.write("成交量为:" + str(toal_vol) + "\n") 50 | print str(toal_vol) 51 | except: 52 | print u"出错" 53 | # f.close() 54 | pass 55 | # f.close() 56 | # print u'写入完成' 57 | over_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 58 | print u"执行结束:" + str(over_time) 59 | 60 | if __name__ == "__main__": 61 | # hqtest()方法需要参数:写入本地文件路径;分K开始时间段,分K结束时间段;分K起始时间,测试股票redis的key值 62 | hqtest("D:/Desktop/detail.txt","stocksir:dt:sz002444",'巨星科技').run() 63 | 64 | 65 | -------------------------------------------------------------------------------- /MTM.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | MTM(动量线指标) 4 | 计算方法: 5 | MTM(N日)=C-CN C=当日收盘价 CN=N日前的收盘价 N默认值12 6 | MAMTM =MTM/M 求MTM的M日平滑数据 M默认值6 7 | """ 8 | 9 | import requests,os 10 | 11 | def Get_stock_detail(stock_code): 12 | times=[] #日期 13 | close=[] #收盘价 14 | MTM = [] #MTM 15 | MAMTM = [] 16 | url = "http://mk.api.guxiansheng.cn/quote/?mod=quote&method=kline_data&begindate=&" \ 17 | "finalCode=" + stock_code + "&fqtype=&num=200&type=day&appid=gxs" #参数中num为获取日K的点数,200为200个日期点 18 | r = requests.get(url) 19 | all_time = r.json()['data']['timeZ'] #获取日K的所有日期 20 | all_data = r.json()['data']['datas'] #获取日K中所有日期的数据明细 21 | 22 | for time in all_time: #交易时间 23 | times.append(time) 24 | 25 | for npri in all_data: #当日收盘价 26 | close.append(npri['NPRI']) 27 | close.reverse() 28 | N = 13 29 | mtm_index = 0 30 | while True: 31 | mtm_value = round((close[N-1] -close[mtm_index]),2) 32 | N = N + 1 33 | mtm_index = mtm_index +1 34 | MTM.append(mtm_value) 35 | if N == len(close)+1: 36 | break 37 | MTM.reverse() 38 | M = 6 39 | 40 | for mamtm_index in range(0,len(MTM)): 41 | mamtm_value = round((sum(MTM[mamtm_index:M])/6),2) 42 | M = M+1 43 | MAMTM.append(mamtm_value) 44 | if M == len(MTM)+1: 45 | break 46 | 47 | for i in range(0,len(MAMTM)): 48 | print str(times[i]) + u"的MTM值为:" + str(MTM[i]) + u";MAMTM值为:" + str(MAMTM[i]) 49 | 50 | if __name__ == "__main__": 51 | Get_stock_detail("sh603868") #传入股票代码,加前缀 52 | os.system("pause") 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /OBV.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | OBV计算方法: 4 | 主公式:当日OBV=前一日OBV+今日成交量 5 | 1.基期OBV值为0,即该股上市的第一天,OBV值为0 6 | 2.若当日收盘价>上日收盘价,则当日OBV=前一日OBV+今日成交量 7 | 3.若当日收盘价<上日收盘价,则当日OBV=前一日OBV-今日成交量 8 | 4.若当日收盘价=上日收盘价,则当日OBV=前一日OBV 9 | """ 10 | 11 | import requests,os 12 | 13 | def Get_stock_detail(stock_code): 14 | times=[] #日期 15 | close=[] #收盘价 16 | tvols = [] #成交量 17 | url = "http://mk.api.guxiansheng.cn/quote/?mod=quote&method=kline_data&begindate=&" \ 18 | "finalCode=" + stock_code + "&fqtype=&num=200&type=day&appid=gxs" #参数中num为获取日K的点数,200为200个日期点 19 | r = requests.get(url) 20 | all_time = r.json()['data']['timeZ'] #获取日K的所有日期 21 | all_data = r.json()['data']['datas'] #获取日K中所有日期的数据明细 22 | 23 | for time in all_time: #交易时间 24 | times.append(time) 25 | times.reverse() 26 | 27 | for npri in all_data: #当日收盘价 28 | close.append(npri['NPRI']) 29 | close.reverse() 30 | 31 | for tovl in all_data: #当日成交量 32 | tvols.append(round(tovl['TVOL']/100,0)) 33 | tvols.reverse() 34 | 35 | base_obv = 0 36 | obv = [] 37 | obv.append(base_obv) 38 | for i in range(0,len(times)-1): #通过收盘价比较计算当日OBV 39 | if close[i+1] > close[i]: 40 | today_obv = base_obv + tvols[i+1] 41 | obv.append(today_obv/100) 42 | elif close[i+1] < close[i]: 43 | today_obv = base_obv - tvols[i+1] 44 | obv.append(today_obv/100) 45 | else: 46 | today_obv = base_obv 47 | obv.append(today_obv/100) 48 | base_obv =today_obv 49 | for i in range(0,len(obv)): 50 | print str(times[i]) + u"的OBV值为:" + str(obv[i]) 51 | 52 | if __name__ == "__main__": 53 | Get_stock_detail("sh603868") #传入股票代码,加前缀 54 | os.system("pause") 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MrStock 2 | 股票行情指标算法(BOLL、MTM、CCI、OBV、TRIX,股票基础数据明细,股票收益算法) 3 | -------------------------------------------------------------------------------- /TRIX.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | TRIX(三重指数平滑平均线) 4 | 计算方法: 5 | 1. X=ema(close,N),首先计算收盘价的N日移动平均值, 6 | 2.对X作N日平滑处理,得出Y值,即Y=ema(X,N), 7 | 3.对Y再次N日平滑处理,得出TR值,即TR=ema(Y,N) 8 | 以上三个步骤的意思是,对N日收盘均价作3次平滑处理后,得出最终值TR 9 | 4.求TRIX值 10 | TRIX=【(TR-上一日TR)/上一日TR】*100 11 | 5.求TRIX值的M日简单移动平均值 MATRIX 12 | MATRIX=MA(TRIX,M) 13 | 使用方法: 14 | TRIX从下上穿MATRIX,设置金叉符号,红色上箭头 15 | TRIX从上下穿MATRIX,设置死叉符号,蓝色下箭头 16 | """ 17 | import requests,os 18 | 19 | def data_replace(data): #截取小数点后两位 20 | data = str(data) 21 | index = data.index('.') 22 | return float(data[:index+3]) 23 | 24 | def Get_stock_detail(stock_code): 25 | times=[] #日期 26 | close=[] #收盘价 27 | 28 | url = "http://mk.api.guxiansheng.cn/quote/?mod=quote&method=kline_data&begindate=&" \ 29 | "finalCode=" + stock_code + "&fqtype=&num=200&type=day&appid=gxs" #参数中num为获取日K的点数,200为200个日期点 30 | r = requests.get(url) 31 | all_time = r.json()['data']['timeZ'] #获取日K的所有日期 32 | all_data = r.json()['data']['datas'] #获取日K中所有日期的数据明细 33 | 34 | for time in all_time: #交易时间 35 | times.append(time) 36 | 37 | for npri in all_data: #当日收盘价 38 | close.append(npri['NPRI']) 39 | close.reverse() 40 | N= 12 41 | X = [] 42 | Y = [] 43 | Z = [] 44 | i = close[0] 45 | 46 | for x_index in range(0,len(close)): #用收盘价计算第一次EMA 47 | EMA_X = round(((2 * close[x_index]) + ((N-1) * i))/(N + 1),3) 48 | X.append(EMA_X) 49 | i = X[x_index] 50 | 51 | j = X[0] 52 | for y_index in range(0,len(X)): #用收盘价计算后的EMA再次计算EMA 53 | EMA_Y = round(((2 * X[y_index]) + ((N-1) * j))/(N + 1),3) 54 | Y.append(EMA_Y) 55 | j = Y[y_index] 56 | 57 | q = Y[0] 58 | for Z_index in range(0,len(Y)): #第三次计算EMA 59 | EMA_Z = round(((2 * Y[Z_index]) + ((N-1) * q))/(N + 1),3) 60 | Z.append(EMA_Z) 61 | q = Z[Z_index] 62 | 63 | TRIX = [] 64 | for TRIX_index in range(1,len(Z)): #计算TRIX值 65 | value = round(((Z[TRIX_index]-Z[TRIX_index-1])/Z[TRIX_index-1])*100,3) 66 | TRIX.append(value) 67 | 68 | MATRIX = [] 69 | M = 20 70 | for MATRIX_index in range(0,len(TRIX)): #计算MATRIX值 71 | MATRIX_value = round(sum(TRIX[MATRIX_index:M])/20,3) 72 | M = M+1 73 | MATRIX.append(MATRIX_value) 74 | if M == len(TRIX)+1: 75 | break 76 | 77 | 78 | TRIX.reverse() 79 | MATRIX.reverse() 80 | for i in range(0,len(MATRIX)): #输出对应日期的TRIX和MATRIX 81 | print str(times[i]) + u"的TRIX值为:" + str(TRIX[i]) + u";MATRIX值为:" + str(MATRIX[i]) 82 | 83 | if __name__ == "__main__": 84 | Get_stock_detail("sh603868") #传入股票代码,加前缀 85 | os.system("pause") 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /minute_k.py: -------------------------------------------------------------------------------- 1 | # encoding:utf-8 2 | from __future__ import division 3 | import redis,time 4 | 5 | class hqtest(): 6 | 7 | def __init__(self,file_path,start_time,end_time,k_tiem,redis_key,stock_name): 8 | self.file_path = file_path 9 | self.start_time = int(start_time) #分K开始时间 10 | self.end_time = int(end_time) #分K结束时间 11 | self.redis_key = redis_key 12 | self.k_tiem = int(k_tiem) 13 | self.price = [] #成交价 14 | self.mvol =[] #成交量 15 | self.price_index = [] #数组中价格对应的角标 16 | self.mark = 0 #数组中每个元素的角标,根据角标,方便取出指定分K的开盘价,昨日收盘价,以及计算成交量 17 | self.time_price=[] #当前分时点的价格 18 | self.stock_name = stock_name 19 | 20 | def run(self): 21 | r = redis.Redis(host="xxxxxxx",port =xxxxxx,db=xxxxxx) #redis连接地址 22 | sum_row = len(r.zrange(self.redis_key,0,5000)) 23 | f = open(self.file_path,'a') 24 | f.write("***************************" + self.stock_name + str(self.end_time +1)+ "分K为*****************************" + "\n") 25 | now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 26 | print u"开始执行:" + str(now_time) 27 | try: 28 | for index in range(0,sum_row): #获取redis中指定股票的所有交易明细valu值条数 29 | hq = r.zrange(self.redis_key,index,index) #遍历redis中指定股票的所有交易明细valu值 30 | detail = str(hq[0]).split(',') #把每一行value值处理为一个数组,方便取出value值中指定的数据 31 | # print detail 32 | if (self.end_time >= int(detail[0]) > self.start_time): #因为不确定当前时间段,在redis中value值是第几行,目前采取遍历所有行;处理所有行中改时间段的分K 33 | # print u"开始写入" 34 | self.price.append(float(detail[5])) #把每笔成交价格写入price数组中 35 | self.mvol.append(int(detail[6])) #把每笔成交量写入mvol数组中 36 | f.write("交易时间:" +str(detail[0]) + "; 成交价:" + str(detail[5]) + 37 | "; 成交量:" + str(detail[6]) + '\n') #把改分K时间段的数据写入本地 38 | self.mark =self.mark +1 39 | if (int(detail[0]) >= self.k_tiem): #分K开始时间 40 | # print mark 41 | self.price_index.append(self.mark) #把分K开始时间的角标,存入price_index角标数组中 42 | # print price_index 43 | # print price 44 | self.time_price.append(self.price[self.mark-1]) #把分K第一点开始的价格存入time_price数组中 45 | # print time_price 46 | 47 | # f.write("交易时间:" +str(detail[0]) + ";卖5-" + str(detail[12]) + ";卖4-" + str(detail[14]) + 48 | # ";卖3-" + str(detail[16]) + ";卖2-" + str(detail[18])+ 49 | # ";卖1-" + str(detail[20]) + "\n") 50 | 51 | toal_vol=round(float((self.mvol[-1]-self.mvol[self.price_index[0]-2])/100),2) #计算改分K的总成交量 52 | zf = round(((self.price[-1]-self.price[self.price_index[0]-2])/self.price[self.price_index[0]-2])*100,2) #计算改分K的涨幅 53 | f.write("***************************" + self.stock_name + str(self.end_time +1)+ "分K为*****************************" + "\n") 54 | f.write("昨日收盘价为:" + str(self.price[self.price_index[0]-2]) + "\n") 55 | f.write("开盘价为:" + str(self.price[self.price_index[0]-1]) + "\n") 56 | f.write("最新价为:" + str(self.price[-1]) + "\n") 57 | f.write("最高价为:" + str(max(self.time_price)) + "\n") 58 | f.write("最低价为:" + str(min(self.time_price)) + "\n") 59 | f.write("今日收盘价:" + str(self.price[-1]) + "\n") 60 | f.write("涨幅为:" +str(zf) + "%" + "\n") 61 | f.write("成交量为:" + str(toal_vol) + "\n") 62 | except: 63 | print u"出错" 64 | f.close() 65 | pass 66 | f.close() 67 | # print u'写入完成' 68 | over_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 69 | print u"执行结束:" + str(over_time) 70 | 71 | if __name__ == "__main__": 72 | # hqtest()方法需要参数:写入本地文件路径;分K开始时间段,分K结束时间段;分K起始时间,测试股票redis的key值 73 | hqtest("D:/Desktop/detail.txt",20161116095900,20161116100499,20161116100000,"stocksir:dt:sz002444",'巨星科技').run() 74 | 75 | -------------------------------------------------------------------------------- /plan_AddRate.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf8 -*- 2 | import datetime,MySQLdb,re,time 3 | def get_date(day): 4 | holiday = [] 5 | try: 6 | conn = MySQLdb.connect(host='xxxxxx', port=3306, user='xxxxxx', passwd='xxxxxx', db='xxxxxx') #数据库连接地址 7 | cursor = conn.cursor() 8 | holiday_sql = "SELECT `value` FROM `stock_setting` WHERE `name`= 'holiday'" #查询法定节假日 9 | cursor.execute(holiday_sql) 10 | for i in cursor.fetchall(): 11 | pass 12 | except Exception as error: 13 | print error 14 | 15 | day_list = (list(i))[0] #查询获得的节假日转换为字符串 16 | holiday_list = re.findall('s:4:"name";s:10:"(.+?)";s:5:"intro";', day_list) #正则匹配出所有节假日,存入holiday_list数组,日期为时间戳格式 17 | for index in holiday_list: 18 | holiday.append(time.strftime("%Y-%m-%d",time.localtime(int(index)))) #将时间戳转为日期xx-xx-xx格式,存入holiday数组 19 | # print holiday 20 | now_time = datetime.date.today() #获取当天日期 21 | 22 | #print now_time 23 | # history_data = now_time - datetime.timedelta(days=0) #日期加减 .weekday()当前日期星期几 24 | # print history_data 25 | week_day = [5,6] #值:5、6分别代表:星期六、星期天 26 | count = 0 27 | hour_time = int(time.strftime('%H', time.localtime(time.time()))) 28 | if hour_time > 15: #盘中时间大于下午15点,计算日期为当天前一天 29 | for i in range(100): 30 | history_data = now_time - datetime.timedelta(days=i+1) 31 | if history_data.weekday() not in week_day: #排除周末 32 | if str(history_data) not in holiday: #排除节假日 33 | #print str(history_data) 34 | count = count + 1 35 | if count == day: 36 | break 37 | count_date = str(history_data).replace('-', '') #count_date为参与累计收益计算日期 38 | 39 | return count_date 40 | else: #计算时间小于当天15点,计算日期为当天日期的前2天 41 | for i in range(100): 42 | history_data = now_time - datetime.timedelta(days=i+2) 43 | if history_data.weekday() not in week_day: #排除周末 44 | if str(history_data) not in holiday: #排除节假日 45 | # print str(history_data) 46 | count = count + 1 47 | print count 48 | if count == day: 49 | break 50 | 51 | count_date = str(history_data).replace('-','') #count_date为参与累计收益计算日期 52 | 53 | return count_date 54 | def plan_rate(date,plan_class): #根据30日的有效日期,计算出30天的累计收益 55 | try: 56 | now_time = str(datetime.date.today()).replace('-', '') 57 | conn = MySQLdb.connect(host='xxxxxx',port=xxxxxx,user='xxxxxx',passwd='xxxxxx',db='xxxxxxx') #数据库连接地址 58 | cursor = conn.cursor() 59 | plan_stock_sql = "SELECT SUM(max_rate) FROM `stock_plan_stock` WHERE plan_id IN" + \ 60 | "(SELECT id FROM `stock_plan` WHERE plan_class_id =" + str(plan_class) + " AND `date` >=" + date + " AND `date` <" + now_time + ")" 61 | cursor.execute(plan_stock_sql) 62 | for count_rate in cursor.fetchall(): 63 | pass 64 | print "股机实际(未加权)累计收益为:" + str(count_rate[0]) 65 | 66 | except Exception as error: 67 | print error 68 | 69 | if __name__ == "__main__": 70 | plan_rate(get_date(30),87) #传入股机累计收益计算天数,和股机类型:85(股机A),86(股机B),87(股机C) 71 | 72 | -------------------------------------------------------------------------------- /rate.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import requests,os,time 3 | 4 | def Get_stock_detail(stock_code,release_time,first_price,second_price,*market_day): 5 | NPR = [] 6 | times = [] 7 | rate = [] 8 | max_rate = [] 9 | url = "https://mk.api.guxiansheng.cn/quote/?appid=gxs&mod=quote&method=dealinfo_data&fromwhere=cd&finalCode=" + stock_code + \ 10 | "&pageNo=1&pageSize=5000" 11 | response = requests.get(url) 12 | detail = response.json()['data'] 13 | # print detail 14 | 15 | now_time = time.strftime('%H:%M', time.localtime(time.time())) #获取当前时间 16 | 17 | if int(market_day[0]) != 0: #交易日大于0 18 | if (9 <= int(release_time[:2]) <= 15) and (25 <= int(release_time[3:])): #股机、锦囊发布时间在盘中 19 | for index in range(0,len(detail)): 20 | NPR.append(float(detail[index]['PRI'])) 21 | times.append(str(detail[index]['DATE'][:5])) 22 | NPR.reverse() 23 | times.reverse() 24 | # print times 25 | # print NPR 26 | # print times.index(release_time) # 发布时间对应的坐标 27 | release_price = NPR[times.index(release_time)] #发布时间对应的股票价格 28 | # print release_price,NPR[0:times.index(release_time)+1] 29 | print u'股票发布后的最高价为:' + str(max(NPR[times.index(release_time):])) #查询出发布时间后的最高价 30 | for now_price in NPR[times.index(release_time):]: #与第一、第二买入价对比,算出当前收益 31 | if now_price > first_price: 32 | rate.append(round(((now_price / release_price)-1)*100,2)) 33 | elif second_price < now_price <= first_price: 34 | rate.append(round(((now_price / first_price) - 1) * 100, 2)) 35 | else: 36 | rate.append(round(((now_price / second_price) - 1) * 100, 2)) 37 | print u'当前收益为:' + str(rate[-1]) 38 | for index in range(times.index(release_time),len(times)): #根据发布时间的价格与第一、第二买入价对比,算出最高收益 39 | if NPR[index] > first_price: 40 | max_rate.append(round(((NPR[index] / release_price) -1)*100,2)) 41 | elif second_price < NPR[index] <= first_price: 42 | max_rate.append(round(((NPR[index] / first_price) - 1) * 100, 2)) 43 | else: 44 | max_rate.append(round(((NPR[index] / second_price) - 1) * 100, 2)) 45 | print u'最大收益为:' + str(max(max_rate)) 46 | else: #股机、锦囊发布时间非盘中 47 | if len(detail) <= 0: 48 | print u'还未开盘' 49 | else: 50 | for index in range(0, len(detail)): 51 | NPR.append(float(detail[index]['PRI'])) 52 | times.append(str(detail[index]['DATE'][:5])) 53 | NPR.reverse() 54 | times.reverse() 55 | release_price = NPR[0] # 发布时间对应的股票价格 56 | print u'股票发布后的最高价为:' + str(max(NPR)) # 查询出发布时间后的最高价 57 | for now_price in NPR[0:]: # 与第一、第二买入价对比,算出当前收益 58 | if now_price > first_price: 59 | rate.append(round(((now_price / release_price) - 1) * 100, 2)) 60 | elif second_price < now_price <= first_price: 61 | rate.append(round(((now_price / first_price) - 1) * 100, 2)) 62 | else: 63 | rate.append(round(((now_price / second_price) - 1) * 100, 2)) 64 | print u'当前收益为:' + str(rate[-1]) 65 | for index in range(times.index(release_time), len(times)): # 根据发布时间的价格与第一、第二买入价对比,算出最高收益 66 | if NPR[index] > first_price: 67 | max_rate.append(round(((max(NPR[index:]) / release_price) - 1) * 100, 2)) 68 | elif second_price < NPR[index] <= first_price: 69 | max_rate.append(round(((max(NPR[index:]) / first_price) - 1) * 100, 2)) 70 | else: 71 | max_rate.append(round(((max(NPR[index:]) / second_price) - 1) * 100, 2)) 72 | print u'最大收益为:' + str(max(max_rate)) 73 | 74 | if (9 <= int(now_time[:2])) and (1 <= int(now_time[3:])): 75 | f = open("E:/plan_jn_test/rate.txt", 'rb') 76 | history_rate = f.readlines() 77 | if float(history_rate[0]) <= max(max_rate): 78 | f = open("E:/plan_jn_test/rate.txt",'w') 79 | f.write(str(max(max_rate)) + '\n') 80 | f.close() 81 | 82 | else: #交易日=0 83 | if (9 <=int(release_time[:2]) <= 15): #股机、锦囊发布时间在盘中 84 | for index in range(0,len(detail)): 85 | NPR.append(float(detail[index]['PRI'])) 86 | times.append(str(detail[index]['DATE'][:5])) 87 | NPR.reverse() 88 | times.reverse() 89 | # print times 90 | # print NPR 91 | # print times.index(release_time) # 发布时间对应的坐标 92 | release_price = NPR[times.index(release_time)] #发布时间对应的股票价格 93 | # print release_price,NPR[0:times.index(release_time)+1] 94 | print u'股票发布后的最高价为:' + str(max(NPR[times.index(release_time):])) #查询出发布时间后的最高价 95 | for now_price in NPR[times.index(release_time):]: #与第一、第二买入价对比,算出当前收益 96 | if now_price > first_price: 97 | rate.append(round(((now_price / release_price)-1)*100,2)) 98 | elif second_price < now_price <= first_price: 99 | rate.append(round(((now_price / first_price) - 1) * 100, 2)) 100 | else: 101 | rate.append(round(((now_price / second_price) - 1) * 100, 2)) 102 | print u'当前收益为:' + str(rate[-1]) + "%" 103 | 104 | for index in range(times.index(release_time),len(times)): #根据发布时间的价格与第一、第二买入价对比,算出最高收益 105 | if NPR[index] > first_price: 106 | max_rate.append(round(((max(NPR[index:]) / release_price) -1)*100,2)) 107 | elif second_price < NPR[index] <= first_price: 108 | max_rate.append(round(((max(NPR[index:]) / first_price) - 1) * 100, 2)) 109 | else: 110 | max_rate.append(round(((max(NPR[index:]) / second_price) - 1) * 100, 2)) 111 | print u'最大收益为:' + str(max(max_rate)) + "%" 112 | 113 | else: #股机、锦囊发布时间非盘中 114 | if len(detail) <= 0: 115 | print u'还未开盘' 116 | else: 117 | for index in range(0, len(detail)): 118 | NPR.append(float(detail[index]['PRI'])) 119 | times.append(str(detail[index]['DATE'][:5])) 120 | NPR.reverse() 121 | times.reverse() 122 | release_price = NPR[0] # 发布时间对应的股票价格 123 | print u'股票发布后的最高价为:' + str(max(NPR)) # 查询出发布时间后的最高价 124 | for now_price in NPR[0:]: # 与第一、第二买入价对比,算出当前收益 125 | if now_price > first_price: 126 | rate.append(round(((now_price / release_price) - 1) * 100, 2)) 127 | elif second_price < now_price <= first_price: 128 | rate.append(round(((now_price / first_price) - 1) * 100, 2)) 129 | else: 130 | rate.append(round(((now_price / second_price) - 1) * 100, 2)) 131 | print u'当前收益为:' + str(rate[-1]) + "%" 132 | for index in range(0, len(times)): # 根据发布时间的价格与第一、第二买入价对比,算出最高收益 133 | if NPR[index] > first_price: 134 | max_rate.append(round(((NPR[index] / release_price) - 1) * 100, 2)) 135 | elif second_price < NPR[index] <= first_price: 136 | max_rate.append(round(((NPR[index] / first_price) - 1) * 100, 2)) 137 | else: 138 | max_rate.append(round(((NPR[index] / second_price) - 1) * 100, 2)) 139 | print u'最大收益为:' + str(max(max_rate)) + "%" 140 | if (9 <= int(now_time[:2])) and (1 <= int(now_time[3:])): 141 | f1 = open("E:/plan_jn_test/rate.txt", 'rb') 142 | f2 = open("E:/plan_jn_test/marke_price.txt", 'w') 143 | history_rate = f1.readlines() 144 | if float(history_rate[0]) <= max(max_rate): 145 | f = open("E:/plan_jn_test/rate.txt",'w') 146 | f.write(str(max(max_rate)) + '\n') 147 | # f.write(stock_code + "在" + time.strftime('%m:%d', time.localtime(time.time())) +"的最高收益为:" + str(max(max_rate)) + '\n') 148 | if (9 <= int(release_time[:2]) <= 15) and (25 <= int(release_time[3:])): 149 | NPR[times.index(release_time)] 150 | f1.close() 151 | 152 | if __name__ == "__main__": 153 | Get_stock_detail('sh600249','10:21',11.91,11.37,0) #传入股票代码、发布时间、第一价格、第二价格,第N个交易日 154 | # os.system("pause") 155 | 156 | 157 | 158 | 159 | -------------------------------------------------------------------------------- /stock_detail.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import requests,time,pymssql,os 4 | 5 | def data_replace(data): #截取小数点后两位 6 | data = str(data) 7 | index = data.index('.') 8 | return data[:index+3] 9 | 10 | def get_one_value(sql): #获取唯一值 11 | try: 12 | conn = pymssql.connect(host = 'xxxxx',user = 'xxxxx',password = 'gxxxxx', 13 | database = 'xxxxxx') #行情数据库连接地址 14 | cur = conn.cursor() # 使用cursor()方法获取操作游标 15 | cur.execute(sql) # 使用execute方法执行SQL语句 16 | rows = cur.fetchall() #获取所有记录列表 #使用 fetchone() 方法是获取单条数据, 17 | conn.close() # 关闭数据库连接 18 | for value in rows: 19 | return float(value[0]) 20 | except: 21 | print u"数据库连接失败" 22 | 23 | 24 | def get_today_money_flow(sql): #获取今日资金流向 25 | try: 26 | conn = pymssql.connect(host = 'xxxxx',user = 'xxxxx',password = 'gxxxxx', 27 | database = 'xxxxxx') #行情数据库连接地址 28 | cur = conn.cursor() 29 | cur.execute(sql) 30 | rows = cur.fetchall() 31 | conn.close() 32 | for value in rows: 33 | return value 34 | except: 35 | print u"数据库连接失败" 36 | 37 | 38 | def get_values(sql): #获取多个值 39 | try: 40 | conn = pymssql.connect(host = 'xxxxx',user = 'xxxxx',password = 'gxxxxx', 41 | database = 'xxxxxx') #行情数据库连接地址 42 | cur = conn.cursor() 43 | cur.execute(sql) 44 | rows = cur.fetchall() 45 | conn.close() 46 | day_value=[] 47 | for value in rows: 48 | day_value.append(float(value[0])) 49 | return day_value 50 | except: 51 | print u"数据库连接失败" 52 | 53 | 54 | def get_zf(day,stock_code): #根据天数获取振幅 55 | close = get_values("select top 10 TCLOSE from dbo.STK_MKT where seccode = " + stock_code[2:] + " order by SEQ DESC") 56 | hour_time = int(time.strftime('%H',time.localtime(time.time()))) #time.time()用于获取当前时间戳,localtime()从返回浮点数的时间辍方式向时间元组转换,strftime()格式化日期 57 | minute_time = int(time.strftime('%M',time.localtime(time.time()))) #time.time()用于获取当前时间戳,localtime()从返回浮点数的时间辍方式向时间元组转换,strftime()格式化日期 58 | if hour_time >= 15 and minute_time>31: 59 | return close[day-1] 60 | else: 61 | return close[day-1] 62 | 63 | def get_tvolume(stock_code): #获取成交量 64 | hour_time = int(time.strftime('%H',time.localtime(time.time()))) 65 | minute_time = int(time.strftime('%M',time.localtime(time.time()))) 66 | if hour_time >= 15 and minute_time>8: 67 | TVOLUME = get_values("select TOP 6 TVOLUME from dbo.STK_MKT where seccode = " + str(stock_code[2:]) + " order by SEQ DESC") 68 | return (sum(TVOLUME[1:]) /1200)/100 69 | else: 70 | TVOLUME = get_values("select TOP 5 TVOLUME from dbo.STK_MKT where seccode = " + str(stock_code[2:]) + " order by SEQ DESC") 71 | return (sum(TVOLUME) /1200)/100 72 | 73 | def unit_replace(value): #单位替换 74 | if value >0: 75 | value = str(value) 76 | if 9>len(value[:value.index('.')])>=5: 77 | value = str(float(value[:value.index('.')])/10000) 78 | index = value.index('.') 79 | value = value[:index +3] + u'万' #以万为单位 80 | return value 81 | elif len(value[:value.index('.')])>=9: 82 | value = str(float(value[:value.index('.')])/100000000) 83 | index = value.index('.') 84 | value = value[:index +3] + u'亿' #亿为单位 85 | return value 86 | else: 87 | return value #普通单位 88 | else: 89 | value = str(value) 90 | if 10>len(value[:value.index('.')])>=6: 91 | value = str(float(value[:value.index('.')])/10000) 92 | index = value.index('.') 93 | value = value[:index +3] + u'万' #以万为单位 94 | return value 95 | elif len(value[:value.index('.')])>=10: 96 | value = str(float(value[:value.index('.')])/100000000) 97 | index = value.index('.') 98 | value = value[:index +3] + u'亿' #亿为单位 99 | return value 100 | else: 101 | return value #普通单位 102 | 103 | def Get_stock_detail(stock_code): 104 | url ="http://mk.api.guxiansheng.cn/quote/?mod=quote&method=time_data&finalCode=" + stock_code +\ 105 | "&fromwhere=cd&num=241&timePoint=0&type=00&appid=gxs&device=36C0FF7E0B81B4C6722D5AA64F3FDD22&token=36C0FF7E0B81B4C6722D5AA64F3FDD22" 106 | r = requests.get(url) #发送请求 107 | NPRI = float(str((r.json()['NPRI'])[:5])) #最新价 108 | print u'最新价为:' + str(NPRI) 109 | OPRI = float(str((r.json()['OPRI'])[:5])) #今开 110 | print u'今开为:' + str(OPRI) 111 | PPRI = float(str((r.json()['PPRI'])[:5] )) #昨收 112 | print u'昨收为:' + str(PPRI) 113 | Limit_Up = data_replace((PPRI + PPRI * 0.1)) #涨停 114 | print u'涨停为:' + str(Limit_Up) 115 | Limit_Down = data_replace((PPRI - PPRI * 0.1)) #跌停 116 | print u'跌停为:' + str(Limit_Down) 117 | HPRI = float(str((r.json()['HPRI'])[:5])) #最高 118 | print u'最高为:' + str(HPRI) 119 | LPRI = float(str((r.json()['LPRI'])[:5])) #最低 120 | print u'最低为:' + str(LPRI) 121 | SSCJL = str(float(r.json()['data'][-1]['TVOL'])/100) #成交量 122 | print u'成交量为:' + unit_replace(SSCJL) 123 | SSCJE = str(float(r.json()['data'][-1]['TVAL'])) #成交额 124 | print u'成交额为:' + unit_replace(SSCJE) #调用自定义的unit_replace函数 125 | 126 | HSL = str(round(((float(SSCJL)*100) /get_one_value("select top 1 FL_SHR from dbo.STK_SHR_STRU where A_STOCKCODE = " + repr(stock_code[2:]) + " order by SEQ desc"))*100,2)) + '%' #个股换手率 = (买卖单成交量总和) / 流通股本 127 | print u'换手率为:' + HSL 128 | #AVPRI = data_replace(float(r.json()['data'][-1]['TVAL']) /float(r.json()['data'][-1]['TVOL'])) #均价 129 | #print u'均价为:' + AVPRI 130 | ZF = data_replace(((HPRI - LPRI)/PPRI ) * 100) + '%' #振幅 131 | print u'振幅为:' + ZF 132 | SYL = data_replace((NPRI / get_one_value("select top 1 EPSP from dbo.ANA_STK_FIN_IDX where A_STOCKCODE = " + repr(stock_code[2:]) + " order by SEQ desc"))) #个股市盈率=每股市价/每股税后利润 133 | print u'市盈率为:' + SYL 134 | Five_ZF = data_replace((NPRI - get_zf(5,stock_code))/ get_zf(5,stock_code)*100) + "%" # 板块N日涨幅 = (当前实时指数 - 第N-1日指数) / 第N-1日指数 * 100%) 135 | print u'5日涨幅为:' + Five_ZF 136 | Eight_ZF = data_replace((NPRI - get_zf(8,stock_code))/ get_zf(8,stock_code)*100) + "%" #8日涨幅 137 | print u'8日涨幅为:' + Eight_ZF 138 | MGSY = round(get_one_value("select top 1 EPSP from ANA_STK_FIN_IDX where A_STOCKCODE = " + repr(stock_code[2:]) + " and RPT_YEAR ='2016' order by SEQ desc"),2) #每股收益EPSP 139 | print u'每股收益为:' + str(MGSY) 140 | MGJZC = data_replace(get_one_value("select top 1 BPS from ANA_STK_FIN_IDX where A_STOCKCODE = " + repr(stock_code[2:]) + " and RPT_YEAR ='2016' order by SEQ desc")) #每股净资产BPS 141 | print u'每股净资产为:' + MGJZC 142 | SJL = data_replace(NPRI/float(MGJZC)) #市净率= 每股市价 / 每股净资产 26492.26400004 143 | print u'市净率为:' + SJL 144 | open_time = int(len(r.json()['data'])) 145 | LB = data_replace(float(SSCJL)/(get_tvolume(stock_code)*(open_time-1))) #量比 = 现成交总手/(过去5日平均每分钟成交量×当日累计开市时间(分)) 146 | # print SSCJL,get_tvolume(stock_code),open_time 147 | print u'量比为:' + LB 148 | 149 | GB = get_one_value(" select top 1 FL_SHR from dbo.STK_SHR_STRU where A_STOCKCODE =" + repr(stock_code[2:]) + " order by SEQ desc ") 150 | LTSZ =unit_replace(str(GB * NPRI)) #流通市值 151 | print u'流通市值为:' + LTSZ 152 | LTGB = unit_replace(str(GB)) 153 | print u'流通股本为:' + LTGB 154 | money_flow = get_today_money_flow("SELECT TOP 1 ZL_BUY_VAL,ZL_SELL_VAL,ZL_NET,YZ_BUY_VAL,YZ_SELL_VAL,YZ_NET FROM QW_STK_CAP_HIS WITH (NOLOCK) " 155 | "where STOCKCODE =" + repr(stock_code[2:]) + " ORDER BY SEQ DESC ") 156 | ZL_BUY = unit_replace(float(money_flow[0])) 157 | print u'主力流入:' + ZL_BUY #主力 158 | ZL_SELL = unit_replace(float(money_flow[1])) 159 | print u'主力流出:' + ZL_SELL 160 | ZL_NET = unit_replace(float(money_flow[2])) 161 | print u'主力净量:' + ZL_NET 162 | YZ_BUY = unit_replace(float(money_flow[3])) 163 | print u'游资流入:' + YZ_BUY 164 | YZ_SELL = unit_replace(float(money_flow[4])) 165 | print u'游资流出:' + YZ_SELL 166 | YZ_NET = unit_replace(float(money_flow[5])) 167 | print u'游资净量:' + YZ_NET 168 | order_moneyfolw = get_today_money_flow(" SELECT TOP 1 HUG_BUY_VAL AS '特大单流入', HUG_SELL_VAL AS '特大单流出'," 169 | "BIG_BUY_VAL AS '大单流入',BIG_SELL_VAL AS'大单流出',MID_BUY_VAL AS '中单流入'," 170 | "MID_SELL_VAL AS '中单流出', SML_BUY_VAL AS '小单流入' ,SML_SELL_VAL AS'小单流出'" 171 | "FROM QW_STK_TRD_STAT WITH (NOLOCK) where STOCKCODE =" + repr(stock_code[2:]) + " ORDER BY SEQ DESC ") 172 | HUG_BUY_VAL = unit_replace(order_moneyfolw[0]) 173 | print u'特大单流入:' + HUG_BUY_VAL 174 | HUG_SELL_VAL = unit_replace(order_moneyfolw[1]) 175 | print u'特大单流出:' + HUG_SELL_VAL 176 | HUG_NET = unit_replace(order_moneyfolw[0]-order_moneyfolw[1]) 177 | print u'特大单净量:' + HUG_NET 178 | 179 | BIG_BUY_VAL = unit_replace(order_moneyfolw[2]) 180 | print u'大单流入:' + BIG_BUY_VAL 181 | BIG_SELL_VAL = unit_replace(order_moneyfolw[3]) 182 | print u'大单流出:' + BIG_SELL_VAL 183 | BIG_NET = unit_replace(order_moneyfolw[2]-order_moneyfolw[3]) 184 | print u'大单净量:' + BIG_NET 185 | 186 | MID_BUY_VAL = unit_replace(order_moneyfolw[4]) 187 | print u'中单流入:' + MID_BUY_VAL 188 | MID_SELL_VAL = unit_replace(order_moneyfolw[5]) 189 | print u'中单流出:' + MID_SELL_VAL 190 | MID_NET = unit_replace(order_moneyfolw[4]-order_moneyfolw[5]) 191 | print u'中单净量:' + MID_NET 192 | 193 | SML_BUY_VAL = unit_replace(order_moneyfolw[6]) 194 | print u'小单流入:' + SML_BUY_VAL 195 | SML_SELL_VAL = unit_replace(order_moneyfolw[7]) 196 | print u'小单流出:' + SML_SELL_VAL 197 | SML_NET = unit_replace(order_moneyfolw[6]-order_moneyfolw[7]) 198 | print u'小单净量:' + SML_NET 199 | 200 | print u'今日主力动向:' + ZL_NET 201 | print u'近期游资动向:' + YZ_NET 202 | 203 | 204 | if __name__ == "__main__": 205 | Get_stock_detail('sz002023') #传入股票代码 206 | os.system("pause") # 运行shell命令,通过执行操作系统的命令来让程序暂停 207 | -------------------------------------------------------------------------------- /stock_rate.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import requests,os,time 3 | 4 | def Get_stock_detail(stock_code,release_time,first_price,second_price): 5 | NPR = [] 6 | second_time = [] 7 | minute_time = [] 8 | rate = [] 9 | now_rate = [] 10 | max_rate = [] 11 | true_MaxRate = [] 12 | release_price = [] #股票当天的价格 13 | release_PriceRange = [] #股票发布时的价格区间(快照一分钟内的价格) 14 | #stock_price = [] #股票发布后的价格 15 | url = "https://mk.api.guxiansheng.cn/quote/?appid=gxs&mod=quote&method=dealinfo_data&fromwhere=cd&finalCode=" + stock_code + \ 16 | "&pageNo=1&pageSize=5000" 17 | response = requests.get(url) 18 | detail = response.json()['data'] #response.json()返回的是一个字典类型的数据,['data']就是从字典里面取键值为data的值 19 | for index in range(0,len(detail)): 20 | NPR.append(float(detail[index]['PRI'])) 21 | second_time.append(str(detail[index]['DATE'][:8])) 22 | minute_time.append(str(detail[index]['DATE'][:5])) 23 | # print times 24 | # print NPR 25 | for i in range(len(second_time)): 26 | if second_time[i][:5] == release_time: 27 | release_price.append(NPR[i]) 28 | release_PriceRange.append(str(second_time[i]) +" "+ str(NPR[i])) 29 | # print str(second_time[i]) +" "+ str(NPR[i]) 30 | print release_PriceRange ##发布时间对应的股票价格区间 31 | # print release_price #发布时间对应的股票价格区间 32 | # print minute_time.index(release_time) # 发布时间对应的坐标 33 | print u'股票发布后的最高价为:' + str(max(NPR[0:minute_time.index(release_time)])) #查询出发布时间后的最高价 34 | #index() 方法检测字符串minute_time中是否包含子字符串release_time;返回开始的索引值 35 | #max() 方法返回其参数最大值 36 | stock_price = (NPR[0:minute_time.index(release_time)]) #股票发布后的价格区间 37 | stock_price.reverse() #用于反向列表中元素 38 | # print stock_price 39 | for price in release_price: 40 | for now_price in stock_price: #与第一、第二买入价对比,算出当前收益 41 | if now_price > first_price: #股机非交易时间发布,开盘后价格没有触及推荐买入价,当前收益=当前价/开盘价-1 42 | #股机交易时间发布,发布后价格没有触及推荐买入价,当前收益=当前价/发出时价格-1 43 | rate.append(round(((now_price / price)-1)*100,2)) #round()返回小数点四舍五入到2个数字。 44 | 45 | elif second_price < now_price <= first_price: #股机非交易时间发布,开盘后价格触及第一推荐买入价后,当前收益=当前价/第一推荐买入价-1 46 | #股机交易时间发布,发布后价格触及第一推荐买入价后,当前收益=当前价/第一推荐买入价-1 47 | rate.append(round(((now_price / first_price) - 1) * 100, 2)) 48 | 49 | else: #股机非交易时间发布,开盘后价格触及第二推荐买入价后,当前收益=当前价/第二推荐买入价-1 50 | #股机交易时间发布,发布后价格触及第二推荐买入价后,当前收益=当前价/第二推荐买入价-1 51 | rate.append(round(((now_price / second_price) - 1) * 100, 2)) 52 | 53 | now_rate.append(rate[-1]) 54 | # print u'当前收益计算价格当前价为:' + str(now_price) + u" ;当前收益为"+ str(rate[-1]) 55 | print now_rate 56 | for price in release_price: 57 | 58 | for index in range(0,minute_time.index(release_time)): #与第一、第二买入价对比,算出最高收益 59 | if NPR[minute_time.index(release_time) - index] > first_price: #股机非交易时间发布,开盘后价格没有触及推荐买入价,最高收益=取所有当前收益的最高值 60 | #股机交易时间发布,发布后价格没有触及推荐买入价,最高收益=取所有当前收益的最高值 61 | max_rate.append(round(((max(NPR[0:(minute_time.index(release_time) - index)]) / price) -1)*100,2)) 62 | 63 | elif second_price < NPR[minute_time.index(release_time) - index] <= first_price: #股机非交易时间发布,开盘后价格触及第一推荐买入价后,最高收益=取所有当前收益的最高值(包含触及第一买入价之前的当前收益) 64 | #股机交易时间发布,发布后价格触及第一推荐买入价后,最高收益=取所有当前收益的最高值(包含触及第一买入价之前的当前收益) 65 | max_rate.append(round(((max(NPR[0:(minute_time.index(release_time) - index)]) / first_price) - 1) * 100, 2)) 66 | 67 | else: #股机非交易时间发布,开盘后价格触及第二推荐买入价后,最高收益=取所有当前收益的最大值(包含触及第二买入价之前的当前收益) 68 | #股机交易时间发布,发布后价格触及第二推荐买入价后,最高收益=取所有当前收益的最高值(包含触及第二买入价之前的当前收益) 69 | max_rate.append(round(((max(NPR[0:(minute_time.index(release_time) - index)]) / second_price) - 1) * 100, 2)) 70 | true_MaxRate.append(max(max_rate)) 71 | print true_MaxRate 72 | # print u'最大收益为:' + str(max(max_rate)) 73 | 74 | if __name__ == "__main__": 75 | Get_stock_detail('sz002023','11:20',15,16) #传入股票代码、发布时间、第一价格、第二价格 76 | # os.system("pause") 77 | --------------------------------------------------------------------------------