Example #1
0
def test_namespace():

    a = Stream([1, 2, 3]).rename("a")

    with Module("world") as world:
        a1 = Stream([4, 5, 6]).rename("a1")
        a2 = Stream([7, 8, 9]).rename("a2")

        with Module("sub-world") as sub_world:
            a3 = Stream([10, 11, 12]).rename("a3")
            a4 = Stream([13, 14, 15]).rename("a4")

            t3 = BinOp(np.add)(a2, a4).rename("t3")

    t1 = BinOp(np.multiply)(a, t3).rename("t1")

    feed = DataFeed([t1, world, sub_world])

    assert feed.next() == {
        "world:/a1": 4,
        "world:/a2": 7,
        "world:/sub-world:/a3": 10,
        "world:/sub-world:/a4": 13,
        "world:/sub-world:/t3": 20,
        "t1": 20
    }
    feed.reset()
    assert feed.next() == {
        "world:/a1": 4,
        "world:/a2": 7,
        "world:/sub-world:/a3": 10,
        "world:/sub-world:/a4": 13,
        "world:/sub-world:/t3": 20,
        "t1": 20
    }
Example #2
0
def test_namespace():

    a = Stream("a", [1, 2, 3])

    with Module("world") as world:
        a1 = Stream("a1", [4, 5, 6])
        a2 = Stream("a2", [7, 8, 9])

        with Module("sub-world") as sub_world:
            a3 = Stream("a3", [10, 11, 12])
            a4 = Stream("a4", [13, 14, 15])

            t3 = BinOp("t3", operator.add)(a2, a4)

    t1 = BinOp("t1", operator.mul)(a, t3)

    feed = DataFeed()(t1, world, sub_world)

    assert feed.next() == {
        "world:/a1": 4,
        "world:/a2": 7,
        "world:/sub-world:/a3": 10,
        "world:/sub-world:/a4": 13,
        "world:/sub-world:/t3": 20,
        "t1": 20
    }
    feed.reset()
    assert feed.next() == {
        "world:/a1": 4,
        "world:/a2": 7,
        "world:/sub-world:/a3": 10,
        "world:/sub-world:/a4": 13,
        "world:/sub-world:/t3": 20,
        "t1": 20
    }
Example #3
0
def create_env():
    def fetch_data(exchange_name, symbol, timeframe):
        url = "https://www.cryptodatadownload.com/cdd/"
        filename = "{}_{}USD_{}.csv".format(exchange_name, symbol, timeframe)
        volume_column = "Volume {}".format(symbol)
        new_volume_column = "Volume_{}".format(symbol)

        df = pd.read_csv(url + filename, skiprows=1)
        df = df[::-1]
        df = df.drop(["Symbol"], axis=1)
        df = df.rename(
            {
                "Volume USD": "volume",
                volume_column: new_volume_column
            }, axis=1)
        df = df.set_index("Date")
        df.columns = [symbol + ":" + name.lower() for name in df.columns]

        return df

    ssl._create_default_https_context = ssl._create_unverified_context  # Only used if pandas gives a SSLError
    coinbase_data = pd.concat([
        fetch_data("Coinbase", "BTC", "1h"),
        fetch_data("Coinbase", "ETH", "1h")
    ],
                              axis=1)

    coinbase = Exchange("coinbase", service=execute_order)(
        Stream("USD-BTC", list(coinbase_data['BTC:close'])),
        Stream("USD-ETH", list(coinbase_data['ETH:close'])))

    with Module("coinbase") as coinbase_ns:
        nodes = [
            Stream(name, list(coinbase_data[name]))
            for name in coinbase_data.columns
        ]

    feed = DataFeed([coinbase_ns])

    portfolio = Portfolio(USD, [
        Wallet(coinbase, 10000 * USD),
        Wallet(coinbase, 10 * BTC),
        Wallet(coinbase, 5 * ETH),
    ])

    env = TradingEnvironment(feed=feed,
                             portfolio=portfolio,
                             action_scheme='managed-risk',
                             reward_scheme='risk-adjusted',
                             window_size=20)

    return env
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 trading(agent_path, n_step = len(test),):

    from tensortrade.exchanges import Exchange
    from tensortrade.exchanges.services.execution.simulated import execute_order
    from tensortrade.data import Stream, DataFeed, Module
    from tensortrade.instruments import USD, BTC
    from tensortrade.wallets import Wallet, Portfolio

    coinbase = Exchange("coinbase", service=execute_order)(
        Stream("USD-BTC", price_history['close'].tolist())
    )


    portfolio = Portfolio(USD, [
        Wallet(coinbase, 10000 * USD),
        Wallet(coinbase, 10 * BTC),
    ])

    with Module("coinbase") as coinbase_ns:
        nodes = [Stream(name, test[name].tolist()) for name in test.columns]

    feed = DataFeed([coinbase_ns])


    from tensortrade.environments import TradingEnvironment

    env = TradingEnvironment(
        feed=feed,
        portfolio=portfolio,
        action_scheme='managed-risk',
        reward_scheme='risk-adjusted',
        window_size=20
    )


    agent = DQNAgent(env)
    agent.restore()
    action = agent.env.action_space.sample()
    state, reward, done, info = agent.env.step(action)
    action = agent.get_action(state)
    action = agent.env.action_space.sample()
    step = 0
    while not done and (step < n_steps):
      state, reward, done, info = agent.env.step(action)
      action = agent.get_action(state)
      print('step:', step, '-- action:', action)
      step += 1

    env.portfolio.performance.plot(figsize=(16, 10))
    env.portfolio.performance.net_worth.plot(figsize=(16, 10))
