예제 #1
0
def portfolio():

    df1 = pd.read_csv("tests/data/input/bitfinex_(BTC,ETH)USD_d.csv").tail(100)
    df1 = df1.rename({"Unnamed: 0": "date"}, axis=1)
    df1 = df1.set_index("date")

    df2 = pd.read_csv("tests/data/input/bitstamp_(BTC,ETH,LTC)USD_d.csv").tail(
        100)
    df2 = df2.rename({"Unnamed: 0": "date"}, axis=1)
    df2 = df2.set_index("date")

    ex1 = Exchange("bitfinex", service=execute_order)(
        Stream.source(list(df1['BTC:close']), dtype="float").rename("USD-BTC"),
        Stream.source(list(df1['ETH:close']), dtype="float").rename("USD-ETH"))

    ex2 = Exchange("binance", service=execute_order)(
        Stream.source(list(df2['BTC:close']), dtype="float").rename("USD-BTC"),
        Stream.source(list(df2['ETH:close']), dtype="float").rename("USD-ETH"),
        Stream.source(list(df2['LTC:close']), dtype="float").rename("USD-LTC"))

    p = Portfolio(USD, [
        Wallet(ex1, 10000 * USD),
        Wallet(ex1, 10 * BTC),
        Wallet(ex1, 5 * ETH),
        Wallet(ex2, 1000 * USD),
        Wallet(ex2, 5 * BTC),
        Wallet(ex2, 20 * ETH),
        Wallet(ex2, 3 * LTC),
    ])
    return p
예제 #2
0
def test_max():

    s1 = Stream.source([-1, 2, -3, 4, -5], dtype="float")
    s2 = Stream.source([-1, 2, 3, 2, 1], dtype="float")

    w = s1.max(s2).rename("w")
    expected = [-1, 2, 3, 4, 1]

    assert_op([w], expected)
예제 #3
0
def test_reduce_prod():

    s1 = Stream.source([1, 2, 3, 4, 5, 6, 7], dtype="float").rename("s1")
    s2 = Stream.source([3, 6, 4, 2, 0, 4, 10], dtype="float").rename("s2")

    w = Stream.reduce([s1, s2]).prod().rename("w")

    expected = [3, 12, 12, 8, 0, 24, 70]

    assert_op([w], expected)
예제 #4
0
def test_reduce_sum():

    s1 = Stream.source([1, 2, 3, 4, 5, 6, 7], dtype="float").rename("s1")
    s2 = Stream.source([7, 6, 5, 4, 3, 2, 1], dtype="float").rename("s2")

    w = Stream.reduce([s1, s2]).sum().rename("w")

    expected = [8, 8, 8, 8, 8, 8, 8]

    assert_op([w], expected)
예제 #5
0
def test_reduce_max():

    s1 = Stream.source([1, 2, 3, 4, 5, 6, 7], dtype="float").rename("s1")
    s2 = Stream.source([-3, 6, 4, 2, 0, 4, 10], dtype="float").rename("s2")

    w = Stream.reduce([s1, s2]).max().rename("w")

    expected = [1, 6, 4, 4, 5, 6, 10]

    assert_op([w], expected)
예제 #6
0
def test_runs_with_external_feed_only(portfolio):

    df = pd.read_csv("tests/data/input/bitfinex_(BTC,ETH)USD_d.csv").tail(100)
    df = df.rename({"Unnamed: 0": "date"}, axis=1)
    df = df.set_index("date")

    bitfinex_btc = df.loc[:, [name.startswith("BTC") for name in df.columns]]
    bitfinex_eth = df.loc[:, [name.startswith("ETH") for name in df.columns]]

    ta.add_all_ta_features(
        bitfinex_btc,
        colprefix="BTC:",
        **{k: "BTC:" + k
           for k in ['open', 'high', 'low', 'close', 'volume']})
    ta.add_all_ta_features(
        bitfinex_eth,
        colprefix="ETH:",
        **{k: "ETH:" + k
           for k in ['open', 'high', 'low', 'close', 'volume']})

    streams = []
    with NameSpace("bitfinex"):
        for name in bitfinex_btc.columns:
            streams += [
                Stream.source(list(bitfinex_btc[name]),
                              dtype="float").rename(name)
            ]
        for name in bitfinex_eth.columns:
            streams += [
                Stream.source(list(bitfinex_eth[name]),
                              dtype="float").rename(name)
            ]

    feed = DataFeed(streams)

    action_scheme = ManagedRiskOrders()
    reward_scheme = SimpleProfit()

    env = default.create(portfolio=portfolio,
                         action_scheme=action_scheme,
                         reward_scheme=reward_scheme,
                         feed=feed,
                         window_size=50,
                         enable_logger=False)

    done = False
    obs = env.reset()
    while not done:
        action = env.action_space.sample()
        obs, reward, done, info = env.step(action)

    assert obs.shape[0] == 50
