Пример #1
0
def portfolio():

    df1 = pd.read_csv("tests/data/input/coinbase_(BTC,ETH)USD_d.csv").tail(100)
    df1 = df1.rename({"Unnamed: 0": "date"}, axis=1)
    df1 = df1.set_index("date")

    df2 = pd.read_csv("tests/data/input/bitstamp_(BTC,ETH,LTC)USD_d.csv").tail(
        100)
    df2 = df2.rename({"Unnamed: 0": "date"}, axis=1)
    df2 = df2.set_index("date")

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

    ex2 = Exchange("binance",
                   service=execute_order)(Stream("USD-BTC",
                                                 list(df2['BTC:close'])),
                                          Stream("USD-ETH",
                                                 list(df2['ETH:close'])),
                                          Stream("USD-LTC",
                                                 list(df2['LTC:close'])))

    p = 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),
    ])
    return p
Пример #2
0
def test_exchange_with_wallets_feed():

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

    ex2 = Exchange("binance",
                   service=execute_order)(Array("USD-BTC", [7005, 7600, 8200]),
                                          Array("USD-ETH", [201, 208, 402]),
                                          Array("USD-LTC", [56, 52, 60]))

    wallet_btc = Wallet(ex1, 10 * BTC)
    wallet_btc_ds = create_wallet_source(wallet_btc)

    wallet_usd = Wallet(ex2, 1000 * USD)
    wallet_usd -= 400 * USD
    wallet_usd += Quantity(USD, 400, path_id="fake_id")
    wallet_usd_ds = create_wallet_source(wallet_usd, include_worth=False)

    feed = DataFeed([ex1, ex2, wallet_btc_ds, wallet_usd_ds])

    assert feed.next() == {
        "coinbase:/USD-BTC": 7000,
        "coinbase:/USD-ETH": 200,
        "coinbase:/BTC:/free": 10,
        "coinbase:/BTC:/locked": 0,
        "coinbase:/BTC:/total": 10,
        "coinbase:/BTC:/worth": 70000,
        "binance:/USD-BTC": 7005,
        "binance:/USD-ETH": 201,
        "binance:/USD-LTC": 56,
        "binance:/USD:/free": 600,
        "binance:/USD:/locked": 400,
        "binance:/USD:/total": 1000
    }
Пример #3
0
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.")
Пример #4
0
def test_simulated_from_config():
    class NoSlippage(SlippageModel):
        def adjust_trade(self, trade: Trade, **kwargs) -> Trade:
            pass

    config = {
        'base_instrument': 'EURO',
        'instruments': ['BTC', 'ETH'],
        'exchanges': {
            'commission': 0.5,
            'base_precision': 0.3,
            'instrument_precision': 10,
            'min_trade_price': 1e-7,
            'max_trade_price': 1e7,
            'min_trade_size': 1e-4,
            'max_trade_size': 1e4,
            'initial_balance': 1e5,
            'window_size': 5,
            'should_pretransform_obs': True,
            'max_allowed_slippage_percent': 3.0,
            'slippage_model': NoSlippage
        }
    }

    with TradingContext(**config):
        df = pd.Source([[900, 849, 9023, 94039, 943]],
                       columns=["open", "high", "low", "close", "volume"])

        exchange_ds = DataFrameSource('prices', df)
        data_feed = DataFeed([exchange_ds])

        exchange = Exchange('Exchange', lambda x: {EUR / ETH: x['close']})

        assert exchange._base_instrument == 'EURO'
        assert exchange._commission == 0.5
Пример #5
0
def test_exchange_feed():

    btc_price = Stream("USD-BTC", [7000, 7500, 8300])
    eth_price = Stream("USD-ETH", [200, 212, 400])

    exchange = Exchange("coinbase", service=execute_order)(btc_price, eth_price)

    feed = DataFeed([exchange])

    assert feed.next() == {"coinbase:/USD-BTC": 7000, "coinbase/USD-ETH": 200}
Пример #6
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 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))
Пример #8
0
def env():
    context = {
        "base_instrument": USD,
        "actions": {
            "pairs": [USD / BTC],
            "stop_loss_percentages": [0.02, 0.04, 0.06],
            "take_profit_percentages": [0.01, 0.02, 0.03],
            "trade_sizes": 10,
            "trade_side": TradeSide.BUY,
            "trade_type": TradeType.MARKET,
            "order_listener": None
        },
        "rewards": {
            "return_algorithm": "sharpe",
            "risk_free_rate": 0,
            "target_returns": 0
        },
        "exchanges": {
            "model_type": "FBM",
            "hurst": 0.61,
            "timeframe": "1d",
            "base_price": 7500,
            "base_volume": 12000
        }
    }

    with TradingContext(**context):
        action_scheme = ManagedRiskOrders()
        reward_scheme = RiskAdjustedReturns()
        exchange = Exchange()

        portfolio = Portfolio(USD, [
            Wallet(exchange, 100000 * USD),
            Wallet(exchange, 0 * BTC)
        ])

        env = TradingEnvironment(
            portfolio=portfolio,
            action_scheme=action_scheme,
            reward_scheme=reward_scheme,
            window_size=14,
            enable_logger=False
        )

    return env
Пример #9
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
Пример #10
0
from tensortrade.exchanges import Exchange
from tensortrade.wallets import Wallet
from tensortrade.instruments import USD, BTC, Quantity

path_id = "f4cfeeae-a3e4-42e9-84b9-a24ccd2eebeb"
other_id = "7f3de243-0474-48d9-bf44-ca55ae07a70e"

PRICE_COLUMN = "close"
data_frame = pd.read_csv("tests/data/input/coinbase-1h-btc-usd.csv")
data_frame.columns = map(str.lower, data_frame.columns)
data_frame = data_frame.rename(columns={'volume btc': 'volume'})

exchange_ds = DataFrameSource('Frame', data_frame)
exchange_feed = DataFeed([exchange_ds])

exchange = Exchange('Exchange', lambda x: x)


def test_init():

    wallet = Wallet(exchange, 10000 * USD)

    assert wallet.balance == 10000 * USD
    assert wallet.exchange == exchange
    assert wallet.instrument == USD
    assert len(wallet.locked) == 0


def test_from_tuple():

    wallet_tuple = (exchange, USD, 10000)
data

# Save actual data into 'price_history' and predicted data into 'data'
price_history = data[['date', 'open', 'high', 'low', 'close', 'volume']]  # chart data
data.drop(columns=['date', 'open', 'high', 'low', 'close', 'volume'], inplace=True)

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
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(
Пример #12
0
        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,
                      wallets=[
                          Wallet(exchange, 100000 * USD),
                          Wallet(exchange, 0 * BTC),
                          Wallet(exchange, 0 * LTC),
                          Wallet(exchange, 0 * ETH)
                      ])
env = TradingEnvironment(feed=data_feed,
                         portfolio=portfolio,
                         action_scheme='managed-risk',
                         reward_scheme='risk-adjusted',