Example #6
0
def test_select():

    a3 = Stream([3, 2, 1]).rename("a3")

    with Module("world") as a:
        a1 = Stream([7, 8, 9]).rename("a1")
        a2 = Stream([3, 2, 1]).rename("a2")

    t1 = BinOp(np.multiply)(a1, a3).rename("t1")

    s = Select("world:/a1")(t1, a)
    feed = DataFeed([s])

    assert feed.next() == {"world:/a1": 7}
Example #7
0
def test_select():

    a3 = Stream("a3", [3, 2, 1])

    with Module("world") as a:
        a1 = Stream("a1", [7, 8, 9])
        a2 = Stream("a2", [3, 2, 1])

    t1 = BinOp("t1", operator.mul)(a1, a3)

    s = Select("world:/a1")(t1, a)
    feed = DataFeed()(s)

    assert feed.next() == {"world:/a1": 7}
Example #8
0
def create_wallet_source(wallet: Wallet, include_worth=True):

    exchange_name = wallet.exchange.name
    symbol = wallet.instrument.symbol

    with Module(exchange_name + ":/" + symbol) as wallet_ds:
        free_balance = Lambda("free", lambda w: w.balance.size, wallet)
        locked_balance = Lambda("locked", lambda w: w.locked_balance.size, wallet)
        total_balance = Lambda("total", lambda w: w.total_balance.size, wallet)

        if include_worth:
            price = Select(lambda node: node.name.endswith(symbol))(wallet.exchange)
            worth = BinOp("worth", operator.mul)(price, total_balance)
            nodes = [free_balance, locked_balance, total_balance, worth]
        else:
            nodes = [free_balance, locked_balance, total_balance]

    return wallet_ds
Example #9
0
def create_wallet_source(wallet: Wallet, include_worth=True):
    exchange_name = wallet.exchange.name
    symbol = wallet.instrument.symbol

    with Module(exchange_name + ":/" + symbol) as wallet_ds:
        free_balance = Lambda(lambda w: w.balance.as_float(),
                              wallet,
                              name="free")
        locked_balance = Lambda(lambda w: w.locked_balance.as_float(),
                                wallet,
                                name="locked")
        total_balance = Lambda(lambda w: w.total_balance.as_float(),
                               wallet,
                               name="total")

        nodes = [free_balance, locked_balance, total_balance]

        if include_worth:
            price = Select(lambda node: node.name.endswith(symbol))(
                wallet.exchange)
            worth = BinOp(operator.mul, name="worth")(price, total_balance)
            nodes += [worth]

    return wallet_ds
from tensortrade.data import Stream, DataFeed, Module
from tensortrade.instruments import USD, BTC
from tensortrade.wallets import Wallet, Portfolio
from tensortrade.agents import A2CAgent

# Instantiate the portfolio and feed (feed consists only of BTC price predictions)
coinbase = Exchange("coinbase", service=execute_order)(
    Stream("USD-BTC", price_history['close'].tolist())
)

portfolio = Portfolio(USD, [
    Wallet(coinbase, 10000 * USD), 
    Wallet(coinbase, 10 * BTC),
])

with Module("coinbase") as coinbase_ns:
    nodes = [Stream(name, data[name].tolist()) for name in data.columns]

feed = DataFeed([coinbase_ns])

# Initialize the trading environment
from tensortrade.environments import TradingEnvironment
env_1 = TradingEnvironment(
    feed=feed,
    portfolio=portfolio,
    action_scheme='managed-risk',
    reward_scheme='risk-adjusted',
    window_size=4 
)

# Load the agent from the external (pre-trained) files
Example #11
0
data = data[(data.index >= last_30pct)]
buy_data = data.copy()
agent_data = data.copy()

for col in agent_data.columns:
    if col != "target" and col != "future_timestamp" and col != "time":
        agent_data[col] = agent_data[col].pct_change()
        agent_data.dropna(inplace=True)
        print(agent_data.columns)
        print(col)
        agent_data[col] = preprocessing.scale(agent_data[col].values)

data.dropna(inplace=True)

with Module("coinbase") as node_stream:
    nodes = []
    for name in data.columns:
        nodes.append(Stream(list(data[name]), name))

data_feed = DataFeed([node_stream])
data_feed.next()
exchange = Exchange("sim-exchange",
                    service=execute_order)(Stream(list(data['BTC-USD_close']),
                                                  "USD-BTC"),
                                           Stream(list(data['ETH-USD_close']),
                                                  "USD-ETH"),
                                           Stream(list(data['LTC-USD_close']),
                                                  "USD-LTC"))

portfolio = Portfolio(base_instrument=USD,