Beispiel #1
0
def test_alpha_strategy_dataview():
    dv_subfolder_name = 'test_dataview'
    save_dataview(sub_folder=dv_subfolder_name)

    dv = DataView()

    fullpath = fileio.join_relative_path('../output/prepared',
                                         dv_subfolder_name)
    dv.load_dataview(folder=fullpath)

    props = {
        "benchmark": "000300.SH",
        # "symbol": ','.join(dv.symbol),
        "universe": ','.join(dv.symbol),
        "start_date": dv.start_date,
        "end_date": dv.end_date,
        "period": "month",
        "days_delay": 0,
        "init_balance": 1e9,
        "position_ratio": 0.7,
    }

    gateway = DailyStockSimGateway()
    gateway.init_from_config(props)

    context = model.Context()
    context.register_gateway(gateway)
    context.register_trade_api(gateway)
    context.register_dataview(dv)

    risk_model = model.FactorRiskModel()
    signal_model = model.FactorRevenueModel_dv()
    cost_model = model.SimpleCostModel()

    risk_model.register_context(context)
    signal_model.register_context(context)
    cost_model.register_context(context)

    signal_model.register_func('my_factor', my_factor)
    signal_model.activate_func({'my_factor': {}})
    cost_model.register_func('my_commission', my_commission)
    cost_model.activate_func({'my_commission': {'myrate': 1e-2}})

    strategy = DemoAlphaStrategy(risk_model, signal_model, cost_model)
    # strategy.active_pc_method = 'equal_weight'
    # strategy.active_pc_method = 'mc'
    strategy.active_pc_method = 'factor_value_weight'

    bt = AlphaBacktestInstance_dv()
    bt.init_from_config(props, strategy, context=context)

    bt.run_alpha()

    bt.save_results(fileio.join_relative_path('../output/'))
Beispiel #2
0
def test_backtest_analyze():
    ta = ana.AlphaAnalyzer()
    
    dv = DataView()
    dv.load_dataview(folder_path=dataview_dir_path)
    
    ta.initialize(dataview=dv, file_folder=backtest_result_dir_path)

    print "process trades..."
    ta.process_trades()
    print "get daily stats..."
    ta.get_daily()
    print "calc strategy return..."
    ta.get_returns(consider_commission=True)
    # position change info is huge!
    # print "get position change..."
    # ta.get_pos_change_info()
    
    selected_sec = list(ta.universe)[:2]
    if len(selected_sec) > 0:
        print "Plot single securities PnL"
        for symbol in selected_sec:
            df_daily = ta.daily.get(symbol, None)
            if df_daily is not None:
                ana.plot_trades(df_daily, symbol=symbol, save_folder=backtest_result_dir_path)
    
    print "Plot strategy PnL..."
    ta.plot_pnl(backtest_result_dir_path)
    
    print "generate report..."
    static_folder = fileio.join_relative_path("trade/analyze/static")
    ta.gen_report(source_dir=static_folder, template_fn='report_template.html',
                  out_folder=backtest_result_dir_path,
                  selected=selected_sec)
Beispiel #3
0
def save_dataview(sub_folder='test_dataview'):
    ds = RemoteDataService()
    dv = DataView()

    props = {
        'start_date':
        20141114,
        'end_date':
        20160327,
        'universe':
        '000300.SH',
        'fields': (
            'open,high,low,close,vwap,volume,turnover,'
            # + 'pb,net_assets,'
            + 's_fa_eps_basic,oper_exp,tot_profit,int_income'),
        'freq':
        1
    }

    dv.init_from_config(props, ds)
    dv.prepare_data()

    factor_formula = 'close > Ts_Max(close, 20)'  # 20 days new high
    factor_name = 'new_high'
    dv.add_formula(factor_name, factor_formula, is_quarterly=False)

    dv.save_dataview(
        folder_path=fileio.join_relative_path('../output/prepared'),
        sub_folder=sub_folder)
