用Python實現一個SVM分類器策略

步入量化學習艾莉絲發表於2018-12-28

支援向量機(SVM)是什麼意思?

正好最近自己學習機器學習,看到reddit上 Please explain Support Vector Machines (SVM) like I am a 5 year old 的帖子,一個字贊!於是整理一下和大家分享。(如有錯歡迎指教!)

什麼是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?複製程式碼

好吧,故事是這樣子的:

在很久以前的情人節,大俠要去救他的愛人,但魔鬼和他玩了一個遊戲。魔鬼在桌子上似乎有規律放了兩種顏色的球,說:“你用一根棍分開它們?要求:儘量在放更多球之後,仍然適用。”….. 文章 詳細內容 地址:www.botvs.com/bbs-topic/6…

  • 我們來用Python 實現一個 SVM 分類器 預測 買賣

程式是 基於發明者量化平臺的,標的物選擇為電子貨幣,因為電子貨幣適合回測。Python機器學習之SVM 預測買賣,Python入門簡單策略 sklearn 機器學習庫的使用,回測系統自帶的庫有:

numpy pandas TA-Lib scipy statsmodels sklearn cvxopt hmmlearn pykalman arch matplotlib複製程式碼

實盤需要在託管者所在機器安裝策略需要的庫,策略原始碼地址: www.botvs.com/strategy/21…

from sklearn import svmimport numpy as npdef 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) ``` <
br>
[閱讀原文](https://quant.la/Article/View/33/%E7%94%A8Python%E5%AE%9E%E7%8E%B0%E4%B8%80%E4%B8%AASVM%E5%88%86%E7%B1%BB%E5%99%A8%E7%AD%96%E7%95%A5.html)複製程式碼

來源:https://juejin.im/post/5c25e30bf265da61223a69c1

相關文章