예제 #7
0
def test_generic():

    s1 = Stream.source(["hello", "my", "name", "is"], dtype="string")
    s2 = Stream.source([1, 2, 3, 4, 5, 6])

    g1 = s1.apply(lambda x: x[0]).rename("g1")
    g2 = s2.lag().rename("g2")

    feed = DataFeed([g1, g2])
    feed.compile()

    feed.next()
    assert feed.next() == {"g1": "m", "g2": 1}
예제 #8
0
def test_cummax():
    for array in arrays:
        s = Stream.source(array, dtype="float")
        w = s.cummax(skipna=True).rename("w")
        expected = list(pd.Series(array).cummax(skipna=True))

        assert_op([w], expected)

    for array in arrays:
        s = Stream.source(array, dtype="float")
        w = s.cummin(skipna=False).rename("w")
        expected = list(pd.Series(array).cummin(skipna=False))

        assert_op([w], expected)
예제 #9
0
def test_diff():

    for array in arrays:
        s = Stream.source(array, dtype="float")
        w = s.diff(periods=1).rename("w")
        expected = list(pd.Series(array).diff(periods=1))

        assert_op([w], expected)

    for array in arrays:
        s = Stream.source(array, dtype="float")
        w = s.diff(periods=2).rename("w")
        expected = list(pd.Series(array).diff(periods=2))

        assert_op([w], expected)
예제 #10
0
def test_neg():
    s = Stream.source([3, -4, 6, -7, 2, -6], dtype="float")

    s1 = s.neg().rename("s1")
    s2 = (-s).rename("s2")

    assert_op([s1, s2], [-3, 4, -6, 7, -2, 6])
예제 #11
0
def test_abs():
    s = Stream.source([3, -4, 6, -7, 2, -6], dtype="float")

    s1 = s.abs().rename("s1")
    s2 = abs(s).rename("s2")

    assert_op([s1, s2], [3, 4, 6, 7, 2, 6])
예제 #12
0
def test_cumprod():
    for array in arrays:
        s = Stream.source(array, dtype="float")
        w = s.cumprod().rename("w")
        expected = list(pd.Series(array).cumprod())

        assert_op([w], expected)
예제 #13
0
def test_log():
    for array in arrays:
        s = Stream.source(array, dtype="float")
        w = s.log().rename("w")
        expected = list(pd.Series(array).apply(np.log))

        assert_op([w], expected)
예제 #14
0
def test_expanding_max():
    for array, config in product(arrays, configurations):
        s = Stream.source(array, dtype="float")
        w = s.expanding(**config).max().rename("w")
        expected = list(pd.Series(array).expanding(**config).max())

        assert_op([w], expected)
예제 #15
0
def test_float_accessor():

    s = Stream.source([1, 2, 3, 4, 5])

    w = s.float.square().rename("w")

    assert_op([w], [1, 4, 9, 16, 25])
예제 #16
0
def test_sub():
    expected = [0, 1, 2, 3, 4, 5]

    # (left, right) : (Stream, Stream)
    s1 = Stream.source([1, 2, 3, 4, 5, 6], dtype="float")
    s2 = Stream.source([1, 1, 1, 1, 1, 1], dtype="float")

    w1 = s1.sub(s2).rename("w1")
    w2 = (s1 - s2).rename("w2")

    assert_op([w1, w2], expected)

    # (left, right) : (Stream, float)
    w1 = s1.sub(1).rename("w1")
    w2 = (s1 - 1).rename("w2")

    assert_op([w1, w2], expected)
예제 #17
0
def test_rsub():
    # (left, right) : (float, Stream)
    s1 = 6
    s2 = Stream.source([1, 2, 3, 4, 5, 6], dtype="float")

    w = (s1 - s2).rename("w")

    assert_op([w], [5, 4, 3, 2, 1, 0])
예제 #18
0
def test_radd():
    # (left, right) : (float, Stream)
    s1 = 1
    s2 = Stream.source([1, 2, 3, 4, 5, 6], dtype="float")

    w = (s1 + s2).rename("w")

    assert_op([w], [2, 3, 4, 5, 6, 7])
예제 #19
0
def test_div():
    expected = [1, 2, 3, 4, 5, 6]

    # (left, right) : (Stream, Stream)
    s1 = Stream.source([2, 4, 6, 8, 10, 12], dtype="float")
    s2 = Stream.source([2, 2, 2, 2, 2, 2], dtype="float")

    w1 = s1.div(s2).rename("w1")
    w2 = (s1 / s2).rename("w2")

    assert_op([w1, w2], expected)

    # (left, right) : (Stream, float)
    w1 = s1.div(2).rename("w1")
    w2 = (s1 / 2).rename("w2")

    assert_op([w1, w2], expected)
