Python 机器学习之 SVM 预测买卖(标的物:比特币)
Python入门简单策略 sklearn 机器学习库的使用
什么是 SVM ?
支持向量机/support vector machine (SVM)。
当然首先看一下wiki.
Support Vector Machines are learning models used for classification: which individuals in a population belong where? So… how do SVM and the mysterious “kernel” work?
好吧,故事是这样子的:
在很久以前的情人节,大侠要去救他的爱人,但魔鬼和他玩了一个游戏。
魔鬼在桌子上似乎有规律放了两种颜色的球,说:“你用一根棍分开它们?要求:尽量在放更多球之后,仍然适用。”
于是大侠这样放,干的不错?
然后魔鬼,又在桌上放了更多的球,似乎有一个球站错了阵营。
SVM就是试图把棍放在最佳位置,好让在棍的两边有尽可能大的间隙。
现在即使魔鬼放了更多的球,棍仍然是一个好的分界线。
然后,在SVM 工具箱中有另一个更加重要的 trick。 魔鬼看到大侠已经学会了一个trick,于是魔鬼给了大侠一个新的挑战。
现在,大侠没有棍可以很好帮他分开两种球了,现在怎么办呢?当然像所有武侠片中一样大侠桌子一拍,球飞到空中。然后,凭借大侠的轻功,大侠抓起一张纸,插到了两种球的中间。
现在,从魔鬼的角度看这些球,这些球看起来像是被一条曲线分开了。
再之后,无聊的大人们,把这些球叫做 「data」,把棍子 叫做 「classifier」, 最大间隙trick 叫做「optimization」, 拍桌子叫做「kernelling」, 那张纸叫做「hyperplane」。
参考:
Please explain Support Vector Machines (SVM) like I am a 5 year old. : MachineLearning
Support Vector Machines explained well
https://www.youtube.com/watch...
[以上例子引用自 网络,侵删]
一个 SVM 应用于 bitcoin trading 的 DEMO
回测系统自带的库有
实现语言 Python 2numpy pandas TA-Lib scipy statsmodels sklearn cvxopt hmmlearn pykalman arch matplotlib
实盘需要在托管者所在机器安装策略需要的库
OK , Talk is cheap, Show code to you!
from sklearn import svm import numpy as 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() 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 > 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 if diff > SpreadVal: Y = 0 sets[0] = True elif diff < -SpreadVal: Y = 1 sets[1] = True else: Y = 2 sets[2] = True output_Y.append(Y) if None in sets: Log("样本不足, 无法预测 ...") continue n += 1 clf = svm.LinearSVC() clf.fit(inputs_X, output_Y) predict = clf.predict(np.array([bar.Open, bar.Close]).reshape((1, -1))) pTime = bar.Time Log("预测当前Bar结束:", bar.Time, ['涨', '跌', '横'][predict]) if marketPosition == 0: if predict == 0: exchange.Buy(initAccount.Balance/2) marketPosition = 1 elif predict == 1: exchange.Sell(initAccount.Stocks/2) marketPosition = -1 else: nowAccount = exchange.GetAccount() if marketPosition > 0 and predict != 0: exchange.Sell(nowAccount.Stocks - initAccount.Stocks) nowAccount = exchange.GetAccount() marketPosition = 0 elif marketPosition < 0 and predict != 1: while True: dif = initAccount.Stocks - nowAccount.Stocks if dif < 0.01: break ticker = exchange.GetTicker() exchange.Buy(ticker.Sell + (ticker.Sell-ticker.Buy)*2, dif) while True: Sleep(1000) orders = exchange.GetOrders() for order in orders: exchange.CancelOrder(order.Id) if len(orders) == 0: break nowAccount = exchange.GetAccount() marketPosition = 0 if marketPosition == 0: LogProfit(_N(nowAccount.Balance - initAccount.Balance, 4), nowAccount)
小样本测试 预测对的概率是 三分之一, 是不是很有趣!(预测情况分三种 涨、跌、横盘)
原文链接 : https://www.botvs.com/strateg...