def test_namespace(): a = Stream([1, 2, 3]).rename("a") with Module("world") as world: a1 = Stream([4, 5, 6]).rename("a1") a2 = Stream([7, 8, 9]).rename("a2") with Module("sub-world") as sub_world: a3 = Stream([10, 11, 12]).rename("a3") a4 = Stream([13, 14, 15]).rename("a4") t3 = BinOp(np.add)(a2, a4).rename("t3") t1 = BinOp(np.multiply)(a, t3).rename("t1") feed = DataFeed([t1, world, sub_world]) assert feed.next() == { "world:/a1": 4, "world:/a2": 7, "world:/sub-world:/a3": 10, "world:/sub-world:/a4": 13, "world:/sub-world:/t3": 20, "t1": 20 } feed.reset() assert feed.next() == { "world:/a1": 4, "world:/a2": 7, "world:/sub-world:/a3": 10, "world:/sub-world:/a4": 13, "world:/sub-world:/t3": 20, "t1": 20 }
def test_namespace(): a = Stream("a", [1, 2, 3]) with Module("world") as world: a1 = Stream("a1", [4, 5, 6]) a2 = Stream("a2", [7, 8, 9]) with Module("sub-world") as sub_world: a3 = Stream("a3", [10, 11, 12]) a4 = Stream("a4", [13, 14, 15]) t3 = BinOp("t3", operator.add)(a2, a4) t1 = BinOp("t1", operator.mul)(a, t3) feed = DataFeed()(t1, world, sub_world) assert feed.next() == { "world:/a1": 4, "world:/a2": 7, "world:/sub-world:/a3": 10, "world:/sub-world:/a4": 13, "world:/sub-world:/t3": 20, "t1": 20 } feed.reset() assert feed.next() == { "world:/a1": 4, "world:/a2": 7, "world:/sub-world:/a3": 10, "world:/sub-world:/a4": 13, "world:/sub-world:/t3": 20, "t1": 20 }
def test_namespace(): a1 = Array("a1", [7, 8, 9]) a2 = Array("a2", [3, 2, 1]) t1 = BinOp("t1", operator.mul)(a1, a2) a = Namespace("world")(a1, a2) feed = DataFeed([t1, a]) assert feed.next() == {"world:/a1": 7, "world:/a2": 3, "t1": 21} feed.reset() assert feed.next() == {"world:/a1": 7, "world:/a2": 3, "t1": 21}
def test_exchange_with_wallets_feed(): ex1 = Exchange("coinbase", service=execute_order)(Array("USD-BTC", [7000, 7500, 8300]), Array("USD-ETH", [200, 212, 400])) ex2 = Exchange("binance", service=execute_order)(Array("USD-BTC", [7005, 7600, 8200]), Array("USD-ETH", [201, 208, 402]), Array("USD-LTC", [56, 52, 60])) wallet_btc = Wallet(ex1, 10 * BTC) wallet_btc_ds = create_wallet_source(wallet_btc) wallet_usd = Wallet(ex2, 1000 * USD) wallet_usd -= 400 * USD wallet_usd += Quantity(USD, 400, path_id="fake_id") wallet_usd_ds = create_wallet_source(wallet_usd, include_worth=False) feed = DataFeed([ex1, ex2, wallet_btc_ds, wallet_usd_ds]) assert feed.next() == { "coinbase:/USD-BTC": 7000, "coinbase:/USD-ETH": 200, "coinbase:/BTC:/free": 10, "coinbase:/BTC:/locked": 0, "coinbase:/BTC:/total": 10, "coinbase:/BTC:/worth": 70000, "binance:/USD-BTC": 7005, "binance:/USD-ETH": 201, "binance:/USD-LTC": 56, "binance:/USD:/free": 600, "binance:/USD:/locked": 400, "binance:/USD:/total": 1000 }
def test_multi_step_adding(): a1 = Array('a1', [1, 2, 3]) a2 = Array('a2', [4, 5, 6]) t1 = BinOp('t1', operator.add)(a1, a2) t2 = BinOp('t2', operator.add)(t1, a2) feed = DataFeed([a1, a2, t1, t2]) output = feed.next() assert output == {'a1': 1, 'a2': 4, 't1': 5, 't2': 9} feed = DataFeed([a1, a2, t2]) output = feed.next() assert output == {'a1': 1, 'a2': 4, 't2': 9}
def test_multi_step_adding(): a1 = Stream([1, 2, 3]).rename("a1") a2 = Stream([4, 5, 6]).rename("a2") t1 = BinOp(np.add)(a1, a2).rename("t1") t2 = BinOp(np.add)(t1, a2).rename("t2") feed = DataFeed([a1, a2, t1, t2]) output = feed.next() assert output == {'a1': 1, 'a2': 4, 't1': 5, 't2': 9} feed = DataFeed([a1, a2, t2]) output = feed.next() assert output == {'a1': 1, 'a2': 4, 't2': 9}
def test_price_ds(): btc_price = Stream("USD-BTC", [7000, 7500, 8300]) eth_price = Stream("USD-ETH", [200, 212, 400]) feed = DataFeed()(btc_price, eth_price) assert feed.next() == {"USD-BTC": 7000, "USD-ETH": 200}
def test_exchange_feed(): btc_price = Stream("USD-BTC", [7000, 7500, 8300]) eth_price = Stream("USD-ETH", [200, 212, 400]) exchange = Exchange("coinbase", service=execute_order)(btc_price, eth_price) feed = DataFeed([exchange]) assert feed.next() == {"coinbase:/USD-BTC": 7000, "coinbase/USD-ETH": 200}
def test_stream_adding(): a1 = Stream([1, 2, 3]).rename("a1") a2 = Stream([4, 5, 6]).rename("a2") t1 = BinOp(np.add)(a1, a2).rename("a1+a2") feed = DataFeed([t1, a1, a2]) output = feed.next() assert output == {'a1': 1, 'a2': 4, 'a1+a2': 5}
def test_stream_adding(): a1 = Array('a1', [1, 2, 3]) a2 = Array('a2', [4, 5, 6]) t1 = BinOp("a1+a2", operator.add)(a1, a2) feed = DataFeed([a1, a2, t1]) output = feed.next() assert output == {'a1': 1, 'a2': 4, 'a1+a2': 5}
def test_log_returns(): s1 = Stream([200.23, 198.35, 244.36, 266.30, 250.40], "price") assert s1.name == "price" lp = s1.log() lr = lp - lp.lag() feed = DataFeed([lr]) feed.compile() while feed.has_next(): print(feed.next()) lr = s1.log().diff().rename("log_return") feed = DataFeed([lr]) feed.compile() while feed.has_next(): print(feed.next())
def test_truediv(): s1 = Stream([2, 3, 4, 5, 6], "s1") s2 = Stream([2, 3, 4, 5, 6], "s2") s = (s1 / s2).rename("div") feed = DataFeed([s]) feed.compile() values = [] while feed.has_next(): values += [feed.next()["div"]] assert values == [1, 1, 1, 1, 1] s1 = Stream([1, 1, 1, 1, 1], "s2") s = (5 / s1).rename("div") feed = DataFeed([s]) feed.compile() values = [] while feed.has_next(): values += [feed.next()["div"]] assert values == [5, 5, 5, 5, 5] s1 = Stream([5, 5, 5, 5, 5], "s2") s = (s1 / 5).rename("div") feed = DataFeed([s]) feed.compile() values = [] while feed.has_next(): values += [feed.next()["div"]] assert values == [1, 1, 1, 1, 1]
def test_select(): a3 = Stream([3, 2, 1]).rename("a3") with Module("world") as a: a1 = Stream([7, 8, 9]).rename("a1") a2 = Stream([3, 2, 1]).rename("a2") t1 = BinOp(np.multiply)(a1, a3).rename("t1") s = Select("world:/a1")(t1, a) feed = DataFeed([s]) assert feed.next() == {"world:/a1": 7}
def test_apply(): s1 = Stream([1, 4, 9, 16, 25], "s1") lag = s1.apply(np.sqrt).rename("apply") feed = DataFeed([lag]) feed.compile() values = [] while feed.has_next(): values += [feed.next()["apply"]] assert values == [1, 2, 3, 4, 5]
def test_select(): a3 = Stream("a3", [3, 2, 1]) with Module("world") as a: a1 = Stream("a1", [7, 8, 9]) a2 = Stream("a2", [3, 2, 1]) t1 = BinOp("t1", operator.mul)(a1, a3) s = Select("world:/a1")(t1, a) feed = DataFeed()(s) assert feed.next() == {"world:/a1": 7}
def test_fillna(): s = Stream([1, np.nan, -3, np.nan, 5], "s") m = s.fillna(-1).rename("fill") feed = DataFeed([m]) feed.compile() expected = [1, -1, -3, -1, 5] actual = [] while feed.has_next(): actual += [feed.next()["fill"]] assert actual == expected
def test_clamp_max(): s = Stream([1, -2, -3, 0, 5], "s") m = s.clamp_max(0).rename("clamp_max") feed = DataFeed([m]) feed.compile() expected = [0, -2, -3, 0, 0] actual = [] while feed.has_next(): actual += [feed.next()["clamp_max"]] assert actual == expected
def test_lag(): s1 = Stream([1, 2, 3, 4, 5]).rename("stream") assert s1.name == "stream" lag = s1.lag() assert lag.name == "Lag(stream,1)" feed = DataFeed([lag]) feed.compile() values = [] while feed.has_next(): values += [feed.next()["Lag(stream,1)"]] assert values == [np.nan, 1, 2, 3, 4]
def test_max(): s1 = Stream([1, 2, 3, 4], "s1") s2 = Stream([1, 4, 3, 2], "s2") m = s1.max(s2).rename("max") feed = DataFeed([m]) feed.compile() expected = [1, 4, 3, 4] actual = [] while feed.has_next(): actual += [feed.next()["max"]] assert actual == expected
def test_select(): a1 = Array("a1", [7, 8, 9]) a2 = Array("a2", [3, 2, 1]) t1 = BinOp("t1", operator.mul)(a1, a2) a = Namespace("world")(a1, a2) s = Select("world:/a1")(t1, a) feed = DataFeed([s]) print(a1.name, a1.inbound, a1.outbound) print(a2.name, a2.inbound, a2.outbound) print(t1.name, t1.inbound, t1.outbound) print(a.name, a.inbound, a.outbound) print(s.name, s.inbound, s.outbound) print(feed.inputs) assert feed.next() == {"world:/a1": 7}
def test_emwmv_unbiased(): # bias: True v = [np.nan, 2, np.nan, 6, 8, 5] s = Stream(v, "s") specs = [{ "alpha": 0.68, "adjust": True, "ignore_na": True, "min_periods": 3 }, { "alpha": 0.68, "adjust": True, "ignore_na": False, "min_periods": 3 }, { "alpha": 0.68, "adjust": False, "ignore_na": True, "min_periods": 3 }, { "alpha": 0.68, "adjust": False, "ignore_na": False, "min_periods": 3 }] for spec in specs: d = spec.copy() d["warmup"] = d["min_periods"] del d["min_periods"] var = s.ewm(**d).var(bias=False).rename("var") feed = DataFeed([var]) feed.compile() expected = list(pd.Series(v).ewm(**spec).var(bias=False)) actual = [] while feed.has_next(): actual += [feed.next()["var"]] assert all(np.isclose(actual, expected))
def test_ewa_beginning_na(): # adjust: True, ignore_na: False v = [np.nan, 2, np.nan, 6, 8, 5] s = Stream(v).rename("s") specs = [{ "alpha": 0.68, "adjust": True, "ignore_na": True, "min_periods": 3 }, { "alpha": 0.68, "adjust": True, "ignore_na": False, "min_periods": 3 }, { "alpha": 0.68, "adjust": False, "ignore_na": True, "min_periods": 3 }, { "alpha": 0.68, "adjust": False, "ignore_na": False, "min_periods": 3 }] for spec in specs: d = spec.copy() d["warmup"] = d["min_periods"] del d["min_periods"] mean = s.ewm(**d).mean().rename("mean") feed = DataFeed([mean]) feed.compile() expected = list(pd.Series(v).ewm(**spec).mean()) actual = [] while feed.has_next(): actual += [feed.next()["mean"]] assert all(np.isclose(actual, expected))
def test_sub(): s1 = Stream([2, 3, 4, 5, 6], "s1") assert s1.name == "s1" s2 = Stream([1, 2, 3, 4, 5], "s2") assert s2.name == "s2" s = s1 - s2 assert s.name == "Subtract(s1,s2)" feed = DataFeed([s]) feed.compile() values = [] while feed.has_next(): values += [feed.next()["Subtract(s1,s2)"]] assert values == [1, 1, 1, 1, 1]
def test_add(): s1 = Stream([1, 2, 3, 4, 5], "s1") assert s1.name == "s1" s2 = Stream([1, 2, 3, 4, 5], "s2") assert s2.name == "s2" s = s1 + s2 assert s.name == "Add(s1,s2)" feed = DataFeed([s]) feed.compile() values = [] while feed.has_next(): values += [feed.next()["Add(s1,s2)"]] assert values == [2, 4, 6, 8, 10]
if col != "target" and col != "future_timestamp" and col != "time": agent_data[col] = agent_data[col].pct_change() agent_data.dropna(inplace=True) print(agent_data.columns) print(col) agent_data[col] = preprocessing.scale(agent_data[col].values) data.dropna(inplace=True) with Module("coinbase") as node_stream: nodes = [] for name in data.columns: nodes.append(Stream(list(data[name]), name)) data_feed = DataFeed([node_stream]) data_feed.next() exchange = Exchange("sim-exchange", service=execute_order)(Stream(list(data['BTC-USD_close']), "USD-BTC"), Stream(list(data['ETH-USD_close']), "USD-ETH"), Stream(list(data['LTC-USD_close']), "USD-LTC")) portfolio = Portfolio(base_instrument=USD, wallets=[ Wallet(exchange, 100000 * USD), Wallet(exchange, 0 * BTC), Wallet(exchange, 0 * LTC), Wallet(exchange, 0 * ETH) ])
def test_ewma(): # adjust: True, ignore_na: True v = [5, 2, 4, 6] s = Stream(v, "s") mean = s.ewm(alpha=0.68, adjust=True, ignore_na=True).mean().rename("mean") feed = DataFeed([mean]) feed.compile() expected = list( pd.Series(v).ewm(alpha=0.68, adjust=True, ignore_na=True).mean()) actual = [] while feed.has_next(): actual += [feed.next()["mean"]] assert all(np.isclose(actual, expected)) # adjust: True, ignore_na: False v = [5, 2, np.nan, 6] s = Stream(v, "s") mean = s.ewm(alpha=0.68, adjust=True, ignore_na=False).mean().rename("mean") feed = DataFeed([mean]) feed.compile() expected = list( pd.Series(v).ewm(alpha=0.68, adjust=True, ignore_na=False).mean()) actual = [] while feed.has_next(): actual += [feed.next()["mean"]] assert all(np.isclose(actual, expected)) # adjust: True, ignore_na: False v = [5, 2, np.nan, 6] s = Stream(v, "s") mean = s.ewm(alpha=0.68, adjust=False, ignore_na=True).mean().rename("mean") feed = DataFeed([mean]) feed.compile() expected = list( pd.Series(v).ewm(alpha=0.68, adjust=False, ignore_na=True).mean()) actual = [] while feed.has_next(): actual += [feed.next()["mean"]] assert all(np.isclose(actual, expected)) # adjust: True, ignore_na: False v = [5, 2, np.nan, 6] s = Stream(v, "s") mean = s.ewm(alpha=0.68, adjust=False, ignore_na=False).mean().rename("mean") feed = DataFeed([mean]) feed.compile() expected = list( pd.Series(v).ewm(alpha=0.68, adjust=False, ignore_na=False).mean()) actual = [] while feed.has_next(): actual += [feed.next()["mean"]] assert all(np.isclose(actual, expected))