创建widget

1. 定义方法

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. 定义接口

# 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接口

# 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天 3天
结果说明:这里显示未来1-3天的各种预测值:开盘价、收盘价、最高价、最低价。
第几天 股票代码 股票名称 最高价 最低价 开盘价 收盘价
      

      

你可能感兴趣的:(创建widget)