1. 定义方法
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
|
def predictAll(tickers, startdt=
'36'
, enddt =
'today'
, predictdays =
1
):
if
enddt ==
'today'
:
enddt = datetime.date.today().strftime(
'%Y-%m-%d'
)
if
len(startdt) <
4
:
startdt = (datetime.date.today() - datetime.timedelta(
int
(startdt) *
365
/
12
)).isoformat()
result = []
for
ticker in tickers:
try
:
df = sdk.getHist([ticker], startdt, enddt) #.query(
"isopen==1"
)
# print df
except:
msg =
'ticker '
+ ticker
# print msg
else
:
dfochl = df[[
'open'
,
'close'
,
'high'
,
'low'
]].as_matrix()
x = dfochl[:-predictdays]
dfh = df[[
'high'
]].as_matrix()
yh = dfh[predictdays :]
dfl = df[[
'low'
]].as_matrix()
yl = dfl[predictdays :]
dfo = df[[
'open'
]].as_matrix()
yo = dfo[predictdays :]
dfc = df[[
'close'
]].as_matrix()
yc = dfc[predictdays :]
x2 = dfochl[-predictdays:]
mh = LinearRegression()
mh.fit(x, yh)
yh2 = mh.predict(x2)
ml = LinearRegression()
ml.fit(x, yl)
yl2 = ml.predict(x2)
mo = LinearRegression()
mo.fit(x, yo)
yo2 = mo.predict(x2)
mc = LinearRegression()
mc.fit(x, yc)
yc2 = mc.predict(x2)
name = df[
'sec_short_name'
][
0
]
i =
0
while
i < predictdays:
ret = {
'Day'
:
'Day '
+ str(i +
1
),
'Ticker'
: ticker,
'Name'
: name,
'High'
: str(round(yh2[i],
2
)),
'Low'
: str(round(yl2[i],
2
)),
'Open'
: str(round(yo2[i],
2
)),
'Close'
: str(round(yc2[i],
2
))
}
result.append(ret)
i +=
1
return
result
|
2. 定义接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# GET /sensitop/api/v1/analyzers/regression
import
RegessionAnalyzer as regession
import
logging
# REQUEST = json.dumps({
#
'path'
: {},
#
'args'
: {
'tickers'
:[
'000078,600048'
],
'startdt'
:[
'2016-01-01'
],
'enddt'
:[
'2016-12-30'
],
#
'predictdays'
:[
'2'
]}
# })
def __0014__(request):
logging.warning(request)
tickers = getFirstParam(request,
"tickers"
).strip().split(
','
)
startdt = getFirstParam(request,
"startdt"
)
enddt = getFirstParam(request,
"enddt"
)
enddt = __notFuture__(enddt)
predictdays = getFirstParam(request,
"predictdays"
)
# print tickers
return
json.dumps(regession.predictAll(tickers, startdt, enddt,
int
(predictdays)))
print __0014__(json.loads(REQUEST))
|
3. 定义widget接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# GET /sensitop/widgets/predict
uid = __getUId__()
template = open(__templateRootPath__ +
"predict.html"
)
try
:
html = template.read().format(endpoint =
"waima.imwork.net:16301"
,
tickers =
"__tickers__%s"
%uid,
startdt =
'__startdt__%s'
%uid,
enddt =
'__enddt__%s'
%uid,
predictdays =
'__predictdays__%s'
%uid,
btnSubmit =
"__btnSubmit__%s"
%uid,
id =
"__id__%s"
%uid,
btnHideOptions =
"__btnHideOptions__%s"
%uid,
pnlOptions =
"__pnlOptions__%s"
%uid,
dt_basic =
"__dt_basic__%s"
%uid,
results =
"__results__%s"
%uid
)
print html
finally
:
template.close()
|
4. 定义widget页面
1
2
|
<!-- widget-start -->
<!--
new
widget -->
|
1
2
|
<!-- end widget -->
<!-- widget-end -->
|
本文转自疯吻IT博客园博客,原文链接:http://www.cnblogs.com/fengwenit/p/6204433.html,如需转载请自行联系原作者