コード例 #1
0
def test_roll_to_roll_two_generics():
    contract_dates = pd.Series([
        TS('2016-10-10'),
        TS('2016-10-13'),
        TS('2016-10-17'),
        TS('2016-10-20')
    ],
                               index=['A', 'B', 'C', 'D'])
    timestamps = pd.date_range(contract_dates.iloc[0] + BDay(-2),
                               contract_dates.iloc[1],
                               freq='b')
    cols = pd.MultiIndex.from_product([[0, 1], ['front', 'back']])
    idx = [-2, -1, 0]
    trans = pd.DataFrame([[1, 0, 1, 0], [0.5, 0.5, 0.5, 0.5], [0, 1, 0, 1]],
                         index=idx,
                         columns=cols)

    wts = mappings.roller(timestamps,
                          contract_dates,
                          mappings.static_transition,
                          transition=trans)
    midx = pd.MultiIndex.from_tuples([
        (timestamps[0], 'A'), (timestamps[0], 'B'), (timestamps[1], 'A'),
        (timestamps[1], 'B'), (timestamps[1], 'C'), (timestamps[2], 'B'),
        (timestamps[2], 'C'), (timestamps[3], 'B'), (timestamps[3], 'C'),
        (timestamps[4], 'B'), (timestamps[4], 'C'), (timestamps[4], 'D'),
        (timestamps[5], 'C'), (timestamps[5], 'D')
    ])
    midx.names = ['date', 'contract']
    cols = pd.Index([0, 1], name='generic')
    vals = [[1, 0], [0, 1], [0.5, 0], [0.5, 0.5], [0, 0.5], [1, 0], [0, 1],
            [1, 0], [0, 1], [0.5, 0], [0.5, 0.5], [0, 0.5], [1, 0], [0, 1]]
    wts_exp = pd.DataFrame(vals, index=midx, columns=cols)
    assert_frame_equal(wts, wts_exp)
コード例 #2
0
ファイル: test_util.py プロジェクト: thetradingflow/mapping
def test_read_price_data(price_files):
    # using default name_func in read_price_data()
    df = util.read_price_data(price_files)
    dt1 = TS("2014-09-30")
    dt2 = TS("2014-10-01")
    idx = pd.MultiIndex.from_tuples([(dt1, "CME-FVU2014"),
                                     (dt1, "CME-FVZ2014"),
                                     (dt2, "CME-FVZ2014")],
                                    names=["date", "contract"])
    df_exp = pd.DataFrame([119.27344, 118.35938, 118.35938],
                          index=idx,
                          columns=["Open"])
    assert_frame_equal(df, df_exp)

    def name_func(fstr):
        file_name = os.path.split(fstr)[-1]
        name = file_name.split('-')[1].split('.')[0]
        return name[-4:] + name[:3]

    df = util.read_price_data(price_files, name_func)
    dt1 = TS("2014-09-30")
    dt2 = TS("2014-10-01")
    idx = pd.MultiIndex.from_tuples([(dt1, "2014FVU"), (dt1, "2014FVZ"),
                                     (dt2, "2014FVZ")],
                                    names=["date", "contract"])
    df_exp = pd.DataFrame([119.27344, 118.35938, 118.35938],
                          index=idx,
                          columns=["Open"])
    assert_frame_equal(df, df_exp)
コード例 #3
0
ファイル: test_util.py プロジェクト: thetradingflow/mapping
def test_unflatten_dict():
    flat_wts = pd.DataFrame({
        "date":
        ([TS('2015-01-03')] * 4 + [TS('2015-01-04')] * 4 + [TS('2015-01-03')]),
        "contract": (['CLF5'] * 2 + ['CLG5'] * 4 + ['CLH5'] * 2 + ["COF5"]),
        "generic": ["CL1", "CL2"] * 4 + ["CO1"],
        "weight": [1, 0, 0, 1, 1, 0, 0, 1, 1],
        "key": ["CL"] * 8 + ["CO"]
    }).loc[:, ["date", "contract", "generic", "weight", "key"]]
    wts = util.unflatten(flat_wts)

    vals = [[1, 0], [0, 1], [1, 0], [0, 1]]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-03'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLH5')],
                                     names=("date", "contract"))
    cols = pd.Index(["CL1", "CL2"], name="generic")
    weights1 = pd.DataFrame(vals, index=widx, columns=cols)
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'COF5')],
                                     names=("date", "contract"))
    cols = pd.Index(["CO1"], name="generic")
    weights2 = pd.DataFrame(1, index=widx, columns=cols)
    wts_exp = {"CL": weights1, "CO": weights2}

    assert_dict_of_frames(wts, wts_exp)
