def test_valid_init(): price = Price(7000, TradingPair(USD, BTC)) assert price assert isinstance(price.rate, Number) assert price.rate == 7000 assert isinstance(price.pair, TradingPair) assert price.pair == TradingPair(USD, BTC) price = Price(7000, USD/BTC) assert price assert isinstance(price.rate, Number) assert price.rate == 7000 assert isinstance(price.pair, TradingPair) assert price.pair == USD / BTC
def test_valid_init(): USD = Instrument("USD", 8, "U.S. Dollar") BTC = Instrument("BTC", 8, "Bitcoin") pair = TradingPair(USD, BTC) assert pair assert pair.base == USD assert pair.quote == BTC
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_valid_rmul(): pair = TradingPair(USD, BTC) # int price = 8 * pair assert isinstance(price, Price) assert price.rate == 8 assert price.pair == pair # float price = 8.0 * pair assert isinstance(price, Price) assert price.rate == 8 assert price.pair == pair
def net_worth(self) -> float: """Calculate the net worth of the active account on thenge. Returns: The total portfolio value of the active account on the exchange. """ net_worth = 0 if not self._wallets: return net_worth for wallet in self._wallets.values(): if wallet.instrument == self._base_instrument: current_price = 1 else: pair = TradingPair(self._base_instrument, wallet.instrument) current_price = wallet.exchange.quote_price(pair) wallet_balance = wallet.total_balance.size net_worth += current_price * wallet_balance return net_worth
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')
# limitations under the License import time import ccxt import numpy as np import pandas as pd from typing import Dict, List, Union from gym.spaces import Space, Box from ccxt import BadRequest from tensortrade.trades import Trade, TradeType, TradeSide from tensortrade.instruments import TradingPair, BTC, ETH from tensortrade.exchanges import Exchange BTC_ETH_PAIR = TradingPair(BTC, ETH) class CCXTExchange(Exchange): """An exchange for trading on CCXT-supported cryptocurrency exchanges.""" def __init__(self, exchange: Union[ccxt.Exchange, str] = 'coinbase', **kwargs): super(CCXTExchange, self).__init__(**kwargs) exchange = self.default('exchange', exchange) self._exchange = getattr(ccxt, exchange)() if \ isinstance(exchange, str) else exchange self._credentials = self.default('credentials', None, kwargs)
def test_valid_init(): pair = TradingPair(USD, BTC) assert pair assert pair.base == USD assert pair.quote == BTC
def test_str(): pair = TradingPair(USD, BTC) assert str(pair) == "USD/BTC"
def test_invalid_rmul(): pair = TradingPair(USD, BTC) with pytest.raises(IncompatibleTradingPairOperation): "btc" * pair
def test_invalid_init(): with pytest.raises(InvalidTradingPair): pair = TradingPair(BTC, BTC)
def test_str(): USD = Instrument("USD", 8, "U.S. Dollar") BTC = Instrument("BTC", 8, "Bitcoin") pair = TradingPair(USD, BTC) assert str(pair) == "USD/BTC"
def test_invalid_init(): BTC = Instrument("BTC", 8, "Bitcoin") with pytest.raises(InvalidTradingPair): pair = TradingPair(BTC, BTC)