def test_generate_weighted_returns(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'returns':
        pd.DataFrame(
            [[np.nan, np.nan, np.nan], [1.59904743, 1.66397210, 1.67345829],
             [-0.37065629, -0.36541822, -0.36015840],
             [-0.41055669, 0.60004777, 0.00536958]], dates, tickers),
        'weights':
        pd.DataFrame([[0.03777059, 0.04733924, 0.05197790],
                      [0.82074874, 0.48533938, 0.75792752],
                      [0.10196420, 0.05866016, 0.09578226],
                      [0.03951647, 0.40866122, 0.09431233]], dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('weighted_returns',
         pd.DataFrame(
             [[np.nan, np.nan, np.nan], [1.31241616, 0.80759119, 1.26836009],
              [-0.03779367, -0.02143549, -0.03449679],
              [-0.01622375, 0.24521625, 0.00050642]], dates, tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_date_top_industries(fn):
    tickers = generate_random_tickers(10)
    dates = generate_random_dates(2)

    fn_inputs = {
        'prices':
        pd.DataFrame(
            [[
                21.050810483942833, 17.013843810658827, 10.984503755486879,
                11.248093428369392, 12.961712733997235, 482.34539247360806,
                35.202580592515041, 3516.5416782257166, 66.405314327318209,
                13.503960481087077
            ],
             [
                 15.63570258751384, 14.69054309070934, 11.353027688995159,
                 475.74195118202061, 11.959640427803022, 10.918933017418304,
                 17.9086438675435, 24.801265417692324, 12.488954191854916,
                 15.63570258751384
             ]], dates, tickers),
        'sector':
        pd.Series([
            'ENERGY', 'MATERIALS', 'ENERGY', 'ENERGY', 'TELECOM', 'FINANCIALS',
            'TECHNOLOGY', 'HEALTH', 'MATERIALS', 'REAL ESTATE'
        ], tickers),
        'date':
        dates[-1],
        'top_n':
        4
    }
    fn_correct_outputs = OrderedDict([('top_industries',
                                       {'ENERGY', 'HEALTH', 'TECHNOLOGY'})])

    assert_output(fn, fn_inputs, fn_correct_outputs)
示例#3
0
def test_calculate_simple_moving_average(fn):
    tickers = generate_random_tickers(5)
    dates = generate_random_dates(6)

    fn_inputs = {
        'rolling_window':
        3,
        'close':
        pd.DataFrame(
            [[
                21.050810483942833, 17.013843810658827, 10.984503755486879,
                11.248093428369392, 12.961712733997235
            ],
             [
                 15.63570258751384, 14.69054309070934, 11.353027688995159,
                 475.74195118202061, 11.959640427803022
             ],
             [
                 482.34539247360806, 35.202580592515041, 3516.5416782257166,
                 66.405314327318209, 13.503960481087077
             ],
             [
                 10.918933017418304, 17.9086438675435, 24.801265417692324,
                 12.488954191854916, 10.52435923388642
             ],
             [
                 10.675971965144655, 12.749401436636365, 11.805257579935713,
                 21.539039489843024, 19.99766036804861
             ],
             [
                 11.545495378369814, 23.981468434099405, 24.974763062186504,
                 36.031962102997689, 14.304332320024963
             ]], dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('simple_moving_average',
         pd.DataFrame([
             [np.nan, np.nan, np.nan, np.nan, np.nan],
             [np.nan, np.nan, np.nan, np.nan, np.nan],
             [
                 173.01063518, 22.30232250, 1179.62640322, 184.46511965,
                 12.80843788
             ],
             [
                 169.63334269, 22.60058918, 1184.23199044, 184.87873990,
                 11.99598671
             ],
             [
                 167.98009915, 21.95354197, 1184.38273374, 33.47776934,
                 14.67532669
             ],
             [11.04680012, 18.21317125, 20.52709535, 23.35331859, 14.94211731]
         ], dates, tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_tracking_error(fn):
    dates = generate_random_dates(4)

    fn_inputs = {
        'benchmark_returns_by_date':
        pd.Series([np.nan, 0.99880148, 0.99876653, 1.00024411], dates),
        'etf_returns_by_date':
        pd.Series([np.nan, 0.63859274, 0.93475823, 2.57295727], dates)
    }
    fn_correct_outputs = OrderedDict([('tracking_error', 16.5262431971)])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_rebalance_portfolio(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(11)

    fn_inputs = {
        'returns':
        pd.DataFrame(
            [[np.nan, np.nan, np.nan], [-0.02202381, 0.02265285, 0.01441961],
             [0.01947657, 0.00551985, 0.00047382],
             [0.00537313, -0.00803232, 0.01160313],
             [0.00593824, -0.00567773, 0.02247191],
             [0.02479339, 0.01758824, -0.00824176],
             [-0.0109447, -0.00383568, 0.01361958],
             [0.01164822, 0.01558719, 0.00614894],
             [0.0109384, -0.00182079, 0.02900868],
             [0.01138952, 0.00218049, -0.00954495],
             [0.0106982, 0.00644535, -0.01815329]], dates, tickers),
        'index_weights':
        pd.DataFrame([[0.00449404, 0.11586048, 0.00359727],
                      [
                          0.00403487,
                          0.12534048,
                          0.0034428,
                      ], [0.00423485, 0.12854258, 0.00347404],
                      [0.00395679, 0.1243466, 0.00335064],
                      [0.00368729, 0.11750295, 0.00333929],
                      [0.00369562, 0.11447422, 0.00325973],
                      [
                          0.00379612,
                          0.11088075,
                          0.0031734,
                      ], [0.00366501, 0.10806014, 0.00314648],
                      [0.00361268, 0.10376514, 0.00323257],
                      [0.00358844, 0.10097531, 0.00319009],
                      [0.00362045, 0.09791232, 0.00318071]], dates, tickers),
        'shift_size':
        2,
        'chunk_size':
        4
    }
    fn_correct_outputs = OrderedDict([('all_rebalance_weights', [
        np.array([0.29341237, 0.41378419, 0.29280344]),
        np.array([0.29654088, 0.40731481, 0.29614432]),
        np.array([0.29868214, 0.40308791, 0.29822995]),
        np.array([0.30100044, 0.39839644, 0.30060312])
    ])])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_calculate_cumulative_returns(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'returns':
        pd.DataFrame(
            [[np.nan, np.nan, np.nan], [1.59904743, 1.66397210, 1.67345829],
             [-0.37065629, -0.36541822, -0.36015840],
             [-0.41055669, 0.60004777, 0.00536958]], dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('cumulative_returns',
         pd.Series([np.nan, 5.93647782, -0.57128454, -0.68260542], dates))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_calculate_returns(fn):
    tickers = generate_random_tickers(5)
    dates = generate_random_dates(6)

    fn_inputs = {
        'close':
        pd.DataFrame(
            [[
                21.050810483942833, 17.013843810658827, 10.984503755486879,
                11.248093428369392, 12.961712733997235
            ],
             [
                 15.63570258751384, 14.69054309070934, 11.353027688995159,
                 475.74195118202061, 11.959640427803022
             ],
             [
                 482.34539247360806, 35.202580592515041, 3516.5416782257166,
                 66.405314327318209, 13.503960481087077
             ],
             [
                 10.918933017418304, 17.9086438675435, 24.801265417692324,
                 12.488954191854916, 10.52435923388642
             ],
             [
                 10.675971965144655, 12.749401436636365, 11.805257579935713,
                 21.539039489843024, 19.99766036804861
             ],
             [
                 11.545495378369814, 23.981468434099405, 24.974763062186504,
                 36.031962102997689, 14.304332320024963
             ]], dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('returns',
         pd.DataFrame([
             [np.nan, np.nan, np.nan, np.nan, np.nan],
             [-0.25723988, -0.13655355, 0.03354944, 41.29534136, -0.07731018],
             [29.84897463, 1.39627496, 308.74483411, -0.86041737, 0.12912763],
             [-0.97736283, -0.49126900, -0.99294726, -0.81192839, -0.22064647],
             [-0.02225135, -0.28808672, -0.52400584, 0.72464717, 0.90013092],
             [0.08144677, 0.88098779, 1.11556274, 0.67286764, -0.28469971]
         ], dates, tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_calculate_dividend_weights(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'dividends':
        pd.DataFrame([[0.0, 0.0, 0.0], [0.0, 0.0, 0.1], [0.0, 1.0, 0.3],
                      [0.0, 0.2, 0.0]], dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('dividend_weights',
         pd.DataFrame(
             [[np.nan, np.nan, np.nan], [0.00000000, 0.00000000, 1.00000000],
              [0.00000000, 0.71428571, 0.28571429],
              [0.00000000, 0.75000000, 0.25000000]], dates, tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_generate_returns(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'prices':
        pd.DataFrame([[35.4411, 34.1799, 34.0223], [92.1131, 91.0543, 90.9572],
                      [57.9708, 57.7814, 58.1982], [34.1705, 92.453, 58.5107]],
                     dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('returns',
         pd.DataFrame(
             [[np.nan, np.nan, np.nan], [1.59904743, 1.66397210, 1.67345829],
              [-0.37065629, -0.36541822, -0.36015840],
              [-0.41055669, 0.60004777, 0.00536958]], dates, tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_get_covariance_returns(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'returns':
        pd.DataFrame(
            [[np.nan, np.nan, np.nan], [1.59904743, 1.66397210, 1.67345829],
             [-0.37065629, -0.36541822, -0.36015840],
             [-0.41055669, 0.60004777, 0.00536958]], dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('returns_covariance',
         np.array([[0.89856076, 0.7205586, 0.8458721],
                   [0.7205586, 0.78707297, 0.76450378],
                   [0.8458721, 0.76450378, 0.83182775]]))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_generate_positions(fn):
    tickers = generate_random_tickers(5)
    dates = generate_random_dates(6)

    fn_inputs = {
        'prices':
        pd.DataFrame(
            [[
                65.40757705426432, 27.556319958924323, 50.59935209411175,
                56.274712269629134, 99.9873070881051
            ],
             [
                 47.82126720752715, 56.812865745668375, 40.75685814634723,
                 27.469680989736023, 41.449858088448735
             ],
             [
                 88.20038097315815, 45.573972499280494, 36.592711369868724,
                 21.36570423559795, 0.698919959739297
             ],
             [
                 14.670236824202721, 49.557949251949054, 18.935364730808935,
                 23.163368660093298, 8.075599541367884
             ],
             [
                 41.499140208637705, 9.75987296846733, 66.08677766062186,
                 37.927861417544385, 10.792730405945827
             ],
             [
                 86.26923464863536, 32.12679487375028, 15.621592524570282,
                 77.1908860965619, 52.733950486350444
             ]], dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('final_positions',
         pd.DataFrame([[30, 0, 30, 30, 30], [0, 30, 0, 0, 0], [
             30, 0, 0, 0, -10
         ], [-10, 0, -10, 0, -10], [0, -10, 30, 0, -10], [30, 0, -10, 30, 30]],
                      dates, tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_calculate_arithmetic_rate_of_return(fn):
    tickers = generate_random_tickers(5)
    dates = generate_random_dates(6)

    fn_inputs = {
        'close':
        pd.DataFrame(
            [[
                21.050810483942833, 17.013843810658827, 10.984503755486879,
                11.248093428369392, 12.961712733997235
            ],
             [
                 15.63570258751384, 14.69054309070934, 11.353027688995159,
                 475.74195118202061, 11.959640427803022
             ],
             [
                 482.34539247360806, 35.202580592515041, 3516.5416782257166,
                 66.405314327318209, 13.503960481087077
             ],
             [
                 10.918933017418304, 17.9086438675435, 24.801265417692324,
                 12.488954191854916, 10.52435923388642
             ],
             [
                 10.675971965144655, 12.749401436636365, 11.805257579935713,
                 21.539039489843024, 19.99766036804861
             ],
             [
                 11.545495378369814, 23.981468434099405, 24.974763062186504,
                 36.031962102997689, 14.304332320024963
             ]], dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('arithmetic_returns',
         pd.Series(
             [4.77892789, 0.22689225, 51.39616553, 6.83675173, 0.07443370],
             tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)
def test_generate_dollar_volume_weights(fn):
    tickers = generate_random_tickers(3)
    dates = generate_random_dates(4)

    fn_inputs = {
        'close':
        pd.DataFrame([[35.4411, 34.1799, 34.0223], [92.1131, 91.0543, 90.9572],
                      [57.9708, 57.7814, 58.1982], [34.1705, 92.453, 58.5107]],
                     dates, tickers),
        'volume':
        pd.DataFrame([[9.83683e+06, 1.78072e+07, 8.82982e+06],
                      [8.22427e+07, 6.85315e+07, 4.81601e+07],
                      [1.62348e+07, 1.30527e+07, 9.51201e+06],
                      [1.06742e+07, 5.68313e+07, 9.31601e+06]], dates, tickers)
    }
    fn_correct_outputs = OrderedDict([
        ('dollar_volume_weights',
         pd.DataFrame([[0.27719777, 0.48394253, 0.23885970],
                       [0.41632975, 0.34293308, 0.24073717],
                       [0.41848548, 0.33536102, 0.24615350],
                       [0.05917255, 0.85239760, 0.08842984]], dates, tickers))
    ])

    assert_output(fn, fn_inputs, fn_correct_outputs)