def main(): trading_pair = TradingPair(USD, BTC) action = DynamicOrders(trading_pair) exchange = CCXTExchange('bitmex', observation_pairs=[trading_pair], timeframe='1m') wallet = Wallet(exchange, .01 * BTC) portfolio = Portfolio(USD, wallets=[wallet]) env = TradingEnvironment(portfolio, exchange, action, 'simple') while True: env.step(0) env.render('human') time.sleep(.02)
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 main(): df = load_dataframe() exchange = SimulatedExchange(df) wallet_usd = Wallet(exchange, 0 * USD) wallet_btc = Wallet(exchange, .01 * BTC) portfolio = Portfolio(BTC, wallets=[wallet_usd, wallet_btc]) trading_pair = TradingPair(USD, BTC) action = DynamicOrders(trading_pair) env = TradingEnvironment(portfolio, exchange, action, 'simple', window_size=20) times = [] for _ in range(300): action = 0 if random.random() > .9: action = int(random.uniform(1, 20)) env.step(action) t1 = time.time() env.render('human') times.append(time.time() - t1) if len(times) > 120: times.pop(0) print(f'FPS: {1 / np.mean(times):.1f}', end='\r')
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)
env = TradingEnvironment(exchange=exchange, action_strategy=action_strategy, reward_strategy=reward_strategy) obs = env.reset() sell_price = 1e9 stop_price = -1 print('Initial portfolio: ', exchange.portfolio) for i in range(1000): action = 0 if obs['close'] < sell_price else 18 action = 19 if obs['close'] < stop_price else action if i == 0 or portfolio['BTC'] == 0: action = 16 sell_price = obs['close'] + (obs['close'] / 50) stop_price = obs['close'] - (obs['close'] / 50) obs, reward, done, info = env.step(action) executed_trade = info['executed_trade'] filled_trade = info['filled_trade'] portfolio = exchange.portfolio print('Obs: ', obs) print('Reward: ', reward) print('Portfolio: ', portfolio) print('Trade executed: ', executed_trade.trade_type, executed_trade.price, executed_trade.amount) print('Trade filled: ', filled_trade.trade_type, filled_trade.price, filled_trade.amount)