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_array_next(): array_ds = Array('a', [1, 2, 3]) next_value = array_ds.next() assert next_value == {'a': 1}
def portfolio(): df1 = pd.read_csv("tests/data/input/coinbase_(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("coinbase", service=execute_order)(Array("USD-BTC", list(df1['BTC:close'])), Array("USD-ETH", list(df1['ETH:close']))) ex2 = Exchange("binance", service=execute_order)(Array("USD-BTC", list(df2['BTC:close'])), Array("USD-ETH", list(df2['ETH:close'])), Array("USD-LTC", list(df2['LTC:close']))) 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
def test_init(): sources = [ Array('a1', [1, 2, 3]), Array('a2', [4, 5, 6]), Array('a3', [7, 8, 9]) ] feed = DataFeed(sources) assert feed
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_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_runs_with__external_feed_only(portfolio): df = pd.read_csv("tests/data/input/coinbase_(BTC,ETH)USD_d.csv").tail(100) df = df.rename({"Unnamed: 0": "date"}, axis=1) df = df.set_index("date") coinbase_btc = df.loc[:, [name.startswith("BTC") for name in df.columns]] coinbase_eth = df.loc[:, [name.startswith("ETH") for name in df.columns]] ta.add_all_ta_features( coinbase_btc, colprefix="BTC:", **{k: "BTC:" + k for k in ['open', 'high', 'low', 'close', 'volume']}) ta.add_all_ta_features( coinbase_eth, colprefix="ETH:", **{k: "ETH:" + k for k in ['open', 'high', 'low', 'close', 'volume']}) nodes = [] for name in coinbase_btc.columns: nodes += [Array(name, list(coinbase_btc[name]))] for name in coinbase_eth.columns: nodes += [Array(name, list(coinbase_eth[name]))] coinbase = Namespace("coinbase")(*nodes) feed = DataFeed([coinbase]) action_scheme = ManagedRiskOrders() reward_scheme = SimpleProfit() env = TradingEnvironment(portfolio=portfolio, action_scheme=action_scheme, reward_scheme=reward_scheme, feed=feed, window_size=50, use_internal=False, enable_logger=False) done = False obs = env.reset() while not done: action = env.action_space.sample() obs, reward, done, info = env.step(action) n_features = coinbase_btc.shape[1] + coinbase_eth.shape[1] assert obs.shape == (50, n_features)
def test_array_init(): array_ds = Array('a', [1, 2, 3]) assert array_ds assert array_ds._array == [1, 2, 3] assert array_ds._cursor == 0
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_array_reset(): array_ds = Array('a', [1, 2, 3]) assert array_ds.next() == {'a': 1} assert array_ds.next() == {'a': 2} assert array_ds.next() == {'a': 3} array_ds.reset() assert array_ds.next() == {'a': 1} assert array_ds.next() == {'a': 2} assert array_ds.next() == {'a': 3}
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_create_internal_data_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])) portfolio = 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), ]) feed = create_internal_feed(portfolio) data = { "coinbase:/USD-BTC": 7000, "coinbase:/USD-ETH": 200, "coinbase:/USD:/free": 10000, "coinbase:/USD:/locked": 0, "coinbase:/USD:/total": 10000, "coinbase:/BTC:/free": 10, "coinbase:/BTC:/locked": 0, "coinbase:/BTC:/total": 10, "coinbase:/BTC:/worth": 7000 * 10, "coinbase:/ETH:/free": 5, "coinbase:/ETH:/locked": 0, "coinbase:/ETH:/total": 5, "coinbase:/ETH:/worth": 200 * 5, "binance:/USD-BTC": 7005, "binance:/USD-ETH": 201, "binance:/USD-LTC": 56, "binance:/USD:/free": 1000, "binance:/USD:/locked": 0, "binance:/USD:/total": 1000, "binance:/BTC:/free": 5, "binance:/BTC:/locked": 0, "binance:/BTC:/total": 5, "binance:/BTC:/worth": 7005 * 5, "binance:/ETH:/free": 20, "binance:/ETH:/locked": 0, "binance:/ETH:/total": 20, "binance:/ETH:/worth": 201 * 20, "binance:/LTC:/free": 3, "binance:/LTC:/locked": 0, "binance:/LTC:/total": 3, "binance:/LTC:/worth": 56 * 3, } coinbase_net_worth = 10000 + (10 * 7000) + (5 * 200) binance_net_worth = 1000 + (5 * 7005) + (20 * 201) + (3 * 56) print(coinbase_net_worth + binance_net_worth) data['net_worth'] = sum( data[k] if k.endswith("worth") or k.endswith("USD:/total") else 0 for k in data.keys()) assert feed.next() == data