Giter Club home page Giter Club logo

wolfquant's Introduction

wolfquant

重新构建集成交易、回测、建模的AI投资框架。 期货接口基于pyctp,使用语言python3.6,环境linux64/ubuntu

使用

交易接口调用

1.安装包

$ python setup.py

2.复制配置文件,更新配置信息

$ cp etc/config-default.json config.json

3.使用案例

# 通过以下方式使用期货版API
>>>from wolfquant.api.future import ApiStruct, MdApi, TraderApi

4.运行测试案例

$ cd tests
$ python test_api.py

因子构建并采用机器学习模型训练

import pandas as pd
import numpy as np
import tushare as ts
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis
from sklearn.svm import LinearSVC, SVC
from sklearn.metrics import confusion_matrix
from wolfquant.utils.factor_utils import Factor_pipeline
from wolfquant.factors import trade_factors as tf

# 获取数据
datasets = ts.get_k_data('000300', start='2005-01-01', index=True).set_index('date')
datasets.index = pd.to_datetime(datasets.index, format='%Y-%m-%d')

# 构建特征
datasets = Factor_pipeline(datasets).add(tf.SMA, 50)\
                                    .add(tf.EWMA, 200)\
                                    .add(tf.BBANDS, 50)\
                                    .add(tf.CCI, 20)\
                                    .add(tf.ForceIndex, 1)\
                                    .add(tf.EVM, 14)\
                                    .add(tf.ROC, 5)\
                                    .add(tf.LAGRETURN, 0)\
                                    .data.dropna()

# 构建标签
datasets['direction'] = np.sign(datasets['LAGRETURN_0']).shift(-1)
datasets = datasets.dropna()

# 构建训练集和测试集
X = datasets[datasets.columns[6:-2]]
y = datasets['direction']
start_test = '2012-01-01'
X_train = X.loc[:start_test]
X_test = X.loc[start_test:]
y_train = y.loc[:start_test]
y_test = y.loc[start_test:]

# 构建模型
print('Hit rates/Confusion Matrices:\n')
models = [('LR', LogisticRegression()),
          ('LDA', LinearDiscriminantAnalysis()),
          ('QDA', QuadraticDiscriminantAnalysis()),
          ('LSVC', LinearSVC()),
          ('RSVM', SVC()),
          ('RF', RandomForestClassifier(n_estimators=1000))]

# 遍历模型
for m in models:
    # 训练模型
    m[1].fit(X_train, y_train)
    # 预测模型
    pred = m[1].predict(X_test)
    # 输出hit-rate和交叉验证结果
    print("%s:\n%0.3f" % (m[0], m[1].score(X_test, y_test)))
    print("%s\n" % confusion_matrix(pred, y_test))

运行策略回测

import datetime
import numpy as np

from wolfquant.backtest import Backtest
from wolfquant.data import HistoricCSVDataHandler
from wolfquant.event import SignalEvent, OrderEvent
from wolfquant.execution import SimulatedExecutionHandler
from wolfquant.portfolio import NaivePortfolio
from wolfquant.strategy import Strategy

# 创建策略
class BuyAndHoldStrategy(Strategy):
    """一直持有策略
    results:
    Total Return: -2.74%
    Sharpe Ratio: -0.05
    Max Drawdown: 25.00%
    Drawdown Duration: 584
    交易信号数: 1
    下单数: 1
    成交数: 1
    """
    def init(self):
        self.bought = dict([(symbol, False) for symbol in self.symbol_list])

    def handle_bar(self, bar_dict):
        for s in self.symbol_list:
            if self.bought[s] is False:
                self.order_shares(s, 10)
                self.bought[s] = True

# 运行策略
csv_dir = 'data/'
symbol_list = ['hs300']
initial_capital = 100000.0
start_date = datetime.datetime(2015, 4, 8, 0, 0, 0)
end_date = datetime.datetime(2017, 10, 27, 0, 0, 0)
heartbeat = 0.0
backtest = Backtest(csv_dir,
                    symbol_list,
                    initial_capital,
                    heartbeat,
                    start_date,
                    end_date,
                    HistoricCSVDataHandler,
                    SimulatedExecutionHandler,
                    NaivePortfolio,
                    MovingAverageCrossStrategy)
backtest.simulate_trading()

路线图

0.0.0

  • 实现了期货python版的交易接接口
  • 整理交易接口的使用文档

0.0.1

  • 添加交易接口的测试案例

0.0.2

  • 期货交易接口二次开发。
  • 添加feature处理模块。

0.0.3

  • 添加回测模块。
  • 更新策略测试案例。

0.0.4

  • 调整回测功能函数,添加交易接口。
    • order_shares: 按照数据来买
    • order_value: 按照市值来买
    • order_percent: 按照仓位百分比来买
    • order_target_percent: 买到目标仓位
  • 接受并保存期货高频行情数据。

0.0.5

  • 添加tushare数据接口
  • 重新定义回测API

附言

该项目会长期做,有志同道合的小伙伴,欢迎一起入坑,我的微信号wolfquant。

wolfquant's People

Contributors

rickyall avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.