def test_double_ma():
    prop_file_path = fileio.join_relative_path("etc/backtest.json")
    print prop_file_path
    prop_file = open(prop_file_path, 'r')
    
    props = json.load(prop_file)
    
    enum_props = {'bar_type': common.QUOTE_TYPE}
    for k, v in enum_props.iteritems():
        props[k] = v.to_enum(props[k])
    
    # strategy   = CtaStrategy()
    strategy = DoubleMaStrategy()
    gateway = BarSimulatorGateway()
    data_service = RemoteDataService()

    context = model.Context()
    context.register_data_api(data_service)
    context.register_gateway(gateway)
    context.register_trade_api(gateway)
    
    backtest = EventBacktestInstance()
    backtest.init_from_config(props, strategy, context=context)
    
    # trade.run()
    backtest.run()
    report = backtest.generate_report(output_format="")
Beispiel #5
0
def test_output():
    static_folder = fileio.join_relative_path('trade/analyze/static')

    r = Report({
        'mytitle': 'Test Title',
        'mytable': 'Hello World!'
    },
               source_dir=static_folder,
               template_fn='test_template.html',
               out_folder='../output')
    r.generate_html()
    r.output_html()
    r.output_pdf()
Beispiel #6
0
def md2rst():
    input_dir = fileio.join_relative_path('../doc')
    output_dir = fileio.join_relative_path('../doc/source')

    for dir_path, dir_names, file_names in os.walk(input_dir):
        for fn in file_names:
            if fn.endswith('.md'):
                print "Converting {:s}...".format(fn)

                fn_pure = fn[:-2]
                fn_md = join(input_dir, fn)
                fn_html = join(input_dir, fn_pure + 'html')
                fn_rst = join(output_dir, fn_pure + 'rst')

                subprocess.check_output([
                    'pandoc', fn_md, '-f', 'markdown_github', '-t', 'html',
                    '-s', '-o', fn_html
                ])
                subprocess.check_output([
                    'pandoc', fn_html, '-f', 'html', '-t', 'rst', '-s', '-o',
                    fn_rst
                ])
                os.remove(fn_html)
Beispiel #7
0
def test_backtest_analyze():
    ta = ana.AlphaAnalyzer()
    data_service = RemoteDataService()

    out_folder = fileio.join_relative_path("../output")

    ta.initialize(data_service, out_folder)

    print "process trades..."
    ta.process_trades()
    print "get daily stats..."
    ta.get_daily()
    print "calc strategy return..."
    ta.get_returns()
    # position change info is huge!
    # print "get position change..."
    # ta.get_pos_change_info()

    selected_sec = list(ta.universe)[:3]
    if len(selected_sec) > 0:
        print "Plot single securities PnL"
        for symbol in selected_sec:
            df_daily = ta.daily.get(symbol, None)
            if df_daily is not None:
                ana.plot_trades(df_daily,
                                symbol=symbol,
                                save_folder=out_folder)

    print "Plot strategy PnL..."
    ta.plot_pnl(out_folder)

    print "generate report..."
    static_folder = fileio.join_relative_path("trade/analyze/static")
    ta.gen_report(source_dir=static_folder,
                  template_fn='report_template.html',
                  out_folder=out_folder,
                  selected=selected_sec)
Beispiel #8
0
    def __init__(self):
        DataService.__init__(self)

        dic = fileio.read_json(
            fileio.join_relative_path('etc/data_config.json'))
        address = dic.get("remote.address", None)
        username = dic.get("remote.username", None)
        password = dic.get("remote.password", None)
        if address is None or username is None or password is None:
            raise ValueError("no address, username or password available!")

        self.api = DataApi(address, use_jrpc=False)
        self.api.set_timeout(60)
        r, msg = self.api.login(username=username, password=password)
        if not r:
            print msg
        else:
            print "DataAPI login success.".format(address)

        self.REPORT_DATE_FIELD_NAME = 'report_date'
Beispiel #9
0
def save_dataview():
    # total 130 seconds
    
    ds = RemoteDataService()
    dv = DataView()
    
    props = {'start_date': 20141114, 'end_date': 20170327, 'universe': '000300.SH',
             # 'symbol': 'rb1710.SHF,rb1801.SHF',
             'fields': ('open,high,low,close,vwap,volume,turnover,'
                        # + 'pb,net_assets,'
                        + 's_fa_eps_basic,oper_exp,tot_profit,int_income'
                        ),
             'freq': 1}
    
    dv.init_from_config(props, ds)
    dv.prepare_data()
    
    dv.add_formula('eps_ret', 'Return(s_fa_eps_basic, 3)', is_quarterly=True)
    
    dv.add_formula('ret20', 'Delay(Return(close_adj, 20), -20)', is_quarterly=False)
    
    dv.save_dataview(folder_path=fileio.join_relative_path('../output/prepared'))
