-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpressure_line.py
123 lines (113 loc) · 4.01 KB
/
pressure_line.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from __future__ import division
import readDayRecord
EndPriceList = [12,13,14,15,10,10,7,11,13,9,8,10]
x1 = 2
x2 = 4
y1 = EndPriceList[x1]
y2 = EndPriceList[x2]
k = (y2-y1)/(x2-x1)
d = y2-k*x2
def GetKDfrom2Point(x1, x2, Ylist):
if len(Ylist) == 0 or len(Ylist) == 1:
return [0, 0]
y1 = Ylist[x1]
y2 = Ylist[x2]
k = (y2-y1)/(x2-x1)
d = y2-k*x2
return [k, d]
def IsKDCoverAll(Ylist, k, d, DownUp):
count = len(Ylist)
if count == 0:
return "false"
for i in range(0, count):
realY = Ylist[i]
CalY = k*i + d
if realY >= CalY and DownUp == "Down":
return "false"
if realY <= CalY and DownUp == "Up":
return "false"
return "true"
def GetYValue(k, d, x):
return k*x +d
def FindPeakValue(Ylist, HighOrLow):
return Ylist[FindPeakValueIndex(Ylist, HighOrLow)]
def FindPeakValueIndex(Ylist, HighOrLow):
i = 0
peak = Ylist[0]
for j in range(1, len(Ylist)):
if Ylist[j] >= peak and HighOrLow == "High":
i = j
peak = Ylist[j]
if Ylist[j] <= peak and HighOrLow == "Low":
i = j
peak = Ylist[j]
return i
[k, d] = GetKDfrom2Point(0, 4, EndPriceList)
def FoundKD_DownLine_seperate(Ylist):
TopIndex = FindPeakValueIndex(Ylist, "High")
toDoYlist = Ylist[TopIndex:]
count = len(toDoYlist)
for i in range(1, count):
[k, d] = GetKDfrom2Point(0, i, toDoYlist)
result = IsKDCoverAll(toDoYlist[1:], k, d, "Down")
if result == "true":
print "found k: " + str(k) + "and d: " + str(d)
return [k, d]
return [0, 0]
def FoundKD_Line(Ylist, Peak, DownUp):
TopIndex = FindPeakValueIndex(Ylist, Peak)
if TopIndex == 0:
toDoYlist = Ylist
else:
toDoYlist = Ylist[TopIndex:]
count = len(toDoYlist)
for i in range(1, count):
[k, d] = GetKDfrom2Point(0, i, toDoYlist)
result = IsKDCoverAll(toDoYlist[1:], k, d, DownUp)
if result == "true":
return [k, d, TopIndex]
return [0, 0, 0]
def FoundKD_UpLine(Ylist):
return FoundKD_Line(Ylist, "Low", "Up")
def FoundKD_DownLine(Ylist):
return FoundKD_Line(Ylist, "High", "Down")
def GetDownPressureValue(Ylist, tday):
[k, d, DownStartIndex] = FoundKD_DownLine(Ylist)
if [k, d] == [0, 0]:
print "failed to calc pressure for the input value"
print Ylist
return []
return k*(len(Ylist[DownStartIndex:]) + tday) + d
def GetUpPressureValue(Ylist, tday):
[k, d, DownStartIndex] = FoundKD_UpLine(Ylist)
if [k, d] == [0, 0]:
print "failed to calc pressure for the input value"
print Ylist
return []
return k*(len(Ylist[DownStartIndex:]) + tday) + d
def GetUpPressureValue_ByStockID(stockid, starttime, endtime, nextdayoffset):
end_price_group = readDayRecord.GetGroupEndPrice(starttime, endtime, stockid)
nextday_list = []
for i in range(1, nextdayoffset):
press_value = GetUpPressureValue(end_price_group, i)
nextday_list.append(round(press_value, 2))
return nextday_list
def GetDownPressureValue_ByStockID(stockid, starttime, endtime, nextdayoffset):
end_price_group = readDayRecord.GetGroupEndPrice(starttime, endtime, stockid)
nextday_list = []
for i in range(1, nextdayoffset):
press_value = GetDownPressureValue(end_price_group, i)
nextday_list.append(round(press_value, 2))
return nextday_list
stockid = raw_input("input stockid:")
startday = int(raw_input("input start time"))
endday = int(raw_input("input end day:"))
nextday = int(raw_input("input how many days you want to know press value from end day:"))
print "downline pressure:" + str(GetDownPressureValue_ByStockID(stockid, startday, endday, nextday))
print "upline pressure:" + str(GetUpPressureValue_ByStockID(stockid, startday, endday, nextday))
"""
BottonIndex = FindPeakValueIndex(EndPriceList, "Low")
toDoDownList = EndPriceList[BottonIndex:]
[k, d] = GetKDfrom2Point(0, 1, toDoDownList)
print IsKDCoverAll(toDoDownList[1:], k, d, "Up")
"""