コード例 #4
0
def test_bdom():
    exp_cols = ["date", "year", "month", "bdom", "month_code"]

    months = {1: "G", 3: "J", 8: "U"}
    date_info = mappings.bdom_roll_date("20160115", "20171231", 2, months)
    date_info_exp = pd.DataFrame({
        "date": [
            TS("20160302"),
            TS("20160802"),
            TS("20170103"),
            TS("20170302"),
            TS("20170802")
        ],
        "year": [2016, 2016, 2017, 2017, 2017],
        "month": [3, 8, 1, 3, 8],
        "bdom":
        2,
        "month_code": ["J", "U", "G", "J", "U"]
    })
    date_info_exp = date_info_exp.loc[:, exp_cols]
    assert_frame_equal(date_info, date_info_exp)

    # with holidays
    months = {1: "G", 3: "J"}
    date_info = mappings.bdom_roll_date("20160115",
                                        "20171231",
                                        3,
                                        months,
                                        holidays=[pd.Timestamp("20160301")])
    date_info_exp = pd.DataFrame({
        "date": [TS("20160304"),
                 TS("20170104"),
                 TS("20170303")],
        "year": [2016, 2017, 2017],
        "month": [3, 1, 3],
        "bdom":
        3,
        "month_code": ["J", "G", "J"]
    })
    date_info_exp = date_info_exp.loc[:, exp_cols]
    assert_frame_equal(date_info, date_info_exp)

    date_info_empty = mappings.bdom_roll_date("20160101", "20150101", 2,
                                              months)
    date_info_exp = pd.DataFrame(index=range(0), columns=exp_cols)
    assert_frame_equal(date_info_empty, date_info_exp, check_dtype=False)

    # forget to pass bdom, check for type error
    holidays = [pd.Timestamp("20160301")]
    with pytest.raises(ValueError):
        mappings.bdom_roll_date("20160115", "20171231", months, holidays)
コード例 #5
0
ファイル: test_util.py プロジェクト: thetradingflow/mapping
def test_calc_rets_missing_instr_rets_key_error():
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLG5')])
    irets = pd.Series([0.02, 0.01, 0.012], index=idx)
    vals = [1, 1 / 2, 1 / 2, 1]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLG5'),
                                      (TS('2015-01-05'), 'CLG5')])
    weights = pd.DataFrame(vals, index=widx, columns=["CL1"])
    with pytest.raises(KeyError):
        util.calc_rets(irets, weights)
コード例 #6
0
ファイル: test_util.py プロジェクト: thetradingflow/mapping
def test_calc_rets_one_generic():
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLG5'),
                                     (TS('2015-01-05'), 'CLG5')])
    rets = pd.Series([0.1, 0.05, 0.1, 0.8], index=idx)
    vals = [1, 0.5, 0.5, 1]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLG5'),
                                      (TS('2015-01-05'), 'CLG5')])
    weights = pd.DataFrame(vals, index=widx, columns=['CL1'])
    wrets = util.calc_rets(rets, weights)
    wrets_exp = pd.DataFrame([0.1, 0.075, 0.8],
                             index=weights.index.levels[0],
                             columns=['CL1'])
    assert_frame_equal(wrets, wrets_exp)
