Exemplo n.º 1
0
    def compile(self):
        """
        Sets the observation space and the action space of the environment.
        Creates the internal feed and sets initialization for different components.
        """
        for component in [self._broker, self.portfolio, self.action_scheme, self.reward_scheme]:
            component.clock = self.clock

        self.action_scheme.set_pairs(exchange_pairs=self.portfolio.exchange_pairs)
        self.action_space = Discrete(len(self.action_scheme))

        if not self.feed:
            self.feed = create_internal_feed(self.portfolio)

        self.feed = self.feed + create_internal_feed(self.portfolio)

        initial_obs = self.feed.next()
        n_features = len(initial_obs.keys()) if self.use_internal else len(self._external_keys)

        self.observation_space = Box(
            low=self._observation_lows,
            high=self._observation_highs,
            shape=(self.window_size, n_features),
            dtype=self._observation_dtype
        )

        self.feed.reset()
Exemplo n.º 2
0
def test_create_internal_data_feed():

    ex1 = Exchange("coinbase", service=execute_order)(
        Stream("USD-BTC", [7000, 7500, 8300]),
        Stream("USD-ETH", [200, 212, 400])
    )

    ex2 = Exchange("binance", service=execute_order)(
        Stream("USD-BTC", [7005, 7600, 8200]),
        Stream("USD-ETH", [201, 208, 402]),
        Stream("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)

    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