예제 #20
0
def test_ffill():
    array = [-1, np.nan, -3, 4, np.nan]

    s = Stream.source(array, dtype="float")

    w = s.ffill().rename("w")
    expected = list(pd.Series(array).ffill())

    assert_op([w], expected)
예제 #21
0
def test_clamp_min():
    array = [-1, 2, -3, 4, -5]

    s = Stream.source(array, dtype="float")

    w = s.clamp_min(0).rename("w")
    expected = list(pd.Series(array).apply(lambda x: x if x > 0 else 0))

    assert_op([w], expected)
예제 #22
0
def test_rmul():
    expected = [2, 4, 6, 8, 10, 12]

    # (left, right) : (Stream, Stream)
    s = Stream.source([1, 2, 3, 4, 5, 6], dtype="float")

    # (left, right) : (Stream, float)
    w = (2 * s).rename("w")

    assert_op([w], expected)
예제 #23
0
def test_rdiv():
    expected = [6, 3, 2, 3 / 2, 6 / 5, 1]

    # (left, right) : (Stream, Stream)
    s = Stream.source([2, 4, 6, 8, 10, 12], dtype="float")

    # (left, right) : (Stream, float)
    w = (12 / s).rename("w")

    assert_op([w], expected)
예제 #24
0
def test_add():

    # (left, right) : (Stream, Stream)
    s1 = Stream.source([3, -4, 6, -7, 2, -6], dtype="float")
    s2 = Stream.source([-3, 4, -6, 7, -2, 6], dtype="float")

    w1 = s1.add(s2).rename("w1")
    w2 = (s1 + s2).rename("w2")

    assert_op([w1, w2], 6 * [0])

    # (left, right) : (Stream, float)
    s1 = Stream.source([1, 2, 3, 4, 5, 6], dtype="float")
    s2 = 1

    w1 = s1.add(s2).rename("w1")
    w2 = (s1 + s2).rename("w2")

    assert_op([w1, w2], [2, 3, 4, 5, 6, 7])
예제 #25
0
def test_ewm_mean():

    array = [1, np.nan, 3, 4, 5, 6, np.nan, 7]

    s = Stream.source(array, dtype="float")

    for config in configurations:
        w = s.ewm(**config).mean().rename("w")
        expected = list(pd.Series(array).ewm(**config).mean())

        assert_op([w], expected)
예제 #26
0
def test_pow():
    array = [1, -2, 3, -4, 5, -6]

    s = Stream.source(array, dtype="float")

    s1 = s.pow(3).rename("s1")
    s2 = (s**3).rename("s2")

    expected = list(pd.Series(array)**3)

    assert_op([s1, s2], expected)
예제 #27
0
def test_str_accessor():

    s = Stream.source(
        ["hello", "my", "name", "i", "am", "the", "data", "feed"])

    w1 = s.str.upper().rename("w1")
    w2 = s.str.lower().rename("w2")
    w3 = w1.str.endswith("E").rename("w3")

    feed = DataFeed([w1, w2, w3])
    feed.compile()

    assert feed.next() == {"w1": "HELLO", "w2": "hello", "w3": False}
예제 #28
0
def test_bool_methods():
    s = Stream.source(
        ["hello", "my", "name", "i", "am", "the", "data", "feed"],
        dtype="string")

    w1 = s.upper().rename("w1")
    w2 = s.lower().rename("w2")
    w3 = w1.endswith("E").invert().rename("w3")

    feed = DataFeed([w1, w2, w3])
    feed.compile()

    assert feed.next() == {"w1": "HELLO", "w2": "hello", "w3": True}
예제 #29
0
def test_pct_change():
    configs = [
        {"periods": 1, "fill_method": None},
        {"periods": 1, "fill_method": "pad"},
        {"periods": 1, "fill_method": "ffill"},
        {"periods": 2, "fill_method": None},
        {"periods": 2, "fill_method": "pad"},
        {"periods": 2, "fill_method": "ffill"},
    ]

    for array, config in product(arrays, configs):

        s = Stream.source(array, dtype="float")
        w = s.pct_change(**config).rename("w")
        expected = list(pd.Series(array).pct_change(**config))

        print(config)
        assert_op([w], expected)
예제 #30
0
def test_boolean():

    s = Stream.source(["hello", "my", "name", "is", "slim", "shady"],
                      dtype="string")

    s.startswith("m").invert()