Exemplo n.º 1
0
def save_dataview():
    ds = RemoteDataService()
    ds.init_from_config()

    dv = DataView()

    props = {
        'start_date':
        20170101,
        'end_date':
        20171030,
        'universe':
        '000300.SH',
        'fields': (
            'open,high,low,close,vwap,volume,turnover,sw1,'
            # + 'pb,net_assets,'
            + 'eps_basic,total_mv,tot_profit,int_income'),
        'freq':
        1
    }

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

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

    dv.add_formula('total_profit_growth',
                   formula='Return(tot_profit, 4)',
                   is_quarterly=True)

    dv.save_dataview(folder_path=dataview_dir_path)
Exemplo n.º 2
0
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}

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

    factor_formula = 'Cutoff(Standardize(turnover / 10000 / float_mv), 2)'
    dv.add_formula('TO', factor_formula, is_quarterly=False)

    factor_formula = 'Cutoff(Standardize(1/pb), 2)'
    dv.add_formula('BP', factor_formula, is_quarterly=False)

    factor_formula = 'Cutoff(Standardize(Return(close_adj, 20)), 2)'
    dv.add_formula('REVS20', factor_formula, is_quarterly=False)

    factor_formula = 'Cutoff(Standardize(Log(float_mv)), 2)'
    dv.add_formula('float_mv_factor', factor_formula, is_quarterly=False)

    factor_formula = 'Delay(Return(close_adj, 1), -1)'
    dv.add_formula('NextRet', factor_formula, is_quarterly=False)

    dv.save_dataview(folder_path=dataview_dir_path)
Exemplo n.º 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)
Exemplo n.º 4
0
def analyze_signal():
    # --------------------------------------------------------------------------------
    # Step.1 load dataview
    dv = DataView()
    dv.load_dataview(dataview_folder)

    # --------------------------------------------------------------------------------
    # Step.2 calculate mask (to mask those ill data points)
    trade_status = dv.get_ts('trade_status')
    mask_sus = trade_status == u'停牌'.encode('utf-8')

    df_index_member = dv.get_ts('index_member')
    mask_index_member = ~(df_index_member > 0)

    dv.add_formula('limit_reached',
                   'Abs((open - Delay(close, 1)) / Delay(close, 1)) > 0.095',
                   is_quarterly=False)
    df_limit_reached = dv.get_ts('limit_reached')
    mask_limit_reached = df_limit_reached > 0

    mask_all = np.logical_or(
        mask_sus, np.logical_or(mask_index_member, mask_limit_reached))

    # --------------------------------------------------------------------------------
    # Step.3 get signal, benchmark and price data
    # dv.add_formula('illi_daily', '(high - low) * 1000000000 / turnover', is_quarterly=False)
    # dv.add_formula('illi', 'Ewma(illi_daily, 11)', is_quarterly=False)

    # dv.add_formula('size', 'Log(float_mv)', is_quarterly=False)
    # dv.add_formula('value', '-1.0/pb', is_quarterly=False)
    # dv.add_formula('liquidity', 'Ts_Mean(volume, 22) / float_mv', is_quarterly=False)
    dv.add_formula('divert',
                   '- Correlation(vwap_adj, volume, 10)',
                   is_quarterly=False)

    signal = dv.get_ts('divert').shift(1, axis=0)  # avoid look-ahead bias
    price = dv.get_ts('close_adj')
    price_bench = dv.data_benchmark

    # Step.4 analyze!
    my_period = 5
    obj = signaldigger.digger.SignalDigger(
        output_folder=jutil.join_relative_path('../output'),
        output_format='pdf')
    obj.process_signal_before_analysis(
        signal,
        price=price,
        mask=mask_all,
        n_quantiles=5,
        period=my_period,
        benchmark_price=price_bench,
    )
    res = obj.create_full_report()