コード例 #7
0
ファイル: fitbit.py プロジェクト: technillogue/qspy
    def get_data(self,
                 start_str: str,
                 stop_str: str,
                 fetch: bool = True,
                 fill: bool = False) -> pd.Series:
        if not fetch:
            return self.cache.iloc[(self.cache.index >= TS(start_str))
                                   & (self.cache.index < TS(stop_str))]
        # date_range includes the last item but everything else doesn't
        requested_dates: Sequence[TS] = pd.date_range(start_str, stop_str)[:-1]
        needed_dates = sorted(
            list(set(requested_dates) - set(self.cache.index)))
        needed_ranges = find_ranges(needed_dates)

        def collect_data(date_range: Tuple[Date, Date]) -> pd.DataFrame:
            # why is this a nested function?
            str_range: Tuple[str, str] = tuple(map(Date.isoformat, date_range))
            raw_data = self.fetch_raw_data_for_range(str_range)
            new_data = parsed_data(raw_data)  # add nap filtering here
            return new_data

        collected_df = pd.concat(map(collect_data, needed_ranges))
        breakpoint()
        if fill:
            missing_dates = set(needed_dates) - set(collected_df.index)
            row_fill = [None] * len(HEADER[1:])
            matrix_fill = [([date] + row_fill) for date in missing_dates]
            df_fill = pd.DataFrame(matrix_fill,
                                   columns=HEADER,
                                   index=missing_dates)
            collected_df = pd.concat([collected_df, df_fill])
        self.cache = pd.concat([self.cache, collected_df],
                               sort=False).sort_index()
        self.cache.name = "dates"
        self.cache.to_csv(self.fname, index=True)
        return self.cache[requested_dates[0]:requested_dates[-1]]
コード例 #8
0
ファイル: test_util.py プロジェクト: thetradingflow/mapping
def test_reindex_with_index_names_smoketest():
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-02'), 'CLF5'),
                                     (TS('2015-01-02'), 'CLH5'),
                                     (TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-03'), 'CLH5')])
    prices = pd.Series([103, 101, 102, 100], index=idx)
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-03'), 'CLH5')],
                                     names=["date", "contract"])
    util.reindex(prices, widx, limit=0)
コード例 #9
0
ファイル: test_util.py プロジェクト: thetradingflow/mapping
def test_flatten_dict():
    vals = [[1, 0], [0, 1], [1, 0], [0, 1]]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-03'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLH5')])
    weights1 = pd.DataFrame(vals, index=widx, columns=["CL1", "CL2"])
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'COF5')])
    weights2 = pd.DataFrame(1, index=widx, columns=["CO1"])
    weights = {"CL": weights1, "CO": weights2}
    flat_wts = util.flatten(weights)
    flat_wts_exp = pd.DataFrame({
        "date":
        ([TS('2015-01-03')] * 4 + [TS('2015-01-04')] * 4 + [TS('2015-01-03')]),
        "contract": (['CLF5'] * 2 + ['CLG5'] * 4 + ['CLH5'] * 2 + ["COF5"]),
        "generic": ["CL1", "CL2"] * 4 + ["CO1"],
        "weight": [1, 0, 0, 1, 1, 0, 0, 1, 1],
        "key": ["CL"] * 8 + ["CO"]
    }).loc[:, ["date", "contract", "generic", "weight", "key"]]
    assert_frame_equal(flat_wts, flat_wts_exp)
コード例 #10
0
ファイル: test_util.py プロジェクト: thetradingflow/mapping
def test_flatten():
    vals = [[1, 0], [0, 1], [1, 0], [0, 1]]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-03'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLH5')])
    weights = pd.DataFrame(vals, index=widx, columns=["CL1", "CL2"])
    flat_wts = util.flatten(weights)

    flat_wts_exp = pd.DataFrame({
        "date": [TS('2015-01-03')] * 4 + [TS('2015-01-04')] * 4,
        "contract": ['CLF5'] * 2 + ['CLG5'] * 4 + ['CLH5'] * 2,
        "generic": ["CL1", "CL2"] * 4,
        "weight": [1, 0, 0, 1, 1, 0, 0, 1]
    }).loc[:, ["date", "contract", "generic", "weight"]]
    assert_frame_equal(flat_wts, flat_wts_exp)