Beispiel #10
0
def build_stock_selection_factor():
    ds = RemoteDataService()
    dv = DataView()

    props = {'start_date': 20120101, 'end_date': 20170901, 'universe': '000300.SH',
             # 'symbol': 'rb1710.SHF,rb1801.SHF',
             'fields': ('open,high,low,close,vwap,volume,turnover,'
                        # + 'pb,net_assets,'
                        + 's_fa_eps_basic,oper_exp,tot_profit,int_income'
                        ),
             'freq': 1}

    dv.init_from_config(props, ds)
    dv.prepare_data()

    dv.add_formula('eps_ret', 'Return(s_fa_eps_basic, 4)', is_quarterly=True)
    dv.add_formula('rule1', '(eps_ret > 0.2) && (Delay(eps_ret, 1) > 0.2)', is_quarterly=True)
    dv.add_formula('rule2', 'close > Ts_Max(close, 120)', is_quarterly=False)
    # dv.add_formula('ytan', 'rule1 && rule2', is_quarterly=False)

    dv.add_formula('ret20', 'Delay(Return(close_adj, 20), -20)', is_quarterly=False)

    dv.save_dataview(folder_path=fileio.join_relative_path('../output/prepared'))
Beispiel #11
0
def test_data_api():
    dic = fileio.read_json(fileio.join_relative_path('etc/data_config.json'))
    address = dic.get("remote.address", None)
    username = dic.get("remote.username", None)
    password = dic.get("remote.password", None)
    if address is None or username is None or password is None:
        raise ValueError("no data service config available!")

    api = DataApi(address, use_jrpc=False)
    login_msg = api.login(username=username, password=password)
    print login_msg

    daily, msg = api.daily(
        symbol="600030.SH,000002.SZ",
        start_date=20170103,
        end_date=20170708,
        fields="open,high,low,close,volume,last,trade_date,settle")
    daily2, msg2 = api.daily(
        symbol="600030.SH",
        start_date=20170103,
        end_date=20170708,
        fields="open,high,low,close,volume,last,trade_date,settle")
    # err_code, err_msg = msg.split(',')
    assert msg == '0,'
    assert msg2 == '0,'
    assert daily.shape == (248, 8)
    assert daily2.shape == (124, 8)

    df, msg = api.bar(symbol="600030.SH",
                      trade_date=20170904,
                      freq='1m',
                      start_time=90000,
                      end_time=150000)
    print df.columns
    assert df.shape == (240, 15)

    print "test passed"
Beispiel #12
0
import numpy as np
import numpy.linalg as nlg
import pandas as pd
import scipy.stats as stats
import jaqs.trade.analyze.analyze as ana

from jaqs.trade.portfoliomanager import PortfolioManager
from jaqs.data.dataservice import RemoteDataService
from jaqs.data.dataview import DataView
from jaqs.trade import model
from jaqs.trade.backtest import AlphaBacktestInstance
from jaqs.trade.tradegateway import AlphaTradeApi
from jaqs.trade.strategy import AlphaStrategy
from jaqs.util import fileio

dataview_dir_path = fileio.join_relative_path('../output/prepared/ICCombine/dataview')
backtest_result_dir_path = fileio.join_relative_path('../output/ICCombine')

ic_weight_hd5_path = fileio.join_relative_path('../output/ICCombine', 'ic_weight.hd5')
custom_data_path = fileio.join_relative_path('../output/ICCombine', 'custom_date.json')


def save_dataview():
    ds = RemoteDataService()
    ds.init_from_config()
    dv = DataView()

    props = {'start_date': 20150101, 'end_date': 20170930, 'universe': '000905.SH',
             'fields': ('turnover,float_mv,close_adj,pe,pb'),
             'freq': 1}