Example #1
0
 def save_results(self, folder_path='.'):
     import os
     import pandas as pd
     folder_path = os.path.abspath(folder_path)
 
     trades = self.ctx.pm.trades
 
     type_map = {'task_id': str,
                 'entrust_no': str,
                 'entrust_action': str,
                 'symbol': str,
                 'fill_price': float,
                 'fill_size': float,
                 'fill_date': int,
                 'fill_time': int,
                 'fill_no': str,
                 'commission': float}
     # keys = trades[0].__dict__.keys()
     ser_list = dict()
     for key in type_map.keys():
         v = [t.__getattribute__(key) for t in trades]
         ser = pd.Series(data=v, index=None, dtype=type_map[key], name=key)
         ser_list[key] = ser
     df_trades = pd.DataFrame(ser_list)
     df_trades.index.name = 'index'
 
     trades_fn = os.path.join(folder_path, 'trades.csv')
     configs_fn = os.path.join(folder_path, 'configs.json')
     jutil.create_dir(trades_fn)
 
     df_trades.to_csv(trades_fn)
     jutil.save_json(self.props, configs_fn)
 
     print ("Backtest results has been successfully saved to:\n" + folder_path)
Example #2
0
 def save_results(self, folder_path='.'):
     import os
     import pandas as pd
     folder_path = os.path.abspath(folder_path)
 
     trades = self.ctx.pm.trades
 
     type_map = {'task_id': str,
                 'entrust_no': str,
                 'entrust_action': str,
                 'symbol': str,
                 'fill_price': float,
                 'fill_size': float,
                 'fill_date': np.integer,
                 'fill_time': np.integer,
                 'fill_no': str,
                 'commission': float}
     # keys = trades[0].__dict__.keys()
     ser_list = dict()
     for key in type_map.keys():
         v = [t.__getattribute__(key) for t in trades]
         ser = pd.Series(data=v, index=None, dtype=type_map[key], name=key)
         ser_list[key] = ser
     df_trades = pd.DataFrame(ser_list)
     df_trades.index.name = 'index'
 
     trades_fn = os.path.join(folder_path, 'trades.csv')
     configs_fn = os.path.join(folder_path, 'configs.json')
     jutil.create_dir(trades_fn)
 
     df_trades.to_csv(trades_fn)
     jutil.save_json(self.props, configs_fn)
 
     print ("Backtest results has been successfully saved to:\n" + folder_path)
Example #3
0
    def save_dataview(self, folder_path):
        """
        Save data and meta_data_to_store to a single hd5 file.
        Store at output/sub_folder

        Parameters
        ----------
        folder_path : str or unicode
            Path to store your data.

        """
        abs_folder = os.path.abspath(folder_path)
        meta_path = os.path.join(folder_path, 'meta_data.json')
        data_path = os.path.join(folder_path, 'data.hd5')

        data_to_store = {'data': self.data,
                         'data_benchmark': self.data_benchmark, 'data_inst': self._data_inst}
        data_to_store = {k: v for k, v in data_to_store.items() if v is not None}
        meta_data_to_store = {key: self.__dict__[key] for key in self.meta_data_list}

        print("\nStore data...")
        jutil.save_json(meta_data_to_store, meta_path)
        self._save_h5(data_path, data_to_store)

        print("Dataview has been successfully saved to:\n"
              + abs_folder + "\n\n"
              + "You can load it with load_dataview('{:s}')".format(abs_folder))
Example #4
0
def test_read_save_json():
    fp = '../../output/tests/test_read_save_pickle.pic'
    d = {'a': 1.0, 'b': 2, 'c': True, 'd': list()}
    jutil.save_json(d, fp)

    d2 = jutil.read_json(fp)
    assert d2['b'] == 2

    d3 = jutil.read_json('a_non_exits_file_blabla.pic')
    assert d3 == dict()
Example #5
0
def store_ic_weight():
    """
    Calculate IC weight and save it to file
    """
    dv = DataView()

    dv.load_dataview(folder_path=dataview_dir_path)

    factorList = ['TO', 'BP', 'REVS20', 'float_mv_factor']

    orthFactor_dic = {}

    for factor in factorList:
        orthFactor_dic[factor] = {}

    # add the orthogonalized factor to dataview
    for trade_date in dv.dates:
        snapshot = dv.get_snapshot(trade_date)
        factorPanel = snapshot[factorList]
        factorPanel = factorPanel.dropna()

        if len(factorPanel) != 0:
            orthfactorPanel = Schmidt(factorPanel)
            orthfactorPanel.columns = [x + '_adj' for x in factorList]

            snapshot = pd.merge(left=snapshot,
                                right=orthfactorPanel,
                                left_index=True,
                                right_index=True,
                                how='left')

            for factor in factorList:
                orthFactor_dic[factor][trade_date] = snapshot[factor]

    for factor in factorList:
        dv.append_df(pd.DataFrame(orthFactor_dic[factor]).T,
                     field_name=factor + '_adj',
                     is_quarterly=False)
    dv.save_dataview(dataview_dir_path)

    factorList_adj = [x + '_adj' for x in factorList]

    jutil.save_json(factorList_adj, custom_data_path)

    w = get_ic_weight(dv)

    store = pd.HDFStore(ic_weight_hd5_path)
    store['ic_weight'] = w
    store.close()
Example #6
0
def store_ic_weight():
    """
    Calculate IC weight and save it to file
    """
    dv = DataView()

    dv.load_dataview(folder_path=dataview_dir_path)

    factorList = ['TO', 'BP', 'REVS20', 'float_mv_factor']

    orthFactor_dic = {}

    for factor in factorList:
        orthFactor_dic[factor] = {}

    # add the orthogonalized factor to dataview
    for trade_date in dv.dates:
        snapshot = dv.get_snapshot(trade_date)
        factorPanel = snapshot[factorList]
        factorPanel = factorPanel.dropna()

        if len(factorPanel) != 0:
            orthfactorPanel = Schmidt(factorPanel)
            orthfactorPanel.columns = [x + '_adj' for x in factorList]

            snapshot = pd.merge(left=snapshot, right=orthfactorPanel,
                                left_index=True, right_index=True, how='left')

            for factor in factorList:
                orthFactor_dic[factor][trade_date] = snapshot[factor]

    for factor in factorList:
        dv.append_df(pd.DataFrame(orthFactor_dic[factor]).T, field_name=factor + '_adj', is_quarterly=False)
    dv.save_dataview(dataview_dir_path)

    factorList_adj = [x + '_adj' for x in factorList]

    jutil.save_json(factorList_adj, custom_data_path)

    w = get_ic_weight(dv)

    store = pd.HDFStore(ic_weight_hd5_path)
    store['ic_weight'] = w
    store.close()