コード例 #11
0
ファイル: test_util.py プロジェクト: thetradingflow/mapping
def test_unflatten():
    flat_wts = pd.DataFrame({
        "date": [TS('2015-01-03')] * 4 + [TS('2015-01-04')] * 4,
        "contract": ['CLF5'] * 2 + ['CLG5'] * 4 + ['CLH5'] * 2,
        "generic": ["CL1", "CL2"] * 4,
        "weight": [1, 0, 0, 1, 1, 0, 0, 1]
    }).loc[:, ["date", "contract", "generic", "weight"]]
    wts = util.unflatten(flat_wts)

    vals = [[1, 0], [0, 1], [1, 0], [0, 1]]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-03'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLH5')],
                                     names=("date", "contract"))
    cols = pd.Index(["CL1", "CL2"], name="generic")
    wts_exp = pd.DataFrame(vals, index=widx, columns=cols)
    assert_frame_equal(wts, wts_exp)
コード例 #12
0
ファイル: test_util.py プロジェクト: thetradingflow/mapping
def test_weighted_expiration_two_generics():
    vals = [[1, 0, 1 / 2, 1 / 2, 0, 1, 0], [0, 1, 0, 1 / 2, 1 / 2, 0, 1]]
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF15'),
                                     (TS('2015-01-03'), 'CLG15'),
                                     (TS('2015-01-04'), 'CLF15'),
                                     (TS('2015-01-04'), 'CLG15'),
                                     (TS('2015-01-04'), 'CLH15'),
                                     (TS('2015-01-05'), 'CLG15'),
                                     (TS('2015-01-05'), 'CLH15')])
    weights = pd.DataFrame({"CL1": vals[0], "CL2": vals[1]}, index=idx)
    contract_dates = pd.Series(
        [TS('2015-01-20'),
         TS('2015-02-21'),
         TS('2015-03-20')],
        index=['CLF15', 'CLG15', 'CLH15'])
    wexp = util.weighted_expiration(weights, contract_dates)
    exp_wexp = pd.DataFrame(
        [[17.0, 49.0], [32.0, 61.5], [47.0, 74.0]],
        index=[TS('2015-01-03'),
               TS('2015-01-04'),
               TS('2015-01-05')],
        columns=["CL1", "CL2"])
    assert_frame_equal(wexp, exp_wexp)
コード例 #13
0
ファイル: test_util.py プロジェクト: thetradingflow/mapping
def test_calc_rets_two_generics_two_asts():
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-03'), 'CLG5'),
                                     (TS('2015-01-04'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLG5'),
                                     (TS('2015-01-04'), 'CLH5'),
                                     (TS('2015-01-05'), 'CLG5'),
                                     (TS('2015-01-05'), 'CLH5')])
    rets1 = pd.Series([0.1, 0.15, 0.05, 0.1, 0.8, -0.5, 0.2], index=idx)
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'COF5'),
                                     (TS('2015-01-03'), 'COG5'),
                                     (TS('2015-01-04'), 'COF5'),
                                     (TS('2015-01-04'), 'COG5'),
                                     (TS('2015-01-04'), 'COH5')])
    rets2 = pd.Series([0.1, 0.15, 0.05, 0.1, 0.4], index=idx)
    rets = {"CL": rets1, "CO": rets2}

    vals = [[1, 0], [0, 1], [0.5, 0], [0.5, 0.5], [0, 0.5], [1, 0], [0, 1]]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-03'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLH5'),
                                      (TS('2015-01-05'), 'CLG5'),
                                      (TS('2015-01-05'), 'CLH5')])
    weights1 = pd.DataFrame(vals, index=widx, columns=["CL0", "CL1"])
    vals = [[1, 0], [0, 1], [0.5, 0], [0.5, 0.5], [0, 0.5]]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'COF5'),
                                      (TS('2015-01-03'), 'COG5'),
                                      (TS('2015-01-04'), 'COF5'),
                                      (TS('2015-01-04'), 'COG5'),
                                      (TS('2015-01-04'), 'COH5')])
    weights2 = pd.DataFrame(vals, index=widx, columns=["CO0", "CO1"])
    weights = {"CL": weights1, "CO": weights2}
    wrets = util.calc_rets(rets, weights)
    wrets_exp = pd.DataFrame(
        [[0.1, 0.15, 0.1, 0.15], [0.075, 0.45, 0.075, 0.25],
         [-0.5, 0.2, pd.np.NaN, pd.np.NaN]],
        index=weights["CL"].index.levels[0],
        columns=['CL0', 'CL1', 'CO0', 'CO1'])
    assert_frame_equal(wrets, wrets_exp)
