from sklearn import svm
def main():
preTime = 0
n = 0
success = 0
predict = None
pTime = None
spread = 2
while True:
r = exchange.GetRecords()
if len(r) < 60:
continue
bar = r[len(r)-1]
if bar.Time > preTime:
preTime = bar.Time
if pTime is not None and r[len(r)-2].Time == pTime:
diff = r[len(r)-2].Close - r[len(r)-3].Close
if diff > spread:
success += 1 if predict == 0 else 0
elif diff < -spread:
success += 1 if predict == 1 else 0
else:
success += 1 if predict == 2 else 0
pTime = None
LogStatus("预测次数", n, "成功次数", success)
else:
Sleep(1000)
continue
diabetes_X, diabetes_Y = [], []
sets = [None, None, None]
for i in range(1, len(r)-2, 1):
diabetes_X.append([r[i].Open, r[i].Close])
Y = 0
diff = r[i+1].Close - r[i].Close
if diff > spread:
Y = 0
sets[0] = True
elif diff < -spread:
Y = 1
sets[1] = True
else:
Y = 2
sets[2] = True
diabetes_Y.append(Y)
if None in sets:
Log("样本不足, 无法预测 ...")
continue
clf = svm.LinearSVC()
clf.fit(diabetes_X, diabetes_Y)
predict = clf.predict([bar.Open, bar.Close])
pTime = bar.Time
Log("预测当前Bar结束:", bar.Time, ['涨', '跌', '横'][predict])
n += 1
# 宫城同学注释
from sklearn import svm #=====================================接入sklearn库svm函数
import numpy as np #============= =============================引用库numpy并更名np
def main():
preTime = 0
n = 0
success = 0
predict = None
pTime = None
marketPosition = 0
initAccount = exchange.GetAccount()
Log("Running...")
while True:
r = exchange.GetRecords() #============================================获得records
if len(r) < 60: #============================================长度小于60 重新获得
continue
bar = r[len(r)-1] #=============================================最后一根k
if bar.Time > preTime:
preTime = bar.Time #==============================================上次时间循环更新
if pTime is not None and r[len(r)-2].Time == pTime: #==============存在预言并且预言做出时间为上一个bar时间
diff = r[len(r)-2].Close - r[len(r)-3].Close #=====================计算倒数第二和第三线的收盘差价
if diff > SpreadVal: #==========================spreadval
success += 1 if predict == 0 else 0 #=========================?
elif diff < -SpreadVal:
success += 1 if predict == 1 else 0
else:
success += 1 if predict == 2 else 0
pTime = None
LogStatus("预测次数", n, "成功次数", success, "准确率:", '%.3f %%' % round(float(success) * 100 / n, 2))
else:
Sleep(1000)
continue
inputs_X, output_Y = [], [] #==================神经网络输入输出定义
sets = [None, None, None]
for i in xrange(1, len(r)-2, 1):
inputs_X.append([r[i].Open, r[i].Close])#=====================输入数组数据二维
Y = 0
diff = r[i+1].Close - r[i].Close #=========================结果diff
if diff > SpreadVal:
Y = 0
sets[0] = True #================================set
elif diff < -SpreadVal:
Y = 1
sets[1] = True
else:
Y = 2
sets[2] = True
output_Y.append(Y) #======================================输出端结果y
if None in sets: # ==========================================有的可能情况不存在样本数据
Log("样本不足, 无法预测 ...")
continue #=========================样本加载完成
n += 1
clf = svm.LinearSVC()
clf.fit(inputs_X, output_Y) #================================启动svm分析函数
predict = clf.predict(np.array([bar.Open, bar.Close]).reshape((1, -1)))#=============启动预言函数
pTime = bar.Time #====================================表示存在预言
Log("预测当前Bar结束:", bar.Time, ['涨', '跌', '横'][predict])#======================神经网络训练完毕
if marketPosition == 0:
モモックスこの策略は少し問題があるか,未来関数 bar = r[len(r) -1], predict = clf.predict ((np.array (([bar.Open, bar.Close]).reshape ((((1, -1))) bar は現在のk行であり,未完了の bar で,close は変化しているので,予測参数として取ります.
発明者 量化 - 微かな夢私は研究を研究しています.