Exemplo n.º 5
0
def test_add_formula():
    dv = DataView()
    dv.load_dataview(folder_path=daily_path)
    nrows, ncols = dv.data_d.shape
    n_securities = len(dv.data_d.columns.levels[0])

    formula = 'Delta(high - close, 1)'
    dv.add_formula('myvar1', formula, is_quarterly=False)
    assert dv.data_d.shape == (nrows, ncols + 1 * n_securities)

    formula2 = 'myvar1 - close'
    dv.add_formula('myvar2', formula2, is_quarterly=False)
    assert dv.data_d.shape == (nrows, ncols + 2 * n_securities)
Exemplo n.º 6
0
def test_add_formula():
    dv = DataView()
    folder_path = '../output/prepared/20160601_20170601_freq=1D'
    dv.load_dataview(folder=folder_path)
    nrows, ncols = dv.data_d.shape
    n_securities = len(dv.data_d.columns.levels[0])
    
    formula = 'Delta(high - close, 1)'
    dv.add_formula('myvar1', formula, is_quarterly=False)
    assert dv.data_d.shape == (nrows, ncols + 1 * n_securities)
    
    formula2 = 'myvar1 - close'
    dv.add_formula('myvar2', formula2, is_quarterly=False)
    assert dv.data_d.shape == (nrows, ncols + 2 * n_securities)
Exemplo n.º 7
0
def test_add_formula_directly():
    from jaqs.data.dataservice import RemoteDataService
    
    ds = RemoteDataService()
    dv = DataView()
    
    secs = '600030.SH,000063.SZ,000001.SZ'
    props = {'start_date': 20160601, 'end_date': 20170601, 'symbol': secs,
             'fields': 'open,close',
             'freq': 1}
    dv.init_from_config(props, data_api=ds)
    dv.prepare_data()
    
    dv.add_formula("myfactor", 'close / open', is_quarterly=False)
    assert dv.data_d.shape == (281, 33)
Exemplo n.º 8
0
def test_q_add_formula():
    dv = DataView()
    folder_path = '../output/prepared/20160609_20170601_freq=1D'
    dv.load_dataview(folder_path=quarterly_path)
    nrows, ncols = dv.data_d.shape
    n_securities = len(dv.data_d.columns.levels[0])

    formula = 'total_oper_rev / close'
    dv.add_formula('myvar1', formula, is_quarterly=False)
    df1 = dv.get_ts('myvar1')
    assert not df1.empty

    formula2 = 'Delta(oper_exp * myvar1 - open, 3)'
    dv.add_formula('myvar2', formula2, is_quarterly=False)
    df2 = dv.get_ts('myvar2')
    assert not df2.empty
Exemplo n.º 9
0
def test_save_dataview():
    ds = RemoteDataService()
    ds.init_from_config()
    dv = DataView()

    props = {'start_date': 20170201, 'end_date': 20171001, 'universe': '000300.SH',
             'fields': ('float_mv,sw2,sw1'),
             'freq': 1}

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

    factor_formula = 'GroupQuantile(float_mv, sw1, 10)'
    dv.add_formula('gq30', factor_formula, is_quarterly=False)

    dv.save_dataview(folder_path=dataview_dir_path)
Exemplo n.º 10
0
def test_save_dataview():
    ds = RemoteDataService()
    ds.init_from_config()
    dv = DataView()

    props = {
        'start_date': 20170101,
        'end_date': 20171001,
        'universe': '000300.SH',
        'fields': 'pe_ttm,net_profit_incl_min_int_inc',
        'freq': 1
    }

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

    factor_formula = 'Return(net_profit_incl_min_int_inc, 4)'
    factor_name = 'net_profit_growth'
    dv.add_formula(factor_name, factor_formula, is_quarterly=True)

    dv.save_dataview(folder_path=dataview_dir_path)
