def test_smoke(): cdd = CryptoDataDownload() coinbase_btc = cdd.fetch("Coinbase", "USD", "BTC", "1h") coinbase_eth = cdd.fetch("Coinbase", "USD", "ETH", "1h") bitstamp_btc = cdd.fetch("Bitstamp", "USD", "BTC", "1h") bitstamp_eth = cdd.fetch("Bitstamp", "USD", "ETH", "1h") bitstamp_ltc = cdd.fetch("Bitstamp", "USD", "LTC", "1h") steps = len(coinbase_btc) coinbase = Exchange("coinbase", service=execute_order)( Stream("USD-BTC", list(coinbase_btc['close'][-steps:])), Stream("USD-ETH", list(coinbase_eth['close'][-steps:]))) bitstamp = Exchange("bitstamp", service=execute_order)( Stream("USD-BTC", list(bitstamp_btc['close'][-steps:])), Stream("USD-ETH", list(bitstamp_eth['close'][-steps:])), Stream("USD-LTC", list(bitstamp_ltc['close'][-steps:]))) portfolio = Portfolio(USD, [ Wallet(coinbase, 200000 * USD), Wallet(coinbase, 0 * BTC), Wallet(bitstamp, 10000 * USD), Wallet(bitstamp, 2 * BTC), Wallet(bitstamp, 20 * ETH), Wallet(bitstamp, 30 * LTC) ]) action_scheme = ManagedRiskOrders( durations=[4, 6, 8, 10], stop_loss_percentages=[0.01, 0.003, 0.3], take_profit_percentages=[0.01, 0.003, 0.3], trade_sizes=[0.99999999999999]) env = TradingEnvironment(action_scheme=action_scheme, reward_scheme="simple", portfolio=portfolio) done = False n_steps = 0 while not done: action = env.action_space.sample() obs, reward, done, info = env.step(action) n_steps += 1 portfolio.ledger.as_frame().sort_values(["step", "poid"]).to_clipboard(index=False) df = portfolio.ledger.as_frame() frames = [] for poid in df.poid.unique(): frames += [df.loc[df.poid == poid, :]] pd.concat(frames, ignore_index=True, axis=0).to_clipboard(index=False) pytest.fail("Failed.")
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 = [] with Module("coinbase") as coinbase: for name in coinbase_btc.columns: nodes += [Stream(name, list(coinbase_btc[name]))] for name in coinbase_eth.columns: nodes += [Stream(name, list(coinbase_eth[name]))] 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_init_multiple_exchanges(portfolio): action_scheme = ManagedRiskOrders() reward_scheme = SimpleProfit() env = TradingEnvironment(portfolio=portfolio, action_scheme=action_scheme, reward_scheme=reward_scheme, window_size=50, enable_logger=False) obs = env.reset() assert obs.shape == (50, 32) assert env.observation_space.shape == (50, 32)
def env(): context = { "base_instrument": USD, "actions": { "pairs": [USD / BTC], "stop_loss_percentages": [0.02, 0.04, 0.06], "take_profit_percentages": [0.01, 0.02, 0.03], "trade_sizes": 10, "trade_side": TradeSide.BUY, "trade_type": TradeType.MARKET, "order_listener": None }, "rewards": { "return_algorithm": "sharpe", "risk_free_rate": 0, "target_returns": 0 }, "exchanges": { "model_type": "FBM", "hurst": 0.61, "timeframe": "1d", "base_price": 7500, "base_volume": 12000 } } with TradingContext(**context): action_scheme = ManagedRiskOrders() reward_scheme = RiskAdjustedReturns() exchange = Exchange() portfolio = Portfolio(USD, [ Wallet(exchange, 100000 * USD), Wallet(exchange, 0 * BTC) ]) env = TradingEnvironment( portfolio=portfolio, action_scheme=action_scheme, reward_scheme=reward_scheme, window_size=14, enable_logger=False ) return env
def test_runs_with_only_internal_data_feed(portfolio): action_scheme = ManagedRiskOrders() reward_scheme = SimpleProfit() env = TradingEnvironment(portfolio=portfolio, action_scheme=action_scheme, reward_scheme=reward_scheme, 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 == (50, 32)