Loading ...

简单的SVM 分类算法

Author: 小小梦, Created: 2016-10-31 16:10:38, Updated: 2017-10-11 10:58:50

简单的SVM 分类算法

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:

More

0v3rfL0w SpreadVal是什么鬼

momox 这个策略是不是有点问题,存在未来函数:bar = r[len(r)-1] , predict = clf.predict(np.array([bar.Open, bar.Close]).reshape((1, -1))) bar是当前的k线,是还未结束的bar,close还在变化,就拿来做预测参数,似乎不妥吧

小小 原策略 定义这个为 全局变量,你查原策略 能看到

小小梦 spread 应该是这个,可以看第一个代码,后面那个是 宫城同学的。可以在QQ群联系。

小小梦 额~~我研究研究。