forked from gaotianpu/forecast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
computeFutureNPrate.py
200 lines (179 loc) · 7 KB
/
computeFutureNPrate.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import web
import da
from config import dbr,dbw
import comm
def getFutureRange(prate):
if prate>10: #1%
return 2
if prate<=10:
return 1
return 0
# if prate < -20: #-2%
# return 1
# if prate<=10 and prate>=-20:
# return 2
# return 0
def computeFuture(records):
for r in records:
i = records.index(r)
if i>1:
prate = int((records[i-2].close - records[i-1].close) *1000 / records[i-1].close)
frange = getFutureRange(prate)
sql = 'update stock_daily set future1_prate=%s,future1_range=%s where pk_id=%s' % (prate,frange,r.pk_id)
dbw.query(sql)
if i>2:
prate = int((records[i-3].close - records[i-1].close) *1000 / records[i-1].close )
frange = getFutureRange(prate)
sql = 'update stock_daily set future2_prate=%s,future2_range=%s where pk_id=%s' % (prate,frange,r.pk_id)
dbw.query(sql)
if i>3:
prate = int((records[i-4].close - records[i-1].close) *1000 / records[i-1].close )
frange = getFutureRange(prate)
sql = 'update stock_daily set future3_prate=%s,future3_range=%s where pk_id=%s' % (prate,frange,r.pk_id)
dbw.query(sql)
def computeTrend(records):
count = len(records)
for i in range(0,count):
if count-i>2:
t3 = comm.get_trend(records[i:i+3])
sql = 'update stock_daily set trend_3=%s where pk_id=%s' % (t3,records[i].pk_id)
dbw.query(sql)
if count-i>4:
t5 = comm.get_trend(records[i:i+5])
sql = 'update stock_daily set trend_5=%s where pk_id=%s' % (t5,records[i].pk_id)
dbw.query(sql)
def computeVolume(records):
count = len(records)
for i in range(0,count):
r10 = records[i:i+10]
l = [r.volume for r in r10]
volume_avg_10 = reduce(lambda x, y: x + y, l) / len(l)
volume_p = float(records[i].volume) / volume_avg_10
#对volume进行分类
volume_level = 0
if volume_p>=3:
volume_level = 31
elif volume_p<3 and volume_p>=2:
volume_level = 21
elif volume_p<2 and volume_p>=0.5:
volume_level = 19
elif volume_p<0.5 and volume_p>=0.33:
volume_level = 12
elif volume_p<0.33:
volume_level = 13
else:
volume_level = 0
sql = 'update stock_daily set volume_avg_10=%s,volume_level=%s where pk_id=%s' % (volume_avg_10,volume_level,records[i].pk_id)
dbw.query(sql)
def computeCandle(records):
count = len(records)
for i in range(0,count):
r = records[i]
result = comm.get_candle_2(r.open,r.close,r.high,r.low)
up_or_down = 2 if result[1]>0 else 1
sql = 'update stock_daily set candle_sort=%s,up_or_down=%s where pk_id=%s' % (result[4],up_or_down,records[i].pk_id)
dbw.query(sql)
def computeMA(records):
count = len(records)
for i in range(0,count):
ma5 = ma10 = 0
if count-i>=5:
l5 = [r.close for r in records[i:i+5]]
ma5 = reduce(lambda x, y: x + y , l5) / 5
if count-i>=10:
l10 = [r.close for r in records[i:i+10]]
ma10 = reduce(lambda x, y: x + y , l10) / 10
ma_5_10 = 0
if ma5<>0 and ma10<>0:
ma_5_10 = 2 if ma5>ma10 else 1
sql = 'update stock_daily set ma_5=%s,ma_10=%s,ma_5_10=%s where pk_id=%s' % (ma5,ma10,ma_5_10,records[i].pk_id)
dbw.query(sql)
import computeP
def computeForecast(records,categories,allpp):
count = len(records)
for i in range(0,count):
if not records[i].trend_3:
continue
if not records[i].trend_5:
continue
if not records[i].candle_sort:
continue
if not records[i].up_or_down:
continue
if not records[i].volume_level:
continue
if not records[i].jump_level:
continue
if not records[i].ma_5_10:
continue
fields = {'trend_3':records[i].trend_3,
'trend_5':records[i].trend_5,
'candle_sort':records[i].candle_sort,
#'up_or_down':records[i].up_or_down,
'volume_level':records[i].volume_level
,'jump_level':records[i].jump_level
,'ma_5_10':records[i].ma_5_10
}
#print fields
x = computeP.run(fields,categories,allpp)
sql = 'update stock_daily set forecast=%s where pk_id=%s' % (x[2]/x[1],records[i].pk_id)
dbw.query(sql)
def computeLastClosePrice(records):
count = len(records)
for i in range(0,count):
if (count-i) == 1:
break
last_close = records[i+1].close
open_last_close = records[i].open - records[i+1].close
high_low = records[i].high - records[i].low
close_open = records[i].close - records[i].open
price_rate = (records[i].close - last_close)/last_close
high_rate = (records[i].high - last_close)/last_close
low_rate = (records[i].low - last_close)/last_close
high_low_rate = high_rate - low_rate
candle = comm.get_candle_2(records[i].open,records[i].close,records[i].high,records[i].low)
range_1 = candle[0]
range_2 = candle[1]
range_3 = candle[2]
jump_rate = open_last_close / last_close
jump_level = 0
if jump_rate*100>=2:
jump_level = 3
elif jump_rate*100>=0 and jump_rate*100<2:
jump_level = 2
elif jump_rate*100<0:
jump_level = 1
else:
jump_level = 50
pk_id = records[i].pk_id
dbw.update('stock_daily',high_low=high_low,last_close=last_close,open_last_close=open_last_close,
close_open = close_open,
price_rate=price_rate,high_rate=high_rate,low_rate=low_rate,hig_low_rate=high_low_rate,
range_1 = range_1,range_2 = range_2,range_3 = range_3,
jump_level = jump_level, jump_rate=jump_rate,
where="pk_id=$pk_id",vars=locals())
def run(stock_daily_records):
computeLastClosePrice(stock_daily_records)
computeFuture(stock_daily_records)
computeTrend(stock_daily_records)
computeCandle(stock_daily_records)
computeVolume(stock_daily_records)
computeMA(stock_daily_records)
def run_all():
# categories = computeP.getCategories()
# allpp = computeP.loadP()
stocks = da.stockbaseinfos.load_all_stocks()
for s in stocks:
if int(s.stock_no)<300006:continue
print s.stock_no
stock_daily_records = da.stockdaily.load_stockno(s.stock_no)
run(stock_daily_records)
# computeForecast(stock_daily_records,categories,allpp)
def run_one(stock_no):
stock_daily_records = da.stockdaily.load_stockno(stock_no)
run(stock_daily_records)
if __name__ == '__main__':
#tmp()
run_all()