profolio.py 文件源码

python
阅读 16 收藏 0 点赞 0 评论 0

项目:stock-price-prediction 作者: chinuy 项目源码 文件源码
def smart_trade(etf, method, delta):
    parameters = [8, 0.0125]

    data = util.get_data(etf, '2014/1/1', '2016/12/31')

    # keep a copy for unscaled data for later gain calculation
    # TODO replace by MinMax_Scaler.inverse_transform()
    #
    # the first day of test is 2015/12/31. Using this data on this day to predict
    # Up/Down of 2016/01/04
    test = data[data.index > datetime.datetime(2015,12,30)]

    le = preprocessing.LabelEncoder()
    test['UpDown'] = (test['Close'] - test['Open']) / test['Open']
    threshold = 0.000
    test.UpDown[test.UpDown >= threshold] = 'Up'
    test.UpDown[test.UpDown < threshold] = 'Down'
    test.UpDown = le.fit(test.UpDown).transform(test.UpDown)
    test.UpDown = test.UpDown.shift(-1) # shift 1, so the y is actually next day's up/down

    dataMod = util.applyFeatures(data, range(1, delta))
    dataMod = util.preprocessData(dataMod)

    tr = dataMod[dataMod.index <= datetime.datetime(2015,12,30)]
    #tr = dataMod[dataMod.index <= datetime.datetime(2016,06,30)]
    te = dataMod[dataMod.index > datetime.datetime(2015,12,30)]
    te = te[te.columns[0:-1]] # remove Up/Down label from testing
    clf = classifier.buildModel(tr, method, parameters)

    if method == 'RNN':
        te = numpy.reshape(numpy.array(te), (te.shape[0], 1, te.shape[1]))

    pred = clf.predict(te)

    profits = numpy.zeros(pred.size)
    for i in range(pred.size):
      if pred[i] < 0.5: # predict long
        p = (test.Close[i+1] - test.Open[i+1]) / test.Open[i+1]
      else: # predict short
        p = -(test.Close[i+1] - test.Open[i+1]) / test.Open[i+1]
      profits[i] = p
    return profits
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号