पायथन मशीन लर्निंग के एसवीएम ने खरीदारी की भविष्यवाणी की

लेखक:शून्य, दिनांकः 2016-09-13 17:55:54
टैगःपायथनमशीन लर्निंग

पायथन का परिचय सरल रणनीति sklearn मशीन सीखने के पुस्तकालय का उपयोग करना

एक रिडिक्शन सिस्टम के पास एक लाइब्रेरी है numpy pandas TA-Lib scipy statsmodels sklearn cvxopt hmmlearn pykalman arch मैटप्लोटलिब

वास्तविक डिस्क को होस्ट की स्थित मशीन पर स्थापित करने की नीति के लिए आवश्यक भंडार की आवश्यकता होती है


'''backtest
start: 2019-09-06 00:00:00
end: 2019-10-05 00:00:00
period: 1h
exchanges: [{"eid":"Bitfinex","currency":"BTC_USD"}]
'''

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)))[0]
        pTime = bar.Time
        Log("预测当前Bar结束:", bar.Time, ['涨', '跌', '横'][predict])
        if marketPosition == 0:
            if predict == 0:
                exchange.Buy(-1, initAccount.Balance/2)
                marketPosition = 1
            elif predict == 1:
                exchange.Sell(-1, initAccount.Stocks/2)
                marketPosition = -1
        else:
            nowAccount = exchange.GetAccount()
            if marketPosition > 0 and predict != 0:
                exchange.Sell(-1, 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)
                

संबंधित

अधिक

टाइटिसexchange.GetRecords ((() क्या यह पुनः परीक्षण करने योग्य नहीं है?

एनमैं अभी पायथन सीख रहा हूँ, Z बहुत तेज है।

मुआबुढ़ापा और मेहनत