def test_convert_to(data): if abs(data) < Decimal('1e16'): number = convert_to.decimal(data) assert number == Decimal.from_float(data).quantize(Decimal('1e-8')) else: with pytest.raises(InvalidOperation): convert_to.decimal(data)
def test_convert_to(data): if abs(data) < Decimal('1e18'): number = convert_to.decimal(data) assert number - Decimal.from_float(data).quantize( Decimal('0e-9')) < Decimal("1e-8") elif abs(data) < Decimal('1e33'): with pytest.raises(InvalidOperation): convert_to.decimal(data) else: with pytest.raises(Overflow): convert_to.decimal(data)
def test_fiat(fresh_env): env = fresh_env with pytest.raises(AssertionError): env.fiat = "USDT" env.add_pairs("USDT_BTC") env.fiat = "USDT" # assert env.fiat == Decimal('0.00000000') with pytest.raises(KeyError): env.fiat env.fiat = 0 assert env.fiat == Decimal('0.00000000') for i in range(10): env.fiat = i assert env.fiat == Decimal(i) value = np.random.rand() * i env.fiat = value assert env.fiat == convert_to.decimal(value) timestamp = env.timestamp env.fiat = {"USDT": 10, 'timestamp': timestamp} assert env.portfolio_df.get_value(timestamp, "USDT") == 10
def test_simulate_trade(self, action): # Normalize action vector action = array_normalize(action, False) assert action.sum() - Decimal('1.00000000') < Decimal( '1E-8'), action.sum() - Decimal('1.00000000') # Get timestamp timestamp = self.env.obs_df.index[-1] # Call method self.env.simulate_trade(action, timestamp) # Assert position for i, symbol in enumerate(self.env.symbols): assert self.env.action_df.get_value( timestamp, symbol) - convert_to.decimal( action[i]) <= Decimal('1E-8') # Assert amount for i, symbol in enumerate(self.env.symbols): if symbol not in self.env._fiat: assert self.env.portfolio_df.get_value(self.env.portfolio_df[symbol].last_valid_index(), symbol) - \ self.env.action_df.get_value(timestamp, symbol) * self.env.calc_total_portval(timestamp) / \ self.env.get_open_price(symbol, timestamp) <= convert_to.decimal('1E-4')
def test_balance(fresh_env): env = fresh_env env.add_pairs("USDT_BTC", "USDT_ETH") env.fiat = "USDT" with pytest.raises(AssertionError): env.balance = [] env.balance = 0 env.balance = '0' env.balance = env.get_balance() for symbol, value in env.balance.items(): assert value == convert_to.decimal(env.balance[symbol]) assert symbol in env.symbols assert env._fiat not in env.crypto.keys()
def test_crypto(fresh_env): env = fresh_env env.add_pairs("USDT_BTC") env.fiat = "USDT" with pytest.raises(AssertionError): env.crypto = [] env.crypto = 0 env.crypto = '0' balance = env.get_balance() env.crypto = balance for symbol, value in env.crypto.items(): assert value == convert_to.decimal(balance[symbol]) assert symbol in env.symbols assert env._fiat not in env.crypto timestamp = env.timestamp env.crypto = {"BTC": 10, 'timestamp': timestamp} assert env.portfolio_df.get_value(timestamp, "BTC") == Decimal('10')