Exemplo n.º 11
0
def save_dataview():
    ds = RemoteDataService()
    ds.init_from_config()
    dv = DataView()

    props = {
        'start_date': 20140101,
        'end_date': 20171001,
        'universe': '000300.SH',
        'fields': 'volume,turnover,float_mv,pb,total_mv',
        'freq': 1
    }

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

    # for convenience to check limit reachers
    dv.add_formula('limit_reached',
                   'Abs((open - Delay(close, 1)) / Delay(close, 1)) > 0.095',
                   is_quarterly=False)

    dv.add_formula('random', 'StdDev(volume, 20)', is_quarterly=False)
    dv.add_formula('momentum', 'Return(close_adj, 20)', is_quarterly=False)
    # dv.add_formula('size', '', is_quarterly=False)

    dv.save_dataview(dataview_folder)
Exemplo n.º 12
0
def test_save_dataview():
    ds = RemoteDataService()
    ds.init_from_config()
    dv = DataView()

    props = {
        'start_date': 20170101,
        'end_date': 20171001,
        'universe': '000300.SH',
        'fields': ('float_mv,pb,pe_ttm,sw2'),
        'freq': 1
    }

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

    factor_formula = 'GroupQuantile(-float_mv, sw2, 10)'
    dv.add_formula('rank_mv', factor_formula, is_quarterly=False)

    factor_formula = 'GroupQuantile(If(pb >= 0.2, pb, 100), sw2, 10)'
    dv.add_formula('rank_pb', factor_formula, is_quarterly=False)

    factor_formula = 'GroupQuantile(If(pe_ttm >= 3, pe_ttm, 9999.0), sw2, 10)'
    dv.add_formula('rank_pe', factor_formula, is_quarterly=False)

    dv.save_dataview(folder_path=dataview_dir_path)
Exemplo n.º 13
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'))
Exemplo n.º 14
0
def test_save_dataview(sub_folder='test_dataview'):
    ds = RemoteDataService()
    ds.init_from_config()
    dv = DataView()

    props = {
        'start_date': 20150101,
        'end_date': 20170930,
        'universe': '000905.SH',
        'fields':
        ('float_mv,tot_shrhldr_eqy_excl_min_int,deferred_tax_assets,sw2'),
        'freq': 1
    }

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

    factor_formula = 'Quantile(-float_mv,5)'
    dv.add_formula('rank_mv', factor_formula, is_quarterly=False)

    factor_formula = 'Quantile(float_mv/(tot_shrhldr_eqy_excl_min_int+deferred_tax_assets), 5)'
    dv.add_formula('rank_pb', factor_formula, is_quarterly=False)

    dv.save_dataview(folder_path=dataview_dir_path)
Exemplo n.º 15
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'))
Exemplo n.º 16
0
def test_save_dataview():
    ds = RemoteDataService()
    ds.init_from_config()
    dv = DataView()
    
    props = {'start_date': 20150101, 'end_date': 20170930, 'universe': '000905.SH',
             'fields': ('tot_cur_assets,tot_cur_liab,inventories,pre_pay,deferred_exp,'
                        'eps_basic,ebit,pe,pb,float_mv,sw1'),
             'freq': 1}
    
    dv.init_from_config(props, ds)
    dv.prepare_data()
    
    factor_formula = 'pe < 30'
    dv.add_formula('pe_condition', factor_formula, is_quarterly=False)
    factor_formula = 'pb < 3'
    dv.add_formula('pb_condition', factor_formula, is_quarterly=False)
    factor_formula = 'Return(eps_basic, 4) > 0'
    dv.add_formula('eps_condition', factor_formula, is_quarterly=True)
    factor_formula = 'Return(ebit, 4) > 0'
    dv.add_formula('ebit_condition', factor_formula, is_quarterly=True)
    factor_formula = 'tot_cur_assets/tot_cur_liab > 2'
    dv.add_formula('current_condition', factor_formula, is_quarterly=True)
    factor_formula = '(tot_cur_assets - inventories - pre_pay - deferred_exp)/tot_cur_liab > 1'
    dv.add_formula('quick_condition', factor_formula, is_quarterly=True)
    
    dv.add_formula('mv_rank', 'Rank(float_mv)', is_quarterly=False)
    
    dv.save_dataview(folder_path=dataview_dir_path)