コード例 #14
0
ファイル: test_util.py プロジェクト: thetradingflow/mapping
def test_calc_rets_two_generics_non_unique_columns():
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-03'), 'CLG5'),
                                     (TS('2015-01-04'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLG5'),
                                     (TS('2015-01-04'), 'CLH5'),
                                     (TS('2015-01-05'), 'CLG5'),
                                     (TS('2015-01-05'), 'CLH5')])
    rets = pd.Series([0.1, 0.15, 0.05, 0.1, 0.8, -0.5, 0.2], index=idx)
    vals = [[1, 0], [0, 1], [0.5, 0], [0.5, 0.5], [0, 0.5], [1, 0], [0, 1]]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-03'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLH5'),
                                      (TS('2015-01-05'), 'CLG5'),
                                      (TS('2015-01-05'), 'CLH5')])
    weights = pd.DataFrame(vals, index=widx, columns=['CL1', 'CL1'])
    with pytest.raises(ValueError):
        util.calc_rets(rets, weights)
コード例 #15
0
ファイル: fitbit.py プロジェクト: technillogue/qspy
def start_ts_parsed_from_raw_data(raw_data: RawDateData) -> TS:
    if isinstance(raw_data["startTime"], str):
        return TS(raw_data["startTime"])
    raise Exception("malformed data")
