参考某个博客,代码不全,地址找不到了见谅,加了些自己弄的算法。
备忘
#-*- coding: utf-8 -*-
import os, re
import time
def filefilter(compiles =‘strs‘,road=‘str‘):
‘‘‘compiles为正则匹配字,road绝对路径‘‘‘
filesobj = os.walk(road)
for root,dirs,files in filesobj:
filess = files
test = re.compile(compiles,re.IGNORECASE)
filesout = list(filter(test.search,filess))
return filesout
def LLV(list1=‘list‘):
min1=min(list1[-1][4],list1[-2][4],list1[-3][4],list1[-4][4])
return float(min1)
def HHV(list1=‘list‘):
max1=max(list1[-1][5],list1[-2][5],list1[-3][5],list1[-4][5])
return float(max1)
def count1(list1=‘list‘):
cn =0
for i in list(range(-4,0)):
if float(list1[i][4])>float(list1[i][1]):
cn+=1
return float(cn)
def twinpost(li=‘list‘):
"""倍量柱"""
if len(li) == 5:
#if float(li[-4][5]) >= float(li[-5][5])*1.9 and OHLC(li[-4]) >= OHLC(li[-5]):
if float(li[-4][5]) >= float(li[-5][5])*1.9 and float(li[-4][4])>=float(li[-4][1]) and LLV(li)>float(li[-4][3]) and HHV(li)<=float(li[-4][5]) and count1(li)==4 :
return 1
else:
return 0
else:
#print ‘twinpost data error‘
return 0
def goldpost(lis=‘list‘):
"""黄金柱"""
if float(lis[-4][4]) > float(lis[-4][1]):
#收盘价大于开盘价,正常阳线,喇叭口或者鹤嘴,经测试鹤嘴不好用
if ((closeavg3(lis)/float(lis[-4][4])) > 1.0 and (postavg3(lis) < float(lis[-4][5]))):
return 1
else:
return 0
else:#假阴真阳
if ((closeavg3(lis)/float(lis[-4][1])) > 1.0 and (postavg3(lis) < float(lis[-4][5]))):
return 1
else:
return 0
def postavg3(list1=‘list‘):
sum1=0
for l in list1:
sum1+=float(l[1])
return float(sum1/len(list1))
def closeavg3(list1=‘list‘):
sum2=0
for l in list1:
sum2+=float(l[4])
return float(sum2/len(list1))
class SingleMapping3:#使用CsvRead4类
‘‘‘N天内某个股票的黄金柱列表‘‘‘
def __init__(self,filename,nem=‘int‘):
self.name=filename
self.nemb=nem
self.csvfile=CsvRead4(self.name)
#self.mapping()
def goldPosts(self,nemb=0):
data = self.csvfile.reading(nemb)
if data != []:
data2 = [data[-4][1],data[-4][4],min(data[-3][3],data[-2][3],data[-1][3])]
data2.sort()
else:
return [1,[]]
if twinpost(data) and goldpost(data):
return [self.csvfile.DATE,data2]
else:
return [1,[]]
def mapping(self):
li=[]
for n in range(self.nemb):
li.append(self.goldPosts(n))
lis = []
for m in li:
if type(m[0]) == str:
lis.append(m)
return [self.name,lis]
class CsvRead4:
def __init__(self,filename=‘strs‘):
global DIRALL
DIRALL="C:\\Users\zyh\Desktop\demo\\gp\\data"
self.filename=DIRALL + ‘\\‘ + filename
self.DATE=‘‘
self.data=[]
self.read()
def read(self):
csv = open(self.filename,‘r‘)
csvdata = csv.readlines()
csv.close()
data1=[]
for n in range(len(csvdata)):
data1.append(csvdata[n].split(‘\n‘))
data2=[]
for n in range(len(data1)):
data2.append(data1[n][0].split(‘\t‘))
data3=data2[:-1]
self.data=data3
def reading(self,nem=0):
if nem==0:
if len(self.data) >= 7:
self.DATE = self.data[-4][0]
return self.data[-5:]
else:
return []
else:
data4=self.data[:-nem]
if len(data4) >= 7:
self.DATE = data4[-4][0]
return data4[-5:]
else:
return []
def main():
"main function"
print(‘begin time ‘+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())))
FilterFile="C:\\Users\zyh\Desktop\demo\\gp\\data"
fi=filefilter(‘.csv‘,FilterFile)
for ll in fi:
a=SingleMapping3(ll,150)
codenem,lis=a.mapping()
if len(lis)>0:
print(codenem)
print (‘共‘+str(len(lis))+‘个黄金柱‘)
for l in lis:
if l[0]>‘06/10/2015‘:
print(l)
print(‘end time ‘+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())))
if __name__ == ‘__main__‘:
main()