forked from josephzhong/stock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify.py
60 lines (54 loc) · 2.16 KB
/
verify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from stockclass import Stock
import logging
from datetime import timedelta
def getnextworkingday(d):
newday = d + timedelta(days=1)
if newday.weekday() > 4:
newday = newday + timedelta(days=1)
if newday.weekday() > 4:
newday = newday + timedelta(days=1)
return newday
def checkTPFP(positives, conditionday, thres):
tpositives = 0
averGrowth = 0.0
for positive in positives:
index = positive.indexof(conditionday)
if index < 0:
continue
if index + 1 >= len(positive.changePrices):
continue
averGrowth += positive.changePrices[index + 1]
if index > -1 and positive.changePrices[index + 1] >= thres:
tpositives += 1
if len(positives)==0:
return [0.0, 0.0, 0.0]
else:
return [float(tpositives)/len(positives), float(len(positives)-tpositives)/len(positives),
averGrowth/len(positives)]
def averageTPFP(targets, filter, conditionday, thres):
verifydays = []
deltadays = []
weekday = conditionday.weekday()
for deltaday in range(1, 35):
if (weekday - deltaday)%7 >= 0 and (weekday - deltaday)%7 <= 4:
deltadays.append(deltaday)
for deltaday in deltadays:
verifydays.append(conditionday - timedelta(days=deltaday))
sum = [0.0, 0.0, 0.0]
verifytimes = 0
for day in verifydays:
filteredstocks = filter(targets, day)
tpfp = checkTPFP(filteredstocks, day, thres)
logging.info("{0} TP/FP: {1[0]:.2%}/{1[1]:.2%} Average Growth: {1[2]:.3%}"
.format(getnextworkingday(day).isoformat(), tpfp))
sum[0] += tpfp[0]
sum[1] += tpfp[1]
sum[2] += tpfp[2]
if tpfp[0] != 0.0 or tpfp[1] != 0.0:
verifytimes += 1
if verifytimes == 0:
return [0.0, 0.0]
return [sum[0]/verifytimes, sum[1]/verifytimes, sum[2]/verifytimes]
def verify(data, func, verifyday, thres):
verified = averageTPFP(data, func, verifyday, thres)
logging.info(func.__name__ + " Average TP/FP: {0[0]:.2%} / {0[1]:.2%} Average Growth: {0[2]:.3%}".format(verified))