コード例 #16
0
ファイル: test_util.py プロジェクト: thetradingflow/mapping
def test_reindex():
    # related to https://github.com/matthewgilbert/mapping/issues/11
    # no op
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-02'), 'CLF5'),
                                     (TS('2015-01-02'), 'CLH5'),
                                     (TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-03'), 'CLH5')])
    prices = pd.Series([103, 101, 102, 100], index=idx)
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-03'), 'CLH5')])
    new_prices = util.reindex(prices, widx, limit=0)
    exp_prices = prices
    assert_series_equal(exp_prices, new_prices)

    # missing front prices error
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5')])
    prices = pd.Series([100], index=idx)
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5')])
    with pytest.raises(ValueError):
        util.reindex(prices, widx, 0)

    # NaN returns introduced and filled
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-01'), 'CLF5'),
                                     (TS('2015-01-02'), 'CLF5'),
                                     (TS('2015-01-02'), 'CLH5'),
                                     (TS('2015-01-04'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLH5')])
    prices = pd.Series([100, 101, 102, 103, 104], index=idx)
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-02'), 'CLF5'),
                                      (TS('2015-01-02'), 'CLH5'),
                                      (TS('2015-01-04'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLH5'),
                                      (TS('2015-01-05'), 'CLF5'),
                                      (TS('2015-01-05'), 'CLH5'),
                                      (TS('2015-01-06'), 'CLF5'),
                                      (TS('2015-01-06'), 'CLH5')])
    new_prices = util.reindex(prices, widx, limit=1)

    idx = pd.MultiIndex.from_tuples([(TS('2015-01-01'), 'CLF5'),
                                     (TS('2015-01-01'), 'CLH5'),
                                     (TS('2015-01-02'), 'CLF5'),
                                     (TS('2015-01-02'), 'CLH5'),
                                     (TS('2015-01-04'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLH5'),
                                     (TS('2015-01-05'), 'CLF5'),
                                     (TS('2015-01-05'), 'CLH5'),
                                     (TS('2015-01-06'), 'CLF5'),
                                     (TS('2015-01-06'), 'CLH5')])
    exp_prices = pd.Series(
        [100, np.NaN, 101, 102, 103, 104, 103, 104, np.NaN, np.NaN], index=idx)
    assert_series_equal(exp_prices, new_prices)

    # standard subset
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-01'), 'CLF5'),
                                     (TS('2015-01-01'), 'CHF5'),
                                     (TS('2015-01-02'), 'CLF5'),
                                     (TS('2015-01-02'), 'CLH5'),
                                     (TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-03'), 'CLH5'),
                                     (TS('2015-01-04'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLH5')])
    prices = pd.Series([100, 101, 102, 103, 104, 105, 106, 107], index=idx)
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-02'), 'CLF5'),
                                      (TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-03'), 'CLH5')])
    new_prices = util.reindex(prices, widx, limit=0)

    idx = pd.MultiIndex.from_tuples([(TS('2015-01-01'), 'CLF5'),
                                     (TS('2015-01-02'), 'CLF5'),
                                     (TS('2015-01-02'), 'CLH5'),
                                     (TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-03'), 'CLH5')])
    exp_prices = pd.Series([100, 102, 103, 104, 105], index=idx)
    assert_series_equal(exp_prices, new_prices)

    # check unique index to avoid duplicates from pd.Series.reindex
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLF5')])
    prices = pd.Series([100.10, 101.13], index=idx)
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-03'), 'CLF5')])
    with pytest.raises(ValueError):
        util.reindex(prices, widx, limit=0)
コード例 #17
0
ファイル: test_util.py プロジェクト: thetradingflow/mapping
def test_calc_rets_missing_weight():
    # see https://github.com/matthewgilbert/mapping/issues/8

    # missing weight for return
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-02'), 'CLF5'),
                                     (TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLF5')])
    rets = pd.Series([0.02, -0.03, 0.06], index=idx)
    vals = [1, 1]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-02'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLF5')])
    weights = pd.DataFrame(vals, index=widx, columns=["CL1"])
    with pytest.raises(ValueError):
        util.calc_rets(rets, weights)

    # extra instrument
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-02'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLF5')])
    weights1 = pd.DataFrame(1, index=idx, columns=["CL1"])
    idx = pd.MultiIndex.from_tuples([
        (TS('2015-01-02'), 'CLF5'),
        (TS('2015-01-02'), 'CLH5'),
        (TS('2015-01-03'), 'CLH5'),  # extra day for no weight instrument
        (TS('2015-01-04'), 'CLF5'),
        (TS('2015-01-04'), 'CLH5')
    ])
    rets = pd.Series([0.02, -0.03, 0.06, 0.05, 0.01], index=idx)
    with pytest.raises(ValueError):
        util.calc_rets(rets, weights1)

    # leading / trailing returns
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-02'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLF5')])
    weights2 = pd.DataFrame(1, index=idx, columns=["CL1"])
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-01'), 'CLF5'),
                                     (TS('2015-01-02'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLF5'),
                                     (TS('2015-01-05'), 'CLF5')])
    rets = pd.Series([0.02, -0.03, 0.06, 0.05], index=idx)
    with pytest.raises(ValueError):
        util.calc_rets(rets, weights2)
コード例 #18
0
ファイル: test_util.py プロジェクト: thetradingflow/mapping
def test_calc_rets_two_generics_nans_in_second_generic():
    idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                     (TS('2015-01-03'), 'CLG5'),
                                     (TS('2015-01-04'), 'CLF5'),
                                     (TS('2015-01-04'), 'CLG5'),
                                     (TS('2015-01-04'), 'CLH5'),
                                     (TS('2015-01-05'), 'CLG5'),
                                     (TS('2015-01-05'), 'CLH5')])
    rets = pd.Series([0.1, np.NaN, 0.05, 0.1, np.NaN, -0.5, 0.2], index=idx)
    vals = [[1, 0], [0, 1], [0.5, 0], [0.5, 0.5], [0, 0.5], [1, 0], [0, 1]]
    widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'CLF5'),
                                      (TS('2015-01-03'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLF5'),
                                      (TS('2015-01-04'), 'CLG5'),
                                      (TS('2015-01-04'), 'CLH5'),
                                      (TS('2015-01-05'), 'CLG5'),
                                      (TS('2015-01-05'), 'CLH5')])
    weights = pd.DataFrame(vals, index=widx, columns=['CL1', 'CL2'])
    wrets = util.calc_rets(rets, weights)
    wrets_exp = pd.DataFrame([[0.1, np.NaN], [0.075, np.NaN], [-0.5, 0.2]],
                             index=weights.index.levels[0],
                             columns=['CL1', 'CL2'])
    assert_frame_equal(wrets, wrets_exp)
コード例 #19
0
def dates():
    return pd.Series([TS('2016-10-20'),
                      TS('2016-11-21'),
                      TS('2016-12-20')],
                     index=['CLX16', 'CLZ16', 'CLF17'])