Python 3 Filter Stocks

Source: Internet
Author: User

Refer to a blog, the code is not complete, the address can not find the forgive, add some of their own algorithm.

Memo

#-*-Coding:utf-8-*-
Import OS, re
Import time

def filefilter (compiles = ' STRs ', road= ' str '):
"' compiles is a regular match word, road absolute path '
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 '):
"" "Huang" ""
If float (lis[-4][4]) > float (lis[-4][1]):
#收盘价大于开盘价, normal Yang line, bell or crane mouth, test the crane mouth is not good
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-day list of Huang in a stock"
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 (' Total ' +str (len) + ' Huang ')
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 ()

Python 3 Filter Stocks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.