【QuantOS】jaqs实例代码(可以使用版本)

简述

jaqs给的demo,其实一开始是没办法用的(在windows条件下)。

还有一些小细节。

  • data_config和trade_config的username 和 password,写的其实是手机号和命令。
  • 命令是在官网上登录之后,把鼠标移到右上角 就可以看到一个命令。然后点击。可以看到这个了。

代码

只要把代码中的账号和密码都输入之后,就可以运行了,运行结束之后,找到report.html用浏览器打开就好了。

"""
A very first example of AlphaStrategy back-test:
    Market value weight among UNIVERSE.
    Benchmark is HS300.

"""

from __future__ import print_function, unicode_literals, division, absolute_import

import os

from jaqs.data import RemoteDataService, DataView

import jaqs.util as jutil

from jaqs.trade import model
from jaqs.trade import (AlphaStrategy, AlphaBacktestInstance, AlphaTradeApi,
                        PortfolioManager, AlphaLiveTradeInstance, RealTimeTradeApi)
import jaqs.trade.analyze as ana

data_config = {
    "remote.data.address": "tcp://data.quantos.org:8910",
    "remote.data.username": "telephoneNumber",
    "remote.data.password": "token"
}
trade_config = {
    "remote.trade.address": "tcp://gw.quantos.org:8901",
    "remote.trade.username": "telephoneNumber",
    "remote.trade.password": "token"
}

# # Data files are stored in this folder:
# dataview_store_folder = '../../output/simplest/dataview'
#
# # Back-test and analysis results are stored here
# backtest_result_folder = '../../output/simplest'
#

# change 1
# Data files are stored in this folder:
dataview_store_folder = './output/simplest/dataview'

# Back-test and analysis results are stored here
backtest_result_folder = './output/simplest'


def checkdir(path):
    if not os.path.exists(path):
        os.makedirs(path)


checkdir(dataview_store_folder)
checkdir(backtest_result_folder)
# END change 1


UNIVERSE = '000807.SH'


def save_data():
    """
    This function fetches data from remote server and stores them locally.
    Then we can use local data to do back-test.
    """
    dataview_props = {'start_date': 20170101,  # Start and end date of back-test
                      'end_date': 20171030,
                      'universe': UNIVERSE,  # Investment universe and performance benchmark
                      'benchmark': '000300.SH',
                      'fields': 'total_mv,turnover',  # Data fields that we need
                      'freq': 1  # freq = 1 means we use daily data. Please do not change this.
                      }

    # RemoteDataService communicates with a remote server to fetch data
    ds = RemoteDataService()

    # Use username and password in data_config to login
    ds.init_from_config(data_config)

    # DataView utilizes RemoteDataService to get various data and store them
    dv = DataView()
    dv.init_from_config(dataview_props, ds)
    dv.prepare_data()
    dv.save_dataview(folder_path=dataview_store_folder)


def do_backtest():
    # Load local data file that we just stored.
    dv = DataView()
    dv.load_dataview(folder_path=dataview_store_folder)

    backtest_props = {"start_date": dv.start_date,  # start and end date of back-test
                      "end_date": dv.end_date,
                      "period": "month",  # re-balance period length
                      "benchmark": dv.benchmark,  # benchmark and universe
                      "universe": dv.universe,
                      "init_balance": 1e8,  # Amount of money at the start of back-test
                      "position_ratio": 1.0,  # Amount of money at the start of back-test
                      }
    backtest_props.update(data_config)
    backtest_props.update(trade_config)

    # Create model context using AlphaTradeApi, AlphaStrategy, PortfolioManager and AlphaBacktestInstance.
    # We can store anything, e.g., public variables in context.

    trade_api = AlphaTradeApi()
    strategy = AlphaStrategy(pc_method='market_value_weight')
    pm = PortfolioManager()
    bt = AlphaBacktestInstance()
    context = model.Context(dataview=dv, instance=bt, strategy=strategy, trade_api=trade_api, pm=pm)

    bt.init_from_config(backtest_props)
    bt.run_alpha()

    # After finishing back-test, we save trade results into a folder
    bt.save_results(folder_path=backtest_result_folder)


def do_livetrade():
    dv = DataView()
    dv.load_dataview(folder_path=dataview_store_folder)

    props = {"period": "day",
             "strategy_no": 1044,
             "init_balance": 1e6}
    props.update(data_config)
    props.update(trade_config)

    strategy = AlphaStrategy(pc_method='market_value_weight')
    pm = PortfolioManager()

    bt = AlphaLiveTradeInstance()
    trade_api = RealTimeTradeApi(props)
    ds = RemoteDataService()

    context = model.Context(dataview=dv, instance=bt, strategy=strategy, trade_api=trade_api, pm=pm, data_api=ds)

    bt.init_from_config(props)
    bt.run_alpha()

    goal_positions = strategy.goal_positions
    print("Length of goal positions:", len(goal_positions))
    task_id, msg = trade_api.goal_portfolio(goal_positions)
    print(task_id, msg)


def analyze_backtest_results():
    # Analyzer help us calculate various trade statistics according to trade results.
    # All the calculation results will be stored as its members.
    ta = ana.AlphaAnalyzer()
    dv = DataView()
    dv.load_dataview(folder_path=dataview_store_folder)

    ta.initialize(dataview=dv, file_folder=backtest_result_folder)

    ta.do_analyze(result_dir=backtest_result_folder,
                  selected_sec=list(ta.universe)[:3])


if __name__ == "__main__":
    is_backtest = True

    if is_backtest:
        save_data()
        do_backtest()
        analyze_backtest_results()
    else:
        save_data()
        do_livetrade()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肥宅_Sean

公众号“肥宅Sean”欢迎关注

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值